forked from rochacbruno/python-base-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_agent_langchain.py
More file actions
62 lines (49 loc) · 1.53 KB
/
Copy path04_agent_langchain.py
File metadata and controls
62 lines (49 loc) · 1.53 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
import json
import os
import subprocess
from langchain.agents import initialize_agent
from langchain.tools import Tool
from langchain_ollama import OllamaLLM
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434")
model = os.environ.get("MODEL", "gemma3:12b")
llm = OllamaLLM(model=model, base_url=OLLAMA_URL)
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 []
tools = [
Tool(
name="list_transactions",
func=list_transactions,
description="Use this when you need to list user transactions.",
)
]
agent = initialize_agent(
llm=llm, tools=tools, verbose=True, agent_type="zero-shot-react-description"
)
def invoke_agent(prompt: str) -> dict:
return agent.invoke({"input": 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()