Skip to content

Commit d1fc12e

Browse files
author
Rushil Sheth
authored
feat: Add tool support to AnthropicMultiModal (#17302)
1 parent 5ac1a41 commit d1fc12e

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

llama-index-integrations/llms/llama-index-llms-bedrock/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
2727
license = "MIT"
2828
name = "llama-index-llms-bedrock"
2929
readme = "README.md"
30-
version = "0.3.1"
30+
version = "0.3.2"
3131

3232
[tool.poetry.dependencies]
3333
python = ">=3.9,<4.0"

llama-index-integrations/multi_modal_llms/llama-index-multi-modal-llms-anthropic/llama_index/multi_modal_llms/anthropic/base.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def _get_response_token_counts(self, raw_response: Any) -> dict:
194194
def _complete(
195195
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
196196
) -> CompletionResponse:
197+
"""Complete the prompt with image support and optional tool calls."""
197198
all_kwargs = self._get_model_kwargs(**kwargs)
198199
message_dict = self._get_multi_modal_chat_messages(
199200
prompt=prompt, role=MessageRole.USER, image_documents=image_documents
@@ -206,8 +207,17 @@ def _complete(
206207
**all_kwargs,
207208
)
208209

210+
# Handle both tool and text responses
211+
content = response.content[0]
212+
if hasattr(content, "input"):
213+
# Tool response - convert to string for compatibility
214+
text = str(content.input)
215+
else:
216+
# Standard text response
217+
text = content.text
218+
209219
return CompletionResponse(
210-
text=response.content[0].text,
220+
text=text,
211221
raw=response,
212222
additional_kwargs=self._get_response_token_counts(response),
213223
)

llama-index-integrations/multi_modal_llms/llama-index-multi-modal-llms-anthropic/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
2727
license = "MIT"
2828
name = "llama-index-multi-modal-llms-anthropic"
2929
readme = "README.md"
30-
version = "0.3.0"
30+
version = "0.3.1"
3131

3232
[tool.poetry.dependencies]
3333
python = ">=3.9,<4.0"

llama-index-integrations/multi_modal_llms/llama-index-multi-modal-llms-anthropic/tests/test_multi-modal-llms_anthropic.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from unittest.mock import Mock, patch
12
from llama_index.core.multi_modal_llms.base import MultiModalLLM
23
from llama_index.multi_modal_llms.anthropic import AnthropicMultiModal
4+
from llama_index.core.base.llms.types import CompletionResponse
35

46

57
def test_embedding_class():
@@ -10,3 +12,45 @@ def test_embedding_class():
1012
def test_init():
1113
m = AnthropicMultiModal(max_tokens=400)
1214
assert m.max_tokens == 400
15+
16+
17+
def test_tool_response():
18+
"""Test handling of tool responses."""
19+
llm = AnthropicMultiModal(max_tokens=400)
20+
21+
# Create mock response with tool input
22+
mock_content = Mock()
23+
mock_content.input = {
24+
"booking_number": "123",
25+
"carrier": "Test Carrier",
26+
"total_amount": 1000.0,
27+
}
28+
mock_response = Mock()
29+
mock_response.content = [mock_content]
30+
31+
with patch.object(llm._client.messages, "create", return_value=mock_response):
32+
response = llm.complete(
33+
prompt="test prompt",
34+
image_documents=[],
35+
tools=[
36+
{
37+
"name": "tms_order_payload",
38+
"description": "Test tool",
39+
"input_schema": {
40+
"type": "object",
41+
"properties": {
42+
"booking_number": {"type": "string"},
43+
"carrier": {"type": "string"},
44+
"total_amount": {"type": "number"},
45+
},
46+
},
47+
}
48+
],
49+
tool_choice={"type": "tool", "name": "tms_order_payload"},
50+
)
51+
52+
assert isinstance(response, CompletionResponse)
53+
assert isinstance(response.text, str)
54+
assert "booking_number" in response.text
55+
assert "123" in response.text
56+
assert response.raw == mock_response

0 commit comments

Comments
 (0)