Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/nodejs/examples/embed/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dotenv/config';
import {Langbase} from 'langbase';

const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});

/**
* Generates embeddings for the given text chunks.
*/
async function main() {
const response = await langbase.embed({
chunks: [
'Langbase is the most powerful serverless platform for building AI agents with memory. Build, scale, and evaluate AI agents with semantic memory (RAG) and world-class developer experience. We process billions of AI messages/tokens daily. Built for every developer, not just AI/ML experts.',
],
embeddingModel: 'openai:text-embedding-3-large',
});

console.log(response);
}

main();
3 changes: 2 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"stream-text-generate-pipe": "npx tsx ./examples/pipes/stream-text-generate-pipe.ts",
"stream-text-chat-pipe": "npx tsx ./examples/pipes/stream-text-chat-pipe.ts",
"tools.web-search": "npx tsx ./examples/tools/web-search.ts",
"tools.crawl": "npx tsx ./examples/tools/crawl.ts"
"tools.crawl": "npx tsx ./examples/tools/crawl.ts",
"embed": "npx tsx ./examples/embed/index.ts"
},
"keywords": [],
"author": "Ahmad Awais <me@AhmadAwais.com> (https://twitter.com/MrAhmadAwais)",
Expand Down
26 changes: 26 additions & 0 deletions packages/langbase/src/langbase/langbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ export interface ToolCrawlResponse {
content: string;
}

export interface EmbedOptions {
chunks: string[];
embeddingModel?: EmbeddingModels;
}

export type EmbedResponse = number[][];

export class Langbase {
private request: Request;
private apiKey: string;
Expand Down Expand Up @@ -314,6 +321,8 @@ export class Langbase {
) => Promise<ToolWebSearchResponse[]>;
};

public embed: (options: EmbedOptions) => Promise<EmbedResponse>;

constructor(options?: LangbaseOptions) {
const baseUrl = 'https://api.langbase.com';
this.apiKey = options?.apiKey ?? '';
Expand Down Expand Up @@ -347,6 +356,8 @@ export class Langbase {
crawl: this.webCrawl.bind(this),
webSearch: this.webSearch.bind(this),
};

this.embed = this.generateEmbeddings.bind(this);
}

private async runPipe(
Expand Down Expand Up @@ -607,4 +618,19 @@ export class Langbase {
},
});
}

/**
* 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 async generateEmbeddings(
options: EmbedOptions,
): Promise<EmbedResponse> {
return this.request.post({
endpoint: '/v1/embed',
body: options,
});
}
}
Loading