total_tokens

galleryApi
Kar 2024-11-08 19:14:25 +05:30
parent 5775c45d8c
commit 2cbf1ebbaa
3 changed files with 87 additions and 90 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "iimttapi", "name": "iimttapi",
"version": "0.0.1", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "iimttapi", "name": "iimttapi",
"version": "0.0.1", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"aws-sdk": "^2.1669.0", "aws-sdk": "^2.1669.0",

View File

@ -1,17 +1,17 @@
const aiFollowupQuestion = async (req, res) => { const aiFollowupQuestion = async (req, res) => {
const { MongoClient } = require('mongodb'); const { MongoClient } = require('mongodb');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const { v4: uuidv4 } = require('uuid'); const { v4: uuidv4 } = require('uuid');
const url = process.env.MONGODB_URL; const url = process.env.MONGODB_URL;
const dbName = process.env.MONGO_DB_NAME; const dbName = process.env.MONGO_DB_NAME;
const client = new MongoClient(url); const client = new MongoClient(url);
await client.connect(); await client.connect();
const database = client.db(dbName); // Replace with your database name const database = client.db(dbName);
const conversationsCollection = database.collection('conversations'); // Replace with your collection name const conversationsCollection = database.collection('conversations');
async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) { async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) {
const response = await fetch('https://api.openai.com/v1/chat/completions', { const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -26,8 +26,8 @@ async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", ma
}); });
const data = await response.json(); const data = await response.json();
return data.choices[0].message.content; return data;
} }
try { try {
@ -46,7 +46,7 @@ async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", ma
conversation = { conversation = {
sessionId: newSessionId, sessionId: newSessionId,
conversationHistory: [ conversationHistory: [
{ role: 'system', content: 'You are a helpful assistant.' }, { role: 'system', content: 'answer within 50 words' },
{ role: 'user', content: prompt }, { role: 'user', content: prompt },
], ],
}; };
@ -62,23 +62,20 @@ async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", ma
conversation.conversationHistory.push({ role: 'user', content: prompt }); conversation.conversationHistory.push({ role: 'user', content: prompt });
} }
// Call OpenAI API with the conversation history
const aiResponse = await fetchOpenAICompletion(prompt, conversation.conversationHistory, model, max_tokens); 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.choices[0].message.content });
conversation.conversationHistory.push({ role: 'assistant', content: aiResponse });
// Update the conversation in MongoDB
await conversationsCollection.updateOne( await conversationsCollection.updateOne(
{ sessionId: conversation.sessionId }, { sessionId: conversation.sessionId },
{ $set: { conversationHistory: conversation.conversationHistory } } { $set: { conversationHistory: conversation.conversationHistory } }
); );
// Send the AI's response and session ID back to the frontend
res.json({ res.json({
success: true, success: true,
data: aiResponse, data: aiResponse.choices[0].message.content,
sessionId: conversation.sessionId, // Return the session ID for subsequent queries total_tokens: aiResponse.usage.total_tokens,
sessionId: conversation.sessionId,
}); });
} catch (error) { } catch (error) {
console.error('Error generating response:', error.message); console.error('Error generating response:', error.message);
@ -89,6 +86,6 @@ async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", ma
} }
} }
module.exports = aiFollowupQuestion; module.exports = aiFollowupQuestion;