-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py.backup
More file actions
43 lines (37 loc) · 1.23 KB
/
main.py.backup
File metadata and controls
43 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import os
from dotenv import load_dotenv
from openai import AzureOpenAI
load_dotenv()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class ChatRequest(BaseModel):
message: str
@app.post("/api/chat")
async def chat(request: ChatRequest):
try:
# Initialize Azure Client inside the request to ensure env vars are loaded
client = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2024-06-01",
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
)
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT_GPT4"),
messages=[{"role": "user", "content": request.message}]
)
return {"reply": response.choices[0].message.content}
except Exception as e:
print(f"Error: {str(e)}")
return {"reply": f"System Error: {str(e)}"}
@app.get("/api/health")
async def health():
return {"status": "healthy", "backend": "operational", "azure": True}