Skip to content

Commit

Permalink
Merge pull request #34 from AVGVSTVS96/chat
Browse files Browse the repository at this point in the history
Add GPT Chat page from FastGPT
  • Loading branch information
AVGVSTVS96 authored Feb 11, 2024
2 parents a076ee8 + 5741993 commit a4f6e8b
Show file tree
Hide file tree
Showing 13 changed files with 1,313 additions and 154 deletions.
1 change: 1 addition & 0 deletions .dev.vars-example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=<value>
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ pnpm-debug.log*

# macOS-specific files
.DS_Store


# Cloudfare
.wrangler
.dev.vars
Binary file modified bun.lockb
Binary file not shown.
70 changes: 70 additions & 0 deletions functions/chat.ts
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 });
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "astro build",
"preview": "astro preview",
"astro": "astro",
"release": "semantic-release"
"release": "semantic-release",
"wrangler": "bunx wrangler pages dev -- bun run dev"
},
"release": {
"branches": [
Expand All @@ -26,6 +27,7 @@
"astro": "^4.2.6",
"astro-expressive-code": "^0.32.3",
"astro-icon": "^1.0.3",
"openai": "^4.27.0",
"remark-sectionize": "github:avgvstvs96/remark-sectionize",
"sharp": "^0.33.2",
"typescript": "^5.3.3"
Expand All @@ -37,6 +39,7 @@
"prettier-plugin-tailwindcss": "^0.5.11",
"semantic-release": "^23.0.0",
"tailwindcss": "^3.4.1",
"type-coverage": "^2.27.1"
"type-coverage": "^2.27.1",
"wrangler": "^3.28.1"
}
}
1 change: 1 addition & 0 deletions src/components/NavBar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Icon } from 'astro-icon/components';
links={[
{ name: 'Minimal Typography', url: '/designProject' },
{ name: 'Old Flask Site', url: '/flaskSite' },
{ name: 'GPT Chat', url: '/gpt'}
]}
showCaret={true}
icon={false}
Expand Down
Loading

0 comments on commit a4f6e8b

Please sign in to comment.