Repository navigation
Expand file tree
/
Copy pathchatbot.py
More file actions
56 lines (46 loc) · 1.98 KB
/
Copy pathchatbot.py
File metadata and controls
56 lines (46 loc) · 1.98 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
import os
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from typing import Sequence
from langchain_core.messages import HumanMessage, BaseMessage
from langgraph.graph.message import add_messages
from typing_extensions import Annotated, TypedDict
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, StateGraph
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import warnings
warnings.filterwarnings("ignore")
load_dotenv()
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
model = init_chat_model("gemini-2.5-flash", model_provider="google_genai")
prompt_template = ChatPromptTemplate.from_messages(
[
("system",
"""You are an appointment scheduling assistant for a local clinic system.
You can directly list hospitals and doctors and create appointments
by calling backend functions I provide. But don't tell which functions you are calling.
Be polite and user-friendly.
You do not refer users to outside systems like MHRS or any external websites.
When you list items (like hospitals or doctors):
- Always show them **vertically**, one per line.
- Use numbered or bulleted format with line breaks (e.g. each starts with '- ' or '1. ').
- Never write them inline separated by commas or asterisks.
When the user gives a hospital, doctor, date, or time,
respond with confirmation in a human way.
All data is stored locally in PostgreSQL."""),
MessagesPlaceholder(variable_name="messages"),
]
)
class State(TypedDict):
messages: Annotated[Sequence[BaseMessage], add_messages]
language: str
workflow = StateGraph(state_schema=State)
def call_model(state: State):
prompt = prompt_template.invoke(state)
response = model.invoke(prompt)
return {"messages": [response]}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)