-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
28 lines (25 loc) · 755 Bytes
/
Copy pathgraph.py
File metadata and controls
28 lines (25 loc) · 755 Bytes
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
from langgraph.graph import StateGraph, END
from chain import llm
from pydantic import BaseModel
class MyState(BaseModel):
input: str
output: str = ""
def llm_node(state):
if isinstance(state, dict):
msg = state["input"]
else:
msg = state.input
reply = llm.invoke(msg)
if isinstance(state, dict):
state["output"] = reply.content
else:
state.output = reply.content
return state
def run_langgraph(message: str) -> str:
workflow = StateGraph(state_schema=MyState)
workflow.add_node("llm", llm_node)
workflow.set_entry_point("llm")
workflow.add_edge("llm", END)
graph = workflow.compile()
final_state = graph.invoke({"input": message})
return final_state["output"]