-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (154 loc) · 5.69 KB
/
main.py
File metadata and controls
190 lines (154 loc) · 5.69 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from pathlib import Path
from typing import Any
from pydantic import Field
from tinygent.agents import TinyReActAgent
from tinygent.agents.middleware import TinyBaseMiddleware
from tinygent.agents.middleware import register_middleware
from tinygent.core.datamodels.tool import AbstractTool
from tinygent.core.factory import build_llm
from tinygent.core.types import TinyModel
from tinygent.logging import setup_logger
from tinygent.memory import BufferChatMemory
from tinygent.prompts import ReActPromptTemplate
from tinygent.tools import register_tool
from tinygent.tools.reasoning_tool import register_reasoning_tool
from tinygent.utils import TinyColorPrinter
from tinygent.utils import tiny_yaml_load
logger = setup_logger('debug')
@register_middleware('react_tool_tracker')
class ReActToolTrackerMiddleware(TinyBaseMiddleware):
"""Middleware that tracks tool calls in ReAct agent."""
def __init__(self) -> None:
self.tool_calls: list[dict[str, Any]] = []
self.total_calls = 0
async def before_tool_call(
self,
*,
run_id: str,
tool: AbstractTool,
args: dict[str, Any],
kwargs: dict[str, Any],
) -> None:
self.total_calls += 1
print(
TinyColorPrinter.custom(
'TOOL CALL',
f'[Call #{self.total_calls}] {tool.info.name}({args})',
color='YELLOW',
)
)
async def after_tool_call(
self,
*,
run_id: str,
tool: AbstractTool,
args: dict[str, Any],
result: Any,
kwargs: dict[str, Any],
) -> None:
call_record = {
'tool': tool.info.name,
'args': args,
'result': str(result),
}
self.tool_calls.append(call_record)
print(
TinyColorPrinter.custom(
'TOOL RESULT',
f'[Call #{self.total_calls}] {str(result)[:100]}...'
if len(str(result)) > 100
else f'[Call #{self.total_calls}] {result}',
color='MAGENTA',
)
)
async def on_tool_reasoning(
self, *, run_id: str, reasoning: str, kwargs: dict[str, Any]
) -> None:
print(
TinyColorPrinter.custom(
'TOOL REASONING',
f'{reasoning[:150]}...' if len(reasoning) > 150 else reasoning,
color='CYAN',
)
)
async def on_answer(
self, *, run_id: str, answer: str, kwargs: dict[str, Any]
) -> None:
print(
TinyColorPrinter.custom(
'FINAL ANSWER',
f'[Run: {run_id[:8]}...] After {self.total_calls} tool calls',
color='GREEN',
)
)
async def on_answer_chunk(
self, *, run_id: str, chunk: str, idx: str, kwargs: dict[str, Any]
) -> None:
print(
TinyColorPrinter.custom(
'STREAM',
f'[{idx}] {chunk}',
color='BLUE',
)
)
async def on_error(
self, *, run_id: str, e: Exception, kwargs: dict[str, Any]
) -> None:
print(TinyColorPrinter.error(f'Error: {e}'))
def get_tool_calls(self) -> list[dict[str, Any]]:
"""Return all tool calls."""
return self.tool_calls
def get_summary(self) -> dict[str, Any]:
"""Return summary of tool usage."""
tools_used = [c['tool'] for c in self.tool_calls]
return {
'total_calls': self.total_calls,
'unique_tools': list(set(tools_used)),
}
# NOTE: Using @register_tool decorator to register tools globally,
# allowing them to be discovered and reused by:
# - quick.py via discover_and_register_components()
# - CLI terminal command via config-based agent building
class WeatherInput(TinyModel):
location: str = Field(..., description='The location to get the weather for.')
@register_tool
def get_weather(data: WeatherInput) -> str:
"""Get the current weather in a given location."""
return f'The weather in {data.location} is sunny with a high of 75°F.'
class GetBestDestinationInput(TinyModel):
top_k: int = Field(..., description='The number of top destinations to return.')
@register_reasoning_tool
def get_best_destination(data: GetBestDestinationInput) -> list[str]:
"""Get the best travel destinations."""
destinations = {'Paris', 'New York', 'Tokyo', 'Barcelona', 'Rome'}
return list(destinations)[: data.top_k]
async def main():
react_agent_prompt = tiny_yaml_load(str(Path(__file__).parent / 'prompts.yaml'))
react_middleware = ReActToolTrackerMiddleware()
react_agent = TinyReActAgent(
llm=build_llm('openai:gpt-4o', temperature=0.1),
max_iterations=3,
memory=BufferChatMemory(),
prompt_template=ReActPromptTemplate(**react_agent_prompt),
tools=[get_weather, get_best_destination],
middleware=[react_middleware],
)
result: str = ''
async for chunk in react_agent.run_stream(
'What is the best travel destination and what is the weather like there?'
):
logger.info('[STREAM CHUNK] %s', chunk)
result += chunk
logger.info('[RESULT] %s', result)
logger.info('[MEMORY] %s', react_agent.memory.load_variables())
logger.info('[AGENT SUMMARY] %s', str(react_agent))
print('\nTool Usage Summary:')
summary = react_middleware.get_summary()
for key, value in summary.items():
print(f'\t{key}: {value}')
print('\nTool Call Log:')
for i, call in enumerate(react_middleware.get_tool_calls(), 1):
print(f'\t{i}. {call["tool"]}({call["args"]}) -> {call["result"][:50]}...')
if __name__ == '__main__':
import asyncio
asyncio.run(main())