Skip to content
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

Support sending image data as part of a user message, using a new ImageUrl.load() method. Add sample and test. #36042

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sdk/ai/azure-ai-inference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,13 @@ print(response.choices[0].message.content)

<!-- END SNIPPET -->

The following types or messages are supported: `SystemMessage`,`UserMessage`, `AssistantMessage`, `ToolMessage`. See sample [sample_chat_completions_with_tools.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_tools.py) for usage of `ToolMessage`. See [sample_chat_completions_with_images.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_images.py) for usage of `UserMessage` that
includes uploading an image.
The following types or messages are supported: `SystemMessage`,`UserMessage`, `AssistantMessage`, `ToolMessage`. See also samples:

* [sample_chat_completions_with_tools.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_tools.py) for usage of `ToolMessage`.
* [sample_chat_completions_with_image_url.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_image_url.py) for usage of `UserMessage` that
includes sending an image URL.
* [sample_chat_completions_with_image_data.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_image_data.py) for usage of `UserMessage` that
includes sending image data read from a local file.

Alternatively, you can provide the messages as dictionary instead of using the strongly typed classes like `SystemMessage` and `UserMessage`:

Expand Down
5 changes: 3 additions & 2 deletions sdk/ai/azure-ai-inference/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ similarly for the other samples.
|[sample_chat_completions_streaming.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_streaming.py) | One chat completion operation using a synchronous client and streaming response. |
|[sample_chat_completions_streaming_with_entra_id_auth.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_streaming_with_entra_id_auth.py) | One chat completion operation using a synchronous client and streaming response, using Entra ID authentication. This sample also shows setting the `azureml-model-deployment` HTTP request header, which may be required for some Managed Compute endpoint. |
|[sample_chat_completions.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions.py) | One chat completion operation using a synchronous client. |
|[sample_chat_completions_with_images.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_images.py) | One chat completion operation using a synchronous client, which includes sending an input image. |
|[sample_chat_completions_with_image_url.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_image_url.py) | One chat completion operation using a synchronous client, which includes sending an input image URL. |
|[sample_chat_completions_with_image_data.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_image_data.py) | One chat completion operation using a synchronous client, which includes sending input image data read from a local file. |
|[sample_chat_completions_with_history.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_history.py) | Two chat completion operations using a synchronous client, with the second completion using chat history from the first. |
|[sample_chat_completions_from_input_bytes.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_from_input_bytes.py) | One chat completion operation using a synchronous client, with input messages provided as `IO[bytes]`. |
|[sample_chat_completions_from_input_json.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_from_input_json.py) | One chat completion operation using a synchronous client, with input messages provided as a dictionary (type `MutableMapping[str, Any]`) |
|[sample_chat_completions_from_input_json_with_images.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_from_input_json_with_images.py) | One chat completion operation using a synchronous client, with input messages provided as a dictionary (type `MutableMapping[str, Any]`). Includes sending an input image. |
|[sample_chat_completions_from_input_json_with_image_url.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_from_input_json_with_image_url.py) | One chat completion operation using a synchronous client, with input messages provided as a dictionary (type `MutableMapping[str, Any]`). Includes sending an input image URL. |
|[sample_chat_completions_with_tools.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_chat_completions_with_tools.py) | Shows how do use a tool (function) in chat completions, for an AI model that supports tools |
|[sample_load_client.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_load_client.py) | Shows how do use the function `load_client` to create the appropriate synchronous client based on the provided endpoint URL. In this example, it creates a synchronous `ChatCompletionsClient`. |
|[sample_get_model_info.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-inference/samples/sample_get_model_info.py) | Get AI model information using the chat completions client. Similarly can be done with all other clients. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
This sample demonstrates how to get a chat completions response from
the service using a synchronous client, and directly providing the
JSON request body (containing input chat messages). The sample
shows how to include an image in the input chat messages.
shows how to include an image URL in the input chat messages.
This sample will only work on AI models that support image input.

USAGE:
python sample_chat_completions_from_input_json_with_image.py
python sample_chat_completions_from_input_json_with_image_url.py

Set these two or three environment variables before running the sample:
1) CHAT_COMPLETIONS_ENDPOINT - Your endpoint URL, in the form
Expand All @@ -26,7 +26,7 @@
# pyright: reportAttributeAccessIssue=false


def sample_chat_completions_from_input_json_with_image():
def sample_chat_completions_from_input_json_with_image_url():
import os
from azure.ai.inference import ChatCompletionsClient
from azure.core.credentials import AzureKeyCredential
Expand Down Expand Up @@ -83,4 +83,4 @@ def sample_chat_completions_from_input_json_with_image():


if __name__ == "__main__":
sample_chat_completions_from_input_json_with_image()
sample_chat_completions_from_input_json_with_image_url()
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# ------------------------------------
# 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

image_data_url = get_image_data_url("sample1.png", "png")

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(
url=image_data_url,
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()
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
DESCRIPTION:
This sample demonstrates how to get a chat completions response from
the service using a synchronous client. The sample
shows how to include an image in the input chat messages.
shows how to include an image URL in the input chat messages.
This sample will only work on AI models that support image input.

USAGE:
python sample_chat_completions_with_images.py
python sample_chat_completions_with_image_url.py

Set these two or three environment variables before running the sample:
1) CHAT_COMPLETIONS_ENDPOINT - Your endpoint URL, in the form
Expand All @@ -23,7 +23,7 @@
"""


def sample_chat_completions_with_images():
def sample_chat_completions_with_image_url():
import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import (
Expand Down Expand Up @@ -74,4 +74,4 @@ def sample_chat_completions_with_images():


if __name__ == "__main__":
sample_chat_completions_with_images()
sample_chat_completions_with_image_url()
Loading