Skip to main content

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

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

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
  }
);
// Search for relevant documents
const documents = await promptEnhancementService.searchDocuments(
  userMessage,
  organizationId,
  {
    limit: 10,
    threshold: 0.7,
    includeContent: true
  }
);

Enhancement Options

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

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

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

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 };
}

Performance Optimization

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

Monitoring and Analytics

Performance Metrics

// 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);
}