From 2cbf1ebbaafb92238cc0c3a873515d6c05fdcc3a Mon Sep 17 00:00:00 2001 From: Kar Date: Fri, 8 Nov 2024 19:14:25 +0530 Subject: [PATCH] total_tokens --- package-lock.json | 4 +- package.json | 2 +- src/routes/api/aiFollowupQuestion.js | 171 +++++++++++++-------------- 3 files changed, 87 insertions(+), 90 deletions(-) diff --git a/package-lock.json b/package-lock.json index fe24eb4..3a3f093 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "iimttapi", - "version": "0.0.1", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "iimttapi", - "version": "0.0.1", + "version": "1.0.0", "license": "MIT", "dependencies": { "aws-sdk": "^2.1669.0", diff --git a/package.json b/package.json index c61c0b1..550ef98 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "docker:dev": "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up", "docker:test": "docker-compose -f docker-compose.yml -f docker-compose.test.yml up", "prepare": "husky install" - }, + }, "keywords": [ "node", "node.js", diff --git a/src/routes/api/aiFollowupQuestion.js b/src/routes/api/aiFollowupQuestion.js index a9e8c3e..49d306c 100644 --- a/src/routes/api/aiFollowupQuestion.js +++ b/src/routes/api/aiFollowupQuestion.js @@ -1,94 +1,91 @@ const aiFollowupQuestion = async (req, res) => { -const { MongoClient } = require('mongodb'); -const fetch = require('node-fetch'); -const { v4: uuidv4 } = require('uuid'); -const url = process.env.MONGODB_URL; -const dbName = process.env.MONGO_DB_NAME; -const client = new MongoClient(url); - -await client.connect(); -const database = client.db(dbName); // Replace with your database name -const conversationsCollection = database.collection('conversations'); // Replace with your collection name - -async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) { - const response = await fetch('https://api.openai.com/v1/chat/completions', { - method: 'POST', - headers: { - 'Authorization': `Bearer ${process.env.OPENAI_KEY}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - model: model, - messages: messages, - max_tokens: max_tokens, - }), - }); - - const data = await response.json(); - return data.choices[0].message.content; -} - - - try { - const { prompt, sessionId, model = "gpt-4o-mini", max_tokens = 200 } = req.body; - if (!conversationsCollection) { - return res.status(500).json({ - success: false, - error: 'MongoDB is not connected yet. Please try again later.', - }); - } - let conversation; - - if (!sessionId) { - const newSessionId = uuidv4(); - - conversation = { - sessionId: newSessionId, - conversationHistory: [ - { role: 'system', content: 'You are a helpful assistant.' }, - { role: 'user', content: prompt }, - ], - }; - await conversationsCollection.insertOne(conversation); - } else { - conversation = await conversationsCollection.findOne({ sessionId: sessionId }); - if (!conversation) { - return res.status(400).json({ + const { MongoClient } = require('mongodb'); + const fetch = require('node-fetch'); + const { v4: uuidv4 } = require('uuid'); + const url = process.env.MONGODB_URL; + const dbName = process.env.MONGO_DB_NAME; + const client = new MongoClient(url); + + await client.connect(); + const database = client.db(dbName); + const conversationsCollection = database.collection('conversations'); + + async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) { + const response = await fetch('https://api.openai.com/v1/chat/completions', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.OPENAI_KEY}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + model: model, + messages: messages, + max_tokens: max_tokens, + }), + }); + + const data = await response.json(); + return data; + } + + + try { + const { prompt, sessionId, model = "gpt-4o-mini", max_tokens = 200 } = req.body; + if (!conversationsCollection) { + return res.status(500).json({ success: false, - error: 'Invalid session ID.', + error: 'MongoDB is not connected yet. Please try again later.', }); } - conversation.conversationHistory.push({ role: 'user', content: prompt }); + let conversation; + + if (!sessionId) { + const newSessionId = uuidv4(); + + conversation = { + sessionId: newSessionId, + conversationHistory: [ + { role: 'system', content: 'answer within 50 words' }, + { role: 'user', content: prompt }, + ], + }; + await conversationsCollection.insertOne(conversation); + } else { + conversation = await conversationsCollection.findOne({ sessionId: sessionId }); + if (!conversation) { + return res.status(400).json({ + success: false, + error: 'Invalid session ID.', + }); + } + conversation.conversationHistory.push({ role: 'user', content: prompt }); + } + + const aiResponse = await fetchOpenAICompletion(prompt, conversation.conversationHistory, model, max_tokens); + + conversation.conversationHistory.push({ role: 'assistant', content: aiResponse.choices[0].message.content }); + + await conversationsCollection.updateOne( + { sessionId: conversation.sessionId }, + { $set: { conversationHistory: conversation.conversationHistory } } + ); + + res.json({ + success: true, + data: aiResponse.choices[0].message.content, + total_tokens: aiResponse.usage.total_tokens, + sessionId: conversation.sessionId, + }); + } catch (error) { + console.error('Error generating response:', error.message); + res.status(500).json({ + success: false, + error: 'Something went wrong. Please try again later.', + }); } - - // Call OpenAI API with the conversation history - const aiResponse = await fetchOpenAICompletion(prompt, conversation.conversationHistory, model, max_tokens); - - // Add the AI's response to the conversation history - conversation.conversationHistory.push({ role: 'assistant', content: aiResponse }); - - // Update the conversation in MongoDB - await conversationsCollection.updateOne( - { sessionId: conversation.sessionId }, - { $set: { conversationHistory: conversation.conversationHistory } } - ); - - // Send the AI's response and session ID back to the frontend - res.json({ - success: true, - data: aiResponse, - sessionId: conversation.sessionId, // Return the session ID for subsequent queries - }); - } catch (error) { - console.error('Error generating response:', error.message); - res.status(500).json({ - success: false, - error: 'Something went wrong. Please try again later.', - }); + + } - - -} - -module.exports = aiFollowupQuestion; \ No newline at end of file + + module.exports = aiFollowupQuestion; \ No newline at end of file