total_tokens
parent
5775c45d8c
commit
2cbf1ebbaa
|
@ -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",
|
||||||
|
|
|
@ -8,8 +8,8 @@ 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', {
|
||||||
|
@ -26,7 +26,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue