Skip to content

Commit

Permalink
Remove db init service && merge with nano couch service, change env e…
Browse files Browse the repository at this point in the history
…xample && Add a GET request handler
  • Loading branch information
Mutugiii committed Jul 13, 2023
1 parent e1253bd commit 9fef794
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
2 changes: 2 additions & 0 deletions chatapi/env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
OPENAI_API_KEY=
COUCHDB_URL=http://localhost:2200
COUCHDB_NAME=chat_history
SERVE_PORT=5000
19 changes: 11 additions & 8 deletions chatapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ 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
});
} 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
10 changes: 0 additions & 10 deletions chatapi/src/services/db-init.service.ts

This file was deleted.

4 changes: 1 addition & 3 deletions chatapi/src/services/gpt-prompt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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[] = [];
Expand Down
10 changes: 6 additions & 4 deletions chatapi/src/utils/nano-couchdb.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<void> {
Expand All @@ -20,7 +21,8 @@ export class NanoCouchService implements DatabaseActions {

async insert(): Promise<DocumentInsertResponse> {
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) {
Expand Down

0 comments on commit 9fef794

Please sign in to comment.