Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: 4.21.0 #545

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "4.20.1"
".": "4.21.0"
}
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Changelog

## 4.21.0 (2023-12-11)

Full Changelog: [v4.20.1...v4.21.0](https://github.com/openai/openai-node/compare/v4.20.1...v4.21.0)

### Features

* **client:** support reading the base url from an env variable ([#547](https://github.com/openai/openai-node/issues/547)) ([06fb68d](https://github.com/openai/openai-node/commit/06fb68de1ff80983e349b6715d1037e2072c8dd4))


### Bug Fixes

* correct some runTools behavior and deprecate runFunctions ([#562](https://github.com/openai/openai-node/issues/562)) ([f5cdd0f](https://github.com/openai/openai-node/commit/f5cdd0f704d3d075cdfc5bc2df1f7a8bae5cd9f1))
* prevent 400 when using runTools/runFunctions with Azure OpenAI API ([#544](https://github.com/openai/openai-node/issues/544)) ([735d9b8](https://github.com/openai/openai-node/commit/735d9b86acdc067e1ee6ebe1ea50de2955431050))


### Documentation

* **readme:** update example snippets ([#546](https://github.com/openai/openai-node/issues/546)) ([566d290](https://github.com/openai/openai-node/commit/566d290006920f536788bb77f4d24a6906e2971f))


### Build System

* specify `packageManager: yarn` ([#561](https://github.com/openai/openai-node/issues/561)) ([935b898](https://github.com/openai/openai-node/commit/935b8983c74f7b03b67d22f4d194989838f963f3))

## 4.20.1 (2023-11-24)

Full Changelog: [v4.20.0...v4.20.1](https://github.com/openai/openai-node/compare/v4.20.0...v4.20.1)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You can import in Deno via:
<!-- x-release-please-start-version -->

```ts
import OpenAI from 'https://deno.land/x/openai@v4.20.1/mod.ts';
import OpenAI from 'https://deno.land/x/openai@v4.21.0/mod.ts';
```

<!-- x-release-please-end -->
Expand All @@ -34,7 +34,7 @@ The full API of this library can be found in [api.md file](api.md) along with ma
import OpenAI from 'openai';

const openai = new OpenAI({
apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"]
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});

async function main() {
Expand Down Expand Up @@ -81,7 +81,7 @@ This library includes TypeScript definitions for all request params and response
import OpenAI from 'openai';

const openai = new OpenAI({
apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"]
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion build-deno
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This is a build produced from https://github.com/openai/openai-node – please g
Usage:

\`\`\`ts
import OpenAI from "https://deno.land/x/openai@v4.20.1/mod.ts";
import OpenAI from "https://deno.land/x/openai@v4.21.0/mod.ts";

const client = new OpenAI();
\`\`\`
Expand Down
144 changes: 144 additions & 0 deletions examples/tool-call-helpers-zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/usr/bin/env -S npm run tsn -T

import OpenAI from 'openai';
import { RunnableToolFunctionWithParse } from 'openai/lib/RunnableFunction';
import { JSONSchema } from 'openai/lib/jsonschema';
import { ZodSchema, z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';

// gets API Key from environment variable OPENAI_API_KEY
const openai = new OpenAI();

// Define your functions, alongside zod schemas.

const ListParams = z.object({
genre: z.enum(['mystery', 'nonfiction', 'memoir', 'romance', 'historical']),
});
type ListParams = z.infer<typeof ListParams>;
async function listBooks({ genre }: ListParams) {
return db.filter((item) => item.genre === genre).map((item) => ({ name: item.name, id: item.id }));
}

const SearchParams = z.object({
name: z.string(),
});
type SearchParams = z.infer<typeof SearchParams>;
async function searchBooks({ name }: SearchParams) {
return db.filter((item) => item.name.includes(name)).map((item) => ({ name: item.name, id: item.id }));
}

const GetParams = z.object({
id: z.string(),
});
type GetParams = z.infer<typeof GetParams>;
async function getBook({ id }: GetParams) {
return db.find((item) => item.id === id)!;
}

async function main() {
const runner = await openai.beta.chat.completions
.runTools({
model: 'gpt-4-1106-preview',
stream: true,
tools: [
zodFunction({
function: listBooks,
schema: ListParams,
description: 'List queries books by genre, and returns a list of names of books',
}),
zodFunction({
function: searchBooks,
schema: SearchParams,
description: 'Search queries books by their name and returns a list of book names and their ids',
}),
zodFunction({
function: getBook,
schema: GetParams,
description:
"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.",
}),
],
messages: [
{
role: 'system',
content:
'Please use our book database, which you can access using functions to answer the following questions.',
},
{
role: 'user',
content:
'I really enjoyed reading To Kill a Mockingbird, could you recommend me a book that is similar and tell me why?',
},
],
})
.on('message', (msg) => console.log('msg', msg))
.on('functionCall', (functionCall) => console.log('functionCall', functionCall))
.on('functionCallResult', (functionCallResult) => console.log('functionCallResult', functionCallResult))
.on('content', (diff) => process.stdout.write(diff));

const result = await runner.finalChatCompletion();
console.log();
console.log('messages');
console.log(runner.messages);

console.log();
console.log('final chat completion');
console.dir(result, { depth: null });
}

const db = [
{
id: 'a1',
name: 'To Kill a Mockingbird',
genre: 'historical',
description: `Compassionate, dramatic, and deeply moving, "To Kill A Mockingbird" takes readers to the roots of human behavior - to innocence and experience, kindness and cruelty, love and hatred, humor and pathos. Now with over 18 million copies in print and translated into forty languages, this regional story by a young Alabama woman claims universal appeal. Harper Lee always considered her book to be a simple love story. Today it is regarded as a masterpiece of American literature.`,
},
{
id: 'a2',
name: 'All the Light We Cannot See',
genre: 'historical',
description: `In a mining town in Germany, Werner Pfennig, an orphan, grows up with his younger sister, enchanted by a crude radio they find that brings them news and stories from places they have never seen or imagined. Werner becomes an expert at building and fixing these crucial new instruments and is enlisted to use his talent to track down the resistance. Deftly interweaving the lives of Marie-Laure and Werner, Doerr illuminates the ways, against all odds, people try to be good to one another.`,
},
{
id: 'a3',
name: 'Where the Crawdads Sing',
genre: 'historical',
description: `For years, rumors of the “Marsh Girl” haunted Barkley Cove, a quiet fishing village. Kya Clark is barefoot and wild; unfit for polite society. So in late 1969, when the popular Chase Andrews is found dead, locals immediately suspect her.
But Kya is not what they say. A born naturalist with just one day of school, she takes life's lessons from the land, learning the real ways of the world from the dishonest signals of fireflies. But while she has the skills to live in solitude forever, the time comes when she yearns to be touched and loved. Drawn to two young men from town, who are each intrigued by her wild beauty, Kya opens herself to a new and startling world—until the unthinkable happens.`,
},
];

/**
* A generic utility function that returns a RunnableFunction
* you can pass to `.runTools()`,
* with a fully validated, typesafe parameters schema.
*
* You are encouraged to copy/paste this into your codebase!
*/
function zodFunction<T extends object>({
function: fn,
schema,
description = '',
name,
}: {
function: (args: T) => Promise<object>;
schema: ZodSchema<T>;
description?: string;
name?: string;
}): RunnableToolFunctionWithParse<T> {
return {
type: 'function',
function: {
function: fn,
name: name ?? fn.name,
description: description,
parameters: zodToJsonSchema(schema) as JSONSchema,
parse(input: string): T {
const obj = JSON.parse(input);
return schema.parse(obj);
},
},
};
}

main();
7 changes: 7 additions & 0 deletions examples/tool-call-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { RunnableToolFunction } from 'openai/lib/RunnableFunction';
// gets API Key from environment variable OPENAI_API_KEY
const openai = new OpenAI();

/**
* Note, this will automatically ensure the model returns valid JSON,
* but won't ensure it conforms to your schema.
*
* For that functionality, please see the `tool-call-helpers-zod.ts` example,
* which shows a fully typesafe, schema-validating version.
*/
const tools: RunnableToolFunction<any>[] = [
{
type: 'function',
Expand Down
Loading