102 使用Weaviate向量存储实现混合搜索

使用Weaviate向量存储实现混合搜索

欢迎来到这篇技术博客!今天我们将探讨如何使用Weaviate向量存储和LlamaIndex实现混合搜索。如果你在Colab上打开这个Notebook,你可能需要安装LlamaIndex。

%pip install llama-index-vector-stores-weaviate
!pip install llama-index

设置日志记录

首先,我们设置日志记录以便更好地调试和查看输出。

import logging
import sys

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

创建Weaviate客户端

接下来,我们需要创建一个Weaviate客户端。我们将使用OpenAI的API密钥,并连接到Weaviate集群。

import os
import openai

os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
openai.api_key = os.environ["OPENAI_API_KEY"]

import weaviate

# 连接到云实例
cluster_url = "your_weaviate_cluster_url"
api_key = "your_weaviate_api_key"

client = weaviate.connect_to_wcs(
    cluster_url=cluster_url,
    auth_credentials=weaviate.auth.AuthApiKey(api_key),
)

# 连接到本地实例
# client = weaviate.connect_to_local()

下载数据

我们将下载一些示例数据,这里以Paul Graham的散文为例。

!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'

加载文档

使用LlamaIndex的SimpleDirectoryReader加载文档。

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# 加载文档
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()

构建向量存储索引

我们将使用WeaviateVectorStore来构建向量存储索引。

from llama_index.vector_stores.weaviate import WeaviateVectorStore
from llama_index.core import StorageContext

vector_store = WeaviateVectorStore(weaviate_client=client)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)

# 注意:你也可以选择手动定义一个index_name。
# index_name = "test_prefix"
# vector_store = WeaviateVectorStore(weaviate_client=client, index_name=index_name)

使用默认向量搜索查询索引

我们可以使用默认的向量搜索来查询索引。

# 设置日志级别为DEBUG以获取更详细的输出
query_engine = index.as_query_engine(similarity_top_k=2)
response = query_engine.query("What did the author do growing up?")
display_response(response)

使用混合搜索查询索引

Weaviate支持混合搜索,结合了bm25和向量搜索。alpha参数决定了权重的分配(alpha = 0 -> bm25,alpha = 1 -> 向量搜索)。默认情况下,alpha=0.75(非常接近向量搜索)。

# 设置日志级别为DEBUG以获取更详细的输出
query_engine = index.as_query_engine(
    vector_store_query_mode="hybrid", similarity_top_k=2
)
response = query_engine.query(
    "What did the author do growing up?",
)
display_response(response)

设置alpha=0.0以偏向bm25

我们可以将alpha设置为0.0,以偏向bm25搜索。

# 设置日志级别为DEBUG以获取更详细的输出
query_engine = index.as_query_engine(
    vector_store_query_mode="hybrid", similarity_top_k=2, alpha=0.0
)
response = query_engine.query(
    "What did the author do growing up?",
)
display_response(response)

总结

通过这篇博客,我们学习了如何使用Weaviate向量存储和LlamaIndex实现混合搜索。我们从创建Weaviate客户端开始,下载并加载文档,构建向量存储索引,并使用默认向量搜索和混合搜索进行查询。希望这篇博客对你有所帮助,让你更好地理解和应用这些技术。

如果你有任何问题或需要进一步的帮助,请随时在评论区留言。感谢阅读!

### Weaviate 安装教程与使用指南 #### 安装 Weaviate 及其 Python 客户端 为了开始使用 Weaviate,首先需要安装 Weaviate 和相应的 Python 客户端。通过以下命令可以完成这些软件包的安装: ```bash %pip install weaviate-client !pip install llama-index-vector-stores-weaviate !pip install llama-index ``` 上述命令不仅会安装 `weaviate-client` 库[^2],还会安装用于构建向量存储索引和支持混合搜索所需的相关工具[^3]。 #### 设置 `setup.py` 文件 对于希望打包并分发自己基于 Weaviate 开发的应用程序或库的人来说,在项目根目录下创建一个名为 `setup.py` 的文件是非常重要的。此文件应包含如下所示的内容来指定项目的元数据以及依赖项: ```python from setuptools import setup, find_packages setup( name='your_project_name', version='0.1.0', packages=find_packages(), install_requires=[ 'weaviate-client>=4.7.1', 'llama-index-vector-stores-weaviate' ], ) ``` 这段代码片段展示了如何设置 `name`, `version`, `packages` 和 `install_requires` 字段以适应大多数情况下的需求。 #### 初始化 Weaviate 客户端 一旦所有必要的组件都已就绪,则可以在应用程序中初始化 Weaviate 客户端实例以便与其交互。下面是一个简单的例子说明了这一点: ```python import weaviate client = weaviate.Client('http://localhost:8080') print(client.is_ready()) ``` 这里假设本地运行着一个 Weaviate 实例;如果不是这种情况,请替换 URL 为实际的服务地址[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要重新演唱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值