from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline import torch import os import requests # from langchain.llms.huggingface_pipeline import HuggingFacePipeline key = os.environ.get("huggingface_key") openai_api_key = os.environ.get("openai_key") app = FastAPI(openapi_url="/api/v1/LLM/openapi.json", docs_url="/api/v1/LLM/docs") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], allow_credentials=True, ) API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1" headers = {"Authorization": f"Bearer {key}"} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() # tokenizer = AutoTokenizer.from_pretrained("WizardLM/WizardCoder-1B-V1.0") # base_model = AutoModelForCausalLM.from_pretrained("WizardLM/WizardCoder-1B-V1.0") model = "mistralai/Mixtral-8x7B-Instruct-v0.1" tokenizer = AutoTokenizer.from_pretrained(model) base_model = AutoModelForCausalLM.from_pretrained(model) pipe = pipeline("text-generation", model=base_model, tokenizer=tokenizer, max_length=4000, do_sample=True, top_p=0.95, repetition_penalty=1.2, ) # hf_llm = HuggingFacePipeline(pipeline=pipe) @app.get("/") def root(): return {"message": "R&D LLM API"} @app.get("/get") def get(): result = pipe("name 5 programming languages",do_sample=False) print(result) return {"message": result} async def askLLM(prompt): output = pipe(prompt,do_sample=False) return output @app.post("/ask_llm") async def ask_llm_endpoint(prompt: str): # result = await askLLM(prompt) result = pipe(prompt,do_sample=False) return {"result": result} @app.post("/ask_HFAPI") def ask_HFAPI_endpoint(prompt: str): result = query(prompt) return {"result": result} from langchain.llms import OpenAI llm = OpenAI(model_name="text-davinci-003", temperature=0.5, openai_api_key=openai_api_key) @app.post("/ask_GPT") def ask_GPT_endpoint(prompt: str): result = llm(prompt) return {"result": result}