Created
February 18, 2024 21:14
-
-
Save AVGVSTVS96/7e688e2723ff00042073a0a02fc4ae4a to your computer and use it in GitHub Desktop.
OpenAI Cloudflare Workers Function for GPT Chat Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OpenAI } from 'openai'; | |
interface Message { | |
role: string; | |
content: string; | |
} | |
interface ChatRequest { | |
messages: Message[]; | |
model_type: string; | |
} | |
export async function onRequest(context) { | |
const { request, env } = context; | |
if (request.method !== 'POST') { | |
return new Response('Method not allowed', { status: 405 }); | |
} | |
let requestBody: ChatRequest; | |
try { | |
requestBody = await request.json(); | |
} catch (error) { | |
return new Response('Invalid JSON', { status: 400 }); | |
} | |
const { messages, model_type } = requestBody; | |
const chatMessages = messages.map((msg) => ({ | |
role: msg.role as 'system' | 'user', | |
content: msg.content, | |
})); | |
const openai = new OpenAI({ | |
apiKey: env.OPENAI_API_KEY, | |
}); | |
try { | |
const stream = await openai.chat.completions.create({ | |
model: model_type, | |
messages: chatMessages, | |
stream: true, | |
}); | |
let { readable, writable } = new TransformStream(); | |
let writer = writable.getWriter(); | |
const textEncoder = new TextEncoder(); | |
(async () => { | |
try { | |
for await (const part of stream) { | |
const content = part.choices[0]?.delta?.content || ''; | |
if (content) { | |
await writer.write(textEncoder.encode(content)); | |
} | |
} | |
} catch (error) { | |
await writer.write( | |
textEncoder.encode(`Error processing stream: ${error.message}`) | |
); | |
} finally { | |
writer.close(); | |
} | |
})(); | |
return new Response(readable); | |
} catch (error) { | |
return new Response(`Error: ${error.message}`, { status: 500 }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment