diff --git a/examples/nodejs/examples/pipes/pipe.run.pipe.key.ts b/examples/nodejs/examples/pipes/pipe.run.pipe.key.ts new file mode 100644 index 0000000..c355c5a --- /dev/null +++ b/examples/nodejs/examples/pipes/pipe.run.pipe.key.ts @@ -0,0 +1,40 @@ +import 'dotenv/config'; +import {getRunner, Langbase} from 'langbase'; + +const langbase = new Langbase({ + apiKey: process.env.LANGBASE_API_KEY!, +}); + +async function main() { + const userMsg = 'Who is an AI Engineer?'; + + // Get readable stream + const {stream, threadId, rawResponse} = await langbase.pipe.run({ + messages: [{role: 'user', content: userMsg}], + stream: true, + rawResponse: true, + apiKey: process.env.PIPE_API_KEY! + }); + + // Convert the stream to a stream runner. + const runner = getRunner(stream); + + // Method 1: Using event listeners + runner.on('connect', () => { + console.log('Stream started.\n'); + }); + + runner.on('content', content => { + process.stdout.write(content); + }); + + runner.on('end', () => { + console.log('\nStream ended.'); + }); + + runner.on('error', error => { + console.error('Error:', error); + }); +} + +main(); diff --git a/examples/nodejs/readme.md b/examples/nodejs/readme.md index c9ffb56..622dcfb 100644 --- a/examples/nodejs/readme.md +++ b/examples/nodejs/readme.md @@ -14,6 +14,7 @@ npm run pipe.run.stream npm run pipe.run.chat npm run pipe.run.stream.chat npm run pipe.run.stream.llmkey +npm run pipe.run.pipe.key npm run pipe.list npm run pipe.create npm run pipe.update diff --git a/packages/langbase/src/langbase/langbase.ts b/packages/langbase/src/langbase/langbase.ts index 4192287..f9c30cf 100644 --- a/packages/langbase/src/langbase/langbase.ts +++ b/packages/langbase/src/langbase/langbase.ts @@ -279,6 +279,7 @@ export interface MemoryListDocResponse { export interface LangbaseOptions { apiKey: string; + baseUrl?: string; } export interface ToolWebSearchOptions { @@ -315,6 +316,7 @@ export type EmbedResponse = number[][]; export class Langbase { private request: Request; private apiKey: string; + private baseUrl: string; public pipe: { list: () => Promise; create: (options: PipeCreateOptions) => Promise; @@ -357,9 +359,12 @@ export class Langbase { public embed: (options: EmbedOptions) => Promise; constructor(options?: LangbaseOptions) { - const baseUrl = 'https://api.langbase.com'; + this.baseUrl = options?.baseUrl ?? 'https://api.langbase.com'; this.apiKey = options?.apiKey ?? ''; - this.request = new Request({apiKey: this.apiKey, baseUrl}); + this.request = new Request({ + apiKey: this.apiKey, + baseUrl: this.baseUrl, + }); // Initialize pipe property with method bindings this.pipe = { @@ -411,6 +416,14 @@ export class Langbase { delete options.stream; } + // if apikey is provided, create a new request instance + if (options.apiKey) { + this.request = new Request({ + apiKey: options.apiKey, + baseUrl: this.baseUrl, + }); + } + return this.request.post({ endpoint: '/v1/pipes/run', body: options,