视频
了解更多
Redis Vector Library 提供了一个简化的客户端,通过增强生成式 AI (GenAI) 应用开发来简化开发者体验。 Redis Enterprise 作为实时向量数据库,用于向量搜索、LLM 缓存和聊天历史。
利用生成式 AI (GenAI) 已成为许多技术人员的核心目标。自 ChatGPT 于 2022 年 11 月发布以来,各组织已为其团队应用 GenAI 设定了新的优先事项。期望很高: AI 可以创作书面语言、构建图像、回答问题和编写代码。但仍然存在一些障碍
人们开发了无数技术和工具来帮助缓解这些挑战。例如,检索增强生成 (RAG) 因能够将存储在向量数据库中的领域特定数据与大型语言模型 (LLM) 的强大功能相结合而备受关注。这种方法最初相对简单,现已演变为一套全面的策略,用于提高对话式 AI 的质量。这一演变反映了技术精细化方面的更广泛趋势,LangChain 发表的这篇引人入胜的 RAG 深度解析文章突显了这一点。
Redis 不仅仅是一个独立的用于 RAG 的向量数据库。它通过作为许多基本任务的实时数据层来提升 GenAI 应用性能:向量搜索、LLM 语义缓存和会话管理(例如,用户聊天历史)。我们一直在倾听客户、用户和社区成员的意见。我们希望让这一切变得更容易。为此,我们开发了 Redis Vector Library,它提供了一个简化的客户端,可以在 AI 驱动的任务中使用 Redis,特别是专注于用于搜索的向量嵌入。
Python Redis Vector Library (redisvl) 是基于知名的 redis-py 客户端构建的扩展。下面我们将通过一个简单的示例进行演示。或者,您可以尝试在 Google Colab 上进行此动手教程,该教程使用 redisvl 从头开始讲解 RAG。
确保您在 Python 3.8 或更高版本的环境中使用。然后,使用 pip 进行安装
pip install redisvl>=0.1.0
按照以下便捷方式之一部署 Redis
docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
redisvl 还附带一个名为 rvl 的专用 CLI 工具。您可以在文档中了解有关使用 CLI 的更多信息。
黑箱搜索应用很少能在生产环境中胜任工作。
Redis 通过允许您显式配置索引设置和数据集模式来优化生产搜索性能。使用 redisvl,定义、加载和管理自定义模式非常简单。
考虑一个由 10k 个 SEC 文件 PDF 组成的数据集,每个文件都被分解为可管理的文本块。此数据集中的每条记录都包含
首先,定义一个模式,该模式将此数据结构建模到名为 sec-filings 的索引中。为了方便起见,使用 YAML 文件
index:
name: sec-filings
prefix: chunk
fields:
- name: id
type: tag
attrs:
sortable: true
- name: content
type: text
attrs:
sortable: true
- name: company
type: tag
attrs:
sortable: true
- name: timestamp
type: numeric
attrs:
sortable: true
- name: content_embedding
type: vector
attrs:
dims: 1024
algorithm: hnsw
datatype: float32
distance_metric: cosine
schema.yaml 文件清晰、声明式地表达了模式。默认情况下,索引将使用 Hash 数据结构将数据存储在 Redis 中。JSON 也可用,并支持不同的字段类型。
现在,加载并验证此模式
from redisvl.schema import IndexSchema
schema = IndexSchema.from_yaml("schema.yaml")
现在,我们将通过将 Redis Python 客户端连接传递给 SearchIndex 来为我们的数据集创建索引
from redis import Redis
from redisvl.index import SearchIndex
# Establish a connection with Redis
client = Redis.from_url("redis://localhost:6379")
# Link the schema with our Redis client to create the search index
index = SearchIndex(schema, client)
# Create the index in Redis
index.create()
向量化模块提供对流行嵌入提供商(如 Cohere、OpenAI、VertexAI 和 HuggingFace)的访问,让您快速将文本转换为密集、语义向量。
以下是使用 Cohere 向量化器的一个示例,假设您已经安装了 cohere Python 库并在环境中设置了 COHERE_API_KEY
from redisvl.utils.vectorize import CohereTextVectorizer
# Instantiate the Cohere text vectorizer
co = CohereTextVectorizer()
# Generate an embedding for a single query
embedding = co.embed(
"How much debt is the company in?", input_type="search_query"
)
# Generate embeddings for multiple queries
embeddings = co.embed_many([
"How much debt is the company in?",
"What do revenue projections look like?"
], input_type="search_query")
在此专用集成指南中了解更多关于 Redis 和 Cohere 的协作信息!
在查询之前,使用向量化器创建文本嵌入并使用您的数据填充索引。如果您的数据集是字典对象的集合,.load() 方法可以简化插入。它批量执行 upsert 操作,高效地将您的数据存储在 Redis 中,并返回每条记录的键
# Example dataset as a list of dictionaries
data = [
{
"id": "doc1",
"content": "raw SEC filing text content",
"company": "nike",
"timestamp": 20230101,
"content_embedding": co.embed(
"raw SEC filing text content", input_type="search_document", as_buffer=True
)
},
# More records...
]
# Insert data into the index
keys = index.load(data)
VectorQuery 是一个简单的抽象,用于执行带有可选过滤器的 KNN/ANN 风格向量搜索。
假设您想找到与用户查询(例如 "这家公司负债多少?")语义上最相关的 5 个 PDF 块。首先,使用文本嵌入模型将查询转换为向量(请参阅下面关于向量化器的部分)。接下来,定义并执行查询
from redisvl.query import VectorQuery
query = "How much debt is the company in?"
query_vector = co.embed(query, input_type="search_query", as_buffer=True)
query = VectorQuery(
vector=query_vector,
vector_field_name="content_embedding",
num_results=5
)
results = index.query(query)
为了进一步优化搜索结果,您可以应用各种元数据过滤器。例如,如果您对与“Nike”特别相关的文档感兴趣,可以在 company 字段上使用 Tag 过滤器
from redisvl.query.filter import Tag
# Apply a filter for the company name
query.set_filter(Tag("company") == "nike")
# Execute the filtered query
results = index.query(query)
过滤器允许您将结构化数据(元数据)上的搜索与向量相似度结合起来,以提高检索精度。
VectorQuery 仅仅是起点。对于那些希望探索更高级查询技术和数据类型(文本、标签、数字、向量、地理位置)的用户,这份专用用户指南将帮助您入门。
redisvl 不仅简化了 Redis 中的向量搜索和查询操作;它还旨在展示实际用例和常见的 LLM 设计模式。
语义缓存旨在通过基于语义相似性缓存响应来提高与 LLM 交互的应用程序的效率。例如,当向应用程序呈现相似的用户查询时,可以使用先前缓存的响应,而不是再次通过模型处理查询,从而显著减少响应时间和 API 成本。
要做到这一点,使用 SemanticCache 接口。您可以按如下方式将用户查询和响应对存储在语义缓存中
from redisvl.extensions.llmcache import SemanticCache
# Set up the LLM cache
llmcache = SemanticCache(
name="llmcache", # underlying search index name
redis_url="redis://localhost:6379", # redis connection url string
distance_threshold=0.2 # semantic cache distance threshold
)
# Cache the question, answer, and arbitrary metadata
llmcache.store(
prompt="What is the capital city of France?",
response="Paris",
metadata={"city": "Paris", "country": "france"}
)
当收到新查询时,其嵌入将与语义缓存中的嵌入进行比较。如果找到足够相似的嵌入,则返回相应的缓存响应,从而绕过另一次昂贵的 LLM 计算。
# Check for a semantically similar result
question = "What actually is the capital of France?"
llmcache.check(prompt=question)[0]['response']
>>> 'Paris'
我们将很快添加更多抽象,包括 LLM 会话管理和 LLM 上下文访问控制模式。关注并 ⭐ redisvl GitHub 仓库以保持关注!
如果您一直跟随到这里,您会想看看我们的端到端 RAG 教程,该教程详细介绍了 PDF 数据准备(提取、分块、建模)、索引、搜索以及使用 LLM 进行问答的过程。
这个特定的用例围绕处理公共 10k 文件 PDF 并从中提取洞察,如上文介绍。它已针对 Google Colab 进行了优化,因此您无需担心依赖管理或环境设置!
我们希望您和我们一样对使用 Redis 构建实时 GenAI 应用感到兴奋。通过使用 pip 安装客户端来开始吧
pip install redisvl>=0.1.0
我们还提供以下额外资源,帮助您将学习提升到新的水平
资源 | 描述 | 链接 |
文档 | redisvl 的托管文档。 | https://redisvl.com |
GitHub | redisvl 的 GitHub 仓库。 | https://github.com/RedisVentures/redisvl |
教程 | 使用 redisvl 从零开始构建 RAG 管道的逐步指南。 | https://github.com/redis-developer/financial-vss |
应用 | 一个端到端应用,展示 Redis 作为向量数据库在具有多种嵌入模型的文档检索应用中的使用。 | https://github.com/redis-developer/redis-arXiv-search |
与Redis 专家联系以了解更多信息。下载 Redis 向量搜索速查表并注册参加我们将于 2024 年 2 月 29 日与 LangChain 联合举办的网络研讨会!
敬请关注我们 AI 生态系统集成、客户端和开发者创新方面的更多更新。