bb-inspired
Version:
Core library for BB-inspired NestJS backend
110 lines (103 loc) • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextAnalysisUtils = void 0;
class TextAnalysisUtils {
static async classifyText(aiService, text, categories, provider) {
const categoriesString = categories.join(', ');
const prompt = `
Classify the following text into exactly one of these categories: ${categoriesString}.
Only respond with the category name and a confidence score between 0 and 1, separated by a comma.
For example: "category_name,0.95"
Text to classify:
"${text}"
`;
const result = await aiService.createCompletion(prompt, {
temperature: 0,
maxTokens: 20,
}, provider);
const [category, confidenceStr] = result.text.trim().split(',');
const confidence = parseFloat(confidenceStr) || 0.5;
return {
category: category.trim(),
confidence,
};
}
static async analyzeSentiment(aiService, text, provider) {
const prompt = `
Analyze the sentiment of the following text.
Respond with the sentiment (positive, negative, or neutral) and a score between -1 and 1 where:
- Negative sentiment: -1 to -0.1
- Neutral sentiment: -0.1 to 0.1
- Positive sentiment: 0.1 to 1
Format your response as "sentiment,score" (e.g., "positive,0.75")
Text to analyze:
"${text}"
`;
const result = await aiService.createCompletion(prompt, {
temperature: 0,
maxTokens: 20,
}, provider);
const [sentimentResult, scoreStr] = result.text.trim().split(',');
const score = parseFloat(scoreStr) || 0;
const sentiment = sentimentResult.trim().toLowerCase();
return {
sentiment,
score,
};
}
static async extractEntities(aiService, text, entityTypes, provider) {
const entityTypesString = entityTypes ? entityTypes.join(', ') : 'person, organization, location, date, time, money, percent, product';
const prompt = `
Extract named entities from the following text.
Entity types to extract: ${entityTypesString}
For each entity, provide:
- The entity text
- The entity type
- The start and end position in the text (0-indexed)
Format your response as valid JSON array. Example:
[
{"text": "John Smith", "type": "person", "start": 10, "end": 20},
{"text": "Google", "type": "organization", "start": 30, "end": 36}
]
Text to analyze:
"${text}"
`;
const result = await aiService.createCompletion(prompt, {
temperature: 0,
maxTokens: 500,
}, provider);
try {
const jsonMatch = result.text.match(/\[.*\]/s);
if (!jsonMatch) {
return { entities: [] };
}
const entities = JSON.parse(jsonMatch[0]);
return { entities };
}
catch (error) {
console.error('Error parsing entity extraction result:', error);
return { entities: [] };
}
}
static async summarizeText(aiService, text, maxLength, provider) {
const lengthInstruction = maxLength ? `The summary should be no more than ${maxLength} characters.` : 'Keep the summary concise.';
const prompt = `
Summarize the following text:
"${text}"
${lengthInstruction}
Focus on the key points and main ideas.
`;
const result = await aiService.createCompletion(prompt, {
temperature: 0.3,
maxTokens: maxLength ? Math.ceil(maxLength / 4) : 200,
}, provider);
const summary = result.text.trim();
const compressionRatio = summary.length / text.length;
return {
summary,
compressionRatio,
};
}
}
exports.TextAnalysisUtils = TextAnalysisUtils;
//# sourceMappingURL=text.utils.js.map