Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b8472d

Browse filesBrowse files
authored
Examples for image inputs (#553)
1 parent 5639606 commit 4b8472d
Copy full SHA for 4b8472d

File tree

3 files changed

+79
-0
lines changed
Filter options

3 files changed

+79
-0
lines changed

‎examples/basic/local_image.py

Copy file name to clipboard
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import base64
3+
import os
4+
5+
from agents import Agent, Runner
6+
7+
FILEPATH = os.path.join(os.path.dirname(__file__), "media/image_bison.jpg")
8+
9+
10+
def image_to_base64(image_path):
11+
with open(image_path, "rb") as image_file:
12+
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
13+
return encoded_string
14+
15+
16+
async def main():
17+
# Print base64-encoded image
18+
b64_image = image_to_base64(FILEPATH)
19+
20+
agent = Agent(
21+
name="Assistant",
22+
instructions="You are a helpful assistant.",
23+
)
24+
25+
result = await Runner.run(
26+
agent,
27+
[
28+
{
29+
"role": "user",
30+
"content": [
31+
{
32+
"type": "input_image",
33+
"detail": "auto",
34+
"image_url": f"data:image/jpeg;base64,{b64_image}",
35+
}
36+
],
37+
},
38+
{
39+
"role": "user",
40+
"content": "What do you see in this image?",
41+
},
42+
],
43+
)
44+
print(result.final_output)
45+
46+
47+
if __name__ == "__main__":
48+
asyncio.run(main())

‎examples/basic/media/image_bison.jpg

Copy file name to clipboard
230 KB
Loading

‎examples/basic/remote_image.py

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import asyncio
2+
3+
from agents import Agent, Runner
4+
5+
URL = "https://upload.wikimedia.org/wikipedia/commons/0/0c/GoldenGateBridge-001.jpg"
6+
7+
8+
async def main():
9+
agent = Agent(
10+
name="Assistant",
11+
instructions="You are a helpful assistant.",
12+
)
13+
14+
result = await Runner.run(
15+
agent,
16+
[
17+
{
18+
"role": "user",
19+
"content": [{"type": "input_image", "detail": "auto", "image_url": URL}],
20+
},
21+
{
22+
"role": "user",
23+
"content": "What do you see in this image?",
24+
},
25+
],
26+
)
27+
print(result.final_output)
28+
29+
30+
if __name__ == "__main__":
31+
asyncio.run(main())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.