Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Step 4: Streaming chat completions from an Astro API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
colbyfayock committed Jan 23, 2024
1 parent ef630f3 commit 7668f2f
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 0 deletions.
177 changes: 177 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -16,10 +16,12 @@
"@astrojs/vercel": "^6.1.3",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"ai": "^2.2.31",
"appwrite": "^13.0.1",
"astro": "^4.1.1",
"marked": "^11.1.1",
"novel": "^0.1.22",
"openai": "^4.25.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.4.1",
Expand Down
37 changes: 37 additions & 0 deletions src/pages/api/generate.ts
@@ -0,0 +1,37 @@
import type { APIRoute } from 'astro';
import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

const openai = new OpenAI({
apiKey: String(import.meta.env.OPENAI_API_KEY),
});

export const POST: APIRoute = async ({ request }) => {
const { prompt } = await request.json();

const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content:
'You are an AI writing assistant that continues existing text based on context from prior text. ' +
'Give more weight/priority to the later characters than the beginning ones. ' +
'Limit your response to no more than 200 characters, but make sure to construct complete sentences.',
},
{
role: 'user',
content: prompt,
},
],
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stream: true,
n: 1,
});

const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
}

0 comments on commit 7668f2f

Please sign in to comment.