Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1310c79

Browse files
authored
Merge b536ca8 into sapling-pr-archive-rm-openai
2 parents 2c6827d + b536ca8 commit 1310c79

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

examples/basic/local_image.py

Lines 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

230 KB
Loading

examples/basic/remote_image.py

Lines 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)