Gauntlet-Incept MCP

/** * LLM Service * * This service provides an API for interacting with Large Language Models (LLMs) * for content generation, tagging, and grading. */ const express = require('express'); const cors = require('cors'); const dotenv = require('dotenv'); const { OpenAI } = require('openai'); // Load environment variables dotenv.config(); // Initialize Express app const app = express(); const port = process.env.PORT || 5000; // Initialize OpenAI client const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); // Middleware app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); // Routes app.get('/', (req, res) => { res.json({ message: 'LLM Service API', version: '0.1.0' }); }); // LLM Routes app.post('/api/llm/generate', async (req, res) => { try { const { prompt, model = 'gpt-4', temperature = 0.7, max_tokens = 1000 } = req.body; if (!prompt) { return res.status(400).json({ success: false, error: 'Prompt is required' }); } // Call OpenAI API const response = await openai.chat.completions.create({ model: model, messages: [{ role: 'user', content: prompt }], temperature: temperature, max_tokens: max_tokens }); res.status(200).json({ success: true, data: { text: response.choices[0].message.content, usage: response.usage } }); } catch (error) { console.error('Error generating content:', error); res.status(500).json({ success: false, error: error.message }); } }); app.post('/api/llm/tag', async (req, res) => { try { const { content, contentType } = req.body; if (!content) { return res.status(400).json({ success: false, error: 'Content is required' }); } if (!contentType || !['question', 'article'].includes(contentType)) { return res.status(400).json({ success: false, error: 'Valid content type (question or article) is required' }); } // Create tagging prompt const prompt = createTaggingPrompt(content, contentType); // Call OpenAI API const response = await openai.chat.completions.create({ model: 'gpt-4', messages: [{ role: 'user', content: prompt }], temperature: 0.3 }); // Parse the response as JSON let tags; try { const responseText = response.choices[0].message.content; tags = JSON.parse(responseText); } catch (parseError) { return res.status(500).json({ success: false, error: 'Failed to parse LLM response as JSON' }); } res.status(200).json({ success: true, data: tags }); } catch (error) { console.error('Error tagging content:', error); res.status(500).json({ success: false, error: error.message }); } }); app.post('/api/llm/grade', async (req, res) => { try { const { content, tags, contentType } = req.body; if (!content) { return res.status(400).json({ success: false, error: 'Content is required' }); } if (!tags) { return res.status(400).json({ success: false, error: 'Tags are required' }); } if (!contentType || !['question', 'article'].includes(contentType)) { return res.status(400).json({ success: false, error: 'Valid content type (question or article) is required' }); } // Create grading prompt const prompt = createGradingPrompt(content, tags, contentType); // Call OpenAI API const response = await openai.chat.completions.create({ model: 'gpt-4', messages: [{ role: 'user', content: prompt }], temperature: 0.3 }); // Parse the response as JSON let gradeResult; try { const responseText = response.choices[0].message.content; gradeResult = JSON.parse(responseText); } catch (parseError) { return res.status(500).json({ success: false, error: 'Failed to parse LLM response as JSON' }); } res.status(200).json({ success: true, data: gradeResult }); } catch (error) { console.error('Error grading content:', error); res.status(500).json({ success: false, error: error.message }); } }); // Helper functions function createTaggingPrompt(content, contentType) { return ` You are an expert educational content tagger. Your task is to analyze the following ${contentType} and identify its subject, grade level, educational standard, and lesson. ${contentType.charAt(0).toUpperCase() + contentType.slice(1)}: ${content} Please provide the following information: - Subject (math, language, science, social studies) - Grade level (K-8) - Standard (CCSS for math and language, NGSS for science) - Lesson ${contentType === 'question' ? '- Difficulty level (1-3, where 1 is easy, 2 is medium, 3 is hard)' : ''} Format your response as a JSON object. `; } function createGradingPrompt(content, tags, contentType) { let qualityCriteria = ''; if (contentType === 'question') { qualityCriteria = ` 1. Consistent with the preceding teaching article 2. Appropriate categorization (subject, grade, standard, lesson, difficulty) 3. All parts present (prompt, interaction type, choices, correct answer, wrong answer explanations, solution) 4. Designated correct answer is accurate 5. None of the distractors can be considered correct 6. At least 2 distractors for multiple choice must be plausible 7. The right answer can't stand out so that students can guess without knowing the concept 8. Clear explanations for each wrong answer 9. Clear solution for how to get the correct answer 10. Grade level appropriate language 11. Consistent wording that is clear, direct, and unambiguous 12. Grammatically correct 13. Properly formatted `; } else { qualityCriteria = ` 1. Appropriate categorization (subject, grade, standard, lesson) 2. Explicitly teaches, with worked examples, the concepts and procedures 3. Worked examples break down steps for students with lower working memory capacity 4. Factually accurate 5. Grade level appropriate language 6. Clear and unambiguous wording 7. Properly formatted 8. Consistent explanations throughout `; } return ` You are an expert educational content evaluator. Your task is to grade the following ${contentType} against quality standards for educational content. ${contentType.charAt(0).toUpperCase() + contentType.slice(1)}: ${content} Tags: ${JSON.stringify(tags, null, 2)} Quality Criteria: ${qualityCriteria} Please evaluate the ${contentType} against each quality criterion and provide: 1. A pass/fail determination for each criterion 2. An overall pass/fail determination 3. Detailed feedback explaining any failures and how to improve Format your response as a JSON object with 'pass' (boolean), 'scorecard' (object with criterion names as keys and boolean values), and 'feedback' (string) properties. `; } // Start the server app.listen(port, () => { console.log(`LLM Service running on port ${port}`); });