跳到主要内容
Vectara 是一个用于构建 GenAI 应用程序的平台。它提供了一个易于使用的 API,用于文档索引和查询,该 API 由 Vectara 管理,并针对性能和准确性进行了优化。 您可以将 Vectara 作为 LangChain.js 的向量存储。

👉 包含嵌入

Vectara 在底层使用自己的嵌入,因此您无需自己提供任何嵌入或调用其他服务来获取嵌入。 这也意味着如果您提供自己的嵌入,它们将无效。
const store = await VectaraStore.fromTexts(
  ["hello world", "hi there"],
  [{ foo: "bar" }, { foo: "baz" }],
  // This won't have an effect. Provide a FakeEmbeddings instance instead for clarity.
  new OpenAIEmbeddings(),
  args
);

设置

您需要 配置您的 .env 文件或提供参数以将 LangChain 连接到您的 Vectara 语料库
VECTARA_CUSTOMER_ID=your_customer_id
VECTARA_CORPUS_ID=your_corpus_id
VECTARA_API_KEY=your-vectara-api-key
请注意,您可以提供以逗号分隔的多个语料库 ID,以便同时查询多个语料库。例如:VECTARA_CORPUS_ID=3,8,9,43。要索引多个语料库,您需要为每个语料库创建一个单独的 VectaraStore 实例。

用法

import { VectaraStore } from "@langchain/community/vectorstores/vectara";
import { VectaraSummaryRetriever } from "@langchain/community/retrievers/vectara_summary";
import { Document } from "@langchain/core/documents";

// Create the Vectara store.
const store = new VectaraStore({
  customerId: Number(process.env.VECTARA_CUSTOMER_ID),
  corpusId: Number(process.env.VECTARA_CORPUS_ID),
  apiKey: String(process.env.VECTARA_API_KEY),
  verbose: true,
});

// Add two documents with some metadata.
const doc_ids = await store.addDocuments([
  new Document({
    pageContent: "Do I dare to eat a peach?",
    metadata: {
      foo: "baz",
    },
  }),
  new Document({
    pageContent: "In the room the women come and go talking of Michelangelo",
    metadata: {
      foo: "bar",
    },
  }),
]);

// Perform a similarity search.
const resultsWithScore = await store.similaritySearchWithScore(
  "What were the women talking about?",
  1,
  {
    lambda: 0.025,
  }
);

// Print the results.
console.log(JSON.stringify(resultsWithScore, null, 2));
/*
[
  [
    {
      "pageContent": "In the room the women come and go talking of Michelangelo",
      "metadata": {
        "lang": "eng",
        "offset": "0",
        "len": "57",
        "foo": "bar"
      }
    },
    0.4678752
  ]
]
*/

const retriever = new VectaraSummaryRetriever({ vectara: store, topK: 3 });
const documents = await retriever.invoke("What were the women talking about?");

console.log(JSON.stringify(documents, null, 2));
/*
[
  {
    "pageContent": "<b>In the room the women come and go talking of Michelangelo</b>",
    "metadata": {
      "lang": "eng",
      "offset": "0",
      "len": "57",
      "foo": "bar"
    }
  },
  {
    "pageContent": "<b>In the room the women come and go talking of Michelangelo</b>",
    "metadata": {
      "lang": "eng",
      "offset": "0",
      "len": "57",
      "foo": "bar"
    }
  },
  {
    "pageContent": "<b>In the room the women come and go talking of Michelangelo</b>",
    "metadata": {
      "lang": "eng",
      "offset": "0",
      "len": "57",
      "foo": "bar"
    }
  }
]
*/

// Delete the documents.
await store.deleteDocuments(doc_ids);
请注意,lambda 是一个与 Vectara 混合搜索功能相关的参数,提供神经搜索和布尔/精确匹配之间的权衡,如此处所述。我们建议将 0.025 作为默认值,同时为高级用户提供根据需要自定义此值的方法。

API

Vectara 的 LangChain 向量存储使用 Vectara 的核心 API
  • 索引 API,用于将文档存储在 Vectara 语料库中。
  • 搜索 API,用于查询此数据。此 API 支持混合搜索。

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.