mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix Multimodal input bug (#799)
* fix multimodal bug python * update file names * precommit fixes * Update python/samples/getting_started/multimodal_input/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * udpate readme * add copyright line, remove audio example function --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
2015f0dc09
commit
f3264966ff
@@ -0,0 +1,85 @@
|
||||
# Multimodal Input Examples
|
||||
|
||||
This folder contains examples demonstrating how to send multimodal content (images, audio, PDF files) to AI agents using the Agent Framework.
|
||||
|
||||
## Examples
|
||||
|
||||
### OpenAI Chat Client
|
||||
|
||||
- **File**: `openai_chat_multimodal.py`
|
||||
- **Description**: Shows how to send images, audio, and PDF files to OpenAI's Chat Completions API
|
||||
- **Supported formats**: PNG/JPEG images, WAV/MP3 audio, PDF documents
|
||||
|
||||
### Azure Chat Client
|
||||
|
||||
- **File**: `azure_chat_multimodal.py`
|
||||
- **Description**: Shows how to send multimodal content to Azure OpenAI service
|
||||
- **Supported formats**: PNG/JPEG images, WAV/MP3 audio, PDF documents
|
||||
|
||||
## Running the Examples
|
||||
|
||||
1. Set your API keys:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY="your-openai-key"
|
||||
export AZURE_OPENAI_API_KEY="your-azure-key"
|
||||
export AZURE_OPENAI_ENDPOINT="your-azure-endpoint"
|
||||
```
|
||||
|
||||
2. Run an example:
|
||||
```bash
|
||||
python openai_chat_client_multimodal.py
|
||||
python azure_chat_client_multimodal.py
|
||||
```
|
||||
|
||||
## Using Your Own Files
|
||||
|
||||
The examples include small embedded test files for demonstration. To use your own files:
|
||||
|
||||
### Method 1: Data URIs (recommended)
|
||||
|
||||
```python
|
||||
import base64
|
||||
|
||||
# Load and encode your file
|
||||
with open("path/to/your/image.jpg", "rb") as f:
|
||||
image_data = f.read()
|
||||
image_base64 = base64.b64encode(image_data).decode('utf-8')
|
||||
image_uri = f"data:image/jpeg;base64,{image_base64}"
|
||||
|
||||
# Use in DataContent
|
||||
DataContent(
|
||||
uri=image_uri,
|
||||
media_type="image/jpeg"
|
||||
)
|
||||
```
|
||||
|
||||
### Method 2: Raw bytes
|
||||
|
||||
```python
|
||||
# Load raw bytes
|
||||
with open("path/to/your/image.jpg", "rb") as f:
|
||||
image_bytes = f.read()
|
||||
|
||||
# Use in DataContent
|
||||
DataContent(
|
||||
data=image_bytes,
|
||||
media_type="image/jpeg"
|
||||
)
|
||||
```
|
||||
|
||||
## Supported File Types
|
||||
|
||||
| Type | Formats | Notes |
|
||||
| --------- | -------------------- | ------------------------------ |
|
||||
| Images | PNG, JPEG, GIF, WebP | Most common image formats |
|
||||
| Audio | WAV, MP3 | For transcription and analysis |
|
||||
| Documents | PDF | Text extraction and analysis |
|
||||
|
||||
## API Differences
|
||||
|
||||
- **Chat Completions API**: Supports images, audio, and PDF files
|
||||
- **Assistants API**: Only supports text and images (no audio/PDF)
|
||||
- **Responses API**: Similar to Chat Completions
|
||||
|
||||
Choose the appropriate client based on your multimodal needs.
|
||||
@@ -0,0 +1,36 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import requests
|
||||
from agent_framework import ChatMessage, DataContent, Role, TextContent
|
||||
from agent_framework.azure import AzureChatClient
|
||||
|
||||
async def test_image():
|
||||
"""Test image analysis with Azure."""
|
||||
client = AzureChatClient()
|
||||
|
||||
# Fetch image from httpbin
|
||||
image_url = "https://httpbin.org/image/jpeg"
|
||||
response = requests.get(image_url)
|
||||
image_b64 = base64.b64encode(response.content).decode()
|
||||
image_uri = f"data:image/jpeg;base64,{image_b64}"
|
||||
|
||||
message = ChatMessage(
|
||||
role=Role.USER,
|
||||
contents=[
|
||||
TextContent(text="What's in this image?"),
|
||||
DataContent(uri=image_uri, media_type="image/jpeg")
|
||||
]
|
||||
)
|
||||
|
||||
response = await client.get_response(message)
|
||||
print(f"Image Response: {response}")
|
||||
|
||||
|
||||
async def main():
|
||||
print("=== Testing Azure Multimodal ===")
|
||||
await test_image()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,63 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import requests
|
||||
import struct
|
||||
from agent_framework import ChatMessage, DataContent, Role, TextContent
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
|
||||
async def test_image():
|
||||
"""Test image analysis with OpenAI."""
|
||||
client = OpenAIChatClient(ai_model_id="gpt-4o")
|
||||
|
||||
# Fetch image from httpbin
|
||||
image_url = "https://httpbin.org/image/jpeg"
|
||||
response = requests.get(image_url)
|
||||
image_b64 = base64.b64encode(response.content).decode()
|
||||
image_uri = f"data:image/jpeg;base64,{image_b64}"
|
||||
|
||||
message = ChatMessage(
|
||||
role=Role.USER,
|
||||
contents=[
|
||||
TextContent(text="What's in this image?"),
|
||||
DataContent(uri=image_uri, media_type="image/jpeg")
|
||||
]
|
||||
)
|
||||
|
||||
response = await client.get_response(message)
|
||||
print(f"Image Response: {response}")
|
||||
|
||||
async def test_audio():
|
||||
"""Test audio analysis with OpenAI."""
|
||||
client = OpenAIChatClient(ai_model_id="gpt-4o-audio-preview")
|
||||
|
||||
# Create minimal WAV file (0.1 seconds of silence)
|
||||
wav_header = (
|
||||
b'RIFF' + struct.pack('<I', 44) + # file size
|
||||
b'WAVEfmt ' + struct.pack('<I', 16) + # fmt chunk
|
||||
struct.pack('<HHIIHH', 1, 1, 8000, 16000, 2, 16) + # PCM, mono, 8kHz
|
||||
b'data' + struct.pack('<I', 1600) + # data chunk
|
||||
b'\x00' * 1600 # 0.1 sec silence
|
||||
)
|
||||
audio_b64 = base64.b64encode(wav_header).decode()
|
||||
audio_uri = f"data:audio/wav;base64,{audio_b64}"
|
||||
|
||||
message = ChatMessage(
|
||||
role=Role.USER,
|
||||
contents=[
|
||||
TextContent(text="What do you hear in this audio?"),
|
||||
DataContent(uri=audio_uri, media_type="audio/wav")
|
||||
]
|
||||
)
|
||||
|
||||
response = await client.get_response(message)
|
||||
print(f"Audio Response: {response}")
|
||||
|
||||
async def main():
|
||||
print("=== Testing OpenAI Multimodal ===")
|
||||
await test_image()
|
||||
await test_audio()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user