forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_calling_structured.py
More file actions
88 lines (73 loc) · 2.81 KB
/
tool_calling_structured.py
File metadata and controls
88 lines (73 loc) · 2.81 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import asyncio
import logging
import sys
import traceback
from dotenv import load_dotenv
from pydantic import BaseModel
from beeai_framework.agents.tool_calling import (
ToolCallingAgent,
ToolCallingAgentStartEvent,
ToolCallingAgentSuccessEvent,
)
from beeai_framework.backend import ChatModel
from beeai_framework.emitter import EventMeta
from beeai_framework.errors import FrameworkError
from beeai_framework.logger import Logger
from beeai_framework.memory import UnconstrainedMemory
from beeai_framework.tools.weather import OpenMeteoTool
from beeai_framework.utils.strings import to_json
from examples.helpers.io import ConsoleReader
# Load environment variables
load_dotenv()
# Configure logging - using DEBUG instead of trace
logger = Logger("app", level=logging.DEBUG)
reader = ConsoleReader()
def log_intermediate_steps(data: ToolCallingAgentStartEvent | ToolCallingAgentSuccessEvent, event: EventMeta) -> None:
"""Process agent events and log appropriately"""
if event.name == "start":
reader.write("Agent (iteration start) 🤖 : ", str(data.state.memory.messages[-1].to_plain())) # input message
elif event.name == "success":
reader.write(
"Agent (iteration success) 🤖 : ", str(data.state.memory.messages[-2].to_plain())
) # tool call + tool result
async def main() -> None:
"""Main application loop"""
# Create agent
agent = ToolCallingAgent(
llm=ChatModel.from_name("ollama:granite3.1-dense:8b"),
memory=UnconstrainedMemory(),
tools=[OpenMeteoTool()],
templates={
"system": lambda template: template.update(
defaults={
"role": "a weather forecast agent",
"instructions": "- If user only provides a location, assume they want to know the weather forecast for it.", # noqa: E501
}
),
},
)
class WeatherForecatModel(BaseModel):
location_name: str
temperature_current_celsius: str
temperature_max_celsius: str
temperature_min_celsius: str
relative_humidity_percent: str
wind_speed_kmh: str
explanation: str
# Main interaction loop with user input
for prompt in reader:
reader.write("ℹ️ ", "enter a location for which you would like to get the weather forecast") # noqa: RUF001
response = await agent.run(prompt, expected_output=WeatherForecatModel).on(
"*",
log_intermediate_steps,
)
reader.write(
"Agent 🤖 : ",
to_json(WeatherForecatModel.model_validate_json(response.result.text), indent=2, sort_keys=False),
)
if __name__ == "__main__":
try:
asyncio.run(main())
except FrameworkError as e:
traceback.print_exc()
sys.exit(e.explain())