Skip to content

Chat Service

The ChatService is the core component of the Chat module, responsible for processing user messages, retrieving context, generating AI responses, and handling errors.

Overview

The ChatService provides a unified interface for processing chat messages with context awareness. It integrates multiple services including conversation management, vector storage for semantic search, and LangChain for LLM interactions to deliver contextually relevant responses.

Dependencies

The ChatService depends on:

  • ConversationService: For managing conversation history
  • PineconeService: For vector database operations
  • LangchainService: For LLM integrations
  • ConfigService: For environment configuration
  • ChatContextService: For document context processing

Key Methods

processMessage

async processMessage(createChatDto: CreateChatDto): Promise<ChatResponse>

Processes a user message and generates an AI response.

Parameters: - createChatDto: Contains user ID, query, optional conversation UUID, and optional documents for context

Returns: - ChatResponse: Contains the AI-generated response, timestamp, and metadata

Functionality: 1. Processes document context if provided 2. Retrieves conversation history 3. Generates a response using the LLM with context from documents and conversation history 4. Handles errors with appropriate fallback responses

Example:

const response = await chatService.processMessage({
  userID: 'user123',
  query: 'How do I create a new project?',
  conversationUUID: 'conv-456',
  documents: [
    {
      name: 'user_manual.pdf',
      type: 'application/pdf',
      content: 'base64EncodedContent...'
    }
  ]
});

generateResponseWithRetry

private async generateResponseWithRetry(
  query: string,
  userID: string,
  conversationUUID: string,
  chatHistory: any[],
  existingMetadata: any,
  retryCount = 0
): Promise<ChatResponse>

Internal method that handles generating responses with automatic retry logic for transient errors.

Parameters: - query: User's message - userID: User identifier - conversationUUID: Conversation identifier - chatHistory: Previous conversation messages - existingMetadata: Existing metadata to include in response - retryCount: Current retry attempt (defaults to 0)

Returns: - ChatResponse: Contains the AI response, timestamp, and metadata

Functionality: 1. Retrieves the vector store for the user 2. Gets the configured LLM 3. Generates an answer using LangChain 4. Updates conversation history 5. Handles errors with exponential backoff retry logic

Error Handling

The service implements comprehensive error handling:

  • Rate Limiting: Automatically retries with exponential backoff
  • Authentication Errors: Returns appropriate error message
  • Timeouts: Retries or returns a timeout error message
  • General Errors: Returns a generic error message

Each error type has a specific error code in the response: - RATE_LIMIT_EXCEEDED - AUTHENTICATION_ERROR - TIMEOUT - INTERNAL_ERROR - DOCUMENT_PROCESSING_ERROR

Configuration

The service can be configured through environment variables:

  • Retry mechanism: Configurable max retries and delay
  • Logging: Configurable logging levels

Integration Example

// In a controller or another service
@Injectable()
export class SomeService {
  constructor(private readonly chatService: ChatService) {}

  async handleUserQuery(userID: string, query: string) {
    return this.chatService.processMessage({
      userID,
      query,
      // Optional fields
      conversationUUID: 'some-conversation-id',
      documents: [],
    });
  }
}