-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Python: feat(python): support both base64 and url image uploads in Anthropi… #14096
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||||||||||||
| from semantic_kernel.contents.function_call_content import FunctionCallContent | ||||||||||||||
| from semantic_kernel.contents.function_result_content import FunctionResultContent | ||||||||||||||
| from semantic_kernel.contents.text_content import TextContent | ||||||||||||||
| from semantic_kernel.contents.image_content import ImageContent | ||||||||||||||
| from semantic_kernel.contents.utils.author_role import AuthorRole | ||||||||||||||
| from semantic_kernel.functions.kernel_function_metadata import KernelFunctionMetadata | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -21,6 +22,7 @@ | |||||||||||||
| from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _format_user_message(message: ChatMessageContent) -> dict[str, Any]: | ||||||||||||||
| """Format a user message to the expected object for the Anthropic client. | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -30,10 +32,23 @@ def _format_user_message(message: ChatMessageContent) -> dict[str, Any]: | |||||||||||||
| Returns: | ||||||||||||||
| The formatted user message. | ||||||||||||||
| """ | ||||||||||||||
| return { | ||||||||||||||
| "role": "user", | ||||||||||||||
| "content": message.content, | ||||||||||||||
| } | ||||||||||||||
| if not any(isinstance(item,ImageContent) for item in message.items): | ||||||||||||||
|
Contributor
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. Missing test coverage for all new code paths. This PR adds five new branches (text-only fast path, base64 image, URL image, mixed content, unsupported-item warning) with zero tests. Other connectors in this repo (e.g., |
||||||||||||||
| return {"role": "user","content": message.content} | ||||||||||||||
| else: | ||||||||||||||
| content_items: list[dict[str, Any]] = [] | ||||||||||||||
| for content in message.items: | ||||||||||||||
| if isinstance(content, TextContent): | ||||||||||||||
| content_items.append({"type": "text", "text": content.text}) | ||||||||||||||
| elif isinstance(content, ImageContent): | ||||||||||||||
|
Contributor
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. Untested base64 path: The base64 image branch accesses |
||||||||||||||
| if (content.data): | ||||||||||||||
|
Comment on lines
+42
to
+43
Contributor
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.
Root cause: Use
Suggested change
Contributor
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. The ternary
Suggested change
|
||||||||||||||
| content_items.append({"type":"image","source":{"type": "base64","data":content.data_string,"media_type":content.mime_type if content.mime_type else content.default_mime_type}}) | ||||||||||||||
|
Contributor
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. Untested URL path: The URL image branch is not covered by any test. A test should construct an |
||||||||||||||
| elif (content.uri): | ||||||||||||||
| content_items.append({"type":"image","source":{"type":"url","url":f"{content.uri}"}}) | ||||||||||||||
| else: | ||||||||||||||
| logger.warning( | ||||||||||||||
| "Unsupported item type in User message while formatting chat history for Anthropic AI" | ||||||||||||||
| f" Inference: {type(content)}") | ||||||||||||||
| return {"role": "user","content": content_items} | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _format_assistant_message(message: ChatMessageContent) -> dict[str, Any]: | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.