UNPKG

commandbase

Version:

The AI SDK for building declarative and composable AI-powered LLM products.

730 lines (724 loc) 25.4 kB
import { ChatCompletionStream } from 'openai/lib/ChatCompletionStream'; import { ChatCompletionMessageToolCall } from 'openai/resources/chat/completions'; type Role = 'user' | 'assistant' | 'system' | 'tool'; interface RunOptionsBase { messages?: Message[]; variables?: Variable[]; threadId?: string; rawResponse?: boolean; runTools?: boolean; tools?: Tools[]; name?: string; apiKey?: string; llmKey?: string; json?: boolean; } interface RunOptionsT extends RunOptionsBase { stream?: false; } interface RunOptionsStreamT extends RunOptionsBase { stream: true; } interface ChoiceGenerate$1 { index: number; message: Message; logprobs: boolean | null; finish_reason: string; } interface Usage { prompt_tokens: number; completion_tokens: number; total_tokens: number; } interface RunResponse { completion: string; threadId?: string; id: string; object: string; created: number; model: string; choices: ChoiceGenerate$1[]; usage: Usage; system_fingerprint: string | null; rawResponse?: { headers: Record<string, string>; }; messages: Message[]; llmKey?: string; name?: string; } interface RunResponseStream { stream: ReadableStream<any>; threadId: string | null; rawResponse?: { headers: Record<string, string>; }; } type RunOptions = (RunOptionsT & { name: string; apiKey?: never; }) | (RunOptionsT & { name?: never; apiKey: string; }); type RunOptionsStream = (RunOptionsStreamT & { name: string; apiKey?: never; }) | (RunOptionsStreamT & { name?: never; apiKey: string; }); interface Function { name: string; arguments: string; } interface ToolCall { id: string; type: 'function'; function: Function; } interface Message { role: Role; content: string | null; name?: string; tool_call_id?: string; tool_calls?: ToolCall[]; } interface Variable { name: string; value: string; } interface ToolChoice { type: 'function'; function: { name: string; }; } interface Tools { type: 'function'; function: { name: string; description?: string; parameters?: Record<string, any>; }; } interface PipeBaseOptions { name: string; description?: string; status?: 'public' | 'private'; upsert?: boolean; model?: string; stream?: boolean; json?: boolean; store?: boolean; moderate?: boolean; top_p?: number; max_tokens?: number; temperature?: number; presence_penalty?: number; frequency_penalty?: number; stop?: string[]; tools?: Tools[]; tool_choice?: 'auto' | 'required' | ToolChoice; parallel_tool_calls?: boolean; messages?: Message[]; variables?: Variable[]; memory?: { name: string; }[]; } interface PipeListResponse { name: string; description: string; status: 'public' | 'private'; owner_login: string; url: string; model: string; stream: boolean; json: boolean; store: boolean; moderate: boolean; top_p: number; max_tokens: number; temperature: number; presence_penalty: number; frequency_penalty: number; stop: string[]; tool_choice: 'auto' | 'required' | ToolChoice; parallel_tool_calls: boolean; messages: Message[]; variables: Variable[] | []; tools: Tools[] | []; memory: { name: string; }[] | []; } interface PipeBaseResponse { name: string; description: string; status: 'public' | 'private'; owner_login: string; url: string; type: 'chat' | 'generate' | 'run'; api_key: string; } interface PipeCreateOptions extends PipeBaseOptions { } interface PipeUpdateOptions extends PipeBaseOptions { } interface PipeCreateResponse extends PipeBaseResponse { } interface PipeUpdateResponse extends PipeBaseResponse { } interface MemoryBaseResponse { name: string; description: string; owner_login: string; url: string; } type EmbeddingModels = 'openai:text-embedding-3-large' | 'cohere:embed-multilingual-v3.0' | 'cohere:embed-multilingual-light-v3.0' | 'google:text-embedding-004'; type ContentType = 'application/pdf' | 'text/plain' | 'text/markdown' | 'text/csv' | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | 'application/vnd.ms-excel'; interface MemoryCreateOptions { name: string; description?: string; embedding_model?: EmbeddingModels; } interface MemoryDeleteOptions { name: string; } type FilterOperator = 'Eq' | 'NotEq' | 'In' | 'NotIn' | 'And' | 'Or'; type FilterConnective = 'And' | 'Or'; type FilterValue = string | string[]; type MemoryFilters = [ FilterOperator | FilterConnective, FilterValue | MemoryFilters ][]; interface MemoryRetrieveOptions { query: string; memory: { name: string; filters?: MemoryFilters; }[]; topK?: number; } interface MemoryListDocOptions { memoryName: string; } interface MemoryDeleteDocOptions { memoryName: string; documentName: string; } interface MemoryUploadDocOptions { memoryName: string; documentName: string; meta?: Record<string, string>; document: Buffer | File | FormData | ReadableStream; contentType: ContentType; } interface MemoryRetryDocEmbedOptions { memoryName: string; documentName: string; } interface MemoryCreateResponse extends MemoryBaseResponse { embedding_model: EmbeddingModels; } interface MemoryListResponse extends MemoryBaseResponse { embedding_model: EmbeddingModels; } interface BaseDeleteResponse { success: boolean; } interface MemoryDeleteResponse extends BaseDeleteResponse { } interface MemoryDeleteDocResponse extends BaseDeleteResponse { } interface MemoryRetryDocEmbedResponse extends BaseDeleteResponse { } interface MemoryRetrieveResponse { text: string; similarity: number; meta: Record<string, string>; } interface MemoryListDocResponse { name: string; status: 'queued' | 'in_progress' | 'completed' | 'failed'; status_message: string | null; metadata: { size: number; type: ContentType; }; enabled: boolean; chunk_size: number; chunk_overlap: number; owner_login: string; } interface LangbaseOptions { apiKey: string; baseUrl?: string; } interface ToolWebSearchOptions { query: string; service: 'exa'; totalResults?: number; domains?: string[]; apiKey?: string; } interface ToolWebSearchResponse { url: string; content: string; } interface ToolCrawlOptions { url: string[]; maxPages?: number; apiKey?: string; } interface ToolCrawlResponse { url: string; content: string; } interface EmbedOptions { chunks: string[]; embeddingModel?: EmbeddingModels; } type EmbedResponse = number[][]; interface ChunkOptions { document: Buffer | File | FormData | ReadableStream; documentName: string; contentType: ContentType; chunkMaxLength?: string; chunkOverlap?: string; separator?: string; } type ChunkResponse = string[]; type ParseOptions = { document: Buffer | File | FormData | ReadableStream; documentName: string; contentType: ContentType; }; type ParseResponse = { documentName: string; content: string; }; interface AddMessageOptions { threadId: string; messages: Message[]; } interface ListMessagesOptions { threadId: string; } interface DeleteThreadOptions { threadId: string; } declare class Langbase { private request; private apiKey; private baseUrl; pipe: { list: () => Promise<PipeListResponse[]>; create: (options: PipeCreateOptions) => Promise<PipeCreateResponse>; update: (options: PipeUpdateOptions) => Promise<PipeUpdateResponse>; run: { (options: RunOptionsStream): Promise<RunResponseStream>; (options: RunOptions): Promise<RunResponse>; }; }; memory: { create: (options: MemoryCreateOptions) => Promise<MemoryCreateResponse>; delete: (options: MemoryDeleteOptions) => Promise<MemoryDeleteResponse>; retrieve: (options: MemoryRetrieveOptions) => Promise<MemoryRetrieveResponse[]>; list: () => Promise<MemoryListResponse[]>; documents: { list: (options: MemoryListDocOptions) => Promise<MemoryListDocResponse[]>; delete: (options: MemoryDeleteDocOptions) => Promise<MemoryDeleteDocResponse>; upload: (options: MemoryUploadDocOptions) => Promise<Response>; embedding: { retry: (options: MemoryRetryDocEmbedOptions) => Promise<MemoryRetryDocEmbedResponse>; }; }; }; thread: { messages: { add: (options: AddMessageOptions) => Promise<Message[]>; list: (options: ListMessagesOptions) => Promise<Message[]>; }; delete: (options: DeleteThreadOptions) => Promise<boolean>; }; tool: { crawl: (options: ToolCrawlOptions) => Promise<ToolCrawlResponse[]>; webSearch: (options: ToolWebSearchOptions) => Promise<ToolWebSearchResponse[]>; }; embed: (options: EmbedOptions) => Promise<EmbedResponse>; chunk: (options: ChunkOptions) => Promise<ChunkResponse>; parse: (options: ParseOptions) => Promise<ParseResponse>; constructor(options?: LangbaseOptions); private runPipe; /** * Creates a new pipe on Langbase. * * @param {PipeCreateOptions} options - The options for creating the pipe. * @returns {Promise<PipeCreateResponse>} A promise that resolves to the response of the pipe creation. */ private createPipe; /** * Updates a pipe on Langbase. * * @param {PipeUpdateOptions} options - The options for updating the pipe. * @returns {Promise<PipeUpdateResponse>} A promise that resolves to the response of the update operation. */ private updatePipe; /** * Retrieves a list of pipes. * * @returns {Promise<PipeListResponse[]>} A promise that resolves to an array of PipeListResponse objects. */ private listPipe; /** * Creates a new memory on Langbase. * * @param {MemoryCreateOptions} options - The options to create the memory instance. * @param {string} options.name - The name of the memory. * @param {string} options.description - The description of the memory. * @returns {Promise<MemoryCreateResponse>} A promise that resolves to the response of the memory creation. */ private createMemory; /** * Retrieves a list of all memories on Langbase. * * @returns {Promise<MemoryListResponse[]>} A promise that resolves to an array of memory list responses. */ private listMemory; /** * Deletes a memory on Langbase. * * @param {MemoryDeleteOptions} options - The options for deleting the memory resource. * @param {string} options.name - The name of the memory to delete. * @returns {Promise<MemoryDeleteResponse>} A promise that resolves to the response of the delete operation. */ private deleteMemory; /** * Retrieves similar text from the memory. * * @param {MemoryRetrieveOptions} options - The options to use for retrieving memory data. * @param {string} options.query - The query text to search for. * @param {object[]} options.memory - The memory to search in. * @param {number} [options.topK] - The number of similar texts to retrieve. * @returns A promise that resolves to an array of `MemoryRetrieveResponse` objects. */ private retrieveMemory; /** * Retrieves a list of documents inside a memory. * * @param {MemoryListDocOptions} options - The options for listing documents, including the memory name. * @param {string} options.memoryName - The name of the memory to list documents from. * @returns A promise that resolves to an array of `MemoryListDocResponse` objects. */ private listDocs; /** * Deletes a document from a memory. * * @param {MemoryDeleteDocOptions} options - The options for deleting the document. * @param {string} options.memoryName - The name of the memory to delete the document from. * @param {string} options.documentName - The name of the document to delete. * @returns A promise that resolves to a `MemoryDeleteDocResponse` indicating the result of the delete operation. */ private deleteDoc; /** * Uploads a document to the memory. * * @param {MemoryUploadDocOptions} options - The options for uploading the document. * @param {string} options.memoryName - The name of the memory to upload the document to. * @param {string} options.fileName - The name of the file being uploaded. * @param {object} [options.meta] - Optional metadata associated with the document. * @param {string} options.contentType - The MIME type of the file being uploaded. * @param {Blob | Buffer} options.file - The file content to be uploaded. * @returns {Promise<Response>} The response from the upload request. * @throws Will throw an error if the upload fails. */ private uploadDocs; /** * Retries the embedding process for a specific document in memory. * * @param options - The options required to retry the document embedding. * @param options.memoryName - The name of the memory containing the document. * @param options.documentName - The name of the document to retry embedding for. * @returns A promise that resolves to the response of the retry operation. */ private retryDocEmbed; /** * Performs a web search using the Langbase API. * * @param options - Web search configuration options * @param options.apiKey - Optional API key for web search authentication * @returns Promise that resolves to an array of web search results */ private webSearch; /** * Performs a web crawls on target websites using the Langbase API. * * @param options - Crawl configuration options * @returns An array of responses containing data from the crawl operation. */ private webCrawl; /** * Generates embeddings for the given input using the LangBase API. * * @param options - Embed options * @returns Promise that resolves to the embedding response containing vector representations */ private generateEmbeddings; /** * Splits a given document into multiple chunks using the Langbase API. * * @param options - The chunking options. * @param options.document - The document to be chunked. * @param options.chunk_max_length - An optional maximum length for each chunk. * @param options.chunk_overlap - An optional number of overlapping characters between chunks. * @param options.separator - An optional separator used to split the document. * @returns A promise that resolves to the chunked document response. */ private chunkDocument; /** * Parses a document using the Langbase API. * * @param options - The options for parsing the document * @param options.document - The document to be parsed * @param options.documentName - The name of the document * @param options.contentType - The content type of the document * * @returns A promise that resolves to the parse response from the API * * @throws {Error} If the API request fails */ private parseDocument; /** * Adds multiple messages to a specified thread. * * @param options - The options for adding messages * @param options.threadId - The ID of the thread to add messages to * @param options.messages - The array of messages to be added * @returns A Promise that resolves to an array of Message objects * @throws May throw an error if the request fails */ private addMessages; /** * Retrieves all messages from a specified thread. * * @param options - The options for listing messages * @param options.threadId - The unique identifier of the thread to list messages from * @returns Promise that resolves to an array of Message objects * @throws {Error} If the request fails or the thread ID is invalid */ private listMessages; /** * Deletes a thread using the provided thread ID. * @param options - The options for deleting a thread * @param options.threadId - The unique identifier of the thread to delete * @returns A promise that resolves to true if the thread was successfully deleted * @throws Will throw an error if the deletion fails or if the thread ID is invalid */ private deleteThread; } declare class Stream<Item> implements AsyncIterable<Item> { private iterator; controller: AbortController; constructor(iterator: () => AsyncIterator<Item>, controller: AbortController); /** * Creates a stream of AsyncIterator from a Server-Sent Events (SSE) response. * * @template Item - The type of items in the stream. * @param {Response} response - The SSE response object. * @param {AbortController} controller - The abort controller used to cancel the ongoing request. * @returns {Stream<AsyncIterator<Item, any, undefined>>} - The stream created from the SSE response. * @throws {Error} - If the stream has already been consumed. */ static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item>; /** * Generates a Stream from a newline-separated ReadableStream * where each item is a JSON value. * * @template Item - The type of items in the stream. * @param {ReadableStream} readableStream - The readable stream to create the stream from. * @param {AbortController} controller - The abort controller to control the stream. * @returns {Stream<Item>} - The created stream. */ static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item>; [Symbol.asyncIterator](): AsyncIterator<Item>; /** * Splits the stream into two streams which can be * independently read from at different speeds. */ tee(): [Stream<Item>, Stream<Item>]; /** * Converts this stream to a newline-separated ReadableStream of * JSON stringified values in the stream which can be turned back into a Stream with `Stream.fromReadableStream()`. */ toReadableStream(): ReadableStream; } interface GenerateOptions { messages?: Message[]; variables?: Variable[]; threadId?: string; chat?: boolean; } interface StreamOptions { messages?: Message[]; variables?: Variable[]; threadId?: string | null; chat?: boolean; } interface ChoiceGenerate { index: number; message: Message; logprobs: boolean | null; finish_reason: string; } interface ChoiceStream$1 { index: number; delta: Delta$1; logprobs: boolean | null; finish_reason: string; } interface Delta$1 { role?: Role; content?: string; tool_calls?: ToolCall[]; } interface GenerateResponse { completion: string; threadId?: string; id: string; object: string; created: number; model: string; choices: ChoiceGenerate[]; usage: Usage; system_fingerprint: string | null; } type StreamText = Stream<StreamChunk>; interface StreamResponse { stream: StreamText; threadId: string | null; } interface StreamChunk { id: string; object: string; created: number; model: string; choices: ChoiceStream$1[]; } interface PipeOptions { apiKey: string; baseUrl?: string; name?: string; } declare class Pipe { private request; constructor(options: PipeOptions); /** * @deprecated This method is deprecated and will be removed in a future version. * * Please use `langbase.pipe.run()` instead * @see https://langbase.com/docs/sdk/pipe/run */ generateText(options: GenerateOptions): Promise<GenerateResponse>; /** * @deprecated This method is deprecated and will be removed in a future version. * * Please use `langbase.pipe.run()` instead * @see https://langbase.com/docs/sdk/pipe/run */ streamText(options: StreamOptions): Promise<StreamResponse>; } /** * Print stream to standard output (console). * @param stream The stream to print */ declare const printStreamToStdout: (stream: StreamText) => Promise<void>; interface Runner extends ChatCompletionStream { } interface ToolCallResult extends ChatCompletionMessageToolCall { } type MessageRole = 'function' | 'assistant' | 'system' | 'user' | 'tool'; interface Delta { role?: MessageRole; content?: string; tool_calls?: ToolCallResult[]; } interface ChoiceStream { index: number; delta: Delta; logprobs: boolean | null; finish_reason: string; } interface ChunkStream { id: string; object: string; created: number; model: string; choices: ChoiceStream[]; } /** * Converts a ReadableStream into a Runner. * * @param readableStream - The ReadableStream to convert. * @returns The converted Runner. */ declare const fromReadableStream: (readableStream: ReadableStream) => Runner; /** * Returns a runner for the given readable stream. * * @param readableStream - The readable stream to create a runner for. * @returns A runner for the given readable stream. */ declare const getRunner: (readableStream: ReadableStream) => Runner; /** * Retrieves the text part from a given ChunkStream. * * @param chunk - The ChunkStream object. * @returns The text content of the first choice's delta, or an empty string if it doesn't exist. */ declare const getTextPart: (chunk: ChunkStream) => string; /** * Handles the response stream from a given `Response` object. * * @param {Object} params - The parameters for handling the response stream. * @param {Response} params.response - The API response to handle. * @param {boolean} params.rawResponse - Optional flag to include raw response headers. * * @returns {Object} An object containing the processed stream, thread ID, and optionally raw response headers. * @returns {ReadableStream<any>} return.stream - The readable stream created from the response. * @returns {string | null} return.threadId - The thread ID extracted from the response headers. * @returns {Object} [return.rawResponse] - Optional raw response headers. * @returns {Record<string, string>} return.rawResponse.headers - The headers from the raw response. */ declare function handleResponseStream({ response, rawResponse, }: { response: Response; rawResponse?: boolean; }): { stream: any; threadId: string | null; rawResponse?: { headers: Record<string, string>; }; }; /** * Retrieves tool calls from a given readable stream. * * @param stream - The readable stream from which to extract tool calls. * @returns A promise that resolves to an array of `ToolCall` objects. */ declare function getToolsFromStream(stream: ReadableStream<any>): Promise<ChatCompletionMessageToolCall[]>; /** * Retrieves tools from a readable stream asynchronously. * * @param stream - The readable stream to extract tools from * @returns A promise that resolves with the tools extracted from the stream */ declare function getToolsFromRunStream(stream: ReadableStream<any>): Promise<ChatCompletionMessageToolCall[]>; /** * Extracts tool calls from non-stream response. * @param response - The run response object * @returns A promise that resolves to an array of tool calls. Returns empty array if no tools are present. */ declare function getToolsFromRun(response: RunResponse): Promise<ChatCompletionMessageToolCall[]>; export { type AddMessageOptions, type BaseDeleteResponse, type ChunkOptions, type ChunkResponse, type ChunkStream, type ContentType, type DeleteThreadOptions, type EmbedOptions, type EmbedResponse, type EmbeddingModels, type Function, type GenerateOptions, type GenerateResponse, Langbase, type LangbaseOptions, type ListMessagesOptions, type MemoryCreateOptions, type MemoryCreateResponse, type MemoryDeleteDocOptions, type MemoryDeleteDocResponse, type MemoryDeleteOptions, type MemoryDeleteResponse, type MemoryListDocOptions, type MemoryListDocResponse, type MemoryListResponse, type MemoryRetrieveOptions, type MemoryRetrieveResponse, type MemoryRetryDocEmbedOptions, type MemoryRetryDocEmbedResponse, type MemoryUploadDocOptions, type Message, type MessageRole, type ParseOptions, type ParseResponse, Pipe, type PipeCreateOptions, type PipeCreateResponse, type PipeListResponse, type PipeOptions, type PipeUpdateOptions, type PipeUpdateResponse, type Role, type RunOptions, type RunOptionsBase, type RunOptionsStream, type RunOptionsStreamT, type RunOptionsT, type RunResponse, type RunResponseStream, type Runner, type StreamChunk, type StreamOptions, type StreamResponse, type StreamText, type ToolCall, type ToolCallResult, type ToolCrawlOptions, type ToolCrawlResponse, type ToolWebSearchOptions, type ToolWebSearchResponse, type Usage, type Variable, fromReadableStream, getRunner, getTextPart, getToolsFromRun, getToolsFromRunStream, getToolsFromStream, handleResponseStream, printStreamToStdout };