-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from AVGVSTVS96/chat
Add GPT Chat page from FastGPT
- Loading branch information
Showing
13 changed files
with
1,313 additions
and
154 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_API_KEY=<value> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,8 @@ pnpm-debug.log* | |
|
||
# macOS-specific files | ||
.DS_Store | ||
|
||
|
||
# Cloudfare | ||
.wrangler | ||
.dev.vars |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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 }); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.