From aa2c934d73a9aa3f36fe4a89131c120e39bcbb5f Mon Sep 17 00:00:00 2001 From: Ashar Irfan Date: Tue, 4 Feb 2025 00:44:11 +0500 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Embed=20sdk=20functio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/langbase/src/langbase/langbase.ts | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/langbase/src/langbase/langbase.ts b/packages/langbase/src/langbase/langbase.ts index 8e47f0c..d860315 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, + }); + } } From 6cca0c15557ad111c91436df4bd102243c958e55 Mon Sep 17 00:00:00 2001 From: Ashar Irfan Date: Tue, 4 Feb 2025 00:44:19 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Embed=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/nodejs/examples/embed/index.ts | 22 ++++++++++++++++++++++ examples/nodejs/package.json | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 examples/nodejs/examples/embed/index.ts 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)", From 5f2aa2c6b48223a054393ae2522280fc0fc04954 Mon Sep 17 00:00:00 2001 From: Ashar Irfan Date: Tue, 4 Feb 2025 00:48:08 +0500 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Optional=20argume?= =?UTF-8?q?nt?= 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 d860315..5a07564 100644 --- a/packages/langbase/src/langbase/langbase.ts +++ b/packages/langbase/src/langbase/langbase.ts @@ -274,7 +274,7 @@ export interface ToolCrawlResponse { export interface EmbedOptions { chunks: string[]; - embeddingModel: EmbeddingModels; + embeddingModel?: EmbeddingModels; } export type EmbedResponse = number[][];