Skip to content

Commit f822fe7

Browse files
Merge pull request #3570 from mrbullwinkle/mrb_03_17_2025_image_input
[Azure OpenAI] Add image input
2 parents 44b2e42 + 4beed2a commit f822fe7

File tree

1 file changed

+136
-3
lines changed

1 file changed

+136
-3
lines changed

articles/ai-services/openai/how-to/responses.md

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ client = AzureOpenAI(
7777
response = client.responses.create(
7878
model="gpt-4o", # replace with your model deployment name
7979
input="This is a test."
80+
#truncation="auto" required when using computer-use-preview model.
8081

8182
)
8283
```
@@ -98,6 +99,7 @@ client = AzureOpenAI(
9899
response = client.responses.create(
99100
model="gpt-4o", # replace with your model deployment name
100101
input="This is a test."
102+
#truncation="auto" required when using computer-use-preview model.
101103

102104
)
103105
```
@@ -119,7 +121,7 @@ curl -X POST "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/responses?api-v
119121
### API Key
120122

121123
```bash
122-
curl -X https://YOUR-RESOURCE-NAME.openai.azure.com/openai/responses?api-version=2025-03-01-preview \
124+
curl -X POST https://YOUR-RESOURCE-NAME.openai.azure.com/openai/responses?api-version=2025-03-01-preview \
123125
-H "Content-Type: application/json" \
124126
-H "api-key: $AZURE_OPENAI_API_KEY" \
125127
-d '{
@@ -315,9 +317,9 @@ client = AzureOpenAI(
315317
api_version="2025-03-01-preview"
316318
)
317319

318-
response = client.responses.del("resp_67cb61fa3a448190bcf2c42d96f0d1a8")
320+
response = client.responses.delete("resp_67cb61fa3a448190bcf2c42d96f0d1a8")
319321

320-
print(response.model_dump_json(indent=2))
322+
print(response)
321323
```
322324

323325
## Chaining responses together
@@ -509,3 +511,134 @@ second_response = client.responses.create(
509511
print(second_response.model_dump_json(indent=2))
510512

511513
```
514+
515+
## List input items
516+
517+
```python
518+
from openai import AzureOpenAI
519+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
520+
521+
token_provider = get_bearer_token_provider(
522+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
523+
)
524+
525+
client = AzureOpenAI(
526+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
527+
azure_ad_token_provider=token_provider,
528+
api_version="2025-03-01-preview"
529+
)
530+
531+
response = client.responses.input_items.list("resp_67d856fcfba0819081fd3cffee2aa1c0")
532+
533+
print(response.model_dump_json(indent=2))
534+
```
535+
536+
**Output:**
537+
538+
```json
539+
{
540+
"data": [
541+
{
542+
"id": "msg_67d856fcfc1c8190ad3102fc01994c5f",
543+
"content": [
544+
{
545+
"text": "This is a test.",
546+
"type": "input_text"
547+
}
548+
],
549+
"role": "user",
550+
"status": "completed",
551+
"type": "message"
552+
}
553+
],
554+
"has_more": false,
555+
"object": "list",
556+
"first_id": "msg_67d856fcfc1c8190ad3102fc01994c5f",
557+
"last_id": "msg_67d856fcfc1c8190ad3102fc01994c5f"
558+
}
559+
```
560+
561+
## Image input
562+
563+
### Image url
564+
565+
```python
566+
from openai import AzureOpenAI
567+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
568+
569+
token_provider = get_bearer_token_provider(
570+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
571+
)
572+
573+
client = AzureOpenAI(
574+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
575+
azure_ad_token_provider=token_provider,
576+
api_version="2025-03-01-preview"
577+
)
578+
579+
response = client.responses.create(
580+
model="gpt-4o",
581+
input=[
582+
{
583+
"role": "user",
584+
"content": [
585+
{ "type": "input_text", "text": "what is in this image?" },
586+
{
587+
"type": "input_image",
588+
"image_url": "<image_URL>"
589+
}
590+
]
591+
}
592+
]
593+
)
594+
595+
print(response)
596+
597+
```
598+
599+
### Base64 encoded image
600+
601+
```python
602+
import base64
603+
from openai import AzureOpenAI
604+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
605+
606+
token_provider = get_bearer_token_provider(
607+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
608+
)
609+
610+
client = AzureOpenAI(
611+
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
612+
azure_ad_token_provider=token_provider,
613+
api_version="2025-03-01-preview"
614+
)
615+
616+
def encode_image(image_path):
617+
with open(image_path, "rb") as image_file:
618+
return base64.b64encode(image_file.read()).decode("utf-8")
619+
620+
# Path to your image
621+
image_path = "path_to_your_image.jpg"
622+
623+
# Getting the Base64 string
624+
base64_image = encode_image(image_path)
625+
626+
response = client.responses.create(
627+
model="gpt-4o",
628+
input=[
629+
{
630+
"role": "user",
631+
"content": [
632+
{ "type": "input_text", "text": "what is in this image?" },
633+
{
634+
"type": "input_image",
635+
"image_url": f"data:image/jpeg;base64,{base64_image}"
636+
}
637+
]
638+
}
639+
]
640+
)
641+
642+
print(response)
643+
```
644+

0 commit comments

Comments
 (0)