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

anthropic: Add support for multi content part and images content in human messages. #1141

Merged
merged 3 commits into from
Feb 13, 2025

Conversation

Neofox
Copy link
Contributor

@Neofox Neofox commented Feb 9, 2025

As of now there wasn't an option to add an image to a Human message to the anthropic client.
Only text chat messages could be sent.

This PR will resolve two issues of the current implementation of handleHumanMessage.
It will also add the anthropic-vision-example example

  1. Allowing multi part content.

Looking at the documentation we can see that there is two way of sending content through the API:

[
  {"role": "user", "content": "Hello there."},
]

or

{"role": "user", "content": [
  { "type": "text", "text": "Hello there" },
  {"type": "text", "text": "How are you?"}
]}

but only the first case was currently supported.

  1. Allowing one of the part to be an image

One of the content type that can be set is image
example:

{"role": "user", "content": [
  {
    "type": "image",
    "source": {
      "type": "base64",
      "media_type": "image/jpeg",
      "data": "/9j/4AAQSkZJRg...",
    }
  },
  {"type": "text", "text": "What is in this image?"}
]}

This feature will be also implemented with this PR, allowing to send Human Chat messages that also integrate images.
I actually needed this feature for my own project, this is how I discovered and fixed the issue.

PR Checklist

  • Read the Contributing documentation.
  • Read the Code of conduct documentation.
  • Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to Good commit messages (such as memory: add interfaces for X, Y or util: add whizzbang helpers).
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. Fixes #123).
  • Describes the source of new concepts.
  • References existing implementations as appropriate.
  • Contains test coverage for new functions.
  • Passes all golangci-lint checks.

Copy link
Owner

@tmc tmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

@tmc tmc merged commit 96d0b28 into tmc:main Feb 13, 2025
3 checks passed
hisunwei pushed a commit to tmrwh/langchaingo that referenced this pull request Feb 18, 2025
…uman messages. (tmc#1141)

* anthropic: Add support for multi content part  in human messages.

* examples: Add Anthropic Vision example

* fix: move base64 encoding in the lib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants