-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (53 loc) · 1.61 KB
/
main.py
File metadata and controls
61 lines (53 loc) · 1.61 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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}
# Root and Health endpoints
@app.get("/")
async def root():
return {
"status": "online",
"platform": "GraTech AI Unified",
"version": "1. 0.0",
"docs": "/docs",
"endpoints": {
"chat": "/api/chat",
"health": "/api/health"
}
}
@app.get("/health")
async def health_root():
return {"status": "healthy"}