-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Support sending image data as part of a user message, using a new ImageUrl.load() method. Add sample and test. #36042
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
90 changes: 90 additions & 0 deletions
90
sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_image_data.py
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| """ | ||
| DESCRIPTION: | ||
| This sample demonstrates how to get a chat completions response from | ||
| the service using a synchronous client. The sample shows how to load | ||
| an image from a file and include it in the input chat messages. | ||
| This sample will only work on AI models that support image input. | ||
|
|
||
| USAGE: | ||
| python sample_chat_completions_with_image_data.py | ||
|
|
||
| Set these two or three environment variables before running the sample: | ||
| 1) CHAT_COMPLETIONS_ENDPOINT - Your endpoint URL, in the form | ||
| https://<your-deployment-name>.<your-azure-region>.inference.ai.azure.com | ||
| where `your-deployment-name` is your unique AI Model deployment name, and | ||
| `your-azure-region` is the Azure region where your model is deployed. | ||
| 2) CHAT_COMPLETIONS_KEY - Your model key (a 32-character string). Keep it secret. | ||
| 3) CHAT_COMPLETIONS_DEPLOYMENT_NAME - Optional. The value for the HTTP | ||
| request header `azureml-model-deployment`. | ||
| """ | ||
|
|
||
|
|
||
| def sample_chat_completions_with_image_data(): | ||
| import os | ||
| from azure.ai.inference import ChatCompletionsClient | ||
| from azure.ai.inference.models import ( | ||
| SystemMessage, UserMessage, TextContentItem, | ||
| ImageContentItem, ImageUrl, ImageDetailLevel | ||
| ) | ||
| from azure.core.credentials import AzureKeyCredential | ||
|
|
||
| try: | ||
| endpoint = os.environ["CHAT_COMPLETIONS_ENDPOINT"] | ||
| key = os.environ["CHAT_COMPLETIONS_KEY"] | ||
| except KeyError: | ||
| print("Missing environment variable 'CHAT_COMPLETIONS_ENDPOINT' or 'CHAT_COMPLETIONS_KEY'") | ||
| print("Set them before running this sample.") | ||
| exit() | ||
|
|
||
| try: | ||
| model_deployment = os.environ["CHAT_COMPLETIONS_DEPLOYMENT_NAME"] | ||
| except KeyError: | ||
| print("Could not read optional environment variable `CHAT_COMPLETIONS_DEPLOYMENT_NAME`.") | ||
| print("HTTP request header `azureml-model-deployment` will not be set.") | ||
| model_deployment = None | ||
|
|
||
| client = ChatCompletionsClient( | ||
| endpoint=endpoint, | ||
| credential=AzureKeyCredential(key), | ||
| headers={"azureml-model-deployment": model_deployment}, | ||
| ) | ||
|
|
||
| response = client.complete( | ||
| messages=[ | ||
| SystemMessage(content="You are an AI assistant that describes images in details."), | ||
| UserMessage( | ||
| content=[ | ||
| TextContentItem(text="What's in this image?"), | ||
| ImageContentItem( | ||
| image_url=ImageUrl.load( | ||
| image_file="sample1.png", | ||
| image_format="png", | ||
| detail=ImageDetailLevel.HIGH, | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) | ||
|
|
||
|
|
||
| def get_image_data_url(image_file: str, image_format: str) -> str: | ||
dargilco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import base64 | ||
| try: | ||
| with open(image_file, "rb") as f: | ||
| image_data = base64.b64encode(f.read()).decode("utf-8") | ||
| except FileNotFoundError: | ||
| print(f"Could not read '{image_file}'.") | ||
| print("Set the correct path to the image file before running this sample.") | ||
| exit() | ||
| return f"data:image/{image_format};base64,{image_data}" | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sample_chat_completions_with_image_data() | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.