歡迎來到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的支持,以真誠的努力為基礎。要報告錯誤,請求功能或接收幫助,請提出問題。