@@ -59,20 +59,20 @@ from llama_index.core.agent.workflow import AgentWorkflow
59
59
from toolbox_llamaindex import ToolboxClient
60
60
61
61
async def run_agent ():
62
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
63
- tools = toolbox.load_toolset()
64
-
65
- vertex_model = GoogleGenAI(
66
- model = " gemini-2.0-flash-001" ,
67
- vertexai_config = {" project" : " project-id" , " location" : " us-central1" },
68
- )
69
- agent = AgentWorkflow.from_tools_or_functions(
70
- tools,
71
- llm = vertex_model,
72
- system_prompt = " You are a helpful assistant." ,
73
- )
74
- response = await agent.run(user_msg = " Get some response from the agent." )
75
- print (response)
62
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
63
+ tools = toolbox.load_toolset()
64
+
65
+ vertex_model = GoogleGenAI(
66
+ model = " gemini-2.0-flash-001" ,
67
+ vertexai_config = {" project" : " project-id" , " location" : " us-central1" },
68
+ )
69
+ agent = AgentWorkflow.from_tools_or_functions(
70
+ tools,
71
+ llm = vertex_model,
72
+ system_prompt = " You are a helpful assistant." ,
73
+ )
74
+ response = await agent.run(user_msg = " Get some response from the agent." )
75
+ print (response)
76
76
77
77
asyncio.run(run_agent())
78
78
```
@@ -90,7 +90,7 @@ Import and initialize the toolbox client.
90
90
from toolbox_llamaindex import ToolboxClient
91
91
92
92
# Replace with your Toolbox service's URL
93
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
93
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
94
94
```
95
95
96
96
## Loading Tools
@@ -222,10 +222,10 @@ You can configure these dynamic headers as follows:
222
222
``` python
223
223
from toolbox_llamaindex import ToolboxClient
224
224
225
- client = ToolboxClient(
225
+ async with ToolboxClient(
226
226
" toolbox-url" ,
227
227
client_headers = {" header1" : header1_getter, " header2" : header2_getter, ... }
228
- )
228
+ ) as client:
229
229
```
230
230
231
231
### Authenticating with Google Cloud Servers
@@ -252,13 +252,13 @@ For Toolbox servers hosted on Google Cloud (e.g., Cloud Run) and requiring
252
252
from toolbox_core import auth_methods
253
253
254
254
auth_token_provider = auth_methods.aget_google_id_token # can also use sync method
255
- client = ToolboxClient(
255
+ async with ToolboxClient(
256
256
URL ,
257
257
client_headers = {" Authorization" : auth_token_provider},
258
- )
259
- tools = await client.aload_toolset()
258
+ ) as client:
259
+ tools = await client.aload_toolset()
260
260
261
- # Now, you can use the client as usual.
261
+ # Now, you can use the client as usual.
262
262
```
263
263
264
264
# # Authenticating Tools
@@ -297,16 +297,16 @@ async def get_auth_token():
297
297
# ### Add Authentication to a Tool
298
298
299
299
```py
300
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
301
- tools = toolbox.load_toolset()
300
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
301
+ tools = toolbox.load_toolset()
302
302
303
- auth_tool = tools[0 ].add_auth_token_getter(" my_auth" , get_auth_token) # Single token
303
+ auth_tool = tools[0 ].add_auth_token_getter(" my_auth" , get_auth_token) # Single token
304
304
305
- multi_auth_tool = tools[0 ].add_auth_token_getters({" auth_1" : get_auth_1}, {" auth_2" : get_auth_2}) # Multiple tokens
305
+ multi_auth_tool = tools[0 ].add_auth_token_getters({" auth_1" : get_auth_1}, {" auth_2" : get_auth_2}) # Multiple tokens
306
306
307
- # OR
307
+ # OR
308
308
309
- auth_tools = [tool.add_auth_token_getter(" my_auth" , get_auth_token) for tool in tools]
309
+ auth_tools = [tool.add_auth_token_getter(" my_auth" , get_auth_token) for tool in tools]
310
310
```
311
311
312
312
# ### Add Authentication While Loading
@@ -332,12 +332,12 @@ async def get_auth_token():
332
332
# This example just returns a placeholder. Replace with your actual token retrieval.
333
333
return " YOUR_ID_TOKEN" # Placeholder
334
334
335
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
336
- tool = toolbox.load_tool(" my-tool" )
335
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
336
+ tool = toolbox.load_tool(" my-tool" )
337
337
338
- auth_tool = tool.add_auth_token_getter(" my_auth" , get_auth_token)
339
- result = auth_tool.call(input = " some input" )
340
- print (result)
338
+ auth_tool = tool.add_auth_token_getter(" my_auth" , get_auth_token)
339
+ result = auth_tool.call(input = " some input" )
340
+ print (result)
341
341
```
342
342
343
343
# # Binding Parameter Values
@@ -352,16 +352,16 @@ modified by the LLM. This is useful for:
352
352
# ## Binding Parameters to a Tool
353
353
354
354
```py
355
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
356
- tools = toolbox.load_toolset()
355
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
356
+ tools = toolbox.load_toolset()
357
357
358
- bound_tool = tool[0 ].bind_param(" param" , " value" ) # Single param
358
+ bound_tool = tool[0 ].bind_param(" param" , " value" ) # Single param
359
359
360
- multi_bound_tool = tools[0 ].bind_params({" param1" : " value1" , " param2" : " value2" }) # Multiple params
360
+ multi_bound_tool = tools[0 ].bind_params({" param1" : " value1" , " param2" : " value2" }) # Multiple params
361
361
362
- # OR
362
+ # OR
363
363
364
- bound_tools = [tool.bind_param(" param" , " value" ) for tool in tools]
364
+ bound_tools = [tool.bind_param(" param" , " value" ) for tool in tools]
365
365
```
366
366
367
367
# ## Binding Parameters While Loading
@@ -407,10 +407,10 @@ import asyncio
407
407
from toolbox_llamaindex import ToolboxClient
408
408
409
409
async def main():
410
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
411
- tool = await client.aload_tool(" my-tool" )
412
- tools = await client.aload_toolset()
413
- response = await tool.ainvoke()
410
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
411
+ tool = await client.aload_tool(" my-tool" )
412
+ tools = await client.aload_toolset()
413
+ response = await tool.ainvoke()
414
414
415
415
if __name__ == " __main__" :
416
416
asyncio.run(main())
0 commit comments