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
Copy file name to clipboardExpand all lines: docs/python-api.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,6 +148,46 @@ for response in chain.responses():
148
148
print(chunk, end="", flush=True)
149
149
```
150
150
151
+
(python-api-tools-debug-hooks)=
152
+
153
+
#### Tool debugging hooks
154
+
155
+
Pass a function to the `before_call=` parameter of `model.chain()` to have that called before every tool call is executed. You can raise `llm.CancelToolCall()` to cancel that tool call.
156
+
157
+
The method signature is `def before_call(tool: llm.Tool, tool_call: llm.ToolCall)`. Here's an example:
print(f"About to call tool {tool.name} with arguments {tool_call.arguments}")
167
+
if tool.name =="upper"and"bad"inrepr(tool_call.arguments):
168
+
raise llm.CancelToolCall("Not allowed to call upper on text containing 'bad'")
169
+
170
+
model = llm.get_model("gpt-4.1-mini")
171
+
response = model.chain(
172
+
"Convert panda to upper and badger to upper",
173
+
tools=[upper],
174
+
before_call=before_call,
175
+
)
176
+
print(response.text())
177
+
```
178
+
The `after_call=` parameter can be used to run a logging function after each tool call has been executed. The method signature is `def after_call(tool: llm.Tool, tool_call: llm.ToolCall, tool_result: llm.ToolResult)`. This continues the previous example:
print(f"Tool {tool.name} called with arguments {tool_call.arguments} returned {tool_result.output}")
182
+
183
+
response = model.chain(
184
+
"Convert panda to upper and badger to upper",
185
+
tools=[upper],
186
+
after_call=after_call,
187
+
)
188
+
print(response.text())
189
+
```
190
+
151
191
(python-api-tools-attachments)=
152
192
153
193
#### Tools can return attachments
@@ -575,6 +615,8 @@ print(conversation.chain(
575
615
"Same with pangolin"
576
616
).text())
577
617
```
618
+
The `before_call=` and `after_call=` parameters {ref}`described above <python-api-tools-debug-hooks>` can be passed directly to the `model.conversation()` method to set those options for all chained prompts in that conversation.
0 commit comments