-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathyaml_tool_config_example.py
More file actions
256 lines (220 loc) · 7.85 KB
/
yaml_tool_config_example.py
File metadata and controls
256 lines (220 loc) · 7.85 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Example demonstrating YAML tool configuration with pre-filled parameters.
This example shows how to configure agents with tools that have pre-filled
parameters using YAML configuration files.
"""
import asyncio
from flo_ai.tool.flo_tool import flo_tool
from flo_ai.builder.agent_builder import AgentBuilder
# Define some example tools
@flo_tool(
description='Query BigQuery database',
parameter_descriptions={
'query': 'SQL query to execute',
'datasource_id': 'ID of the data source',
'project_id': 'Google Cloud project ID',
'dataset': 'BigQuery dataset name',
},
)
async def bigquery_query(
query: str, datasource_id: str, project_id: str, dataset: str
) -> str:
"""Execute a BigQuery query."""
return f"Executed query '{query}' on {project_id}.{dataset} using datasource {datasource_id}"
@flo_tool(
description='Search the web for information',
parameter_descriptions={
'query': 'Search query',
'max_results': 'Maximum number of results to return',
'language': 'Language for search results',
},
)
async def web_search(query: str, max_results: int = 10, language: str = 'en') -> str:
"""Search the web for information."""
return f"Found {max_results} results for '{query}' in {language}"
@flo_tool(
description='Perform mathematical calculations',
parameter_descriptions={'expression': 'Mathematical expression to evaluate'},
)
async def calculate(expression: str) -> str:
"""Calculate mathematical expressions."""
try:
result = eval(expression)
return f'Result: {result}'
except Exception as e:
return f'Error: {str(e)}'
@flo_tool(
description='Send email notifications',
parameter_descriptions={
'to': 'Recipient email address',
'subject': 'Email subject',
'body': 'Email body content',
'smtp_server': 'SMTP server address',
'smtp_port': 'SMTP server port',
},
)
async def send_email(
to: str, subject: str, body: str, smtp_server: str, smtp_port: int
) -> str:
"""Send an email notification."""
return f"Email sent to {to} with subject '{subject}' via {smtp_server}:{smtp_port}"
async def main():
"""Demonstrate YAML tool configuration."""
print('=== YAML Tool Configuration Example ===\n')
# Create a tool registry
tool_registry = {
'bigquery_query': bigquery_query.tool,
'web_search': web_search.tool,
'calculate': calculate.tool,
'send_email': send_email.tool,
}
# Example 1: Simple YAML with tool references
print('1. Simple YAML with tool references...')
simple_yaml = """
agent:
name: "Simple Data Analyst"
job: "You are a data analyst. Use the provided tools to analyze data."
model:
provider: "openai"
name: "gpt-4"
tools:
- "calculate"
- "web_search"
"""
try:
agent1 = AgentBuilder.from_yaml(simple_yaml, tool_registry=tool_registry)
print(f'Created agent: {agent1._name}')
print(f'Tools: {[tool.name for tool in agent1._tools]}')
except Exception as e:
print(f'Error (expected - no API key): {e}')
print()
# Example 2: YAML with tool configurations and pre-filled parameters
print('2. YAML with tool configurations and pre-filled parameters...')
config_yaml = """
agent:
name: "Advanced Data Analyst"
job: "You are an advanced data analyst with access to BigQuery and web search."
model:
provider: "openai"
name: "gpt-4"
tools:
- name: "bigquery_query"
prefilled_params:
datasource_id: "ds_production_123"
project_id: "my-company-prod"
dataset: "analytics"
name_override: "query_production_data"
description_override: "Query production BigQuery data"
- name: "web_search"
prefilled_params:
max_results: 5
language: "en"
name_override: "search_web"
description_override: "Search the web for information"
- name: "send_email"
prefilled_params:
smtp_server: "smtp.company.com"
smtp_port: 587
name_override: "send_notification"
description_override: "Send email notifications"
- "calculate" # Regular tool without pre-filling
"""
try:
agent2 = AgentBuilder.from_yaml(config_yaml, tool_registry=tool_registry)
print(f'Created agent: {agent2._name}')
print('Tools available to AI:')
for tool in agent2._tools:
print(f' - {tool.name}: {list(tool.parameters.keys())}')
except Exception as e:
print(f'Error (expected - no API key): {e}')
print()
# Example 3: Environment-specific configurations
print('3. Environment-specific configurations...')
dev_yaml = """
agent:
name: "Development Data Analyst"
job: "You are a data analyst working in the development environment."
model:
provider: "openai"
name: "gpt-4"
tools:
- name: "bigquery_query"
prefilled_params:
datasource_id: "ds_dev_456"
project_id: "my-company-dev"
dataset: "test_data"
name_override: "query_dev_data"
description_override: "Query development BigQuery data"
- name: "web_search"
prefilled_params:
max_results: 3
language: "en"
name_override: "search_web"
description_override: "Search the web for information"
"""
try:
agent3 = AgentBuilder.from_yaml(dev_yaml, tool_registry=tool_registry)
print(f'Created agent: {agent3._name}')
print('Tools available to AI:')
for tool in agent3._tools:
print(f' - {tool.name}: {list(tool.parameters.keys())}')
except Exception as e:
print(f'Error (expected - no API key): {e}')
print()
# Example 4: Mixed tool types
print('4. Mixed tool types...')
mixed_yaml = """
agent:
name: "Mixed Tool Agent"
job: "You are an agent with mixed tool configurations."
model:
provider: "openai"
name: "gpt-4"
tools:
- "calculate" # Simple reference
- name: "bigquery_query"
prefilled_params:
datasource_id: "ds_mixed_789"
project_id: "mixed-project"
dataset: "mixed_data"
- name: "web_search"
prefilled_params:
max_results: 10
language: "en"
name_override: "search_web"
description_override: "Search the web for information"
"""
try:
agent4 = AgentBuilder.from_yaml(mixed_yaml, tool_registry=tool_registry)
print(f'Created agent: {agent4._name}')
print('Tools available to AI:')
for tool in agent4._tools:
print(f' - {tool.name}: {list(tool.parameters.keys())}')
except Exception as e:
print(f'Error (expected - no API key): {e}')
print()
# Example 5: Show the difference between original and configured tools
print('5. Comparing original vs configured tools...')
# Create a tool with pre-filled parameters
configured_tool = tool_registry['bigquery_query']
print('Original BigQuery tool parameters:')
for param, info in configured_tool.parameters.items():
print(f" - {param}: {info['type']} (required: {info['required']})")
print('\nConfigured BigQuery tool parameters (AI view):')
# This would be the tool as seen by the AI after YAML configuration
# In practice, this would be created by the YAML processing
from flo_ai.tool.tool_config import ToolConfig
tool_config = ToolConfig(
tool=configured_tool,
prefilled_params={
'datasource_id': 'ds_production_123',
'project_id': 'my-company-prod',
'dataset': 'analytics',
},
)
configured_tool_for_ai = tool_config.to_tool()
for param, info in configured_tool_for_ai.parameters.items():
print(f" - {param}: {info['type']} (required: {info['required']})")
print(f'\nPre-filled parameters (hidden from AI): {tool_config.prefilled_params}')
if __name__ == '__main__':
asyncio.run(main())