Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tool Call History Support for Anthropic Agent #1564

Open
erik-balfe opened this issue Dec 12, 2024 · 0 comments · May be fixed by #1565
Open

Add Tool Call History Support for Anthropic Agent #1564

erik-balfe opened this issue Dec 12, 2024 · 0 comments · May be fixed by #1565
Labels
enhancement New feature or request

Comments

@erik-balfe
Copy link
Contributor

Feature Overview

Added proper message formatting for tool calls and results in chat history when using Anthropic agent. This makes Anthropic agent handle tool history consistently with OpenAI agent, which already had this functionality.

Problem Solved

Previously, when providing chat history with tool calls to Anthropic agent, it would throw API errors about incorrect message format. The same chat history worked fine with OpenAI agent. This has been fixed by implementing correct message transformation for Anthropic's specific format requirements.

Error Before Fix

When trying to use chat history with tool calls in Anthropic agent, the following error was thrown:

error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.1.content.1.tool_use.input: Input should be a valid dictionary"}}
      at new AnthropicError (path/to/project/node_modules/.pnpm/@[email protected][email protected]/node_modules/@anthropic-ai/sdk/error.mjs:7:9)
      at new APIError (path/to/project/node_modules/.pnpm/@[email protected][email protected]/node_modules/@anthropic-ai/sdk/error.mjs:86:9)

This happened because Anthropic's API expects tool calls in a specific format, different from the standard LlamaIndex format that worked with OpenAI.

Chat History with Tool Calls (now works with both agents)

const chatHistory: ChatMessage[] = [
  {
    role: "user",
    content: "What's the weather in London?",
  },
  {
    role: "assistant",
    content: "Let me check the weather.",
    options: {
      toolCall: [
        {
          id: "call_123",
          name: "weather",
          input: JSON.stringify({ location: "London" }),
        },
      ],
    },
  },
  {
    role: "tool",
    content: "The weather in London is sunny, +20°C",
    options: {
      toolResult: {
        id: "call_123",
      },
    },
  },
];

Implementation Details

  • Added proper message format transformation in Anthropic provider
  • Maintains consistent chat history format across providers while handling provider-specific requirements internally
  • Tool calls and results are now correctly recognized by Anthropic agent

Testing

Added comprehensive tests to verify:

  • Correct transformation of tool call messages for Anthropic format
  • Proper handling of tool results
  • Consistent behavior with OpenAI agent's existing functionality

Usage Example

Here's a complete example demonstrating the fixed functionality:

import { Anthropic, ChatMessage, FunctionTool } from "llamaindex";
import { AnthropicAgent } from "llamaindex/agent/anthropic";

// Chat history with a previous tool call
const chatHistory: ChatMessage[] = [
  {
    role: "user",
    content: "What's the weather in London?",
  },
  {
    role: "assistant",
    content: "Let me check the weather.",
    options: {
      toolCall: [
        {
          id: "call_123",
          name: "weather",
          input: JSON.stringify({ location: "London, UK" }),
        },
      ],
    },
  },
  {
    role: "tool",
    content: "The weather in London is sunny, +20°C",
    options: {
      toolResult: {
        id: "call_123",
      },
    },
  },
];

// Create agent with the weather tool and chat history
const agent = new AnthropicAgent({
  llm: new Anthropic({ model: "claude-3-opus" }),
  tools: [
    FunctionTool.from<{ location: string }>(
      (query) => `The weather in ${query.location} is sunny`,
      {
        name: "weather",
        description: "Get the weather",
        parameters: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "The location to get the weather for",
            },
          },
          required: ["location"],
        },
      },
    ),
  ],
  chatHistory,
});

// Agent now correctly processes the chat history and demonstrates awareness of the tool call arguments while not making extra tool calls.
const response = await agent.chat({
  message: "What exact location did you use in the weather tool?",
});
@erik-balfe erik-balfe linked a pull request Dec 12, 2024 that will close this issue
@himself65 himself65 added the enhancement New feature or request label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants