From 5f929e0dacc8eb0da1ec0485e7f2d58b28f056d1 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 14 Oct 2024 10:27:32 +0200 Subject: [PATCH 1/2] fix code formatting --- .../workers/examples/openai-sdk-streaming.mdx | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/content/docs/workers/examples/openai-sdk-streaming.mdx b/src/content/docs/workers/examples/openai-sdk-streaming.mdx index 092255024743c2..30b417607b8ec1 100644 --- a/src/content/docs/workers/examples/openai-sdk-streaming.mdx +++ b/src/content/docs/workers/examples/openai-sdk-streaming.mdx @@ -12,49 +12,50 @@ sidebar: order: 1001 head: [] description: Use the OpenAI v4 SDK to stream responses from OpenAI. - --- In order to run this code, you must install the OpenAI SDK by running `npm i openai`. :::note - For analytics, caching, rate limiting, and more, you can also send requests like this through Cloudflare's [AI Gateway](/ai-gateway/providers/openai/). - ::: ```ts import OpenAI from "openai"; export default { - async fetch(request, env, ctx): Promise { - const openai = new OpenAI({ - apiKey: env.OPENAI_API_KEY - }); + async fetch(request, env, ctx): Promise { + const openai = new OpenAI({ + apiKey: env.OPENAI_API_KEY, + }); -// Create a TransformStream to handle streaming data + // Create a TransformStream to handle streaming data let { readable, writable } = new TransformStream(); let writer = writable.getWriter(); const textEncoder = new TextEncoder(); - ctx.waitUntil((async () => { + ctx.waitUntil( + (async () => { const stream = await openai.chat.completions.create({ - model: 'gpt-4o-mini', - messages: [{ role: 'user', content: 'Tell me a story' }], - stream: true, + model: "gpt-4o-mini", + messages: [{ role: "user", content: "Tell me a story" }], + stream: true, }); // loop over the data as it is streamed and write to the writeable for await (const part of stream) { - writer.write(textEncoder.encode(part.choices[0]?.delta?.content || '')); + writer.write( + textEncoder.encode(part.choices[0]?.delta?.content || ""), + ); } writer.close(); - })()); + })(), + ); // Send the readable back to the browser - return new Response(readable); - }, + return new Response(readable); + }, } satisfies ExportedHandler; ``` From 801105c0f1df236487f088254951a89f7a5329a0 Mon Sep 17 00:00:00 2001 From: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> Date: Thu, 17 Oct 2024 10:06:42 +0100 Subject: [PATCH 2/2] PCX review (prettify) --- .../workers/examples/openai-sdk-streaming.mdx | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/content/docs/workers/examples/openai-sdk-streaming.mdx b/src/content/docs/workers/examples/openai-sdk-streaming.mdx index 30b417607b8ec1..c9c416a30fe5fc 100644 --- a/src/content/docs/workers/examples/openai-sdk-streaming.mdx +++ b/src/content/docs/workers/examples/openai-sdk-streaming.mdx @@ -26,36 +26,36 @@ For analytics, caching, rate limiting, and more, you can also send requests like import OpenAI from "openai"; export default { - async fetch(request, env, ctx): Promise { - const openai = new OpenAI({ - apiKey: env.OPENAI_API_KEY, - }); - - // Create a TransformStream to handle streaming data - let { readable, writable } = new TransformStream(); - let writer = writable.getWriter(); - const textEncoder = new TextEncoder(); - - ctx.waitUntil( - (async () => { - const stream = await openai.chat.completions.create({ - model: "gpt-4o-mini", - messages: [{ role: "user", content: "Tell me a story" }], - stream: true, - }); - - // loop over the data as it is streamed and write to the writeable - for await (const part of stream) { - writer.write( - textEncoder.encode(part.choices[0]?.delta?.content || ""), - ); - } - writer.close(); - })(), - ); - - // Send the readable back to the browser - return new Response(readable); - }, + async fetch(request, env, ctx): Promise { + const openai = new OpenAI({ + apiKey: env.OPENAI_API_KEY, + }); + + // Create a TransformStream to handle streaming data + let { readable, writable } = new TransformStream(); + let writer = writable.getWriter(); + const textEncoder = new TextEncoder(); + + ctx.waitUntil( + (async () => { + const stream = await openai.chat.completions.create({ + model: "gpt-4o-mini", + messages: [{ role: "user", content: "Tell me a story" }], + stream: true, + }); + + // loop over the data as it is streamed and write to the writeable + for await (const part of stream) { + writer.write( + textEncoder.encode(part.choices[0]?.delta?.content || ""), + ); + } + writer.close(); + })(), + ); + + // Send the readable back to the browser + return new Response(readable); + }, } satisfies ExportedHandler; ```