-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path05_agent_langgraph.py
More file actions
66 lines (52 loc) · 1.63 KB
/
05_agent_langgraph.py
File metadata and controls
66 lines (52 loc) · 1.63 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
import json
import os
import subprocess
from langchain_core.tools import tool
from langchain_ollama import ChatOllama
from langgraph.prebuilt import create_react_agent
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434")
model = os.environ.get("MODEL", "qwen3:4b")
llm = ChatOllama(model=model, base_url=OLLAMA_URL)
@tool
def list_transactions(email: str) -> list[dict]:
"""List user transaction by email"""
try:
# Executa o comando da CLI
result = subprocess.run(
["dundie", "list", "--email", email, "--asjson"],
capture_output=True,
text=True,
check=True,
)
return json.loads(result.stdout.strip())
except subprocess.CalledProcessError as e:
print(f"Error listing transactions: {e}")
return []
@tool
def sum_transactions(transactions: list[dict]) -> float:
"""
Sum the values of a list of transactions.
"""
print("sum_transactions", len(transactions))
return sum([t["value"] for t in transactions])
# Create the agent
agent = create_react_agent(
llm,
tools=[list_transactions, sum_transactions],
)
def invoke_agent(prompt: str) -> dict:
return agent.invoke({"messages": [{"role": "user", "content": prompt}]})
def main():
while True:
prompt = input("Enter your prompt: ")
if not prompt:
print("Prompt cannot be empty.")
continue
if prompt.strip() in ["exit", "quit", "q"]:
print("Exiting...")
break
result = invoke_agent(prompt)
print("-" * 50)
print(result)
if __name__ == "__main__":
main()