Examples for image inputs (#553)
This commit is contained in:
parent
5639606163
commit
4b8472da7e
3 changed files with 79 additions and 0 deletions
48
examples/basic/local_image.py
Normal file
48
examples/basic/local_image.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import asyncio
|
||||
import base64
|
||||
import os
|
||||
|
||||
from agents import Agent, Runner
|
||||
|
||||
FILEPATH = os.path.join(os.path.dirname(__file__), "media/image_bison.jpg")
|
||||
|
||||
|
||||
def image_to_base64(image_path):
|
||||
with open(image_path, "rb") as image_file:
|
||||
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
|
||||
return encoded_string
|
||||
|
||||
|
||||
async def main():
|
||||
# Print base64-encoded image
|
||||
b64_image = image_to_base64(FILEPATH)
|
||||
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You are a helpful assistant.",
|
||||
)
|
||||
|
||||
result = await Runner.run(
|
||||
agent,
|
||||
[
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "input_image",
|
||||
"detail": "auto",
|
||||
"image_url": f"data:image/jpeg;base64,{b64_image}",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What do you see in this image?",
|
||||
},
|
||||
],
|
||||
)
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
BIN
examples/basic/media/image_bison.jpg
Normal file
BIN
examples/basic/media/image_bison.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 230 KiB |
31
examples/basic/remote_image.py
Normal file
31
examples/basic/remote_image.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import asyncio
|
||||
|
||||
from agents import Agent, Runner
|
||||
|
||||
URL = "https://upload.wikimedia.org/wikipedia/commons/0/0c/GoldenGateBridge-001.jpg"
|
||||
|
||||
|
||||
async def main():
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
instructions="You are a helpful assistant.",
|
||||
)
|
||||
|
||||
result = await Runner.run(
|
||||
agent,
|
||||
[
|
||||
{
|
||||
"role": "user",
|
||||
"content": [{"type": "input_image", "detail": "auto", "image_url": URL}],
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What do you see in this image?",
|
||||
},
|
||||
],
|
||||
)
|
||||
print(result.final_output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Loading…
Reference in a new issue