forked from rochacbruno/python-base-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_agent_pydantic.py
More file actions
66 lines (52 loc) · 1.63 KB
/
Copy path06_agent_pydantic.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 typing import Any, Dict, List
from pydantic_ai import Agent
from pydantic_ai.agent import AgentRunResult
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
# Set up Ollama LLM
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434")
model = os.environ.get("MODEL", "qwen3:4b")
model = OpenAIModel(
model_name=model, provider=OpenAIProvider(base_url=f"{OLLAMA_URL}/v1")
)
def list_transactions(email: str) -> list[dict]:
"""List user transaction by email"""
try:
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 []
def sum_transactions(transactions: List[Dict[str, Any]]) -> float:
"""
Sum the amounts of a list of transactions.
"""
return sum(t["value"] for t in transactions)
agent = Agent(
model,
tools=[list_transactions, sum_transactions],
)
def invoke_agent(prompt: str) -> AgentRunResult:
return agent.run_sync(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()