gpt-3.5-turbo-16k
parent
02ec79b262
commit
7fcc27ada1
|
@ -3,7 +3,7 @@ const aiFeedbackOnReport = async (req, res) => {
|
||||||
const url = process.env.AI_API_ENDOINT;
|
const url = process.env.AI_API_ENDOINT;
|
||||||
const api_key = process.env.AI_API_KEY;
|
const api_key = process.env.AI_API_KEY;
|
||||||
const payload = {
|
const payload = {
|
||||||
"model": "gpt-4o-mini",
|
"model": "gpt-3.5-turbo-16k",
|
||||||
"messages": [{ "role": "user",
|
"messages": [{ "role": "user",
|
||||||
"content": JSON.stringify(req.body.grades) + " - first dataset(grades) " + JSON.stringify(req.body.suggested) + " - second dataset(suggested actions) " + req.body.instruction }]
|
"content": JSON.stringify(req.body.grades) + " - first dataset(grades) " + JSON.stringify(req.body.suggested) + " - second dataset(suggested actions) " + req.body.instruction }]
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ const aiFeedbackOnReport = async (req, res) => {
|
||||||
const url = process.env.AI_API_ENDOINT;
|
const url = process.env.AI_API_ENDOINT;
|
||||||
const api_key = process.env.AI_API_KEY;
|
const api_key = process.env.AI_API_KEY;
|
||||||
const payload = {
|
const payload = {
|
||||||
"model": "gpt-4o-mini",
|
"model": "gpt-3.5-turbo-16k",
|
||||||
"messages": [{ "role": "user",
|
"messages": [{ "role": "user",
|
||||||
"content": JSON.stringify(req.body.score) + " - is the students report card. " + req.body.instruction }]
|
"content": JSON.stringify(req.body.score) + " - is the students report card. " + req.body.instruction }]
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
const database = client.db(dbName); // Replace with your database name
|
const database = client.db(dbName); // Replace with your database name
|
||||||
const conversationsCollection = database.collection('conversations'); // Replace with your collection name
|
const conversationsCollection = database.collection('conversations'); // Replace with your collection name
|
||||||
|
|
||||||
async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) {
|
async function fetchOpenAICompletion(prompt, messages, model = "gpt-3.5-turbo-16k", 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: {
|
||||||
|
@ -31,7 +31,7 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { prompt, sessionId, model = "gpt-4o-mini", max_tokens = 200 } = req.body;
|
const { prompt, sessionId, model = "gpt-3.5-turbo-16k", max_tokens = 200 } = req.body;
|
||||||
if (!conversationsCollection) {
|
if (!conversationsCollection) {
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
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');
|
||||||
|
@ -10,7 +11,7 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
const database = client.db(dbName);
|
const database = client.db(dbName);
|
||||||
const conversationsCollection = database.collection('conversations');
|
const conversationsCollection = database.collection('conversations');
|
||||||
|
|
||||||
async function fetchOpenAICompletion(prompt, messages, model = "gpt-4o-mini", max_tokens = 200) {
|
async function fetchOpenAICompletion(prompt, messages, model = "gpt-3.5-turbo-16k", 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: {
|
||||||
|
@ -28,21 +29,20 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { prompt, sessionId, model = "gpt-4o-mini", max_tokens = 200 } = req.body;
|
const { prompt, sessionId, model = "gpt-3.5-turbo-16k", max_tokens = 200 } = req.body;
|
||||||
if (!conversationsCollection) {
|
if (!conversationsCollection) {
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
error: 'MongoDB is not connected yet. Please try again later.',
|
error: 'MongoDB is not connected yet. Please try again later.',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let conversation;
|
let conversation;
|
||||||
|
|
||||||
if (!sessionId) {
|
if (!sessionId) {
|
||||||
const newSessionId = uuidv4();
|
const newSessionId = uuidv4();
|
||||||
|
|
||||||
// New conversation, start with system and user message
|
|
||||||
conversation = {
|
conversation = {
|
||||||
sessionId: newSessionId,
|
sessionId: newSessionId,
|
||||||
conversationHistory: [
|
conversationHistory: [
|
||||||
|
@ -52,7 +52,6 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
};
|
};
|
||||||
await conversationsCollection.insertOne(conversation);
|
await conversationsCollection.insertOne(conversation);
|
||||||
} else {
|
} else {
|
||||||
// Existing conversation, find it by sessionId
|
|
||||||
conversation = await conversationsCollection.findOne({ sessionId: sessionId });
|
conversation = await conversationsCollection.findOne({ sessionId: sessionId });
|
||||||
if (!conversation) {
|
if (!conversation) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
|
@ -60,22 +59,13 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
error: 'Invalid session ID.',
|
error: 'Invalid session ID.',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep only the last 2 messages (1 user and 1 assistant)
|
|
||||||
conversation.conversationHistory.push({ role: 'user', content: prompt });
|
conversation.conversationHistory.push({ role: 'user', content: prompt });
|
||||||
|
|
||||||
// Limit conversation history to the last two entries: one user, one assistant
|
|
||||||
const lastTwoMessages = conversation.conversationHistory.slice(-2);
|
|
||||||
conversation.conversationHistory = lastTwoMessages;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch AI response based on the last two messages (user + assistant)
|
|
||||||
const aiResponse = await fetchOpenAICompletion(prompt, conversation.conversationHistory, model, max_tokens);
|
const aiResponse = await fetchOpenAICompletion(prompt, conversation.conversationHistory, model, max_tokens);
|
||||||
|
|
||||||
// Add the AI response to conversation history
|
|
||||||
conversation.conversationHistory.push({ role: 'assistant', content: aiResponse.choices[0].message.content });
|
conversation.conversationHistory.push({ role: 'assistant', content: aiResponse.choices[0].message.content });
|
||||||
|
|
||||||
// Update conversation in the database with new history
|
|
||||||
await conversationsCollection.updateOne(
|
await conversationsCollection.updateOne(
|
||||||
{ sessionId: conversation.sessionId },
|
{ sessionId: conversation.sessionId },
|
||||||
{ $set: { conversationHistory: conversation.conversationHistory } }
|
{ $set: { conversationHistory: conversation.conversationHistory } }
|
||||||
|
@ -94,6 +84,8 @@ const aiFollowupQuestion = async (req, res) => {
|
||||||
error: 'Something went wrong. Please try again later.',
|
error: 'Something went wrong. Please try again later.',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = aiFollowupQuestion;
|
module.exports = aiFollowupQuestion;
|
Loading…
Reference in New Issue