From f43a921de1bc9887582335c010156b58067e37fa Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Date: Sat, 8 Mar 2025 14:37:34 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20LLM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/langbase/src/langbase/langbase.ts | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/packages/langbase/src/langbase/langbase.ts b/packages/langbase/src/langbase/langbase.ts index ed1b7a1..6dde797 100644 --- a/packages/langbase/src/langbase/langbase.ts +++ b/packages/langbase/src/langbase/langbase.ts @@ -24,6 +24,29 @@ export interface RunOptionsStreamT extends RunOptionsBase { stream: true; } +export interface LlmOptionsBase { + messages: PromptMessage[]; + model: string; + llmKey: string; + top_p?: number; + max_tokens?: number; + temperature?: number; + presence_penalty?: number; + frequency_penalty?: number; + stop?: string[]; + tool_choice?: any; + parallel_tool_calls?: boolean; + customModelParams?: Record; +} + +export interface LlmOptionsT extends LlmOptionsBase { + stream?: false; +} + +export interface LlmOptionsStreamT extends LlmOptionsBase { + stream: true; +} + interface ChoiceGenerate { index: number; message: Message; @@ -91,6 +114,27 @@ export interface Message { tool_calls?: ToolCall[]; } +// Message with proper content type for Vision support +export interface PromptMessage { + role: Role; + content: string | MessageContentType[] | null; + name?: string; + tool_call_id?: string; + tool_calls?: ToolCall[]; +} + +export interface MessageContentType { + type: string; + text?: string; + image_url?: { + url: string; + detail?: string; + }; + cache_control?: { + type: 'ephemeral'; + }; +} + export interface ThreadMessage extends Message { attachments?: any[]; metadata?: Record; @@ -506,6 +550,11 @@ export class Langbase { public chunk: (options: ChunkOptions) => Promise; public parse: (options: ParseOptions) => Promise; + public llm: { + (options: LlmOptionsStreamT): Promise; + (options: LlmOptionsT): Promise; + }; + constructor(options?: LangbaseOptions) { this.baseUrl = options?.baseUrl ?? 'https://api.langbase.com'; this.apiKey = options?.apiKey ?? ''; @@ -584,6 +633,8 @@ export class Langbase { list: this.listThreadMessages.bind(this), }, }; + + this.llm = this.runLlm.bind(this); } private async runPipe( @@ -1004,4 +1055,32 @@ export class Langbase { endpoint: `/v1/threads/${options.threadId}/messages`, }); } + + // Add the private implementation + private async runLlm( + options: LlmOptionsStreamT, + ): Promise; + + private async runLlm(options: LlmOptionsT): Promise; + + private async runLlm( + options: LlmOptionsT | LlmOptionsStreamT, + ): Promise { + if (!options.llmKey) { + throw new Error('LLM API key is required to run this LLM.'); + } + + // Remove stream property if it's not set to true + if (typeof options.stream === 'undefined') { + delete options.stream; + } + + return this.request.post({ + endpoint: '/v1/llm', + body: options, + headers: { + ...(options.llmKey && {'LB-LLM-Key': options.llmKey}), + }, + }); + } } From 8ff474c1f5340b6930bf5f2bfc0a2c806bbd1689 Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Date: Wed, 12 Mar 2025 00:22:21 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Tool=20choice=20t?= =?UTF-8?q?ypes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/langbase/src/langbase/langbase.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/langbase/src/langbase/langbase.ts b/packages/langbase/src/langbase/langbase.ts index 6dde797..183624c 100644 --- a/packages/langbase/src/langbase/langbase.ts +++ b/packages/langbase/src/langbase/langbase.ts @@ -34,7 +34,7 @@ export interface LlmOptionsBase { presence_penalty?: number; frequency_penalty?: number; stop?: string[]; - tool_choice?: any; + tool_choice?: 'auto' | 'required' | ToolChoice; parallel_tool_calls?: boolean; customModelParams?: Record; }