跳到主要内容
AzionVectorStore 用于在 Azion Edge 平台上直接使用 Edge SQL 管理和搜索通过向量嵌入的文档集合。 本指南提供了 Azion EdgeSQL 向量存储的快速入门概述。有关所有 AzionVectorStore 功能和配置的详细文档,请查阅 API 参考

概览

集成详情

类别[PY 支持]版本
AzionVectorStore@langchain/communityNPM - Version

设置

要使用 AzionVectorStore 向量存储,您需要安装 @langchain/community 包。此外,您还需要一个 Azion 账户和一个 令牌才能使用 Azion API,将其配置为环境变量 AZION_TOKEN。有关此内容的更多信息,请参阅 文档 本指南还将使用 OpenAI 嵌入,这需要您安装 @langchain/openai 集成包。如果您愿意,也可以使用 其他支持的嵌入模型
npm install azion @langchain/openai @langchain/community

凭据

完成此操作后,设置 AZION_TOKEN 环境变量
process.env.AZION_TOKEN = "your-api-key"
如果您在本指南中使用 OpenAI 嵌入,您还需要设置您的 OpenAI 密钥
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
如果您想获取模型调用的自动化跟踪,您还可以通过取消注释下方来设置您的 LangSmith API 密钥
// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"

实例化

import { AzionVectorStore } from "@langchain/community/vectorstores/azion_edgesql";
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});

// Instantiate with the constructor if the database and table have already been created
const vectorStore = new AzionVectorStore(embeddings, { dbName: "langchain", tableName: "documents" });

// If you have not created the database and table yet, you can do so with the setupDatabase method
// await vectorStore.setupDatabase({ columns:["topic","language"], mode: "hybrid" })

// OR instantiate with the static method if the database and table have not been created yet
// const vectorStore = await AzionVectorStore.initialize(embeddingModel, { dbName: "langchain", tableName: "documents" }, { columns:[], mode: "hybrid" })

管理向量存储

向向量存储添加项目

import type { Document } from "@langchain/core/documents";

const document1: Document = {
  pageContent: "The powerhouse of the cell is the mitochondria",
  metadata: { language: "en", topic: "biology" }
};

const document2: Document = {
  pageContent: "Buildings are made out of brick",
  metadata: { language: "en", topic: "history" }
};

const document3: Document = {
  pageContent: "Mitochondria are made out of lipids",
  metadata: { language: "en", topic: "biology" }
};

const document4: Document = {
  pageContent: "The 2024 Olympics are in Paris",
  metadata: { language: "en", topic: "history" }
}

const documents = [document1, document2, document3, document4];

await vectorStore.addDocuments(documents);
Inserting chunks
Inserting chunk 0
Chunks inserted!

从向量存储中删除项目

await vectorStore.delete(["4"]);
Deleted 1 items from documents

查询向量存储

一旦您的向量存储被创建并添加了相关文档,您很可能希望在链或代理运行期间查询它。

直接查询

执行简单的相似性搜索可以按如下方式完成
const filter = [{ operator: "=", column: "language", value: "en" }]

const hybridSearchResults = await vectorStore.azionHybridSearch("biology", {kfts:2, kvector:1,
                                      filter:[{ operator: "=", column: "language", value: "en" }]});

console.log("Hybrid Search Results")
for (const doc of hybridSearchResults) {
  console.log(`${JSON.stringify(doc)}`);
}
Hybrid Search Results
[{"pageContent":"The Australian dingo is a unique species that plays a key role in the ecosystem","metadata":{"searchtype":"fulltextsearch"},"id":"6"},-0.25748711028997995]
[{"pageContent":"The powerhouse of the cell is the mitochondria","metadata":{"searchtype":"fulltextsearch"},"id":"16"},-0.31697985337654005]
[{"pageContent":"Australia s indigenous people have inhabited the continent for over 65,000 years","metadata":{"searchtype":"similarity"},"id":"3"},0.14822345972061157]
const similaritySearchResults = await vectorStore.azionSimilaritySearch("australia", {kvector:3, filter:[{ operator: "=", column: "topic", value: "history" }]});

console.log("Similarity Search Results")
for (const doc of similaritySearchResults) {
  console.log(`${JSON.stringify(doc)}`);
}
Similarity Search Results
[{"pageContent":"Australia s indigenous people have inhabited the continent for over 65,000 years","metadata":{"searchtype":"similarity"},"id":"3"},0.4486490488052368]

通过转换为检索器进行查询

您还可以将向量存储转换为检索器,以便在您的链中更轻松地使用。
const retriever = vectorStore.asRetriever({
  // Optional filter
  filter: filter,
  k: 2,
});
await retriever.invoke("biology");
[
  Document {
    pageContent: 'Australia s indigenous people have inhabited the continent for over 65,000 years',
    metadata: { searchtype: 'similarity' },
    id: '3'
  },
  Document {
    pageContent: 'Mitochondria are made out of lipids',
    metadata: { searchtype: 'similarity' },
    id: '18'
  }
]

用于检索增强生成的使用

有关如何将此向量存储用于检索增强生成 (RAG) 的指南,请参阅以下部分

API 参考

有关所有 AzionVectorStore 功能和配置的详细文档,请查阅 API 参考
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.