diff --git a/examples/nodejs/examples/embed/index.ts b/examples/nodejs/examples/embed/index.ts new file mode 100644 index 0000000..1abc06e --- /dev/null +++ b/examples/nodejs/examples/embed/index.ts @@ -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(); diff --git a/examples/nodejs/package.json b/examples/nodejs/package.json index 422a799..fcbda8c 100644 --- a/examples/nodejs/package.json +++ b/examples/nodejs/package.json @@ -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 (https://twitter.com/MrAhmadAwais)", diff --git a/packages/langbase/src/langbase/langbase.ts b/packages/langbase/src/langbase/langbase.ts index 8e47f0c..5a07564 100644 --- a/packages/langbase/src/langbase/langbase.ts +++ b/packages/langbase/src/langbase/langbase.ts @@ -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; @@ -314,6 +321,8 @@ export class Langbase { ) => Promise; }; + public embed: (options: EmbedOptions) => Promise; + constructor(options?: LangbaseOptions) { const baseUrl = 'https://api.langbase.com'; this.apiKey = options?.apiKey ?? ''; @@ -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( @@ -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 { + return this.request.post({ + endpoint: '/v1/embed', + body: options, + }); + } }