-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (44 loc) · 1.35 KB
/
Copy pathmain.py
File metadata and controls
59 lines (44 loc) · 1.35 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
from dotenv import load_dotenv
load_dotenv()
from langchain.chat_models import init_chat_model
from graph.builder import build_graph
from tools.price_list import price_list
from tools.term_and_condition import term_and_condition
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
# Init LLM
llm = init_chat_model("openai:gpt-4o", temperature=0)
# Bind tools to LLM
llm_with_tools = llm.bind_tools([price_list, term_and_condition])
graph = build_graph(llm_with_tools)
# --- Request Models ---
class ChatRequest(BaseModel):
message: str
thread_id: str = "default-thread"
# --- Response Models ---
class ChatResponse(BaseModel):
answer: str
# Init FastAPI
app = FastAPI(title="Jasa Sewa Rote Chatbot API")
origins = [
"https://www.jasasewarote.web.id",
'http://localhost:5500',
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --- API Endpoint ---
@app.post("/chat", response_model=ChatResponse)
def chat(req: ChatRequest):
config = {"configurable": {"thread_id": req.thread_id}}
state = graph.invoke(
{"messages": [{"role": "user", "content": req.message}]},
config=config
)
answer = state["messages"][-1].content
return ChatResponse(answer=answer)