Skip to content

Commit f470833

Browse files
authored
doc(langchain-sdk): Documentation updates to README for changes related to sync/async support. (#16)
1 parent 9885469 commit f470833

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

DEVELOPER.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Below are the details to set up a development environment and run tests.
55
## Install
66
1. Clone the repository:
77
```bash
8-
98
git clone https://github.com/googleapis/genai-toolbox-langchain-python
109
```
1110
1. Navigate to the repo directory:

README.md

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ applications, enabling advanced orchestration and interaction with GenAI models.
3030
- [Binding Parameters to a Tool](#binding-parameters-to-a-tool)
3131
- [Binding Parameters While Loading](#binding-parameters-while-loading)
3232
- [Binding Dynamic Values](#binding-dynamic-values)
33-
- [Error Handling](#error-handling)
33+
- [Asynchronous Usage](#asynchronous-usage)
3434

3535
<!-- /TOC -->
3636

@@ -39,21 +39,16 @@ applications, enabling advanced orchestration and interaction with GenAI models.
3939
Here's a minimal example to get you started:
4040

4141
```py
42-
import asyncio
4342
from toolbox_langchain_sdk import ToolboxClient
4443
from langchain_google_vertexai import ChatVertexAI
4544

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()
5447

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)
5752
```
5853

5954
## Installation
@@ -79,18 +74,6 @@ from toolbox_langchain_sdk import ToolboxClient
7974
toolbox = ToolboxClient("http://127.0.0.1:5000")
8075
```
8176

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-
9477
## Loading Tools
9578

9679
### Load a toolset
@@ -100,16 +83,16 @@ or a specific one:
10083

10184
```py
10285
# Load all tools
103-
tools = await toolbox.load_toolset()
86+
tools = toolbox.load_toolset()
10487

10588
# Load a specific toolset
106-
tools = await toolbox.load_toolset("my-toolset")
89+
tools = toolbox.load_toolset("my-toolset")
10790
```
10891

10992
### Load a single tool
11093

11194
```py
112-
tool = await toolbox.load_tool("my-tool")
95+
tool = toolbox.load_tool("my-tool")
11396
```
11497

11598
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")]}
190173

191174
## Manual usage
192175

193-
Execute a tool manually using the `ainvoke` method:
176+
Execute a tool manually using the `invoke` method:
194177

195178
```py
196-
result = await tools[0].ainvoke({"name": "Alice", "age": 30})
179+
result = tools[0].invoke({"name": "Alice", "age": 30})
197180
```
198181

199182
This is useful for testing tools or when you need precise control over tool
@@ -235,8 +218,8 @@ async def get_auth_token():
235218
#### Add Authentication to a Tool
236219

237220
```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()
240223

241224
auth_tool = tools[0].add_auth_token("my_auth", get_auth_token) # Single token
242225

@@ -250,9 +233,9 @@ auth_tools = [tool.add_auth_token("my_auth", get_auth_token) for tool in tools]
250233
#### Add Authentication While Loading
251234

252235
```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})
254237

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})
256239
```
257240

258241
> [!NOTE]
@@ -262,24 +245,19 @@ auth_tools = await toolbox.load_toolset(auth_tokens={"my_auth": get_auth_token})
262245
### Complete Example
263246

264247
```py
265-
import asyncio
266248
from toolbox_langchain_sdk import ToolboxClient
267249

268250
async def get_auth_token():
269251
# ... Logic to retrieve ID token (e.g., from local storage, OAuth flow)
270252
# This example just returns a placeholder. Replace with your actual token retrieval.
271253
return "YOUR_ID_TOKEN" # Placeholder
272254

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")
280257

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)
283261
```
284262

285263
## Binding Parameter Values
@@ -294,8 +272,8 @@ modified by the LLM. This is useful for:
294272
### Binding Parameters to a Tool
295273

296274
```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()
299277

300278
bound_tool = tool[0].bind_param("param", "value") # Single param
301279

@@ -309,9 +287,9 @@ bound_tools = [tool.bind_param("param", "value") for tool in tools]
309287
### Binding Parameters While Loading
310288

311289
```py
312-
bound_tool = await toolbox.load_tool(bound_params={"param": "value"})
290+
bound_tool = toolbox.load_tool(bound_params={"param": "value"})
313291

314-
bound_tools = await toolbox.load_toolset(bound_params={"param": "value"})
292+
bound_tools = toolbox.load_toolset(bound_params={"param": "value"})
315293
```
316294

317295
> [!NOTE]
@@ -332,15 +310,28 @@ dynamic_bound_tool = tool.bind_param("param", get_dynamic_value)
332310
> [!IMPORTANT]
333311
> You don't need to modify tool configurations to bind parameter values.
334312
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`.
336318

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).
339324
340325
```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())
346337
```

0 commit comments

Comments
 (0)