diff --git a/chatapi/env.example b/chatapi/env.example index 979e6721e7..bd6588a3a6 100644 --- a/chatapi/env.example +++ b/chatapi/env.example @@ -1,2 +1,4 @@ OPENAI_API_KEY= COUCHDB_URL=http://localhost:2200 +COUCHDB_NAME=chat_history +SERVE_PORT=5000 diff --git a/chatapi/src/index.ts b/chatapi/src/index.ts index 1e1375fedc..4c0082d197 100644 --- a/chatapi/src/index.ts +++ b/chatapi/src/index.ts @@ -9,14 +9,21 @@ const app = express(); // Parse JSON bodies (as sent by API clients) app.use(express.json()); -app.post('/', async (req: any, res: any, next: any) => { +app.get('/', (req: any, res: any) => { + res.status(200).json({ + 'status': 'Success', + 'message': 'OLE Chat API Service', + }); +}); + +app.post('/', async (req: any, res: any) => { try { const userInput = req.body.content; if (userInput && typeof userInput === 'string') { const response = await chatWithGpt(userInput); res.status(200).json({ - 'message': 'Success', + 'status': 'Success', 'chat': response?.completionText, 'history': response?.history, 'couchDBResponse': response?.couchSaveResponse @@ -24,15 +31,11 @@ app.post('/', async (req: any, res: any, next: any) => { } else { res.status(400).json({ 'error': 'Bad Request', 'message': 'The "content" field must be a non-empty string.' }); } - } catch (error) { - next(error); + } catch (error: any) { + res.status(500).json({ 'error': 'Internal Server Error', 'message': error.message }); } }); -app.use((err: Error, req: any, res: any) => { - res.status(500).json({ 'error': 'Internal Server Error', 'message': err.message }); -}); - const port = process.env.SERVE_PORT || 5000; app.listen(port, () => console.log(`Server running on port ${port}`)); // eslint-disable-line no-console diff --git a/chatapi/src/services/db-init.service.ts b/chatapi/src/services/db-init.service.ts deleted file mode 100644 index b1a9217afc..0000000000 --- a/chatapi/src/services/db-init.service.ts +++ /dev/null @@ -1,10 +0,0 @@ -import nano from 'nano'; - -export class DbInitService { - db: any; - - constructor(databaseUrl: string, databaseName: string) { - const nanoDB = nano(databaseUrl); - this.db = nanoDB.use(databaseName); - } -} diff --git a/chatapi/src/services/gpt-prompt.service.ts b/chatapi/src/services/gpt-prompt.service.ts index 93fe47612a..aa59954cd1 100644 --- a/chatapi/src/services/gpt-prompt.service.ts +++ b/chatapi/src/services/gpt-prompt.service.ts @@ -4,7 +4,6 @@ import { DocumentInsertResponse } from 'nano'; import { ChatItem } from '../models/chat-item.model'; import { ChatMessage } from '../models/chat-message.model'; -import { DbInitService } from './db-init.service'; import { NanoCouchService } from '../utils/nano-couchdb'; dotenv.config(); @@ -14,11 +13,10 @@ const configuration = new Configuration({ }); const openai = new OpenAIApi(configuration); -const dbInit = new DbInitService( +const db = new NanoCouchService( process.env.COUCHDB_URL || 'http://localhost:2200/', process.env.COUCHDB_NAME || 'chat_history', ); -const db = new NanoCouchService(dbInit.db); // history = db.get the history | [] if empty; const history: ChatItem[] = []; diff --git a/chatapi/src/utils/nano-couchdb.ts b/chatapi/src/utils/nano-couchdb.ts index df2d839fee..137a1bcd2b 100644 --- a/chatapi/src/utils/nano-couchdb.ts +++ b/chatapi/src/utils/nano-couchdb.ts @@ -1,4 +1,4 @@ -import { DocumentInsertResponse } from 'nano'; +import nano, { DocumentInsertResponse } from 'nano'; import { DatabaseActions } from '../models/database-actions.model'; export class NanoCouchService implements DatabaseActions { @@ -7,8 +7,9 @@ export class NanoCouchService implements DatabaseActions { [key: string]: any; private db: DatabaseActions; - constructor(db: DatabaseActions) { - this.db = db; + constructor(databaseUrl: string, databaseName: string) { + const nanoDB = nano(databaseUrl); + this.db = nanoDB.use(databaseName); } async processAPIResponse(response: DocumentInsertResponse): Promise { @@ -20,7 +21,8 @@ export class NanoCouchService implements DatabaseActions { async insert(): Promise { try { - const res = await this.db.insert(this); + const { db, ...document } = this; // eslint-disable-line @typescript-eslint/no-unused-vars + const res = await this.db.insert(document); this.processAPIResponse(res); return res; } catch (error) {