UNPKG

@websolutespa/payload-plugin-bowl-llm

Version:

LLM plugin for Bowl PayloadCms plugin

575 lines (560 loc) 16.5 kB
import { PayloadHandler } from 'payload/config'; import { TypeWithID, PayloadRequest } from 'payload/types'; import { IMedia, IEquatable } from '@websolutespa/bom-core'; import { BowlBlock, BowlPlugin } from '@websolutespa/payload-plugin-bowl'; import { Response } from 'express'; import { Payload } from 'payload'; import * as payload_dist_collections_config_types from 'payload/dist/collections/config/types'; import { Resource } from 'i18next'; type LlmChunkLog = Record<string, unknown> & { type: 'log'; }; type LlmChunkError = { type: 'error'; error: string; }; type LlmChunkHeader = { type: 'header'; threadId: string; messageId: string; }; type LlmChunkInfo = { type: 'info'; threadId: string; }; type LlmChunkEnd = { type: 'end'; }; type LlmChunkText = { type: 'text'; text: string; }; type LlmChunkString = { type: 'string'; content: string; }; type LlmChunkTextOrString = LlmChunkText | LlmChunkString; declare function isChunkText(chunk: LlmChunkItem): chunk is LlmChunkText; declare function isChunkString(chunk: LlmChunkItem): chunk is LlmChunkString; declare function isChunkTextOrString(chunk: LlmChunkItem): chunk is LlmChunkTextOrString; type ImageUrl = { url: string; }; type LlmChunkImageUrl = { type: 'image_url'; image_url: ImageUrl; }; type LlmChunkImage = { type: 'image'; id: string; src: string; }; type LlmChunkKnownItem = LlmChunkEnd | LlmChunkError | LlmChunkImage | LlmChunkImageUrl | LlmChunkHeader | LlmChunkInfo | LlmChunkLog | LlmChunkText | LlmChunkString; type LlmChunkUnknownItemType = Exclude<string, LlmChunkKnownItem['type']>; type LlmChunkUnknownItem = Omit<Record<string, unknown>, 'type'>; type LlmChunkItem = LlmChunkKnownItem; type LlmChunk = string | LlmChunkItem; type LlmMessage = { messageId?: string; role: string; content: string | LlmChunkItem[]; feedback?: { rating?: number; notes?: string; }; }; type LlmSerializedMessage = { messageId?: string; role: string; content: string; }; type LlmDecodedMessage = LlmMessage & { chunks: LlmChunkItem[]; }; type StreamResponse = { chunks: LlmChunkItem[]; threadId: string; messageId: string; }; type LlmUpload = { file: File; base64: string; }; type LlmSummary = { fullName: string; email: string; privacy: boolean; appId: string; threadId: string; summaryUrl: string; html: string; }; type LlmAppReferrer = { id: string; referrer: string; }; type LlmMapReplacedField = { srcName: string; destName: string; }; type LlmMapNewField = { name: string; value: string; }; type LlmMapDeletedField = { name: string; }; type LlmMapMetaField = { name: string; description: string; type: string; }; type LlmFieldsMapping = { replacedFields: LlmMapReplacedField[]; newFields: LlmMapNewField[]; deletedFields: LlmMapDeletedField[]; metaFields: LlmMapMetaField[]; }; type LlmKnowledgeBaseEndpoint = { endpointUrl: string; authentication: string; authSecret: string; fieldsMapping: LlmFieldsMapping; id: string; }; type VectorDb = Record<string, unknown> & { id: string; filename: string; mimeType: string; filesize: number; createdAt: Date | string; updatedAt?: Date | string; url: string; }; type LlmKnowledgeBaseFile = { media: IMedia; }; type LlmKnowledgeBase = { integrations: BowlBlock[]; externalEndpoints: LlmKnowledgeBaseEndpoint[]; files?: LlmKnowledgeBaseFile[]; vectorDbType?: string; vectorDbFile: VectorDb; }; type LlmSearchSettings = { searchType: string; scoreThreshold?: number; searchK?: number; metadataFilters?: string[]; }; type LlmChainSettings = { model: string; prompt: string; temperature: number; }; type LlmDbSettings = { connectionString: string; }; type SharepointSettings = { clientId: string; clientSecret: string; tenantId: string; siteId: string; driveId: string; }; type LlmAppTool = { id: string; isActive: boolean; nightlyKbGeneration: boolean; name: string; description?: string; type: string; functionId: string; functionName: string; functionDescription: string; secrets: LlmAppToolSecret[]; llmChainSettings: LlmChainSettings; dataSource: string; dbSettings: LlmDbSettings; waitingMessage: string; knowledgeBase: LlmKnowledgeBase; searchSettings: LlmSearchSettings; }; type LlmRules = { vector_type: string; vectorDbFile: VectorDb; threshold: number; }; type LlmFineTuning = { modelNameSuffix: string; fineTunedModelName: string; currentJobId?: string; }; type LlmAppSettings = { credentials: LlmAppCredentials; knowledgeBase: LlmKnowledgeBase; llmConfig: LlmAppConfig; fineTuning: LlmFineTuning; appTools: LlmAppTool[]; rules: LlmRules; nightlyKbGeneration: boolean; }; type LlmAppCredentials = { apiKey: string; appKey: string; httpReferrers: LlmAppReferrer[]; previewEnabled?: boolean; }; type LlmAppPrompt = { systemMessage: string; }; type LlmTool = { name: string; description?: string; functionName: string; functionDescription: string; waitingMessage: string; }; type LlmAppSecrets = Record<string, string>; type LlmAppToolSecret = Record<string, string>; type LlmAppConfig = { provider: string; model: string; temperature: number; prompt: { systemMessage: string; prompt: LlmAppPrompt; }; secrets: LlmAppSecrets; tools: LlmTool[]; langChainTracing: boolean; langChainProject: string; }; type LlmAppCard = { icon?: IMedia; title: string; body: string; message: string; }; type LlmLogo = { logo: IMedia; }; type LlmSampleInputText = { id: string; sampleInputText: string; }; type LlmHeaderCta = { id: string; title: string; href: string; target: string; icon?: IMedia; }; type LlmRecognitionMode = ('none' | 'default') & string; type LlmSynthesisMode = ('none' | 'default' | 'elevenlabs') & string; type LlmAppContents = { additionalLogos?: LlmLogo[]; collapsedWelcomeText?: string; collapsedWelcomeTextCta?: string; collapsedWelcomeTextHover?: string; customIntro?: string; customTheme?: {}; disableSpeechRecognition?: boolean; disableSpeechSynthesis?: boolean; disclaimer?: string; disclaimerShort?: string; disclaimerFooter?: boolean; enableUpload?: boolean; extendedWelcomeText?: string; intro?: string; layoutBuilder?: LlmAppCard[]; logo?: IMedia; logoMini?: IMedia; openingSound?: IMedia; promptPlaceholder?: string; recognitionMode?: LlmRecognitionMode; sampleInputTexts?: LlmSampleInputText[]; headerCtas?: LlmHeaderCta[]; shortWelcomeText?: string; synthesisMode?: LlmSynthesisMode; textToSpeechApiKey?: string; textToSpeechVoiceId?: string; enableFeedback?: boolean; }; type LlmWebhook = { webhook: string; }; type LlmThread = { message: LlmMessage[]; } & TypeWithID; type LlmInfoThread = { messages: LlmMessage[]; threadId: string; }; type LlmInfo = { contents: LlmAppContents; thread?: LlmInfoThread; }; type LlmAppLink = { title: string; url: string; }; type LlmAppLinksGroup = { title: string; links: LlmAppLink[]; }; type LlmMode = ('site' | 'chat' | 'url2text' | 'translation') & string; type LlmApp = { createdAt: Date | string; settings: LlmAppSettings; id: string; isActive: boolean; name: string; mode: LlmMode; updatedAt: Date | string; contents: LlmAppContents; webhooks: LlmWebhook[]; linksGroups: LlmAppLinksGroup[]; }; type RobotTaskResult = { success: boolean; file?: string; error?: string; }; type RobotTaskMetadataExtra = { traceId: string; appId: string; toolId?: string; [key: string]: string | undefined; }; type RobotTaskCompleted = { status: 'completed'; result: RobotTaskResult; }; type RobotTaskFailed = { status: 'failed'; error: string; }; type RobotTaskResponse = { id: string; type: string; metadata: { extra?: RobotTaskMetadataExtra; [key: string]: string | object | undefined; }; } & (RobotTaskCompleted | RobotTaskFailed); type RobotTaskRequest = { provider: string; model: string; taskId: string; files?: string[]; dataSource?: string; vectorDbType?: string; integrations?: BowlBlock[]; endpoints?: LlmKnowledgeBaseEndpoint[]; secrets: LlmAppSecrets; downloadPath: string; metadata: { traceId: string; appId: string; toolId?: string; [key: string]: string | undefined; }; }; type LlmTaskLogData = { taskStatus?: string; taskRequest?: RobotTaskRequest; taskResponse?: RobotTaskResponse; savedVectorDb?: {}; error?: string; }; type LlmFeedbackRequest = { appId: string; apiKey: string; threadId: string; messageId?: string; feedbackRating: number; feedbackMessage?: string; }; type RobotFeedbackRequest = { api_key: string; provider: string; user_id: IEquatable; comment: string; rating: number; anonymize: boolean; timestamp: string; message_id?: string; message_input?: string; message_output?: string; }; declare const appHandler: (req: PayloadRequest) => Promise<LlmApp>; declare const llmAppsHandler: PayloadHandler; declare const errorHandler: (error: any, res: Response) => void; declare function fineTuningJobsHandler(payload: Payload): Promise<void>; declare const fineTuningHandler: PayloadHandler; declare const infoHandler: PayloadHandler; type LlmKnowledgeBasePollResponse = { processing: boolean; success?: boolean; errors?: { job: string; taskStatus: string; error: string; }[]; }; declare const KbFileNoToolOption = "none"; declare const kbPollEndpointHandler: PayloadHandler; declare const kbEndpointHandler: PayloadHandler; declare const LlmKnowledgeBaseCreationMode: { readonly appAndTools: "appAndTools"; readonly appOnly: "appOnly"; readonly toolsOnly: "toolsOnly"; readonly specificTool: "specificTool"; }; type LlmKnowledgeBaseCreationMode = keyof typeof LlmKnowledgeBaseCreationMode; type LlmKnowledgeBaseCreationOptions = { mode: LlmKnowledgeBaseCreationMode; forceKbGeneration: boolean; toolFunctionId?: string; }; declare function llmKnowledgeBase(payload: Payload, app?: LlmApp, creationOptions?: LlmKnowledgeBaseCreationOptions): Promise<string>; declare const downloadVectorDbFile: (vectorDbFileUrl: string | undefined, destFilepath: string) => Promise<string | null>; declare const messageHandler: PayloadHandler; type PythonStreamRequest = { mode: string; systemMessage: string; systemContext: object; messages: LlmMessage[]; provider: string | null; model: string | null; temperature: number | null; secrets: LlmAppSecrets; threadId: string; msgId: string; tools: LlmTool[]; appTools: PythonStreamTool[]; vectorDb: string | null; rules: { vector_db: string | null; threshold: number; }; fineTunedModel: string | null; langChainTracing: boolean; langChainProject: string | null; }; type PythonStreamLlmChainSettings = { prompt: string; temperature: number; }; type PythonStreamDbSettings = { connectionString: string; }; type PythonStreamTool = { name: string; description?: string; type: string; functionId: string; functionName: string; functionDescription: string; model?: string; secrets: LlmAppToolSecret[]; llmChainSettings: PythonStreamLlmChainSettings; dataSource: string; dbSettings: PythonStreamDbSettings; searchSettings: LlmSearchSettings; integrations?: BowlBlock[]; endpoints?: LlmKnowledgeBaseEndpoint[]; waitingMessage: string; vectorDbType: string | null; vectorDbFile: string | null; rulesVectorDb: string | null; }; type PythonStreamResponse = Partial<{ created: number; id: string; model: string; object: string; python: string; system_fingerprint: string | null; threadId: string; messageId: string; version: string; }> & { chunks: LlmChunkItem[]; }; declare const textToChunksLastUnparsed: { text: string; }; /** * Executes the ROBOT endpoint returning a stream response. * * @param request - The request to be sent to the ROBOT endpoint. * @param callback - Optional callback function to handle the stream response. * @returns The payload handler function. */ declare const pythonStreamHandler: (request: PythonStreamRequest, raw: boolean, callback?: (response: PythonStreamResponse) => void) => PayloadHandler; type LlmRulesResult = { name: string; rules?: LlmRules; status: string; error?: string; }; declare const rulesEndpointHandler: PayloadHandler; declare function getRulesPath(basePath: string, appName: string): string; declare function rulesHandler(payload: Payload, app?: LlmApp): Promise<LlmRulesResult[]>; declare function llmVectorDbsCleaningHandler(payload: Payload): Promise<payload_dist_collections_config_types.BulkOperationResult<string>>; type RobotVectorDbType = { id: string; value: string; }; declare const vectorDbTypesHandler: PayloadHandler; type LlmSlug = { llmApp: string; llmTool: string; llmAppTool: string; llmThread: string; llmFineTuning: string; llmPrompt: string; llmRule: string; llmKbConfluence: string; llmKbAzure: string; llmKbDropbox: string; llmKbFile: string; llmKbGcs: string; llmKbGithub: string; llmKbGoogledrive: string; llmKbJira: string; llmKbS3: string; llmKbSftp: string; llmKbSharepoint: string; llmKbSitemap: string; llmKbSlack: string; llmVectorDb: string; [key: string]: string; }; type LlmGroup = { llm: string; [key: string]: string; }; type LlmRoles = { LlmEditor: string; LlmContributor: string; }; type LlmOptions = { group: LlmGroup; slug: LlmSlug; translations: Resource; roles: LlmRoles; }; type LlmInitOptions = { group?: Partial<LlmGroup>; slug?: Partial<LlmSlug>; }; type LocalizationOptions = {}; declare const llm: (sourceOptions?: LlmInitOptions) => BowlPlugin; declare const defaultSlug: LlmSlug; declare const defaultGroup: LlmGroup; declare const options: LlmOptions; declare const internalSlugs: string[]; export { ImageUrl, KbFileNoToolOption, LlmApp, LlmAppCard, LlmAppConfig, LlmAppContents, LlmAppCredentials, LlmAppLink, LlmAppLinksGroup, LlmAppPrompt, LlmAppReferrer, LlmAppSecrets, LlmAppSettings, LlmAppTool, LlmAppToolSecret, LlmChainSettings, LlmChunk, LlmChunkEnd, LlmChunkError, LlmChunkHeader, LlmChunkImage, LlmChunkImageUrl, LlmChunkInfo, LlmChunkItem, LlmChunkKnownItem, LlmChunkLog, LlmChunkString, LlmChunkText, LlmChunkTextOrString, LlmChunkUnknownItem, LlmChunkUnknownItemType, LlmDbSettings, LlmDecodedMessage, LlmFeedbackRequest, LlmFieldsMapping, LlmFineTuning, LlmGroup, LlmHeaderCta, LlmInfo, LlmInfoThread, LlmInitOptions, LlmKnowledgeBase, LlmKnowledgeBaseCreationMode, LlmKnowledgeBaseCreationOptions, LlmKnowledgeBaseEndpoint, LlmKnowledgeBaseFile, LlmKnowledgeBasePollResponse, LlmLogo, LlmMapDeletedField, LlmMapMetaField, LlmMapNewField, LlmMapReplacedField, LlmMessage, LlmMode, LlmOptions, LlmRecognitionMode, LlmRoles, LlmRules, LlmRulesResult, LlmSampleInputText, LlmSearchSettings, LlmSerializedMessage, LlmSlug, LlmSummary, LlmSynthesisMode, LlmTaskLogData, LlmThread, LlmTool, LlmUpload, LlmWebhook, LocalizationOptions, PythonStreamDbSettings, PythonStreamLlmChainSettings, PythonStreamRequest, PythonStreamResponse, PythonStreamTool, RobotFeedbackRequest, RobotTaskCompleted, RobotTaskFailed, RobotTaskMetadataExtra, RobotTaskRequest, RobotTaskResponse, RobotTaskResult, RobotVectorDbType, SharepointSettings, StreamResponse, VectorDb, appHandler, llm as default, defaultGroup, defaultSlug, downloadVectorDbFile, errorHandler, fineTuningHandler, fineTuningJobsHandler, getRulesPath, infoHandler, internalSlugs, isChunkString, isChunkText, isChunkTextOrString, kbEndpointHandler, kbPollEndpointHandler, llm, llmAppsHandler, llmKnowledgeBase, llmVectorDbsCleaningHandler, messageHandler, options, pythonStreamHandler, rulesEndpointHandler, rulesHandler, textToChunksLastUnparsed, vectorDbTypesHandler };