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

feat: Add tool support to AnthropicMultiModal #17302

Merged

Conversation

rushilsheth
Copy link
Contributor

Description

This PR adds support for handling tool responses in the AnthropicMultiModal class. Currently, when using tools with Claude 3, the LlamaIndex wrapper expects a text response but fails when receiving a tool response instead. This change enables handling both response types seamlessly.

Key changes:

  • Modify _complete method to handle both tool and text responses
  • Add unit tests to verify tool response handling
  • Update documentation with tool usage examples and best practices

Type of Change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • I added new unit tests to cover this change

Added tests for:

  • Tool response handling
  • Standard text response compatibility
  • Error cases
  • Response format validation

Detailed Changes

Code Changes

Modified _complete method to detect and handle tool responses:

def _complete(self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any) -> CompletionResponse:
    # ... existing setup code ...
    
    content = response.content[0]
    if hasattr(content, "input"):
        # Handle tool response
        text = str(content.input)
    else:
        # Handle standard text response
        text = content.text
        
    return CompletionResponse(
        text=text,
        raw=response,
        additional_kwargs=self._get_response_token_counts(response),
    )

New Tests

def test_anthropic_tool_response():
    """Test tool response handling"""
    # ... test implementation ...

Checklist:

  • 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 ran make format; make lint to appease the lint gods

New Package?

  • No

- Handle both tool and text responses in completion
- Add tests for tool response handling
- Update documentation with tool usage examples
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Dec 17, 2024
@logan-markewich
Copy link
Collaborator

logan-markewich commented Dec 17, 2024

@rushilsheth I'm happy to merge this, but tbh I recently updated the normal Anthropic class to work with images, albiet with our newer chat-message format (we are slowly rolling out support for this, so far openai, azure openai, and anthropic support this)

from llama_index.core.llms import (
    ChatMessage,
    ImageBlock,
    TextBlock,
    MessageRole,
)

msg = ChatMessage(
    role=MessageRole.USER,
    blocks=[
        TextBlock(text="Describe the images as an alternative text"),
        ImageBlock(path="some_file.png", image_mimetype="image/png"),
    ],
)

response = llm.chat(messages=[msg])

This means you can use the same llm class for multimodal, tool calling, etc, (assuming you use the lower-level llm operations I guess)

@rushilsheth
Copy link
Contributor Author

It would be great if you could merge this. We'll be sure to migrate our internal code base to the new base class after some testing

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Dec 17, 2024
@logan-markewich
Copy link
Collaborator

ngl this PR is pretty hacky. But since this class

  • a isn't handling tool calls properly anyways
  • isn't meant to be maintained much further

Should be ok

@logan-markewich logan-markewich merged commit d1fc12e into run-llama:main Dec 17, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants