Skip to content

Commit f18ced8

Browse files
stainless-emRobertCraigie
authored andcommitted
chore(docs): update zod tool call example, fix azure tests
1 parent 45a372c commit f18ced8

File tree

3 files changed

+16
-47
lines changed

3 files changed

+16
-47
lines changed

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"express": "^4.18.2",
1212
"next": "^14.1.1",
1313
"openai": "file:..",
14-
"zod-to-json-schema": "^3.21.4"
14+
"zod-to-json-schema": "^3.21.4",
15+
"@azure/identity": "^4.2.0"
1516
},
1617
"devDependencies": {
1718
"@types/body-parser": "^1.19.3",

examples/tool-call-helpers-zod.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import OpenAI from 'openai';
4-
import { RunnableToolFunctionWithParse } from 'openai/lib/RunnableFunction';
5-
import { JSONSchema } from 'openai/lib/jsonschema';
6-
import { ZodSchema, z } from 'zod';
7-
import { zodToJsonSchema } from 'zod-to-json-schema';
4+
import { zodFunction } from 'openai/helpers/zod';
5+
import { z } from 'zod';
86

97
// gets API Key from environment variable OPENAI_API_KEY
108
const openai = new OpenAI();
@@ -42,18 +40,21 @@ async function main() {
4240
stream: true,
4341
tools: [
4442
zodFunction({
43+
name: 'listBooks',
4544
function: listBooks,
46-
schema: ListParams,
45+
parameters: ListParams,
4746
description: 'List queries books by genre, and returns a list of names of books',
4847
}),
4948
zodFunction({
49+
name: 'searchBooks',
5050
function: searchBooks,
51-
schema: SearchParams,
51+
parameters: SearchParams,
5252
description: 'Search queries books by their name and returns a list of book names and their ids',
5353
}),
5454
zodFunction({
55+
name: 'getBook',
5556
function: getBook,
56-
schema: GetParams,
57+
parameters: GetParams,
5758
description:
5859
"Get returns a book's detailed information based on the id of the book. Note that this does not accept names, and only IDs, which you can get by using search.",
5960
}),
@@ -108,37 +109,4 @@ But Kya is not what they say. A born naturalist with just one day of school, she
108109
},
109110
];
110111

111-
/**
112-
* A generic utility function that returns a RunnableFunction
113-
* you can pass to `.runTools()`,
114-
* with a fully validated, typesafe parameters schema.
115-
*
116-
* You are encouraged to copy/paste this into your codebase!
117-
*/
118-
function zodFunction<T extends object>({
119-
function: fn,
120-
schema,
121-
description = '',
122-
name,
123-
}: {
124-
function: (args: T) => Promise<object>;
125-
schema: ZodSchema<T>;
126-
description?: string;
127-
name?: string;
128-
}): RunnableToolFunctionWithParse<T> {
129-
return {
130-
type: 'function',
131-
function: {
132-
function: fn,
133-
name: name ?? fn.name,
134-
description: description,
135-
parameters: zodToJsonSchema(schema) as JSONSchema,
136-
parse(input: string): T {
137-
const obj = JSON.parse(input);
138-
return schema.parse(obj);
139-
},
140-
},
141-
};
142-
}
143-
144112
main();

tests/lib/azure.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('azure request building', () => {
376376
expect(
377377
await client.audio.translations.create({
378378
model,
379-
file: { url: 'https://example.com', blob: async () => new Blob([]) },
379+
file: new File([], ''),
380380
}),
381381
).toMatchObject({
382382
url: `https://example.com/openai/deployments/${deployment}/audio/translations?api-version=${apiVersion}`,
@@ -387,7 +387,7 @@ describe('azure request building', () => {
387387
expect(
388388
await client.audio.transcriptions.create({
389389
model,
390-
file: { url: 'https://example.com', blob: async () => new Blob([]) },
390+
file: new File([], ''),
391391
}),
392392
).toMatchObject({
393393
url: `https://example.com/openai/deployments/${deployment}/audio/transcriptions?api-version=${apiVersion}`,
@@ -432,7 +432,7 @@ describe('azure request building', () => {
432432
test('handles files', async () => {
433433
expect(
434434
await client.files.create({
435-
file: { url: 'https://example.com', blob: async () => new Blob([]) },
435+
file: new File([], ''),
436436
purpose: 'assistants',
437437
}),
438438
).toMatchObject({
@@ -509,7 +509,7 @@ describe('azure request building', () => {
509509
expect(
510510
await client.audio.translations.create({
511511
model: deployment,
512-
file: { url: 'https://example.com', blob: async () => new Blob([]) },
512+
file: new File([], ''),
513513
}),
514514
).toMatchObject({
515515
url: `https://example.com/openai/deployments/${deployment}/audio/translations?api-version=${apiVersion}`,
@@ -520,7 +520,7 @@ describe('azure request building', () => {
520520
expect(
521521
await client.audio.transcriptions.create({
522522
model: deployment,
523-
file: { url: 'https://example.com', blob: async () => new Blob([]) },
523+
file: new File([], ''),
524524
}),
525525
).toMatchObject({
526526
url: `https://example.com/openai/deployments/${deployment}/audio/transcriptions?api-version=${apiVersion}`,
@@ -565,7 +565,7 @@ describe('azure request building', () => {
565565
test('handles files', async () => {
566566
expect(
567567
await client.files.create({
568-
file: { url: 'https://example.com', blob: async () => new Blob([]) },
568+
file: new File([], ''),
569569
purpose: 'assistants',
570570
}),
571571
).toMatchObject({

0 commit comments

Comments
 (0)