Skip to main content

Installation

The SDK is available on npm. You can install it using your preferred package manager:
npm install @larkup/rag-sdk

Initialization

Import and initialize the client. It automatically picks up the LARKUP_RAG_API_URL and LARKUP_RAG_API_KEY from your environment.
import { LarkupRAGClient } from "@larkup/rag-sdk";

const client = new LarkupRAGClient({
  baseUrl: "http://localhost:8080",
  apiKey: "your-api-key",
});

Basic Usage

Adding a Document

const response = await client.addDocument({
  id: "doc-1",
  text: "Larkup RAG is a flexible, high-performance RAG pipeline.",
  title: "Introduction",
});

console.log("Document added:", response.success);

Querying

const results = await client.query("What is Larkup RAG?", 5);

for (const hit of results.hits) {
  console.log(`Score: ${hit.score} | Text: ${hit.text}`);
}

Integrations

Vercel AI SDK (Agent Tool)

You can seamlessly integrate Larkup RAG into a Vercel AI SDK agent as a tool. This enables the LLM to search your knowledge base for real-time, context-aware answers.
import { tool } from "ai";
import { z } from "zod";
import { LarkupRAGClient } from "@larkup/rag-sdk";

const ragClient = new LarkupRAGClient();

export const ragTool = tool({
  description: "Search the knowledge base for relevant context.",
  parameters: z.object({
    query: z.string().describe("The search query"),
  }),
  execute: async ({ query }) => {
    const results = await ragClient.query(query, 5);
    return results.hits.map((hit) => hit.text).join("\n\n");
  },
});

LangChain.js

You can wrap the SDK into a custom retriever for LangChain JS to use within LangChain chains or agents.
import { BaseRetriever } from "@langchain/core/retrievers";
import { Document } from "@langchain/core/documents";
import { LarkupRAGClient } from "@larkup/rag-sdk";

export class LarkupRetriever extends BaseRetriever {
  lc_namespace = ["langchain", "retrievers"];
  client: LarkupRAGClient;

  constructor() {
    super();
    this.client = new LarkupRAGClient();
  }

  async _getRelevantDocuments(query: string): Promise<Document[]> {
    const results = await this.client.query(query, 5);
    return results.hits.map(
      (hit) => new Document({ pageContent: hit.text, metadata: { score: hit.score } })
    );
  }
}