-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
45 lines (36 loc) · 1.11 KB
/
server.py
File metadata and controls
45 lines (36 loc) · 1.11 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
from fastapi import FastAPI
from pydantic import BaseModel
from agents import Runner, SQLiteSession
from connection import config
from main import poetry_agent
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
class Message(BaseModel):
message: str
session = SQLiteSession("poetry_agent.db")
app = FastAPI()
# CORS Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["https://poetry-assistant-aazs.vercel.app/",
"http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/chat")
async def chat(request: Message):
user_input = request.message.strip()
if not user_input:
return {"response": "Please provide a valid message."}
result = await Runner.run(
poetry_agent,
input=user_input,
run_config=config,
session=session,
)
return {"response": result.final_output or "Sorry, I couldn't compose that."}
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
uvicorn.run("server:app", host="0.0.0.0", port=port)