Skip to content

Commit 762447a

Browse files
authored
Add local_file example to align with TS SDK repo (#1514)
1 parent a4f7204 commit 762447a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

examples/basic/local_file.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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/partial_o3-and-o4-mini-system-card.pdf")
8+
9+
10+
def file_to_base64(file_path: str) -> str:
11+
with open(file_path, "rb") as f:
12+
return base64.b64encode(f.read()).decode("utf-8")
13+
14+
15+
async def main():
16+
agent = Agent(
17+
name="Assistant",
18+
instructions="You are a helpful assistant.",
19+
)
20+
21+
b64_file = file_to_base64(FILEPATH)
22+
result = await Runner.run(
23+
agent,
24+
[
25+
{
26+
"role": "user",
27+
"content": [
28+
{
29+
"type": "input_file",
30+
"file_data": f"data:application/pdf;base64,{b64_file}",
31+
"filename": "partial_o3-and-o4-mini-system-card.pdf",
32+
}
33+
],
34+
},
35+
{
36+
"role": "user",
37+
"content": "What is the first sentence of the introduction?",
38+
},
39+
],
40+
)
41+
print(result.final_output)
42+
43+
44+
if __name__ == "__main__":
45+
asyncio.run(main())
Binary file not shown.

0 commit comments

Comments
 (0)