You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This project shows how to build an agent that performs 2 advanced methods of tool calling (brought by Claude).
4
+
5
+
1st is to encode tool metadata that could be search with embedding, instead of loading all tool info into context. **Progressively disclose** most matching tool info into context window, hence tool info could be more rich like adding best examples.
6
+
7
+
2nd is (quote) "**Programmatic Tool Calling (PTC)** enables Claude to orchestrate tools **through code** rather than **through individual API round-trips**. Instead of Claude requesting tools one at a time with each result being returned to its context, Claude writes code that calls multiple tools, processes their outputs, and controls what information actually enters its context window."
8
+
9
+
This implementation is based on :
10
+
11
+
- the article: [Introducing advanced tool use on the Claude Developer Platform](https://www.anthropic.com/engineering/advanced-tool-use)
12
+
13
+
- the cookbook: [Programmatic Tool Calling (PTC)](https://github.com/anthropics/claude-cookbooks/blob/main/tool_use/programmatic_tool_calling_ptc.ipynb)
14
+
15
+
- the cookbook: [Tool Search With Embeddings](https://github.com/anthropics/claude-cookbooks/blob/main/tool_use/tool_search_with_embeddings.ipynb)
16
+
17
+
## Features
18
+
19
+
- Search relatively a big number of tools based on embedding
20
+
- Match with similairy score (easy to scale), progressively loading tool info into context
21
+
- Support both single tool calling based on PocketFlow
22
+
- Support multiple tool calling (by generated code using provided tools)
23
+
24
+
## How to Run
25
+
26
+
1. Set your API key:
27
+
```bash
28
+
export OPENAI_API_KEY="your-api-key-here"
29
+
```
30
+
Or update it directly in `utils.py`
31
+
32
+
2. Install and run:
33
+
```bash
34
+
pip install -r requirements.txt
35
+
python main.py
36
+
```
37
+
38
+
## An Easier Way for Multiple Tool Calling and Save Lots of Tokens
39
+
40
+
### Typical Tool Calling Flow
41
+
42
+
- User input a complex question (like a sequence of compound SQL statements) that needed rounds of tool calling (depending on how smart of the model either).
43
+
44
+
-**Model Loaded all tool info into context**
45
+
46
+
- Model run the **1st call and get the 1st result** .
47
+
48
+
- Pass result into next round of prompt .
49
+
50
+
- Start the **2nd round , and then 3rd , 4th**....
51
+
52
+
- According Claude's article , this will cause 10x more token usage
53
+
54
+
### New Method
55
+
56
+
- User input a complex question that needed rounds of tool calling.
57
+
58
+
- Model search most-relevant tools and get tool name , params, input schema into context .
59
+
60
+
- Model knew the tool could be called with python scripting , so generate python code to run .
61
+
62
+
- Code finished running in sandbox and return results back to Model .
63
+
64
+
- Then Model will process all the results in next round .
The agent uses PocketFlow to create a workflow where:
87
+
1. ReasonNode takes user input about Stock Tickers
88
+
2. ReasonNode choose what string to search with Tools (embeddings)
89
+
3. ToolsNode returned search results (similarity score, name, parameters)
90
+
4. ReasonNode choose to make single tool calling , or generate python code for multiple rounds of tool calling in one shot (which saved A LOT of tokens!)
91
+
5. AnswerNode craft final answers based on ReasonNode's context
92
+
93
+
## Files
94
+
95
+
-[`main.py`](./main.py): Implementation of nodes and flow assembling
96
+
-[`utils.py`](./utils.py): Helper functions tool calling and tool coding running (minimal unsafe way of using subprocess, only for demo purpose)
0 commit comments