-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (48 loc) · 1.97 KB
/
Copy pathmain.py
File metadata and controls
69 lines (48 loc) · 1.97 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
60
61
62
63
64
65
66
67
68
69
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langgraph.graph.state import RunnableConfig
from langgraph.pregel.main import asyncio
from rich import print
from rich.markdown import Markdown
from rich.prompt import Prompt
from examples.ex010.checkpointer import build_checkpointer
from examples.ex010.context import Context
from examples.ex010.graph import build_graph
from examples.ex010.prompts import SYSTEM_PROMPT
from examples.ex010.utils import Connection, async_lifespan
async def run_graph(connection: Connection) -> None:
checkpointer = build_checkpointer(connection)
graph = build_graph(checkpointer)
context = Context(user_type="plus")
config = RunnableConfig(
configurable={"thread_id": 1},
)
all_messages: list[BaseMessage] = []
prompt = Prompt()
Prompt.prompt_suffix = ""
while True:
user_input = prompt.ask("[bold cyan]Você: \n")
print(Markdown("\n\n --- \n\n"))
if user_input.lower() in ["q", "quit"]:
break
human_message = HumanMessage(user_input)
current_loop_messages = [human_message]
if len(all_messages) == 0:
current_loop_messages = [SystemMessage(SYSTEM_PROMPT), human_message]
result = graph.invoke(
{"messages": current_loop_messages}, config=config, context=context
)
model_name = ""
last_message = result["messages"][-1]
if isinstance(last_message, AIMessage):
model_name = last_message.response_metadata.get("model", "")
print(f"[bold cyan]RESPOSTA ({model_name}): \n")
print(Markdown(last_message.text))
print(last_message)
print(Markdown("\n\n --- \n\n"))
all_messages = result["messages"]
print(graph.get_state(config=config))
async def main() -> None:
async with async_lifespan() as connection:
await run_graph(connection)
if __name__ == "__main__":
asyncio.run(main())