-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcode002.py
More file actions
57 lines (43 loc) · 1.71 KB
/
code002.py
File metadata and controls
57 lines (43 loc) · 1.71 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
import threading
from collections.abc import Sequence
from typing import Annotated, TypedDict
from langchain.chat_models import init_chat_model
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import END, START, StateGraph, add_messages
from langgraph.graph.state import RunnableConfig
from rich import print
from rich.markdown import Markdown
# llm = init_chat_model("google_genai:gemini-2.5-flash")
llm = init_chat_model("ollama:gpt-oss:20b")
# 1 - Defino o meu state
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], add_messages]
# 2 - Defino os meus nodes
def call_llm(state: AgentState) -> AgentState:
llm_result = llm.invoke(state["messages"])
return {"messages": [llm_result]}
# 3 - Crio o StateGraph
builder = StateGraph(
AgentState, context_schema=None, input_schema=AgentState, output_schema=AgentState
)
# 4 - Adicionar nodes ao grafo
builder.add_node("call_llm", call_llm)
builder.add_edge(START, "call_llm")
builder.add_edge("call_llm", END)
# 5 - Compilar o grafo
checkpointer = InMemorySaver()
graph = builder.compile(checkpointer=checkpointer)
config = RunnableConfig(configurable={"thread_id": threading.get_ident()})
if __name__ == "__main__":
while True:
user_input = input("Digite sua mensage: ")
print(Markdown("---"))
if user_input.lower() in ["q", "quit"]:
print("Bye 👋")
print(Markdown("---"))
break
human_message = HumanMessage(user_input)
result = graph.invoke({"messages": [human_message]}, config=config)
print(Markdown(str(result["messages"][-1].content)))
print(Markdown("---"))