UNPKG

@gsb-core/ai-assistant

Version:
116 lines (115 loc) 4.16 kB
/** * GSB AI Chat Service * * Provides functionality for interacting with AI chat models through the GSB backend. * This service integrates with the 'aiChat' serverless function to process AI chat requests. * * Key features: * - Template-based prompt generation using nunjucks (handled by backend) * - Multi-provider support (OpenAI, Azure, Anthropic, HuggingFace) * - Chat history management * - Context-aware conversations */ import { QueryParams } from '@gsb-core/core'; import { GsbAiChat, GsbAiMessage } from '../interfaces/entity-interfaces'; export interface ChatOptions { prompt: string; llmConfId: string; chatId?: string; data?: any; options?: any; } /** * AI Chat Service * * This service acts as a client for the GSB backend AI chat functionality. * It communicates with the 'aiChat' serverless function to process templates, * manage chat history, and generate AI responses. */ export declare class GsbAiChatService { /** * Singleton instance */ private static instance; /** * Entity service for data operations */ private entityService; /** * Runtime service for serverless function execution */ private runtime; /** * Get the singleton instance of the AI Chat Service * * @param token Authentication token for API calls * @returns The singleton instance */ static getInstance(useCache?: boolean): GsbAiChatService; /** * Constructor * * @param token Authentication token for API calls */ constructor(useCache?: boolean); /** * Send a message to the AI chat and get a response * * This method calls the 'aiChat' serverless function which: * 1. Gets or creates a chat session * 2. Processes the template using nunjucks (if configured) * 3. Stores the user message * 4. Generates an AI response using the configured LLM provider * 5. Stores the AI response in the chat history * 6. Returns the AI response * @param prompt User's message/prompt * @param llmConfId LLM configuration ID (from a saved LlmConfiguration entity) * @param chatId Existing chat ID (if continuing a conversation) or undefined for a new chat * @param data Context data containing the entity,entityDefinition,entity_id and other information for template processing, matches GsbWorkflowInstance * @param options Additional options like testMode, system prompts, etc. matches GsbWorkflowInstance.prms * @returns Response object containing the AI message and updated chat * * @example * ```typescript * // Start a new chat * const result = await aiChatService.chat( * "What's the status of our project?", * "llm-config-id", // ID of saved LlmConfiguration * undefined, // New chat * { entity: projectEntity, entityDefinition: projectEntityDefinition, entity_id: projectEntityId } * ); * * // Continue the conversation * const followUp = await aiChatService.chat( * "What should be our next steps?", * "llm-config-id", * result.chat.id, // Use existing chat ID * { entity: projectEntity, entityDefinition: projectEntityDefinition, entity_id: projectEntityId } * ); * ``` */ chat({ prompt, llmConfId, chatId, data, options }: ChatOptions, token?: string, tenantCode?: string): Promise<{ message: string; chat: GsbAiChat; }>; /** * Get a chat by ID * * @param chatId The chat ID * @returns The chat entity */ getChat(chatId: string, token?: string, tenantCode?: string): Promise<GsbAiChat>; /** * Get all chats * * @returns Array of chat entities */ getChats(queryParams?: QueryParams<GsbAiChat>, token?: string, tenantCode?: string): Promise<GsbAiChat[]>; /** * Get messages for a specific chat * * @param chatId The chat ID * @returns Array of message entities */ getChatMessages(chatId: string, queryParams?: QueryParams<GsbAiMessage>, token?: string, tenantCode?: string): Promise<GsbAiMessage[]>; }