Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to LLM (get wrong answers) and function for front-end. #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion questionservice/question-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const questionSchema = new mongoose.Schema({
imageUrl: { type: String, required: true },
options: { type: [String], required: true }, // Opciones de respuesta
correctAnswer: { type: String, required: true },
createdAt: { type: Date, default: Date.now }
createdAt: { type: Date, default: Date.now },
alreadyShown: { type: Boolean, default:false }
});

const Question = mongoose.model('Question', questionSchema);
Expand Down
27 changes: 26 additions & 1 deletion questionservice/question-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const axios = require('axios');
const mongoose = require('mongoose');
const Question = require('./question-model');
const LLMService = require('../llmservice/llm-service');

// Define the connection URL
const mongoURI = 'mongodb://localhost:27017/mongo-db-wichat_en2a';
Expand Down Expand Up @@ -34,10 +35,11 @@ async function fetchFlagData(){
}
});


const results = response.data.results.bindings.map(entry => ({
type: "flag",
imageUrl: entry.flag.value,
options: [entry.countryLabel.value],
options: getIncorrectAnswers(entry.countryLabel.value), // Insert incorrect answers using LLM.
correctAnswer: entry.countryLabel.value
}));

Expand All @@ -52,6 +54,11 @@ async function fetchFlagData(){
}
}

/// Returns array including correct and incorrect answers.
function getIncorrectAnswers(correctAnswer) {
return [correctAnswer, ...LLMService.getIncorrectAnswers(correctAnswer)];
}

async function saveQuestionsToDB(questions){
try{
await Question.insertMany(questions) //insert questions to db
Expand All @@ -62,6 +69,24 @@ async function saveQuestionsToDB(questions){
}
}

// Should be called from front-end
async function getAndUpdateQuestion() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name is a bit confusing, i would just call it getQuestion(), the update is done because of how we deal with the question so users don't get repeated ones, but they don't have to know how it works. Good implementation.

try {
// Find one question where alreadyShown is false
const question = await Question.findOneAndUpdate(
{ alreadyShown: false }, // Find condition
{ alreadyShown: true }, // Update to true
{ new: true } // Return the updated document
);

return question; // Return the extracted question
} catch (error) {
console.error("Error fetching question:", error);
return null;
}
}


//Just for testing purposes
fetchFlagData();

Expand Down