@@ -30,7 +30,7 @@ applications, enabling advanced orchestration and interaction with GenAI models.
30
30
- [ Binding Parameters to a Tool] ( #binding-parameters-to-a-tool )
31
31
- [ Binding Parameters While Loading] ( #binding-parameters-while-loading )
32
32
- [ Binding Dynamic Values] ( #binding-dynamic-values )
33
- - [ Error Handling ] ( #error-handling )
33
+ - [ Asynchronous Usage ] ( #asynchronous-usage )
34
34
35
35
<!-- /TOC -->
36
36
@@ -39,21 +39,16 @@ applications, enabling advanced orchestration and interaction with GenAI models.
39
39
Here's a minimal example to get you started:
40
40
41
41
``` py
42
- import asyncio
43
42
from toolbox_langchain_sdk import ToolboxClient
44
43
from langchain_google_vertexai import ChatVertexAI
45
44
46
- async def main ():
47
- toolbox = ToolboxClient(" http://127.0.0.1:5000" )
48
- tools = await toolbox.load_toolset()
49
-
50
- model = ChatVertexAI(model = " gemini-1.5-pro-002" )
51
- agent = model.bind_tools(tools)
52
- result = agent.invoke(" How's the weather today?" )
53
- print (result)
45
+ toolbox = ToolboxClient(" http://127.0.0.1:5000" )
46
+ tools = toolbox.load_toolset()
54
47
55
- if __name__ == " __main__" :
56
- asyncio.run(main())
48
+ model = ChatVertexAI(model = " gemini-1.5-pro-002" )
49
+ agent = model.bind_tools(tools)
50
+ result = agent.invoke(" How's the weather today?" )
51
+ print (result)
57
52
```
58
53
59
54
## Installation
@@ -79,18 +74,6 @@ from toolbox_langchain_sdk import ToolboxClient
79
74
toolbox = ToolboxClient(" http://127.0.0.1:5000" )
80
75
```
81
76
82
- > [ !IMPORTANT]
83
- > The toolbox client requires an asynchronous environment.
84
- > For guidance on running asynchronous Python programs, see
85
- > [ asyncio documentation] ( https://docs.python.org/3/library/asyncio-runner.html#running-an-asyncio-program ) .
86
-
87
- > [ !TIP]
88
- > You can also pass your own ` ClientSession ` to reuse the same session:
89
- > ``` py
90
- > async with ClientSession() as session:
91
- > toolbox = ToolboxClient(" http://localhost:5000" , session)
92
- > ```
93
-
94
77
## Loading Tools
95
78
96
79
### Load a toolset
@@ -100,16 +83,16 @@ or a specific one:
100
83
101
84
``` py
102
85
# Load all tools
103
- tools = await toolbox.load_toolset()
86
+ tools = toolbox.load_toolset()
104
87
105
88
# Load a specific toolset
106
- tools = await toolbox.load_toolset(" my-toolset" )
89
+ tools = toolbox.load_toolset(" my-toolset" )
107
90
```
108
91
109
92
### Load a single tool
110
93
111
94
``` py
112
- tool = await toolbox.load_tool(" my-tool" )
95
+ tool = toolbox.load_tool(" my-tool" )
113
96
```
114
97
115
98
Loading individual tools gives you finer-grained control over which tools are
@@ -190,10 +173,10 @@ graph.invoke({"messages": [HumanMessage(content="Do something with the tools")]}
190
173
191
174
## Manual usage
192
175
193
- Execute a tool manually using the ` ainvoke ` method:
176
+ Execute a tool manually using the ` invoke ` method:
194
177
195
178
``` py
196
- result = await tools[0 ].ainvoke ({" name" : " Alice" , " age" : 30 })
179
+ result = tools[0 ].invoke ({" name" : " Alice" , " age" : 30 })
197
180
```
198
181
199
182
This is useful for testing tools or when you need precise control over tool
@@ -235,8 +218,8 @@ async def get_auth_token():
235
218
#### Add Authentication to a Tool
236
219
237
220
``` py
238
- toolbox = ToolboxClient(" http://localhost :5000" )
239
- tools = await toolbox.load_toolset()
221
+ toolbox = ToolboxClient(" http://127.0.0.1 :5000" )
222
+ tools = toolbox.load_toolset()
240
223
241
224
auth_tool = tools[0 ].add_auth_token(" my_auth" , get_auth_token) # Single token
242
225
@@ -250,9 +233,9 @@ auth_tools = [tool.add_auth_token("my_auth", get_auth_token) for tool in tools]
250
233
#### Add Authentication While Loading
251
234
252
235
``` py
253
- auth_tool = await toolbox.load_tool(auth_tokens = {" my_auth" : get_auth_token})
236
+ auth_tool = toolbox.load_tool(auth_tokens = {" my_auth" : get_auth_token})
254
237
255
- auth_tools = await toolbox.load_toolset(auth_tokens = {" my_auth" : get_auth_token})
238
+ auth_tools = toolbox.load_toolset(auth_tokens = {" my_auth" : get_auth_token})
256
239
```
257
240
258
241
> [ !NOTE]
@@ -262,24 +245,19 @@ auth_tools = await toolbox.load_toolset(auth_tokens={"my_auth": get_auth_token})
262
245
### Complete Example
263
246
264
247
``` py
265
- import asyncio
266
248
from toolbox_langchain_sdk import ToolboxClient
267
249
268
250
async def get_auth_token ():
269
251
# ... Logic to retrieve ID token (e.g., from local storage, OAuth flow)
270
252
# This example just returns a placeholder. Replace with your actual token retrieval.
271
253
return " YOUR_ID_TOKEN" # Placeholder
272
254
273
- async def main ():
274
- toolbox = ToolboxClient(" http://localhost:5000" )
275
- tool = await toolbox.load_tool(" my-tool" )
276
-
277
- auth_tool = tool.add_auth_token(" my_auth" , get_auth_token)
278
- result = await auth_tool.ainvoke({" input" : " some input" })
279
- print (result)
255
+ toolbox = ToolboxClient(" http://127.0.0.1:5000" )
256
+ tool = toolbox.load_tool(" my-tool" )
280
257
281
- if __name__ == " __main__" :
282
- asyncio.run(main())
258
+ auth_tool = tool.add_auth_token(" my_auth" , get_auth_token)
259
+ result = auth_tool.invoke({" input" : " some input" })
260
+ print (result)
283
261
```
284
262
285
263
## Binding Parameter Values
@@ -294,8 +272,8 @@ modified by the LLM. This is useful for:
294
272
### Binding Parameters to a Tool
295
273
296
274
``` py
297
- toolbox = ToolboxClient(" http://localhost :5000" )
298
- tools = await toolbox.load_toolset()
275
+ toolbox = ToolboxClient(" http://127.0.0.1 :5000" )
276
+ tools = toolbox.load_toolset()
299
277
300
278
bound_tool = tool[0 ].bind_param(" param" , " value" ) # Single param
301
279
@@ -309,9 +287,9 @@ bound_tools = [tool.bind_param("param", "value") for tool in tools]
309
287
### Binding Parameters While Loading
310
288
311
289
``` py
312
- bound_tool = await toolbox.load_tool(bound_params = {" param" : " value" })
290
+ bound_tool = toolbox.load_tool(bound_params = {" param" : " value" })
313
291
314
- bound_tools = await toolbox.load_toolset(bound_params = {" param" : " value" })
292
+ bound_tools = toolbox.load_toolset(bound_params = {" param" : " value" })
315
293
```
316
294
317
295
> [ !NOTE]
@@ -332,15 +310,28 @@ dynamic_bound_tool = tool.bind_param("param", get_dynamic_value)
332
310
> [ !IMPORTANT]
333
311
> You don't need to modify tool configurations to bind parameter values.
334
312
335
- ## Error Handling
313
+ ## Asynchronous Usage
314
+
315
+ For better performance through [ cooperative
316
+ multitasking] ( https://en.wikipedia.org/wiki/Cooperative_multitasking ) , you can
317
+ use the asynchronous interfaces of the ` ToolboxClient ` .
336
318
337
- When interacting with the Toolbox service or executing tools, you might
338
- encounter errors. Handle potential exceptions gracefully:
319
+ > [ !Note]
320
+ > Asynchronous interfaces like ` aload_tool ` and ` aload_toolset ` require an
321
+ > asynchronous environment. For guidance on running asynchronous Python
322
+ > programs, see [ asyncio
323
+ > documentation] ( https://docs.python.org/3/library/asyncio-runner.html#running-an-asyncio-program ) .
339
324
340
325
``` py
341
- try :
342
- result = await tool.ainvoke({" input" : " some input" })
343
- except Exception as e:
344
- print (f " An error occurred: { e} " )
345
- # Implement error recovery logic, e.g., retrying the request or logging the error
326
+ import asyncio
327
+ from toolbox_langchain_sdk import ToolboxClient
328
+
329
+ async def main ():
330
+ toolbox = ToolboxClient(" http://127.0.0.1:5000" )
331
+ tool = await client.aload_tool(" my-tool" )
332
+ tools = await client.aload_toolset()
333
+ response = await tool.ainvoke()
334
+
335
+ if __name__ == " __main__" :
336
+ asyncio.run(main())
346
337
```
0 commit comments