Skip to content

Commit

Permalink
fix: empty tools (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Apr 26, 2024
1 parent ca348a6 commit 6277105
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/brown-dodos-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"llamaindex": patch
"@llamaindex/edge": patch
---

fix: allow passing empty tools to llms
10 changes: 8 additions & 2 deletions packages/core/src/llm/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ClientOptions } from "@anthropic-ai/sdk";
import { Anthropic as SDKAnthropic } from "@anthropic-ai/sdk";
import type {
MessageCreateParamsNonStreaming,
Tool,
ToolResultBlockParam,
ToolUseBlock,
Expand Down Expand Up @@ -264,15 +265,20 @@ 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),
temperature: this.temperature,
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",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/llm/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6277105

Please sign in to comment.