Skip to content

Commit

Permalink
👌 IMPROVE: Pipe API key support
Browse files Browse the repository at this point in the history
  • Loading branch information
msaaddev committed Feb 3, 2025
1 parent 7181020 commit e533004
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
40 changes: 40 additions & 0 deletions examples/nodejs/examples/pipes/pipe.run.pipe.key.ts
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions examples/nodejs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions packages/langbase/src/langbase/langbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export interface MemoryListDocResponse {

export interface LangbaseOptions {
apiKey: string;
baseUrl?: string;
}

export interface ToolWebSearchOptions {
Expand Down Expand Up @@ -315,6 +316,7 @@ export type EmbedResponse = number[][];
export class Langbase {
private request: Request;
private apiKey: string;
private baseUrl: string;
public pipe: {
list: () => Promise<PipeListResponse[]>;
create: (options: PipeCreateOptions) => Promise<PipeCreateResponse>;
Expand Down Expand Up @@ -357,9 +359,12 @@ export class Langbase {
public embed: (options: EmbedOptions) => Promise<EmbedResponse>;

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 = {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e533004

Please sign in to comment.