Spaces:
Sleeping
Sleeping
add FastAPI & mistral call
Browse files- .gitignore +1 -0
- __pycache__/app.cpython-313.pyc +0 -0
- app.py +43 -0
.gitignore
CHANGED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
__pycache__/app.cpython-313.pyc
ADDED
Binary file (2.3 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from typing import List
|
4 |
+
import os
|
5 |
+
from mistralai import Mistral
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
+
api_key = os.getenv("MISTRAL_API_KEY")
|
11 |
+
|
12 |
+
# Initialize FastAPI app
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
# Initialize Mistral client
|
16 |
+
client = Mistral(api_key=api_key)
|
17 |
+
|
18 |
+
class Message(BaseModel):
|
19 |
+
role: str
|
20 |
+
content: str
|
21 |
+
|
22 |
+
class ChatRequest(BaseModel):
|
23 |
+
model: str
|
24 |
+
messages: List[Message]
|
25 |
+
|
26 |
+
@app.post("/chat/complete")
|
27 |
+
async def chat_complete(request: ChatRequest):
|
28 |
+
try:
|
29 |
+
response = client.chat.complete(
|
30 |
+
model=request.model,
|
31 |
+
messages=[{"role": msg.role, "content": msg.content} for msg in request.messages]
|
32 |
+
)
|
33 |
+
return {
|
34 |
+
"content": response.choices[0].message.content,
|
35 |
+
"finish_reason": response.choices[0].finish_reason
|
36 |
+
}
|
37 |
+
except Exception as e:
|
38 |
+
raise HTTPException(status_code=500, detail=str(e))
|
39 |
+
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
import uvicorn
|
43 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|