const express = require('express'); const http = require('http'); const { Server } = require('socket.io'); const { default: Groq } = require('groq-sdk'); require('dotenv').config() const app = express(); const server = http.createServer(app); const io = new Server(server, { cors: { origin: '*', methods: ['GET', 'POST'], }, }); const groq = new Groq({ apiKey: process.env.GROK_KI_API_KEY }); // Replace with your actual API key async function makeApiCall(messages, maxTokens, isFinalAnswer = false) { for (let attempt = 0; attempt < 3; attempt++) { try { const response = await groq.chat.completions.create({ model: "llama-3.1-70b-versatile", // Use appropriate model if different messages: messages, max_tokens: maxTokens, temperature: isFinalAnswer ? 0.2 : 0.7, // Adjust temperature as needed response_format: isFinalAnswer ? undefined : { type: "json_object" } }); if (isFinalAnswer) { return response.choices[0].message.content; } else { return JSON.parse(response.choices[0].message.content); } } catch (e) { if (attempt === 2) { return isFinalAnswer ? { title: "Error", content: `Failed to generate final answer after 3 attempts. Error: ${e}` } : { title: "Error", content: `Failed to generate step after 3 attempts. Error: ${e}`, next_action: "final_answer" }; } await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second before retrying } } } async function generateResponse(prompt, socket) { const systemMessage = { role: "system", content: `You are an expert AI assistant that explains your reasoning step by step. For each step, provide a title that describes what you're doing in that step, along with the content. Decide if you need another step or if you're ready to give the final answer. Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. USE AS MANY REASONING STEPS AS POSSIBLE. AT LEAST 3. BE AWARE OF YOUR LIMITATIONS AS AN LLM AND WHAT YOU CAN AND CANNOT DO. IN YOUR REASONING, INCLUDE EXPLORATION OF ALTERNATIVE ANSWERS. CONSIDER YOU MAY BE WRONG, AND IF YOU ARE WRONG IN YOUR REASONING, WHERE IT WOULD BE. FULLY TEST ALL OTHER POSSIBILITIES. YOU CAN BE WRONG. WHEN YOU SAY YOU ARE RE-EXAMINING, ACTUALLY RE-EXAMINE, AND USE ANOTHER APPROACH TO DO SO. DO NOT JUST SAY YOU ARE RE-EXAMINING. USE AT LEAST 3 METHODS TO DERIVE THE ANSWER. USE BEST PRACTICES.` }; let messages = [systemMessage, { role: "user", content: prompt }]; let steps = []; let totalThinkingTime = 0; let stepCount = 1; while (true) { const startTime = performance.now(); const stepData = await makeApiCall(messages, 300); const endTime = performance.now(); const thinkingTime = (endTime - startTime) / 1000; totalThinkingTime += thinkingTime; steps.push({ title: `Step ${stepCount}: ${stepData.title}`, content: stepData.content, thinkingTime }); messages.push({ role: "assistant", content: JSON.stringify(stepData) }); socket.emit('thoughtStep', steps[steps.length - 1]); // Emit individual steps if (stepData.next_action === 'final_answer' || stepCount > 25) break; stepCount++; } messages.push({ role: "user", content: "Please provide the final answer based solely on your reasoning above. Do not use JSON formatting. Only provide the text response without any titles or preambles. Retain any formatting as instructed by the original prompt, such as exact formatting for free response or multiple choice." }); const startTime = performance.now(); const finalAnswer = await makeApiCall(messages, 1200, true); const endTime = performance.now(); totalThinkingTime += (endTime - startTime) / 1000; socket.emit('finalAnswer', { content: finalAnswer, thinkingTime: (endTime - startTime) / 1000, totalThinkingTime }); } io.on('connection', (socket) => { console.log('A user connected'); socket.on('startChainOfThought', (inputQuestion) => { generateResponse(inputQuestion, socket); }); socket.on('disconnect', () => { console.log('User disconnected'); }); }); const PORT = process.env.PORT || 3001; server.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });