├── .env ├── README.md ├── main.py ├── requirements.txt └── test-api.py /.env: -------------------------------------------------------------------------------- 1 | API_KEY="secretkey" 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API-For-Your-LLM -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, Depends, HTTPException, Header 2 | import ollama 3 | import os 4 | from dotenv import load_dotenv 5 | 6 | load_dotenv() 7 | 8 | API_KEY_CREDITS = {os.getenv("API_KEY"): 5} 9 | print(API_KEY_CREDITS) 10 | app = FastAPI() 11 | 12 | def verify_api_key(x_api_key: str = Header(None)): 13 | credits = API_KEY_CREDITS.get(x_api_key, 0) 14 | if credits <= 0: 15 | raise HTTPException(status_code=401, detail="Invalid API Key, or no credits") 16 | 17 | return x_api_key 18 | 19 | @app.post("/generate") 20 | def generate(prompt: str, x_api_key: str = Depends(verify_api_key)): 21 | API_KEY_CREDITS[x_api_key] -= 1 22 | response = ollama.chat(model="mistral", messages=[{"role": "user", "content": prompt}]) 23 | return {"response": response["message"]["content"]} 24 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi 2 | uvicorn 3 | ollama 4 | python-dotenv 5 | requests -------------------------------------------------------------------------------- /test-api.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from dotenv import load_dotenv 3 | import os 4 | 5 | load_dotenv() 6 | 7 | url = "http://127.0.0.1:8000/generate?prompt=Tell me about Python" 8 | headers = {"x-api-key": os.getenv("API_KEY"), "Content-Type": "application/json"} 9 | 10 | response = requests.post(url, headers=headers) 11 | print(response.json()) --------------------------------------------------------------------------------