欢迎来到Redis Vector Library - 最终的Python客户端,专为AI应用程序而设计,以利用Redis的力量。
redisvl是您的首选工具:
使用pip安装redisvl (> = 3.8)环境中:
pip install redisvl有关更多详细说明,请访问“安装指南”。
从多个REDIS部署选项中进行选择:
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest通过免费的Redis Insight GUI增强您的经验和可观察性。
为您的用例设计一个模式,该架构使用内置的Redis和可索引字段(例如文本,标签,数字,GEO和向量)建模数据集。从yaml文件加载模式:
index :
name : user-idx
prefix : user
storage_type : json
fields :
- name : user
type : tag
- name : credit_score
type : tag
- name : embedding
type : vector
attrs :
algorithm : flat
dims : 4
distance_metric : cosine
datatype : float32 from redisvl . schema import IndexSchema
schema = IndexSchema . from_yaml ( "schemas/schema.yaml" )或直接从Python词典加载:
schema = IndexSchema . from_dict ({
"index" : {
"name" : "user-idx" ,
"prefix" : "user" ,
"storage_type" : "json"
},
"fields" : [
{ "name" : "user" , "type" : "tag" },
{ "name" : "credit_score" , "type" : "tag" },
{
"name" : "embedding" ,
"type" : "vector" ,
"attrs" : {
"algorithm" : "flat" ,
"datatype" : "float32" ,
"dims" : 4 ,
"distance_metric" : "cosine"
}
}
]
})使用输入架构和客户端连接创建搜索index类,以便在redis中的索引上执行管理和搜索操作:
from redis import Redis
from redisvl . index import SearchIndex
# Establish Redis connection and define index
client = Redis . from_url ( "redis://localhost:6379" )
index = SearchIndex ( schema , client )
# Create the index in Redis
index . create ()异步符合搜索索引类也可用:AsyncSearchIndex。
加载并从您的redis实例中获取数据:
data = { "user" : "john" , "credit_score" : "high" , "embedding" : [ 0.23 , 0.49 , - 0.18 , 0.95 ]}
# load list of dictionaries, specify the "id" field
index . load ([ data ], id_field = "user" )
# fetch by "id"
john = index . fetch ( "john" )定义查询并在您的索引上执行高级搜索,包括向量,元数据过滤器等的组合。
矢量图 - 具有可自定义过滤器的灵活矢量查询启用语义搜索:
from redisvl . query import VectorQuery
query = VectorQuery (
vector = [ 0.16 , - 0.34 , 0.98 , 0.23 ],
vector_field_name = "embedding" ,
num_results = 3
)
# run the vector search query against the embedding field
results = index . query ( query )将复杂的元数据过滤器纳入您的查询:
from redisvl . query . filter import Tag
# define a tag match filter
tag_filter = Tag ( "user" ) == "john"
# update query definition
query . set_filter ( tag_filter )
# execute query
results = index . query ( query )rangequery-在定义的范围内与可自定义过滤器配对的矢量搜索
FilterQuery-使用过滤器和全文搜索的标准搜索
CountQuery-计数给定属性的索引记录的数量
阅读有关构建高级Redis查询的更多信息。
与流行的嵌入提供商集成,以极大地简化索引和查询的非结构化数据的过程:
from redisvl . utils . vectorize import CohereTextVectorizer
# set COHERE_API_KEY in your environment
co = CohereTextVectorizer ()
embedding = co . embed (
text = "What is the capital city of France?" ,
input_type = "search_query"
)
embeddings = co . embed_many (
texts = [ "my document chunk content" , "my other document chunk content" ],
input_type = "search_document"
)了解有关在嵌入工作流中使用矢量器的更多信息。
与流行的重读提供商集成,以改善Redis的初始搜索结果的相关性
我们很高兴地宣布对重新配置扩展的支持。这些模块实现了界面,以公开最佳实践和设计模式,以使用LLM内存和代理。我们从从用户(就是您)和出血的客户那里学到的东西中汲取了最好的作用,并将其包装好了。
有另一个扩展名的想法吗?打开PR或通过[email protected]与我们联系。我们总是愿意接受反馈。
通过利用SemanticCache利用先前生成的知识来增加应用吞吐量并降低生产中使用LLM模型的成本。
from redisvl . extensions . llmcache import SemanticCache
# init cache with TTL and semantic distance threshold
llmcache = SemanticCache (
name = "llmcache" ,
ttl = 360 ,
redis_url = "redis://localhost:6379" ,
distance_threshold = 0.1
)
# store user queries and LLM responses in the semantic cache
llmcache . store (
prompt = "What is the capital city of France?" ,
response = "Paris"
)
# quickly check the cache with a slightly different prompt (before invoking an LLM)
response = llmcache . check ( prompt = "What is France's capital city?" )
print ( response [ 0 ][ "response" ]) >>> Paris
了解有关LLMS语义缓存的更多信息。
通过提供用户聊天历史记录作为上下文来提高LLM响应的个性化和准确性。使用新的或相关性来管理对会话数据的访问,该访问由矢量搜索供应使用SemanticSessionManager 。
from redisvl . extensions . session_manager import SemanticSessionManager
session = SemanticSessionManager (
name = "my-session" ,
redis_url = "redis://localhost:6379" ,
distance_threshold = 0.7
)
session . add_messages ([
{ "role" : "user" , "content" : "hello, how are you?" },
{ "role" : "assistant" , "content" : "I'm doing fine, thanks." },
{ "role" : "user" , "content" : "what is the weather going to be today?" },
{ "role" : "assistant" , "content" : "I don't know" }
])获取最近的聊天历史记录:
session . get_recent ( top_k = 1 ) >>> [{"role": "assistant", "content": "I don't know"}]
获取相关的聊天历史记录(由向量搜索提供支持):
session . get_relevant ( "weather" , top_k = 1 ) >>> [{"role": "user", "content": "what is the weather going to be today?"}]
了解有关LLM会话管理的更多信息。
构建直接在Redis中运行的快速决策模型,并将用户查询到最近的“路由”或“主题”。
from redisvl . extensions . router import Route , SemanticRouter
routes = [
Route (
name = "greeting" ,
references = [ "hello" , "hi" ],
metadata = { "type" : "greeting" },
distance_threshold = 0.3 ,
),
Route (
name = "farewell" ,
references = [ "bye" , "goodbye" ],
metadata = { "type" : "farewell" },
distance_threshold = 0.3 ,
),
]
# build semantic router from routes
router = SemanticRouter (
name = "topic-router" ,
routes = routes ,
redis_url = "redis://localhost:6379" ,
)
router ( "Hi, good morning" ) >>> RouteMatch(name='greeting', distance=0.273891836405)
了解有关语义路由的更多信息。
从专用CLI接口: rvl创建,破坏和管理REDIS索引配置。
$ rvl -h
usage: rvl < command > [ < args > ]
Commands:
index Index manipulation (create, delete, etc.)
version Obtain the version of RedisVL
stats Obtain statistics about an index阅读有关使用CLI的更多信息。
在Genai时代,向量数据库和LLM正在转换信息检索系统。借助Langchain和Llamaindex等新兴和流行的框架,创新很快。然而,许多组织面临快速和大规模提供AI解决方案的挑战。
输入Redis - NOSQL世界的基石,以其多功能数据结构和处理引擎而闻名。 Redis在实时工作负载中出色,例如缓存,会话管理和搜索。作为抹布,LLM缓存的矢量数据库,也是对话式AI的聊天会话记忆存储。
REDIS矢量库弥合了AI-NATIANIT开发人员生态系统与Redis强大功能之间的差距。 Redisvl具有轻巧,优雅和直观的界面,可轻松利用Redis的力量。 redisvl建立在Redis Python客户端的基础上,将Redis的功能转化为一种与当今AI/ML工程师和数据科学家的需求完全一致的语法。
有关其他帮助,请查看以下资源:
请通过贡献PR,为错误或新功能想法开辟GitHub问题,改进文档或增加测试覆盖范围来帮助我们。阅读有关如何贡献的更多信息!
该项目得到了Redis,Inc的支持,以真诚的努力为基础。要报告错误,请求功能或接收帮助,请提出问题。