-
Notifications
You must be signed in to change notification settings - Fork 6.5k
add unified tool call block #19947
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
Closed
+808
−376
Closed
add unified tool call block #19947
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a7f0e85
wip: add tool call block
AstraBert db697dd
chore: add anthropic support2
AstraBert c2bfa65
chore: adding Google GenAI
AstraBert 83d5983
chore: more integrations with ToolCallBlock
AstraBert 24a1dc7
feat: add tool block also to openai responses
AstraBert faa9994
Merge branch 'main' into clelia/tool-content-block
AstraBert 218ffc8
chore: make type checker happy again
AstraBert a780c41
Merge branch 'clelia/tool-content-block' of https://github.com/run-ll…
AstraBert 4798bcb
chore: make type checker happy againpt2
AstraBert 58b6b9a
fix: make bedrock converse pass on 3.9
AstraBert 4b594cb
Merge branch 'main' into clelia/tool-content-block
AstraBert 898e725
chore: implement PR review request changes
AstraBert bd95703
ci: lint
AstraBert 4c6bc93
Merge branch 'main' into clelia/tool-content-block
logan-markewich 8c598f4
Merge branch 'main' into clelia/tool-content-block
logan-markewich a0f2bef
Merge branch 'main' into clelia/tool-content-block
AstraBert 1a25ca8
chore: fixes after merge :grimacing:
AstraBert 1c2f12e
fix: re-include tool call and thinking in google-genai
AstraBert 7c0e4b6
ci: lint
AstraBert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
Utility functions for the Anthropic SDK LLM integration. | ||
""" | ||
|
||
from typing import Any, Dict, List, Sequence, Tuple, Optional | ||
from typing import Any, Dict, List, Sequence, Tuple, Optional, cast, Union | ||
|
||
from llama_index.core.base.llms.types import ( | ||
ChatMessage, | ||
|
@@ -15,6 +15,7 @@ | |
CitableBlock, | ||
CitationBlock, | ||
ThinkingBlock, | ||
ToolCallBlock, | ||
ContentBlock, | ||
) | ||
|
||
|
@@ -26,6 +27,7 @@ | |
ImageBlockParam, | ||
CacheControlEphemeralParam, | ||
Base64PDFSourceParam, | ||
ToolUseBlock, | ||
) | ||
from anthropic.types import ContentBlockParam as AnthropicContentBlock | ||
from anthropic.types.beta import ( | ||
|
@@ -198,6 +200,19 @@ def _to_anthropic_document_block(block: DocumentBlock) -> DocumentBlockParam: | |
) | ||
|
||
|
||
def _anthropic_tool_call_to_tool_call_block(tool_calls: list[ToolUseBlock]): | ||
blocks = [] | ||
for tool_call in tool_calls: | ||
blocks.append( | ||
ToolCallBlock( | ||
tool_call_id=tool_call.id, | ||
tool_kwargs=cast(Union[Dict[str, Any], str], tool_call.input), | ||
tool_name=tool_call.name, | ||
) | ||
) | ||
return blocks | ||
|
||
|
||
def blocks_to_anthropic_blocks( | ||
blocks: Sequence[ContentBlock], kwargs: dict[str, Any] | ||
) -> List[AnthropicContentBlock]: | ||
|
@@ -275,24 +290,18 @@ def blocks_to_anthropic_blocks( | |
elif isinstance(block, CitationBlock): | ||
# No need to pass these back to Anthropic | ||
continue | ||
elif isinstance(block, ToolCallBlock): | ||
anthropic_blocks.append( | ||
ToolUseBlockParam( | ||
id=block.tool_call_id or "", | ||
input=block.tool_kwargs, | ||
name=block.tool_name, | ||
type="tool_use", | ||
) | ||
) | ||
else: | ||
raise ValueError(f"Unsupported block type: {type(block)}") | ||
|
||
tool_calls = kwargs.get("tool_calls", []) | ||
for tool_call in tool_calls: | ||
assert "id" in tool_call | ||
assert "input" in tool_call | ||
assert "name" in tool_call | ||
|
||
anthropic_blocks.append( | ||
ToolUseBlockParam( | ||
id=tool_call["id"], | ||
input=tool_call["input"], | ||
name=tool_call["name"], | ||
type="tool_use", | ||
) | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to add back this handling (in each LLM), just to ensure that old chat histories don't suddenly stop working. No harm in keeping this here |
||
return anthropic_blocks | ||
|
||
|
||
|
@@ -351,6 +360,12 @@ def messages_to_anthropic_messages( | |
|
||
|
||
def force_single_tool_call(response: ChatResponse) -> None: | ||
tool_calls = response.message.additional_kwargs.get("tool_calls", []) | ||
tool_calls = [ | ||
block for block in response.message.blocks if isinstance(block, ToolCallBlock) | ||
] | ||
if len(tool_calls) > 1: | ||
response.message.additional_kwargs["tool_calls"] = [tool_calls[0]] | ||
response.message.blocks = [ | ||
block | ||
for block in response.message.blocks | ||
if not isinstance(block, ToolCallBlock) | ||
] + [tool_calls[0]] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this assumes that tool calls always come after content (I think this is true? just flagging)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that was my assumption: first there is the "explanation" of the tool call and then we have the tool call itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be safe, it might be best to just match the order that the content came in, rather than assuming 👍🏻