skailan-ai
Version:
Servicio de IA y procesamiento de lenguaje natural para Skailan
80 lines • 3.81 kB
JavaScript
import { IntentClassificationService } from '../../domain/services/IntentClassificationService';
import { LLMConfigPrismaRepository } from '../../infra/database/prisma/LLMConfigPrismaRepository';
export const classifyIntent = async (req, res) => {
try {
const { text, llmConfigName, intents } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
if (!text) {
return res.status(400).json({ error: 'Text is required.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const llmConfigRepository = new LLMConfigPrismaRepository(prisma);
const llmConfig = await llmConfigRepository.findByName(llmConfigName || 'default', organizationId);
if (!llmConfig || !llmConfig.isActive) {
return res.status(400).json({ error: 'LLM configuration not found or inactive.' });
}
const intentService = new IntentClassificationService(llmConfig, intents);
const result = await intentService.classifyIntent(text);
res.status(200).json(result);
}
catch (error) {
res.status(500).json({ error: error.message || 'Error classifying intent.' });
}
};
export const classifyConversationIntent = async (req, res) => {
try {
const { messages, llmConfigName, intents } = req.body;
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
if (!messages || !Array.isArray(messages)) {
return res.status(400).json({ error: 'Messages array is required.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const llmConfigRepository = new LLMConfigPrismaRepository(prisma);
const llmConfig = await llmConfigRepository.findByName(llmConfigName || 'default', organizationId);
if (!llmConfig || !llmConfig.isActive) {
return res.status(400).json({ error: 'LLM configuration not found or inactive.' });
}
const intentService = new IntentClassificationService(llmConfig, intents);
const result = await intentService.classifyConversationIntent(messages);
res.status(200).json(result);
}
catch (error) {
res.status(500).json({ error: error.message || 'Error classifying conversation intent.' });
}
};
export const getIntents = async (req, res) => {
try {
const organizationId = req.organization?.id;
if (!organizationId) {
return res.status(400).json({ error: 'Organization ID not found in request.' });
}
const prisma = req.tenantPrisma;
if (!prisma) {
return res.status(500).json({ error: 'Prisma client not initialized for tenant.' });
}
const llmConfigRepository = new LLMConfigPrismaRepository(prisma);
const llmConfig = await llmConfigRepository.findByName('default', organizationId);
if (!llmConfig || !llmConfig.isActive) {
return res.status(400).json({ error: 'LLM configuration not found or inactive.' });
}
const intentService = new IntentClassificationService(llmConfig);
const intents = intentService.getIntents();
res.status(200).json(intents);
}
catch (error) {
res.status(500).json({ error: error.message || 'Error getting intents.' });
}
};
//# sourceMappingURL=intentController.js.map