Skip to content

Feature/anthropic sdk and tools#235

Open
TheApeMachine wants to merge 7 commits intohenomis:mainfrom
TheApeMachine:feature/anthropic-sdk-and-tools
Open

Feature/anthropic sdk and tools#235
TheApeMachine wants to merge 7 commits intohenomis:mainfrom
TheApeMachine:feature/anthropic-sdk-and-tools

Conversation

@TheApeMachine
Copy link

Pull Request Template

Description

This PR enhances the Anthropic integration to make it more aligned with the OpenAI implementation. The changes include adding proper streaming support, implementing multimodal capabilities, improving error handling, and ensuring consistent behavior between the two LLM implementations.

Specifically, this PR addresses:

  1. Implemented enhanced stream method to correctly handle streaming responses and properly accumulate text content
  2. Added support for stop sequences and the tool_choice parameter in the buildChatCompletionRequest method
  3. Improved error handling by wrapping errors with ErrAnthropicChat for consistent error reporting
  4. Removed debug print statements for cleaner production code
  5. Added multimodal support by properly handling image content in the threadToChatCompletionMessages function

Fixes #225

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

The changes have been tested using the example code provided in the repository:

  • Streaming example (examples/llm/antropic/stream/main.go) - Verified that streaming responses are correctly displayed
  • Multimodal example (examples/llm/antropic/multimodal/main.go) - Verified that image content is correctly processed and described

Checklist:

  • I have read the Contributing documentation.
  • I have read the Code of conduct documentation.
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have checked my code and corrected any misspellings

TheApeMachine and others added 6 commits December 29, 2024 06:02
- Bump Go version to 1.22.0 and update toolchain.
- Add new dependency: `github.com/anthropics/anthropic-sdk-go` v0.2.0-alpha.8.
- Update existing dependencies: `golang.org/x/net` to v0.27.0 and `github.com/stretchr/testify` to v1.9.0.
- Refactor Anthropic client initialization to use API key from environment variable.
- Change method calls from `Generate` to `Chat` for improved functionality.
- Remove deprecated `api.go` and `formatter.go` files; introduce new `common.go` and `function.go` for better structure.
- Enhance message handling and tool integration in the Anthropic client.
- Add support for response format (JSON/text)
- Implement caching mechanism for responses
- Add usage metadata tracking
- Introduce new methods for configuration (WithResponseFormat, SetStop)
- Improve error handling and logging
- Refactor Generate method with more robust generation process
- Improve streaming response handling in `stream` method
- Enhance message content processing in `threadToChatCompletionMessages`
- Add support for multi-content messages (text and images)
- Optimize tool choice and tools parameter handling
- Remove redundant token tracking and simplify streaming logic
Clean up unused constants related to stream delta types, simplifying the Anthropic client configuration
} else {
err = o.generate(ctx, t, chatRequest)
// NewAnthropic creates a new Anthropic instance with explicit API key.
func NewAnthropic(apiKey string) *Anthropic {
Copy link
Owner

Choose a reason for hiding this comment

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

Please implement a WithKey method here

Copy link
Author

Choose a reason for hiding this comment

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

I am not 100% sure about my implementation here.


func main() {
anthropicllm := anthropic.New().WithModel("claude-3-opus-20240229")
anthropicllm := anthropic.NewAnthropic(os.Getenv("ANTHROPIC_API_KEY")).WithModel(anthropic.ModelClaude_3_Opus_20240229)
Copy link
Owner

Choose a reason for hiding this comment

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

Please provide some examples showing tools/functions usage like the ones here:https://github.com/henomis/lingoose/tree/main/examples/llm/openai/tools
Same for the response format like the one here https://github.com/henomis/lingoose/tree/main/examples/llm/openai/response_format

Copy link
Owner

@henomis henomis left a comment

Choose a reason for hiding this comment

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

Left some comments, just a quick review, need to go deeper, Great job!

@TheApeMachine
Copy link
Author

I will work on this coming week.

- Remove deprecated NewAnthropic method and replace it with a simplified New method that retrieves the API key from the environment.
- Update example usages in the `stream`, `multimodal`, and `voyage` examples to use the new client initialization and method calls.
- Change method calls from `Chat` to `Generate` for improved functionality and consistency across examples.
- Clean up unused imports and improve overall code clarity.

Ran existing examples to confirm they are still working. Will implement new examples as requested this week.
EOS = "\x00"
)
// WithKey returns a new Anthropic instance with the given API key.
func WithKey(key string) *Anthropic {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
func WithKey(key string) *Anthropic {
func (a *Anthropic) WithKey(key string) *Anthropic {

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.

Use Anthropic Go SDK and Mirror Current OpenAI Implementation

2 participants