-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (48 loc) · 1.89 KB
/
main.py
File metadata and controls
67 lines (48 loc) · 1.89 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
import builtins
from typing import Literal
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langchain_core.tracers.stdout import FunctionCallbackHandler
from langgraph.graph.state import RunnableConfig
from rich import print
from rich.markdown import Markdown
from rich.prompt import Prompt
from examples.ex007.graph import build_graph
from examples.ex007.prompts import SYSTEM_PROMPT
print()
def main() -> None:
graph = build_graph()
fn_handler_cb = FunctionCallbackHandler(function=builtins.print)
user_type: Literal["plus", "enterprise"] = "plus"
config = RunnableConfig(
run_name="meu_grafo",
tags=["enterprise"],
configurable={"thread_id": 1, "user_type": user_type},
max_concurrency=4,
recursion_limit=25,
callbacks=[fn_handler_cb],
)
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)
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))
if __name__ == "__main__":
main()