From be7a4c70785fee52cb8b8893b48ee146b438badc Mon Sep 17 00:00:00 2001 From: steven Date: Thu, 30 Mar 2023 16:34:42 +0800 Subject: [PATCH] chore: update env variables in readme --- .env.example | 1 + README.md | 10 ++++++++-- pages/api/chat.ts | 14 ++++++++------ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index bc132bd..71c7bd2 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ +# Do not share your OpenAI API key with anyone! It should remain a secret. OPENAI_API_KEY= OPENAI_API_ENDPOINT= diff --git a/README.md b/README.md index 2b3497c..a85cca2 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,15 @@ SQL Chat is built by Next.js, it supports following databases and will add more ## Local Development -1. Create a `.env` file in the root directory with your `OPENAI_API_KEY`; +1. Make a copy of the example environment variables file; -2. Install dependencies and start the dev server; + ```bash + cp .env.example .env + ``` + +2. Add your [API key](https://platform.openai.com/account/api-keys) and OpenAI API Endpoint(optional) to the newly created `.env` file; + +3. Install dependencies and start the dev server; ```bash pnpm i && pnpm dev diff --git a/pages/api/chat.ts b/pages/api/chat.ts index 4604453..62b92d8 100644 --- a/pages/api/chat.ts +++ b/pages/api/chat.ts @@ -6,9 +6,11 @@ export const config = { runtime: "edge", }; +const apiEndpoint = new URL(`${openAIApiEndpoint}/v1/chat/completions`); + const handler = async (req: NextRequest) => { const reqBody = await req.json(); - const rawRes = await fetch(`${openAIApiEndpoint}/v1/chat/completions`, { + const res = await fetch(apiEndpoint, { headers: { "Content-Type": "application/json", Authorization: `Bearer ${openAIApiKey}`, @@ -23,10 +25,10 @@ const handler = async (req: NextRequest) => { stream: true, }), }); - if (!rawRes.ok) { - return new Response(rawRes.body, { - status: rawRes.status, - statusText: rawRes.statusText, + if (!res.ok) { + return new Response(res.body, { + status: res.status, + statusText: res.statusText, }); } @@ -52,7 +54,7 @@ const handler = async (req: NextRequest) => { } }; const parser = createParser(streamParser); - for await (const chunk of rawRes.body as any) { + for await (const chunk of res.body as any) { parser.feed(decoder.decode(chunk)); } },