向量搜索
GenAIScript 提供多种向量数据库以支持嵌入搜索和检索增强生成(RAG)。
// index creationconst index = await retrieval.index("animals")// indexingawait index.insertOrUpdate(env.files)// searchconst res = await index.search("cat dog")def("RAG", res)索引创建
retrieve.index 用于创建或加载现有索引。索引创建过程中需要设置一些选项这些选项在多次执行之间不应更改。
// index creationconst index = await retrieval.index("animals")本地索引
默认情况下,向量会使用基于vectra的本地向量数据库,存储在.genaiscript/vector文件夹下的本地文件中。嵌入计算使用的是embeddings model alias
embeddings 也可以通过选项进行配置。
const index = await retrieval.index("animals", { embeddingsModel: "ollama:nomic-embed-text",})默认情况下索引会被序列化。如果希望在每次执行时重置索引,请设置deleteIfExists: true。
Azure AI 搜索
GenAIScript 还支持使用 Azure AI Search 服务。 Azure AI Search 使用 简单查询语法。
const index = retrieval.index("animals", { type: "azure_ai_search" })要配置服务,您需要在.env文件中设置AZURE_AI_SEARCH_ENDPOINT
和AZURE_AI_SEARCH_API_KEY环境变量。
详情请参阅身份验证文档。
AZURE_AI_SEARCH_ENDPOINT=https://{{service-name}}.search.windows.net/AZURE_AI_SEARCH_API_KEY=...进一步的索引管理可以通过Azure门户完成。
模型和分块配置
嵌入计算是通过LLM API完成的,使用与LLM API相同的授权令牌。
const index = await retrieval.index("animals", { embeddingsModel: "ollama:all-minilm",})您还可以配置输入文件的分块方式。
可以通过设置chunkSize和chunkOverlap选项来更改此设置。
const index = await retrieval.index("animals", { chunkSize: 512, chunkOverlap: 0,})索引
index.insertOrUpdate 函数负责处理分块、向量化以及更新向量数据库。
// indexingawait index.insertOrUpdate(env.files)搜索
index.search 使用索引执行搜索(向量或混合)。
const hits = await retrieval.search("keyword")返回值是一个包含从匹配块重建内容的文件数组。
const hits = await retrieval.search("keyword")def("FILE", files)