Skip to main content

AzionVectorStore

AzionVectorStore 用于使用向量嵌入管理和搜索文档集合,直接在 Azion 的边缘平台上使用 Edge SQL 进行操作。

¥The AzionVectorStore is used to manage and search through a collection of documents using vector embeddings, directly on Azion’s Edge Plataform using Edge SQL.

本指南提供了 Azion EdgeSQL 向量存储 入门的快速概述。有关 AzionVectorStore 所有功能和配置的详细文档,请参阅 API 参考

¥This guide provides a quick overview for getting started with Azion EdgeSQL vector stores. For detailed documentation of all AzionVectorStore features and configurations head to the API reference.

概述

¥Overview

集成详情

¥Integration details

Class Package [PY support] Package latest
AzionVectorStore @langchain/community NPM - Version

设置

¥Setup

要使用 AzionVectorStore 向量存储,你需要安装 @langchain/community 包。此外,你还需要一个 Azion 账户 和一个 令牌 来使用 Azion API,并将其配置为环境变量 AZION_TOKEN。更多信息请参阅 文档

¥To use the AzionVectorStore vector store, you will need to install the @langchain/community package. Besides that, you will need an Azion account and a Token to use the Azion API, configuring it as environment variable AZION_TOKEN. Further information about this can be found in the Documentation.

本指南还将使用 OpenAI 嵌入,这需要你安装 @langchain/openai 集成包。如果你愿意,也可以使用 其他支持的嵌入模型

¥This guide will also use OpenAI embeddings, which require you to install the @langchain/openai integration package. You can also use other supported embeddings models if you wish.

yarn add azion @langchain/openai @langchain/community

凭证

¥Credentials

完成此操作后,请设置 AZION_TOKEN 环境变量:

¥Once you’ve done this set the AZION_TOKEN environment variable:

process.env.AZION_TOKEN = "your-api-key";

如果你在本指南中使用 OpenAI 嵌入,则还需要设置你的 OpenAI 密钥:

¥If you are using OpenAI embeddings for this guide, you’ll need to set your OpenAI key as well:

process.env.OPENAI_API_KEY = "YOUR_API_KEY";

如果你想自动追踪模型调用,也可以通过取消注释以下内容来设置你的 LangSmith API 密钥:

¥If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"

实例化

¥Instantiation

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" })

管理向量存储

¥Manage vector store

将项目添加到向量存储

¥Add items to vector store

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!

从向量存储中删除项目

¥Delete items from vector store

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

查询向量存储

¥Query vector store

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

¥Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.

直接查询

¥Query directly

执行简单的相似性搜索可以按如下方式完成:

¥Performing a simple similarity search can be done as follows:

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]

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

¥Query by turning into retriever

你还可以将向量存储转换为 retriever,以便在你的链中更轻松地使用。

¥You can also transform the vector store into a retriever for easier usage in your chains.

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'
}
]

检索增强生成用法

¥Usage for retrieval-augmented generation

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

¥For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

API 参考

¥API reference

有关所有 AzionVectorStore 功能和配置的详细文档,请前往 API 参考

¥For detailed documentation of all AzionVectorStore features and configurations head to the API reference.