{"id":13479183,"url":"https://github.com/SpellcraftAI/openai-streams","last_synced_at":"2025-03-27T09:30:53.540Z","repository":{"id":65896641,"uuid":"601850047","full_name":"SpellcraftAI/openai-streams","owner":"SpellcraftAI","description":"Tools for working with OpenAI streams in Node.js and TypeScript.","archived":false,"fork":false,"pushed_at":"2023-07-20T20:15:40.000Z","size":351,"stargazers_count":291,"open_issues_count":4,"forks_count":26,"subscribers_count":2,"default_branch":"canary","last_synced_at":"2025-03-19T21:54:00.892Z","etag":null,"topics":["openai","streams"],"latest_commit_sha":null,"homepage":"https://openai-streams.vercel.app","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SpellcraftAI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-02-15T00:33:31.000Z","updated_at":"2025-03-04T08:01:23.000Z","dependencies_parsed_at":"2024-01-16T06:22:04.824Z","dependency_job_id":"a50404ec-8e4b-4604-8a86-c5ded6e78df4","html_url":"https://github.com/SpellcraftAI/openai-streams","commit_stats":null,"previous_names":["gptlabs/openai-streams"],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SpellcraftAI%2Fopenai-streams","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SpellcraftAI%2Fopenai-streams/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SpellcraftAI%2Fopenai-streams/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SpellcraftAI%2Fopenai-streams/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SpellcraftAI","download_url":"https://codeload.github.com/SpellcraftAI/openai-streams/tar.gz/refs/heads/canary","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245316782,"owners_count":20595496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["openai","streams"],"created_at":"2024-07-31T16:02:10.994Z","updated_at":"2025-03-27T09:30:53.514Z","avatar_url":"https://github.com/SpellcraftAI.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# OpenAI Streams\n\n[**GitHub**](https://github.com/SpellcraftAI/openai-streams) |\n[**NPM**](https://npmjs.com/package/openai-streams) |\n[**Docs**](https://openai-streams.vercel.app)\n\n\u003e Now with ChatGPT API support! See [**Use with ChatGPT\n\u003e API**](#use-with-chatgpt-api). (Whisper coming soon!)\n\nThis library returns OpenAI API responses as streams only. Non-stream endpoints\nlike `edits` etc. are simply a stream with only one chunk update.\n\n- Prioritizes streams, so you can display a completion as it arrives.\n- Auto-loads `OPENAI_API_KEY` from `process.env`.\n- One single function with inferred parameter type based on the endpoint you\n  provide.\n\nUses `ReadableStream` by default for browser, Edge Runtime, and Node 18+, with\na `NodeJS.Readable` version available at `openai-streams/node`.\n\n### Installation\n\n```bash\nyarn add openai-streams\n# -or-\nnpm i --save openai-streams\n```\n\n### Usage\n\n```ts\nawait OpenAI(\n  /** 'completions', 'chat', etc. */\n  ENDPOINT,\n  /** max_tokens, temperature, messages, etc. */\n  PARAMS,\n  /** apiBase, apiKey, mode, controller, etc */\n  OPTIONS\n);\n```\n\n1. **Set the `OPENAI_API_KEY` env variable** (or pass the `{ apiKey }` option).\n\n   The library will throw if it cannot find an API key. Your program will load\n   this at runtime from `process.env.OPENAI_API_KEY` by default, but you may\n   override this with the `{ apiKey }` option.\n\n   **IMPORTANT:** For security, you should only load this from a `process.env`\n   variable.\n\n   ```ts\n   await OpenAI(\n     \"completions\",\n     {\n       /* endpoint params */\n     },\n     { apiKey: process.env.MY_SECRET_API_KEY }\n   );\n   ```\n\n2. **Call the API via `await OpenAI(endpoint, params, options?)`.**\n\n   The `params` type will be inferred based on the `endpoint` you provide, i.e.\n   for the `\"edits\"` endpoint, `import('openai').CreateEditRequest` will be\n   enforced.\n\n   Example with `raw` streaming mode:\n\n   ```ts\n   await OpenAI(\n     \"chat\",\n     {\n       messages: [\n         /* ... */\n       ],\n     },\n     { mode: \"raw\" }\n   );\n   ```\n\n#### Edge/Browser: Consuming streams in Next.js Edge functions\n\nThis will also work in the browser, but you'll need users to paste their OpenAI\nkey and pass it in via the `{ apiKey }` option.\n\n```ts\nimport { OpenAI } from \"openai-streams\";\n\nexport default async function handler() {\n  const stream = await OpenAI(\"completions\", {\n    model: \"text-davinci-003\",\n    prompt: \"Write a happy sentence.\\n\\n\",\n    max_tokens: 100,\n  });\n\n  return new Response(stream);\n}\n\nexport const config = {\n  runtime: \"edge\",\n};\n```\n\n#### Node: Consuming streams in Next.js API Route (Node)\n\nIf you cannot use an Edge runtime or want to consume Node.js streams for another\nreason, use `openai-streams/node`:\n\n```ts\nimport type { NextApiRequest, NextApiResponse } from \"next\";\nimport { OpenAI } from \"openai-streams/node\";\n\nexport default async function test(_: NextApiRequest, res: NextApiResponse) {\n  const stream = await OpenAI(\"completions\", {\n    model: \"text-davinci-003\",\n    prompt: \"Write a happy sentence.\\n\\n\",\n    max_tokens: 25,\n  });\n\n  stream.pipe(res);\n}\n```\n\n\u003csub\u003eSee the example in\n[`example/src/pages/api/hello.ts`](https://github.com/SpellcraftAI/openai-streams/blob/master/example/src/pages/api/hello.ts).\u003c/sub\u003e\n\u003csub\u003e\n\n#### Use with ChatGPT API\n\nBy default, with `mode = \"tokens\"`, you will receive just the message deltas.\nFor full events, use `mode = \"raw\"`.\n\nSee: https://platform.openai.com/docs/guides/chat/introduction\n\n```ts\nconst stream = await OpenAI(\"chat\", {\n  model: \"gpt-3.5-turbo\",\n  messages: [\n    {\n      role: \"system\",\n      content: \"You are a helpful assistant that translates English to French.\",\n    },\n    {\n      role: \"user\",\n      content: 'Translate the following English text to French: \"Hello world!\"',\n    },\n  ],\n});\n```\n\nIn `tokens` mode, you will just receive the response chunks, which look like this\n(separated with newlines for illustration):\n\n```\nHello\n!\n How\n can\n I\n assist\n you\n today\n?\n```\n\nUse `mode = \"raw\"` for access to raw events.\n\n### Notes\n\n1. Internally, streams are often manipulated using generators via `for await\n(const chunk of yieldStream(stream)) { ... }`. We recommend following this\n   pattern if you find it intuitive.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSpellcraftAI%2Fopenai-streams","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSpellcraftAI%2Fopenai-streams","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSpellcraftAI%2Fopenai-streams/lists"}