Skip to content

Commit 56b3ebe

Browse files
chore(examples): add local file input example (#88)
1 parent f693ece commit 56b3ebe

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

examples/basic/local-file.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { Agent, run } from '@openai/agents';
4+
5+
const filePath = path.join(
6+
__dirname,
7+
'media/partial_o3-and-o4-mini-system-card.pdf',
8+
);
9+
10+
function fileToBase64(filePath: string): string {
11+
const fileBuffer = fs.readFileSync(filePath);
12+
return fileBuffer.toString('base64');
13+
}
14+
15+
async function main() {
16+
const agent = new Agent({
17+
name: 'Assistant',
18+
instructions: 'You are a helpful assistant.',
19+
});
20+
21+
const b64File = fileToBase64(filePath);
22+
const result = await run(agent, [
23+
{
24+
role: 'user',
25+
content: [
26+
{
27+
type: 'input_file',
28+
file: `data:application/pdf;base64,${b64File}`,
29+
providerData: {
30+
filename: 'partial_o3-and-o4-mini-system-card.pdf',
31+
},
32+
},
33+
],
34+
},
35+
{
36+
role: 'user',
37+
content: 'What is the first sentence of the introduction?',
38+
},
39+
]);
40+
41+
console.log(result.finalOutput);
42+
// OpenAI o3 and OpenAI o4-mini combine state-of-the-art reasoning with full tool capabilities — web browsing, Python, image and file analysis, image generation, canvas, automations, file search, and memory.
43+
}
44+
45+
if (require.main === module) {
46+
main().catch(console.error);
47+
}
211 KB
Binary file not shown.

examples/basic/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"start:json-schema-output-type": "tsx json-schema-output-type.ts",
2222
"start:tool-use-behavior": "tsx tool-use-behavior.ts",
2323
"start:tools": "tsx tools.ts",
24-
"start:reasoning": "tsx reasoning.ts"
24+
"start:reasoning": "tsx reasoning.ts",
25+
"start:local-file": "tsx local-file.ts"
2526
}
2627
}

0 commit comments

Comments
 (0)