Documentation Index Fetch the complete documentation index at: https://docs.vchata.com/llms.txt
Use this file to discover all available pages before exploring further.
AI Services
The AI Services provide comprehensive artificial intelligence capabilities including conversation processing, prompt template management, document enhancement, and intelligent automation for the VChata platform.
Overview
The AI Services layer includes:
🤖 AI Service - Core AI response generation and provider management
📝 Prompt Template Service - Template creation, management, and optimization
🧠 Prompt Enhancement Service - Context-aware prompt enhancement with documents
🎯 AI Agent Service - Intelligent agent configuration and management
🔄 Conversation Processing - Advanced conversation flow management
📊 Document Vector Service - Semantic search and document context
AI Service
Core AI Response Generation
The AI Service is the central component for generating intelligent responses using various LLM providers.
Provider Management
// Set the AI provider (OpenAI, Claude, etc.)
aiService . setProvider ( LLMProviderType . OPENAI );
Basic Response Generation
// Generate a simple response
const response = await aiService . generateResponse (
"You are a helpful sales assistant." ,
"Tell me about your products"
);
Response with Conversation History
// Generate response with conversation context
const response = await aiService . generateResponseWithHistory (
"You are a helpful sales assistant." ,
[
{ role: "user" , content: "Hello" },
{ role: "assistant" , content: "Hi! How can I help you?" },
{ role: "user" , content: "What products do you have?" }
]
);
Enhanced Response Generation
// Generate response with document context enhancement
const response = await aiService . generateEnhancedResponse (
basePrompt ,
userMessage ,
organizationId ,
aiAgentId ,
{
includeDocuments: true ,
maxTokens: 1000 ,
temperature: 0.7
}
);
Available Methods
Show generateResponse(prompt: string, message: string)
Description : Generates an AI response using a simple prompt and user message.Parameters :
prompt: The system prompt/instructions for the AI
message: The user’s message to respond to
Returns : Promise<string> - The AI-generated response
Show generateResponseWithHistory(prompt: string, conversationHistory: ConversationMessage[])
Description : Generates an AI response with full conversation context.Parameters :
prompt: The system prompt/instructions for the AI
conversationHistory: Array of previous messages in the conversation
Returns : Promise<string> - The AI-generated response with context
Show generateEnhancedResponse(basePrompt, userMessage, organizationId, aiAgentId?, options?)
Description : Generates an AI response with document context enhancement.Parameters :
basePrompt: Base system prompt
userMessage: User’s message
organizationId: Organization ID for context
aiAgentId: Optional AI agent ID
options: Enhancement options (documents, tokens, temperature)
Returns : Promise<string> - Enhanced AI response
Prompt Template Service
Template Management
The Prompt Template Service handles creation, management, and optimization of AI prompt templates.
Create Template
// Create a new prompt template
const template = await promptTemplateService . create ( organizationId , {
name: "Sales Assistant" ,
description: "Template for sales conversations" ,
templateContent: "You are a helpful sales assistant for {{companyName}}..." ,
inputVariables: [ "companyName" , "productName" ],
businessInfo: {
companyName: "Acme Corp" ,
industry: "Technology"
},
personalitySettings: {
tone: "professional" ,
style: "conversational"
},
isActive: true
});
Find Templates
// Find templates with filtering
const templates = await promptTemplateService . findMany ( organizationId , {
page: 1 ,
limit: 10 ,
campaignId: "campaign_123" ,
isActive: true ,
search: "sales" ,
sortBy: "createdAt" ,
sortOrder: "desc"
});
Update Template
// Update an existing template
const updatedTemplate = await promptTemplateService . update ( templateId , {
templateContent: "Updated template content..." ,
personalitySettings: {
tone: "friendly" ,
style: "casual"
}
});
Delete Template
// Delete a template
await promptTemplateService . delete ( templateId );
Template Features
Show Variable Substitution
Templates support dynamic variable substitution using {{variableName}} syntax: {
templateContent : "Hello {{customerName}}, welcome to {{companyName}}!" ,
inputVariables : [ "customerName" , "companyName" ]
}
Templates can include business-specific information: {
businessInfo : {
companyName : "Acme Corp" ,
industry : "Technology" ,
products : [ "Product A" , "Product B" ],
services : [ "Consulting" , "Support" ]
}
}
Show Personality Settings
Configure AI personality and response style: {
personalitySettings : {
tone : "professional" , // professional, friendly, casual
style : "conversational" , // conversational, formal, technical
expertise : "sales" , // sales, support, marketing
language : "en" // Language code
}
}
Templates support A/B testing variants: {
abTestVariant : {
variant : "A" ,
testId : "test_123" ,
weight : 0.5
}
}
Prompt Enhancement Service
Document Context Enhancement
The Prompt Enhancement Service enhances prompts with relevant document context and business information.
Enhance Prompt
// Enhance prompt with document context
const enhancedPrompt = await promptEnhancementService . enhancePrompt (
basePrompt ,
userMessage ,
organizationId ,
aiAgentId ,
{
includeDocuments: true ,
maxDocuments: 5 ,
similarityThreshold: 0.7 ,
includeBusinessInfo: true ,
includeProductInfo: true
}
);
Document Search
// Search for relevant documents
const documents = await promptEnhancementService . searchDocuments (
userMessage ,
organizationId ,
{
limit: 10 ,
threshold: 0.7 ,
includeContent: true
}
);
Enhancement Options
{
includeDocuments : true ,
maxDocuments : 5 ,
similarityThreshold : 0.7 ,
documentTypes : [ "knowledge_base" , "product_info" , "faq" ]
}
Show Business Information
{
includeBusinessInfo : true ,
includeProductInfo : true ,
includeContactInfo : true ,
includePricingInfo : false
}
Show Response Configuration
{
maxTokens : 1000 ,
temperature : 0.7 ,
includeExamples : true ,
includeInstructions : true
}
AI Agent Service
Agent Configuration
The AI Agent Service manages intelligent agent configurations for automated responses.
Create Agent
// Create an AI agent
const agent = await aiAgentService . create ( organizationId , {
name: "Sales Bot" ,
description: "Automated sales assistant" ,
promptTemplateId: "template_123" ,
channels: [ "DIRECT_MESSAGE" , "COMMENT" ],
autoResponse: true ,
responseDelay: 30 ,
maxResponsesPerConversation: 5 ,
businessHoursOnly: false
});
Update Agent
// Update agent configuration
const updatedAgent = await aiAgentService . update ( agentId , {
autoResponse: false ,
responseDelay: 60 ,
businessHoursOnly: true ,
businessHours: {
start: "09:00" ,
end: "17:00" ,
timezone: "America/New_York"
}
});
Process Message
// Process incoming message with agent
const response = await aiAgentService . processMessage (
agentId ,
userMessage ,
conversationHistory ,
{
includeContext: true ,
trackInteraction: true
}
);
Agent Features
Show Multi-Channel Support
Agents can respond across multiple channels:
DIRECT_MESSAGE - Private messages
COMMENT - Public comments
POST - Direct posts
{
autoResponse : true ,
responseDelay : 30 , // seconds
maxResponsesPerConversation : 5 ,
businessHoursOnly : true ,
businessHours : {
start : "09:00" ,
end : "17:00" ,
timezone : "America/New_York"
}
}
{
includeConversationHistory : true ,
includeUserProfile : true ,
includeBusinessContext : true ,
maxHistoryLength : 10
}
Conversation Processing Service
Advanced Conversation Management
The Conversation Processing Service handles complex conversation flows and context management.
Process Conversation
// Process a conversation with advanced features
const result = await conversationProcessingService . processConversation ({
organizationId ,
aiAgentId ,
userMessage ,
conversationHistory ,
context: {
userProfile: userProfile ,
businessContext: businessContext ,
sessionData: sessionData
},
options: {
includeSentiment: true ,
includeIntent: true ,
trackMetrics: true
}
});
Conversation Analytics
// Get conversation analytics
const analytics = await conversationProcessingService . getAnalytics ( organizationId , {
dateRange: {
start: "2024-01-01" ,
end: "2024-01-31"
},
metrics: [ "response_time" , "satisfaction" , "conversion_rate" ]
});
Processing Features
Analyzes user sentiment and adjusts responses accordingly: {
includeSentiment : true ,
sentimentThreshold : 0.7 ,
adjustToneBasedOnSentiment : true
}
Identifies user intent and routes to appropriate responses: {
includeIntent : true ,
intentCategories : [ "sales" , "support" , "general" ],
intentConfidenceThreshold : 0.8
}
Show Context Preservation
Maintains conversation context across sessions: {
preserveContext : true ,
contextExpiration : 3600 , // seconds
maxContextLength : 1000
}
Document Vector Service
Semantic Search and Context
The Document Vector Service provides semantic search capabilities for document context.
Search Documents
// Search for relevant documents
const results = await documentVectorService . search (
query ,
organizationId ,
{
limit: 10 ,
threshold: 0.7 ,
includeContent: true ,
documentTypes: [ "knowledge_base" , "product_info" ]
}
);
Add Document
// Add document to vector store
const document = await documentVectorService . addDocument ({
organizationId ,
title: "Product Guide" ,
content: "Complete product information..." ,
metadata: {
type: "product_info" ,
category: "electronics" ,
tags: [ "guide" , "product" ]
}
});
Update Document
// Update existing document
const updatedDocument = await documentVectorService . updateDocument ( documentId , {
content: "Updated product information..." ,
metadata: {
updated: true ,
version: "2.0"
}
});
Vector Features
Advanced semantic search using vector embeddings:
Similarity-based matching
Context-aware results
Multi-language support
{
autoIndexing : true ,
chunkSize : 1000 ,
overlap : 200 ,
supportedFormats : [ "text" , "markdown" , "pdf" ]
}
{
metadata : {
type : "knowledge_base" ,
category : "support" ,
tags : [ "faq" , "troubleshooting" ],
language : "en"
}
}
Integration Examples
Complete AI Response Flow
// Complete flow for generating AI responses
async function generateIntelligentResponse (
organizationId : string ,
aiAgentId : string ,
userMessage : string ,
conversationHistory : ConversationMessage []
) {
// 1. Get agent configuration
const agent = await aiAgentService . getAgent ( aiAgentId );
// 2. Get prompt template
const template = await promptTemplateService . getTemplate ( agent . promptTemplateId );
// 3. Enhance prompt with context
const enhancedPrompt = await promptEnhancementService . enhancePrompt (
template . templateContent ,
userMessage ,
organizationId ,
aiAgentId ,
{
includeDocuments: true ,
includeBusinessInfo: true ,
maxDocuments: 5
}
);
// 4. Generate response
const response = await aiService . generateEnhancedResponseWithHistory (
enhancedPrompt . enhancedPrompt ,
userMessage ,
organizationId ,
aiAgentId ,
conversationHistory ,
{
maxTokens: 1000 ,
temperature: 0.7
}
);
// 5. Track interaction
await conversationProcessingService . trackInteraction ({
agentId: aiAgentId ,
userMessage ,
aiResponse: response ,
metrics: {
responseTime: Date . now () - startTime ,
tokensUsed: response . length ,
contextDocuments: enhancedPrompt . documents . length
}
});
return response ;
}
Template Management Workflow
// Template creation and testing workflow
async function createAndTestTemplate ( organizationId : string ) {
// 1. Create template
const template = await promptTemplateService . create ( organizationId , {
name: "Customer Support" ,
templateContent: "You are a helpful customer support agent..." ,
inputVariables: [ "customerName" , "issueType" ],
businessInfo: {
companyName: "Acme Corp" ,
supportHours: "24/7"
}
});
// 2. Test template
const testResponse = await aiService . testPrompt (
template . templateContent ,
"I need help with my order"
);
// 3. Validate response quality
const quality = await promptTemplateService . validateResponse (
template . id ,
testResponse ,
{
checkTone: true ,
checkCompleteness: true ,
checkRelevance: true
}
);
// 4. Activate if quality is good
if ( quality . score > 0.8 ) {
await promptTemplateService . update ( template . id , { isActive: true });
}
return { template , testResponse , quality };
}
Caching Strategy
// Implement caching for frequently used prompts
const cachedPrompt = await promptCache . get ( promptId );
if ( ! cachedPrompt ) {
const prompt = await promptTemplateService . getTemplate ( promptId );
await promptCache . set ( promptId , prompt , 3600 ); // 1 hour cache
}
Batch Processing
// Batch process multiple messages
const responses = await Promise . all (
messages . map ( message =>
aiService . generateEnhancedResponse (
basePrompt ,
message . content ,
organizationId ,
aiAgentId
)
)
);
Rate Limiting
// Implement rate limiting for AI requests
const rateLimiter = new RateLimiter ({
windowMs: 60000 , // 1 minute
max: 100 // 100 requests per minute
});
Error Handling
Common Error Scenarios
try {
const response = await aiService . generateResponse ( prompt , message );
} catch ( error ) {
if ( error . code === 'PROVIDER_UNAVAILABLE' ) {
// Fallback to backup provider
await aiService . setProvider ( LLMProviderType . BACKUP );
const response = await aiService . generateResponse ( prompt , message );
}
}
try {
const template = await promptTemplateService . create ( organizationId , data );
} catch ( error ) {
if ( error . code === 'TEMPLATE_CONFLICT' ) {
throw new BadRequestException ( 'Template name already exists' );
}
}
try {
const enhanced = await promptEnhancementService . enhancePrompt ( prompt , message , orgId );
} catch ( error ) {
if ( error . code === 'NO_DOCUMENTS_FOUND' ) {
// Fallback to basic prompt
return { enhancedPrompt: prompt , documents: [] };
}
}
Monitoring and Analytics
// Track AI service performance
const metrics = {
responseTime: Date . now () - startTime ,
tokensUsed: response . length ,
providerLatency: providerResponseTime ,
cacheHitRate: cacheHits / totalRequests ,
errorRate: errors / totalRequests
};
await analyticsService . track ( 'ai_response_generated' , metrics );
Quality Monitoring
// Monitor response quality
const quality = await qualityService . assessResponse ( response , {
checkGrammar: true ,
checkTone: true ,
checkCompleteness: true ,
checkRelevance: true
});
if ( quality . score < 0.7 ) {
await alertService . sendQualityAlert ( agentId , quality );
}