tmc / langchaingo

LangChain for Go, the easiest way to write LLM-based programs in Go
https://tmc.github.io/langchaingo/
MIT License
3.73k stars 518 forks source link

llms/anthropic: Fix Content set incorrectly for anthropic llm requests #908

Closed JckHoe closed 1 week ago

JckHoe commented 1 week ago

PR Checklist

JckHoe commented 1 week ago

Updating the Content field to use the same as response and added ToolUseContent.

The example is the way to recreate the issue where the llm generates a incorrect response.

{
  "id": "msg_01PZ5755vfhLUM1Bf6fRtN1J",
  "type": "message",
  "role": "assistant",
  "model": "claude-3-haiku-20240307",
  "content": [
    {
      "type": "text",
      "text": "{\"type\":\"tool_use\",\"id\":\"toolu_01FjodET1i2Lo8NqkoHZYHXe\",\"name\":\"getCurrentWeather\"}"
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 611,
    "output_tokens": 41
  }
}

This was the incorrect request Content field .

{
  "model": "claude-3-haiku-20240307",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather like in Boston?"
    },
    {
      "role": "assistant",
      "content": "{\"type\":\"tool_use\",\"id\":\"toolu_01FjodET1i2Lo8NqkoHZYHXe\",\"name\":\"getCurrentWeather\"}"
    },
    {
      "role": "user",
      "content": "{\"type\":\"tool_result\",\"tool_use_id\":\"toolu_01FjodET1i2Lo8NqkoHZYHXe\",\"content\":\"\\\"72 and sunny\\\"\"}"
    },
    {
      "role": "assistant",
      "content": "According to the weather information, the current weather in Boston is 72 degrees and sunny."
    },
    {
      "role": "user",
      "content": "How about the weather in Chicago?"
    },
    {
      "role": "assistant",
      "content": "{\"type\":\"tool_use\",\"id\":\"toolu_019gLSWVyyiZ5zGLqZJcSkrf\",\"name\":\"getCurrentWeather\"}"
    },
    {
      "role": "user",
      "content": "{\"type\":\"tool_result\",\"tool_use_id\":\"toolu_019gLSWVyyiZ5zGLqZJcSkrf\",\"content\":\"\\\"65 and windy\\\"\"}"
    },
    {
      "role": "assistant",
      "content": "The current weather in Chicago is 65 degrees and windy."
    },
    {
      "role": "user",
      "content": "How about the weather in Chicago?"
    }
  ],
  "max_tokens": 2560,
  "temperature": 0,
  "tools": [
    {
      "name": "getCurrentWeather",
      "description": "Get the current weather in a given location",
      "input_schema": {
        "properties": {
          "location": {
            "description": "The city and state, e.g. San Francisco, CA",
            "type": "string"
          },
          "unit": {
            "enum": ["fahrenheit", "celsius"],
            "type": "string"
          }
        },
        "required": ["location"],
        "type": "object"
      }
    }
  ]
}

an example of expected request will look like this

{
  "model": "claude-3-opus-20240229",
  "max_tokens": 1024,
  "tools": [
    {
      "name": "get_weather",
      "description": "Get the current weather in a given location",
      "input_schema": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          }
        },
        "required": ["location"]
      }
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": "What is the weather like in San Francisco?"
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "let me look up that for you"
        },
        {
          "type": "tool_use",
          "id": "toolu_01A09q90qw90lq917835lq9",
          "name": "get_weather",
          "input": { "location": "San Francisco, CA" }
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "tool_result",
          "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
          "content": "15 degrees"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "The current weather in San Francisco, CA is 15 degrees Celsius which is 59 degrees Fahrenheit. San Francisco has a cool Mediterranean climate, so temperatures are mild year-round, rarely getting very hot or very cold. 15C/59F is a fairly typical temperature there."
        }
      ]
    },
    {
      "role": "user",
      "content": "What is the weather like in Malaysia?"
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "let me look up that for you"
        },
        {
          "type": "tool_use",
          "id": "toolu_01A09q90qw90lq917835lq9",
          "name": "get_weather",
          "input": { "location": "San Francisco, CA" }
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "tool_result",
          "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
          "content": "15 degrees"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": "Malaysia is 15 degrees"
        }
      ]
    },
    {
      "role": "user",
      "content": "What is the weather like in Boston?"
    }
  ]
}
JckHoe commented 1 week ago

@tmc I will resolve the conflict no worries

JckHoe commented 1 week ago

@tmc Thank you for the review, resolved conflicts