-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
23 lines (18 loc) · 741 Bytes
/
Copy pathmain.py
File metadata and controls
23 lines (18 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from fastapi import FastAPI, Depends, HTTPException, Header
import ollama
import os
from dotenv import load_dotenv
load_dotenv()
API_KEY_CREDITS = {os.getenv("API_KEY"): 5}
print(API_KEY_CREDITS)
app = FastAPI()
def verify_api_key(x_api_key: str = Header(None)):
credits = API_KEY_CREDITS.get(x_api_key, 0)
if credits <= 0:
raise HTTPException(status_code=401, detail="Invalid API Key, or no credits")
return x_api_key
@app.post("/generate")
def generate(prompt: str, x_api_key: str = Depends(verify_api_key)):
API_KEY_CREDITS[x_api_key] -= 1
response = ollama.chat(model="mistral", messages=[{"role": "user", "content": prompt}])
return {"response": response["message"]["content"]}