chore: update env variables in readme

This commit is contained in:
steven
2023-03-30 16:34:42 +08:00
parent 04ab812070
commit be7a4c7078
3 changed files with 17 additions and 8 deletions

View File

@ -1,2 +1,3 @@
# Do not share your OpenAI API key with anyone! It should remain a secret.
OPENAI_API_KEY=<YOUR_API_KEY>
OPENAI_API_ENDPOINT=<YOUR_API_ENDPOINT>

View File

@ -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

View File

@ -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));
}
},