Skip to content
Snippets Groups Projects
Unverified Commit 62771058 authored by Alex Yang's avatar Alex Yang Committed by GitHub
Browse files

fix: empty tools (#772)

parent ca348a65
No related branches found
No related tags found
No related merge requests found
---
"llamaindex": patch
"@llamaindex/edge": patch
---
fix: allow passing empty tools to llms
import type { ClientOptions } from "@anthropic-ai/sdk";
import { Anthropic as SDKAnthropic } from "@anthropic-ai/sdk";
import type {
MessageCreateParamsNonStreaming,
Tool,
ToolResultBlockParam,
ToolUseBlock,
......@@ -264,7 +265,7 @@ export class Anthropic extends ToolCallLLM<AnthropicAdditionalChatOptions> {
const anthropic = this.session.anthropic;
if (tools) {
const response = await anthropic.beta.tools.messages.create({
const params: MessageCreateParamsNonStreaming = {
messages: this.formatMessages<true>(messages),
tools: tools.map(Anthropic.toTool),
model: this.getModelName(this.model),
......@@ -272,7 +273,12 @@ export class Anthropic extends ToolCallLLM<AnthropicAdditionalChatOptions> {
max_tokens: this.maxTokens ?? 4096,
top_p: this.topP,
...(systemPrompt && { system: systemPrompt }),
});
};
// Remove tools if there are none, as it will cause an error
if (tools.length === 0) {
delete params.tools;
}
const response = await anthropic.beta.tools.messages.create(params);
const toolUseBlock = response.content.find(
(content): content is ToolUseBlock => content.type === "tool_use",
......
......@@ -349,6 +349,14 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
...Object.assign({}, this.additionalChatOptions, additionalChatOptions),
};
if (
Array.isArray(baseRequestParams.tools) &&
baseRequestParams.tools.length === 0
) {
// remove empty tools array to avoid OpenAI error
delete baseRequestParams.tools;
}
// Streaming
if (stream) {
return this.streamChat(baseRequestParams);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment