-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathtool_calling.py
60 lines (44 loc) · 1.69 KB
/
tool_calling.py
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
import asyncio
import logging
import sys
import traceback
from typing import Any
from dotenv import load_dotenv
from beeai_framework.agents.tool_calling import ToolCallingAgent
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 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 process_agent_events(data: Any, event: EventMeta) -> None:
"""Process agent events and log appropriately"""
if event.name == "start":
reader.write("Agent (debug) 🤖 : ", "starting new iteration")
elif event.name == "success":
reader.write("Agent (debug) 🤖 : ", data.state.memory.messages[-1])
async def main() -> None:
"""Main application loop"""
# Create agent
agent = ToolCallingAgent(
llm=ChatModel.from_name("ollama:llama3.1"), memory=UnconstrainedMemory(), tools=[OpenMeteoTool()]
)
# Main interaction loop with user input
for prompt in reader:
response = await agent.run(prompt).on("*", process_agent_events)
reader.write("Agent 🤖 : ", response.result.text)
print("======DONE (showing the full message history)=======")
for msg in response.memory.messages:
print(msg)
if __name__ == "__main__":
try:
asyncio.run(main())
except FrameworkError as e:
traceback.print_exc()
sys.exit(e.explain())