@@ -57,20 +57,20 @@ from toolbox_langchain import ToolboxClient
57
57
from langchain_google_vertexai import ChatVertexAI
58
58
from langgraph.prebuilt import create_react_agent
59
59
60
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
61
- tools = toolbox.load_toolset()
60
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
61
+ tools = toolbox.load_toolset()
62
62
63
- model = ChatVertexAI(model = " gemini-2.0-flash-001" )
64
- agent = create_react_agent(model, tools)
63
+ model = ChatVertexAI(model = " gemini-2.0-flash-001" )
64
+ agent = create_react_agent(model, tools)
65
65
66
- prompt = " How's the weather today?"
66
+ prompt = " How's the weather today?"
67
67
68
- for s in agent.stream({" messages" : [(" user" , prompt)]}, stream_mode = " values" ):
69
- message = s[" messages" ][- 1 ]
70
- if isinstance (message, tuple ):
71
- print (message)
72
- else :
73
- message.pretty_print()
68
+ for s in agent.stream({" messages" : [(" user" , prompt)]}, stream_mode = " values" ):
69
+ message = s[" messages" ][- 1 ]
70
+ if isinstance (message, tuple ):
71
+ print (message)
72
+ else :
73
+ message.pretty_print()
74
74
```
75
75
76
76
> [ !TIP]
@@ -86,7 +86,7 @@ Import and initialize the toolbox client.
86
86
from toolbox_langchain import ToolboxClient
87
87
88
88
# Replace with your Toolbox service's URL
89
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
89
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
90
90
```
91
91
92
92
## Loading Tools
@@ -241,10 +241,10 @@ You can configure these dynamic headers as follows:
241
241
``` python
242
242
from toolbox_langchain import ToolboxClient
243
243
244
- client = ToolboxClient(
244
+ async with ToolboxClient(
245
245
" toolbox-url" ,
246
246
client_headers = {" header1" : header1_getter, " header2" : header2_getter, ... }
247
- )
247
+ ) as client:
248
248
```
249
249
250
250
### Authenticating with Google Cloud Servers
@@ -273,13 +273,13 @@ For Toolbox servers hosted on Google Cloud (e.g., Cloud Run) and requiring
273
273
from toolbox_core import auth_methods
274
274
275
275
auth_token_provider = auth_methods.aget_google_id_token # can also use sync method
276
- client = ToolboxClient(
276
+ async with ToolboxClient(
277
277
URL ,
278
278
client_headers = {" Authorization" : auth_token_provider},
279
- )
280
- tools = client.load_toolset()
279
+ ) as client:
280
+ tools = client.load_toolset()
281
281
282
- # Now, you can use the client as usual.
282
+ # Now, you can use the client as usual.
283
283
```
284
284
285
285
@@ -319,16 +319,16 @@ async def get_auth_token():
319
319
# ### Add Authentication to a Tool
320
320
321
321
```py
322
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
323
- tools = toolbox.load_toolset()
322
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
323
+ tools = toolbox.load_toolset()
324
324
325
- auth_tool = tools[0 ].add_auth_token_getter(" my_auth" , get_auth_token) # Single token
325
+ auth_tool = tools[0 ].add_auth_token_getter(" my_auth" , get_auth_token) # Single token
326
326
327
- multi_auth_tool = tools[0 ].add_auth_token_getters({" auth_1" : get_auth_1}, {" auth_2" : get_auth_2}) # Multiple tokens
327
+ multi_auth_tool = tools[0 ].add_auth_token_getters({" auth_1" : get_auth_1}, {" auth_2" : get_auth_2}) # Multiple tokens
328
328
329
- # OR
329
+ # OR
330
330
331
- auth_tools = [tool.add_auth_token_getter(" my_auth" , get_auth_token) for tool in tools]
331
+ auth_tools = [tool.add_auth_token_getter(" my_auth" , get_auth_token) for tool in tools]
332
332
```
333
333
334
334
# ### Add Authentication While Loading
@@ -354,12 +354,12 @@ async def get_auth_token():
354
354
# This example just returns a placeholder. Replace with your actual token retrieval.
355
355
return " YOUR_ID_TOKEN" # Placeholder
356
356
357
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
358
- tool = toolbox.load_tool(" my-tool" )
357
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
358
+ tool = toolbox.load_tool(" my-tool" )
359
359
360
- auth_tool = tool.add_auth_token_getter(" my_auth" , get_auth_token)
361
- result = auth_tool.invoke({" input" : " some input" })
362
- print (result)
360
+ auth_tool = tool.add_auth_token_getter(" my_auth" , get_auth_token)
361
+ result = auth_tool.invoke({" input" : " some input" })
362
+ print (result)
363
363
```
364
364
365
365
# # Binding Parameter Values
@@ -374,16 +374,16 @@ modified by the LLM. This is useful for:
374
374
# ## Binding Parameters to a Tool
375
375
376
376
```py
377
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
378
- tools = toolbox.load_toolset()
377
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
378
+ tools = toolbox.load_toolset()
379
379
380
- bound_tool = tool[0 ].bind_param(" param" , " value" ) # Single param
380
+ bound_tool = tool[0 ].bind_param(" param" , " value" ) # Single param
381
381
382
- multi_bound_tool = tools[0 ].bind_params({" param1" : " value1" , " param2" : " value2" }) # Multiple params
382
+ multi_bound_tool = tools[0 ].bind_params({" param1" : " value1" , " param2" : " value2" }) # Multiple params
383
383
384
- # OR
384
+ # OR
385
385
386
- bound_tools = [tool.bind_param(" param" , " value" ) for tool in tools]
386
+ bound_tools = [tool.bind_param(" param" , " value" ) for tool in tools]
387
387
```
388
388
389
389
# ## Binding Parameters While Loading
@@ -429,10 +429,10 @@ import asyncio
429
429
from toolbox_langchain import ToolboxClient
430
430
431
431
async def main():
432
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
433
- tool = await client.aload_tool(" my-tool" )
434
- tools = await client.aload_toolset()
435
- response = await tool.ainvoke()
432
+ async with ToolboxClient(" http://127.0.0.1:5000" ) as toolbox:
433
+ tool = await client.aload_tool(" my-tool" )
434
+ tools = await client.aload_toolset()
435
+ response = await tool.ainvoke()
436
436
437
437
if __name__ == " __main__" :
438
438
asyncio.run(main())
0 commit comments