diff --git a/ecosystem-tests/bun/bun.lockb b/ecosystem-tests/bun/bun.lockb index 4ece80c92..4cae9efcd 100755 Binary files a/ecosystem-tests/bun/bun.lockb and b/ecosystem-tests/bun/bun.lockb differ diff --git a/ecosystem-tests/bun/sample1.mp3 b/ecosystem-tests/bun/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/bun/sample1.mp3 and b/ecosystem-tests/bun/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts-cjs-auto/sample1.mp3 b/ecosystem-tests/node-ts-cjs-auto/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts-cjs-auto/sample1.mp3 and b/ecosystem-tests/node-ts-cjs-auto/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts-cjs-web/sample1.mp3 b/ecosystem-tests/node-ts-cjs-web/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts-cjs-web/sample1.mp3 and b/ecosystem-tests/node-ts-cjs-web/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts-cjs/sample1.mp3 b/ecosystem-tests/node-ts-cjs/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts-cjs/sample1.mp3 and b/ecosystem-tests/node-ts-cjs/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts-esm-auto/sample1.mp3 b/ecosystem-tests/node-ts-esm-auto/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts-esm-auto/sample1.mp3 and b/ecosystem-tests/node-ts-esm-auto/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts-esm-web/sample1.mp3 b/ecosystem-tests/node-ts-esm-web/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts-esm-web/sample1.mp3 and b/ecosystem-tests/node-ts-esm-web/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts-esm/sample1.mp3 b/ecosystem-tests/node-ts-esm/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts-esm/sample1.mp3 and b/ecosystem-tests/node-ts-esm/sample1.mp3 differ diff --git a/ecosystem-tests/node-ts4.5-jest27/sample1.mp3 b/ecosystem-tests/node-ts4.5-jest27/sample1.mp3 index 1e787cd7c..3606c98f2 100644 Binary files a/ecosystem-tests/node-ts4.5-jest27/sample1.mp3 and b/ecosystem-tests/node-ts4.5-jest27/sample1.mp3 differ diff --git a/helpers.md b/helpers.md index 8b53e284e..76423ee07 100644 --- a/helpers.md +++ b/helpers.md @@ -38,20 +38,22 @@ as a string. client.chat.completions.runTools({ model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: 'How is the weather this week?' }], - tools: [{ - type: 'function', - function: { - function: getWeather as (args: { location: string, time: Date}) => any, - parse: parseFunction as (args: strings) => { location: string, time: Date }, - parameters: { - type: 'object', - properties: { - location: { type: 'string' }, - time: { type: 'string', format: 'date-time' }, + tools: [ + { + type: 'function', + function: { + function: getWeather as (args: { location: string; time: Date }) => any, + parse: parseFunction as (args: strings) => { location: string; time: Date }, + parameters: { + type: 'object', + properties: { + location: { type: 'string' }, + time: { type: 'string', format: 'date-time' }, + }, }, }, - } - }], + }, + ], }); ``` @@ -236,7 +238,6 @@ async function main() { main(); ``` - ### Integrate with `zod` [`zod`](https://www.npmjs.com/package/zod) is a schema validation library which can help with validating the @@ -261,8 +262,8 @@ async function main() { function: getWeather, parse: GetWeatherParameters.parse, parameters: zodToJsonSchema(GetWeatherParameters), - } - } + }, + }, ], }) .on('message', (message) => console.log(message)); @@ -293,4 +294,3 @@ See an example of a Next.JS integration here [`examples/stream-to-client-next.ts ### Proxy Streaming to a Browser See an example of using express to stream to a browser here [`examples/stream-to-client-express.ts`](examples/stream-to-client-express.ts). - diff --git a/scripts/fix-index-exports.cjs b/scripts/fix-index-exports.cjs index 0909af7cb..b61b2ea33 100644 --- a/scripts/fix-index-exports.cjs +++ b/scripts/fix-index-exports.cjs @@ -1,7 +1,11 @@ const fs = require('fs'); const path = require('path'); -const indexJs = path.resolve(__dirname, '..', 'dist', 'index.js'); +const indexJs = + process.env['DIST_PATH'] ? + path.resolve(process.env['DIST_PATH'], 'index.js') + : path.resolve(__dirname, '..', 'dist', 'index.js'); + let before = fs.readFileSync(indexJs, 'utf8'); let after = before.replace( /^\s*exports\.default\s*=\s*(\w+)/m, diff --git a/scripts/make-dist-package-json.cjs b/scripts/make-dist-package-json.cjs index def768ed0..d4a0a69b3 100644 --- a/scripts/make-dist-package-json.cjs +++ b/scripts/make-dist-package-json.cjs @@ -1,4 +1,4 @@ -const pkgJson = require('../package.json'); +const pkgJson = require(process.env['PKG_JSON_PATH'] || '../package.json'); function processExportMap(m) { for (const key in m) { diff --git a/scripts/postprocess-files.cjs b/scripts/postprocess-files.cjs index 5379f9fa2..8fd9ec8db 100644 --- a/scripts/postprocess-files.cjs +++ b/scripts/postprocess-files.cjs @@ -2,7 +2,12 @@ const fs = require('fs'); const path = require('path'); const { parse } = require('@typescript-eslint/parser'); -const distDir = path.resolve(__dirname, '..', 'dist'); +const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'openai/' + +const distDir = + process.env['DIST_PATH'] ? + path.resolve(process.env['DIST_PATH']) + : path.resolve(__dirname, '..', 'dist'); const distSrcDir = path.join(distDir, 'src'); /** @@ -105,11 +110,11 @@ async function postprocess() { let transformed = mapModulePaths(code, (importPath) => { if (file.startsWith(distSrcDir)) { - if (importPath.startsWith('openai/')) { + if (importPath.startsWith(pkgImportPath)) { // convert self-references in dist/src to relative paths let relativePath = path.relative( path.dirname(file), - path.join(distSrcDir, importPath.substring('openai/'.length)), + path.join(distSrcDir, importPath.substring(pkgImportPath.length)), ); if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`; return relativePath; diff --git a/src/core.ts b/src/core.ts index b7037bd50..f431b7d58 100644 --- a/src/core.ts +++ b/src/core.ts @@ -342,6 +342,11 @@ export abstract class APIClient { return reqHeaders; } + /** + * Used as a callback for mutating the given `FinalRequestOptions` object. + */ + protected async prepareOptions(options: FinalRequestOptions): Promise {} + /** * Used as a callback for mutating the given `RequestInit` object. * @@ -387,6 +392,8 @@ export abstract class APIClient { retriesRemaining = options.maxRetries ?? this.maxRetries; } + await this.prepareOptions(options); + const { req, url, timeout } = this.buildRequest(options); await this.prepareRequest(req, { url, options }); @@ -1139,3 +1146,7 @@ export const toBase64 = (str: string | null | undefined): string => { throw new OpenAIError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined'); }; + +export function isObj(obj: unknown): obj is Record { + return obj != null && typeof obj === 'object' && !Array.isArray(obj); +}