diff --git a/package.json b/package.json index eb00fd1..367f9a0 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "dependencies": { "@fastify/cors": "^8.2.0", + "@fastify/env": "^5.0.1", "@fastify/static": "^7.0.4", "@fastify/swagger": "^8.3.1", "@fastify/swagger-ui": "^1.5.0", @@ -31,6 +32,7 @@ "@tabler/icons-react": "^3.19.0", "dotenv": "^16.4.6", "fastify": "4.23.2", + "fastify-env": "^2.1.1", "next": "^14.2.15", "nodemon": "^2.0.20", "openai": "^4.74.0", diff --git a/src/api.ts b/src/api.ts index 2980985..bcc6380 100644 --- a/src/api.ts +++ b/src/api.ts @@ -6,9 +6,8 @@ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; import { Static, Type } from '@sinclair/typebox'; import { FastifyPluginCallback } from 'fastify'; import apiService from './api_service'; -import dotnev from 'dotenv'; -dotnev.config(); +const openAiApiKey = process.env.OPENAI_API_KEY as string; const HelloWorld = Type.String({ description: 'The magical words!' }); @@ -67,16 +66,10 @@ export default (opts: ApiOptions) => { api.register(healthcheck, { prefix: '/api', title: opts.title }); // register other API routes here - console.log('process.env.OPENAI_API_KEY', process.env.OPENAI_API_KEY); - if (!process.env.OPENAI_API_KEY) { - throw new Error( - 'The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: "My API Key" }).' - ); - } api.register(apiService, { prefix: '/api/v1', - openAiApiKey: process.env.OPENAI_API_KEY + openAiApiKey: openAiApiKey as string }); return api; diff --git a/src/api_service.ts b/src/api_service.ts index a65f88f..37b13b0 100644 --- a/src/api_service.ts +++ b/src/api_service.ts @@ -11,7 +11,6 @@ const apiService: FastifyPluginCallback = ( next ) => { fastify.post('/message', async (request, reply) => { - console.log('request.body', request.body); const userMessage = request.body as string; await chat(reply, userMessage); }); @@ -21,6 +20,9 @@ const apiService: FastifyPluginCallback = ( async function chat(reply: FastifyReply, userMessage: string) { const openai = opts.openAiApiKey; + if (!openai) { + throw new Error('OpenAI API key is required'); + } const client = new OpenAI({ apiKey: openai }); reply.raw.setHeader('Content-Type', 'text/event-stream'); diff --git a/src/app/page.tsx b/src/app/page.tsx index 08ccc68..bd3221b 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -25,7 +25,6 @@ export default function Page() { method: 'POST', body: JSON.stringify({ userMessage: input }) }); - console.log('response', response); if (response.ok) { setResponse(await response.text()); }