diff --git a/README.md b/README.md index 1493376ce..f88338db8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ You can import in Deno via: ```ts -import OpenAI from 'https://raw.githubusercontent.com/openai/openai-node/v4.14.2-deno/mod.ts'; +import OpenAI from 'https://raw.githubusercontent.com/openai/openai-node/v4.15.0-deno/mod.ts'; ``` @@ -395,6 +395,45 @@ console.log(raw.headers.get('X-My-Header')); console.log(chatCompletion.choices); ``` +## Customizing the fetch client + +By default, this library uses `node-fetch` in Node, and expects a global `fetch` function in other environments. + +If you would prefer to use a global, web-standards-compliant `fetch` function even in a Node environment, +(for example, if you are running Node with `--experimental-fetch` or using NextJS which polyfills with `undici`), +add the following import before your first import `from "OpenAI"`: + + +```ts +// Tell TypeScript and the package to use the global web fetch instead of node-fetch. +// Note, despite the name, this does not add any polyfills, but expects them to be provided if needed. +import "openai/shims/web"; +import OpenAI from "openai"; +``` + +To do the inverse, add `import "openai/shims/node"` (which does import polyfills). +This can also be useful if you are getting the wrong TypeScript types for `Response` - more details [here](https://github.com/openai/openai-node/src/_shims#readme). + +You may also provide a custom `fetch` function when instantiating the client, +which can be used to inspect or alter the `Request` or `Response` before/after each request: + +```ts +import { fetch } from 'undici'; // as one example +import OpenAI from 'openai'; + +const client = new OpenAI({ + fetch: (url: RequestInfo, init?: RequestInfo): Response => { + console.log('About to make request', url, init); + const response = await fetch(url, init); + console.log('Got response', response); + return response; + }, +}); +``` + +Note that if given a `DEBUG=true` environment variable, this library will log all requests and responses automatically. +This is intended for debugging purposes only and may change in the future without notice. + ## Configuring an HTTP(S) Agent (e.g., for proxies) By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.