249 lines
7.6 KiB
JavaScript
249 lines
7.6 KiB
JavaScript
const Groq = require('groq-sdk');
|
|
const logger = require('../utils/logger');
|
|
const { groqConfig, validateConfig, getModelConfig } = require('../config/groq');
|
|
|
|
class GroqService {
|
|
constructor() {
|
|
// Validate configuration
|
|
validateConfig();
|
|
|
|
this.groq = new Groq({
|
|
apiKey: groqConfig.apiKey,
|
|
baseURL: groqConfig.baseURL
|
|
});
|
|
this.model = groqConfig.model;
|
|
this.config = groqConfig;
|
|
}
|
|
|
|
async generateResponse(messages, options = {}) {
|
|
try {
|
|
const startTime = Date.now();
|
|
|
|
const response = await this.groq.chat.completions.create({
|
|
model: this.model,
|
|
messages: messages,
|
|
temperature: options.temperature || 0.7,
|
|
max_tokens: options.maxTokens || 3000,
|
|
top_p: options.topP || 0.9,
|
|
stream: options.stream || false
|
|
});
|
|
|
|
const endTime = Date.now();
|
|
const processingTime = (endTime - startTime) / 1000;
|
|
|
|
const result = {
|
|
content: response.choices[0].message.content,
|
|
usage: response.usage,
|
|
processingTime,
|
|
model: this.model,
|
|
finishReason: response.choices[0].finish_reason
|
|
};
|
|
|
|
logger.info(`Groq API call completed in ${processingTime}s, tokens: ${result.usage.total_tokens}`);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
logger.error('Groq API error:', error);
|
|
throw new Error(`Groq API error: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
async generateEngineeringPlan(query, context = {}) {
|
|
const systemPrompt = `You are an expert engineering consultant with deep knowledge in structural engineering, mechanical engineering, electrical engineering, and civil engineering.
|
|
|
|
Your task is to analyze engineering problems and create detailed, step-by-step plans to solve them.
|
|
|
|
Guidelines:
|
|
1. Break down complex problems into clear, actionable steps
|
|
2. Consider safety, feasibility, and best practices
|
|
3. Include relevant calculations, standards, and regulations
|
|
4. Suggest appropriate tools and resources
|
|
5. Provide time estimates for each step
|
|
6. Consider potential challenges and mitigation strategies
|
|
|
|
Context: ${JSON.stringify(context)}
|
|
|
|
Create a comprehensive plan for the following engineering question:`;
|
|
|
|
const messages = [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: query }
|
|
];
|
|
|
|
return await this.generateResponse(messages, {
|
|
temperature: 0.3, // Lower temperature for more focused, technical responses
|
|
maxTokens: 3000
|
|
});
|
|
}
|
|
|
|
async executePlan(plan, tools = []) {
|
|
const systemPrompt = `You are an engineering execution specialist. You have access to various tools to help execute engineering plans.
|
|
|
|
Available tools:
|
|
${tools.map(tool => `- ${tool.name}: ${tool.description}`).join('\n')}
|
|
|
|
Your task is to execute the given plan step by step, using the appropriate tools when needed.
|
|
|
|
Guidelines:
|
|
1. Execute each step of the plan systematically
|
|
2. Use tools when necessary to gather information or perform calculations
|
|
3. Provide detailed results for each step
|
|
4. Document any issues or deviations from the plan
|
|
5. Ensure all safety and quality standards are met
|
|
|
|
Execute the following plan:`;
|
|
|
|
const messages = [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: plan }
|
|
];
|
|
|
|
return await this.generateResponse(messages, {
|
|
temperature: 0.5,
|
|
maxTokens: 4000
|
|
});
|
|
}
|
|
|
|
async expandQuery(query, context = {}) {
|
|
const systemPrompt = `You are a query expansion specialist for engineering problems. Your task is to take a user's engineering question and expand it into a more comprehensive, detailed query that will help find the most relevant information.
|
|
|
|
Guidelines:
|
|
1. Identify key engineering concepts and terminology
|
|
2. Suggest related questions and considerations
|
|
3. Include relevant standards, codes, and regulations
|
|
4. Consider different engineering disciplines that might be relevant
|
|
5. Add context about project scope, constraints, and requirements
|
|
|
|
Context: ${JSON.stringify(context)}
|
|
|
|
Expand the following engineering query:`;
|
|
|
|
const messages = [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: query }
|
|
];
|
|
|
|
return await this.generateResponse(messages, {
|
|
temperature: 0.4,
|
|
maxTokens: 1500
|
|
});
|
|
}
|
|
|
|
async generateReport(data, reportType = 'general') {
|
|
const systemPrompts = {
|
|
general: `You are an engineering report generator. Create a comprehensive, professional engineering report based on the provided data.`,
|
|
technical: `You are a technical engineering report generator. Create a detailed technical report with calculations, analysis, and recommendations.`,
|
|
summary: `You are an engineering summary generator. Create a concise executive summary of the engineering analysis and findings.`
|
|
};
|
|
|
|
const systemPrompt = systemPrompts[reportType] || systemPrompts.general;
|
|
|
|
const messages = [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: `Generate a ${reportType} report based on this data: ${JSON.stringify(data)}` }
|
|
];
|
|
|
|
return await this.generateResponse(messages, {
|
|
temperature: 0.3,
|
|
maxTokens: 2500
|
|
});
|
|
}
|
|
|
|
async searchAndAnalyze(query, searchResults = []) {
|
|
const systemPrompt = `You are an engineering analysis specialist. Analyze the provided search results and provide a comprehensive analysis of the engineering question.
|
|
|
|
Guidelines:
|
|
1. Synthesize information from multiple sources
|
|
2. Identify key findings and insights
|
|
3. Highlight important calculations, formulas, or methodologies
|
|
4. Note any conflicting information or gaps
|
|
5. Provide recommendations based on the analysis
|
|
6. Cite relevant sources and standards
|
|
|
|
Search results: ${JSON.stringify(searchResults)}
|
|
|
|
Analyze the following engineering question:`;
|
|
|
|
const messages = [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: query }
|
|
];
|
|
|
|
return await this.generateResponse(messages, {
|
|
temperature: 0.4,
|
|
maxTokens: 3000
|
|
});
|
|
}
|
|
|
|
async validatePlan(plan, feedback = []) {
|
|
const systemPrompt = `You are an engineering plan validator. Review the provided plan and feedback to determine if the plan is valid, complete, and follows engineering best practices.
|
|
|
|
Guidelines:
|
|
1. Check for completeness and logical flow
|
|
2. Verify technical accuracy
|
|
3. Ensure safety considerations are addressed
|
|
4. Validate against engineering standards
|
|
5. Consider the provided feedback
|
|
6. Suggest improvements if needed
|
|
|
|
Feedback: ${JSON.stringify(feedback)}
|
|
|
|
Validate the following plan:`;
|
|
|
|
const messages = [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: plan }
|
|
];
|
|
|
|
return await this.generateResponse(messages, {
|
|
temperature: 0.2,
|
|
maxTokens: 2000
|
|
});
|
|
}
|
|
|
|
async getModelInfo() {
|
|
try {
|
|
// Get available models
|
|
const models = await this.groq.models.list();
|
|
|
|
return {
|
|
currentModel: this.model,
|
|
availableModels: models.data,
|
|
apiStatus: 'connected'
|
|
};
|
|
} catch (error) {
|
|
logger.error('Error getting model info:', error);
|
|
return {
|
|
currentModel: this.model,
|
|
availableModels: [],
|
|
apiStatus: 'error',
|
|
error: error.message
|
|
};
|
|
}
|
|
}
|
|
|
|
async testConnection() {
|
|
try {
|
|
const testResponse = await this.generateResponse([
|
|
{ role: 'user', content: 'Hello, this is a test message.' }
|
|
], {
|
|
maxTokens: 10
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
response: testResponse,
|
|
model: this.model
|
|
};
|
|
} catch (error) {
|
|
logger.error('Groq connection test failed:', error);
|
|
return {
|
|
success: false,
|
|
error: error.message
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new GroqService();
|