-
Notifications
You must be signed in to change notification settings - Fork 152
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
vertexai: refactor: simplify content processing in anthropic formatter #601
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,15 +153,20 @@ def validate_environment(self) -> Self: | |
AsyncAnthropicVertex, | ||
) | ||
|
||
if self.project is None: | ||
raise ValueError("project is required for ChatAnthropicVertex") | ||
|
||
project_id: str = self.project | ||
|
||
self.client = AnthropicVertex( | ||
project_id=self.project, | ||
project_id=project_id, | ||
region=self.location, | ||
max_retries=self.max_retries, | ||
access_token=self.access_token, | ||
credentials=self.credentials, | ||
) | ||
self.async_client = AsyncAnthropicVertex( | ||
project_id=self.project, | ||
project_id=project_id, | ||
region=self.location, | ||
max_retries=self.max_retries, | ||
access_token=self.access_token, | ||
|
@@ -205,14 +210,18 @@ def _format_params( | |
|
||
def _format_output(self, data: Any, **kwargs: Any) -> ChatResult: | ||
data_dict = data.model_dump() | ||
content = [c for c in data_dict["content"] if c["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. This is the main difference, here, we would only have elements in 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. awesome -- thanks for calling this out @Reprazent! updated the description as well. |
||
content = content[0]["text"] if len(content) == 1 else content | ||
content = data_dict["content"] | ||
llm_output = { | ||
k: v for k, v in data_dict.items() if k not in ("content", "role", "type") | ||
} | ||
tool_calls = _extract_tool_calls(data_dict["content"]) | ||
if tool_calls: | ||
msg = AIMessage(content=content, tool_calls=tool_calls) | ||
if len(content) == 1 and content[0]["type"] == "text": | ||
msg = AIMessage(content=content[0]["text"]) | ||
elif any(block["type"] == "tool_use" for block in content): | ||
tool_calls = _extract_tool_calls(content) | ||
msg = AIMessage( | ||
content=content, | ||
tool_calls=tool_calls, | ||
) | ||
else: | ||
msg = AIMessage(content=content) | ||
# Collect token usage | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Shall we add a test for
_format_output
similar to the test that we have in the main lanchain library: https://github.com/langchain-ai/langchain/blob/f1222739f88bfdf37513af146da6b9dbf2a091c4/libs/partners/anthropic/tests/unit_tests/test_chat_models.py#L87-L109We should base the input for the test here on what we noticed was causing the errors. So instead of passing only a
TextBlock
, we should only pass aToolUseBlock
like we do in our test. I suspect this will fail in the previous implementation.What do you think?