以编程方式使用文档

>Valkey is an open source, high-performance key/value datastore that supports workloads such as caching, message queues, and can act as a primary database. Valkey can run as either a standalone daemon or in a cluster, with options for replication and high availability.

本页面介绍如何将 Valkey 向量存储与 Amazon ElastiCache for Valkey or Amazon MemoryDB for Valkey.

设置

安装所需依赖项:

    pip install "langchain-aws[valkey]"
    
    uv add langchain-aws --extra valkey
    

基本用法

使用 Bedrock 嵌入

from langchain_aws import BedrockEmbeddings
from langchain_aws.vectorstores import ValkeyVectorStore

# Initialize embeddings
embeddings = BedrockEmbeddings(
    model_id="amazon.titan-embed-text-v1",
    region_name="us-east-1"
)

# Create vector store from texts
vectorstore = ValkeyVectorStore.from_texts(
    texts=["Valkey is fast", "Valkey supports vector search"],
    embedding=embeddings,
    valkey_url="valkey://localhost:6379",
    index_name="my_index"
)

# Perform similarity search
results = vectorstore.similarity_search("fast database", k=2)
for doc in results:
    print(doc.page_content)

使用 Ollama 嵌入

from langchain_ollama import OllamaEmbeddings
from langchain_aws.vectorstores import ValkeyVectorStore

# Initialize Ollama embeddings
embeddings = OllamaEmbeddings(
    model="nomic-embed-text",
    base_url="http://localhost:11434"
)

# Create vector store
vectorstore = ValkeyVectorStore(
    embedding=embeddings,
    valkey_url="valkey://localhost:6379",
    index_name="my_index",
    vector_schema={
        "name": "content_vector",
        "algorithm": "FLAT",
        "dims": 768,  # nomic-embed-text dimension
        "distance_metric": "COSINE",
        "datatype": "FLOAT32",
    }
)

# Add texts
vectorstore.add_texts(
    texts=["Document 1", "Document 2"],
    metadatas=[{"source": "doc1"}, {"source": "doc2"}]
)

# Search
results = vectorstore.similarity_search("query", k=2)

连接 URL

ValkeyVectorStore 支持多种连接 URL 格式:

# Standalone
valkey_url = "valkey://localhost:6379"

# With authentication
valkey_url = "valkey://username:password@host:6379"

# SSL/TLS
valkey_url = "valkeyss://host:6379"

# SSL with authentication
valkey_url = "valkeyss://username:password@host:6379"

AWS ElastiCache for Valkey

from langchain_aws import BedrockEmbeddings
from langchain_aws.vectorstores import ValkeyVectorStore

embeddings = BedrockEmbeddings()

# Connect to ElastiCache cluster
vectorstore = ValkeyVectorStore(
    embedding=embeddings,
    valkey_url="valkeyss://my-cluster.cache.amazonaws.com:6379",
    index_name="my_index"
)

# Add documents
vectorstore.add_texts(
    texts=["Document 1", "Document 2"],
    metadatas=[{"source": "doc1"}, {"source": "doc2"}]
)

元数据过滤

from langchain_aws.vectorstores.valkey.filters import ValkeyTag, ValkeyNum

# Add documents with metadata
vectorstore.add_texts(
    texts=["AI article from 2024", "ML paper from 2023"],
    metadatas=[
        {"category": "ai", "year": 2024},
        {"category": "ml", "year": 2023}
    ]
)

# Search with filters
filter_expr = (ValkeyTag("category") == "ai") & (ValkeyNum("year") >= 2024)
results = vectorstore.similarity_search(
    "artificial intelligence",
    k=5,
    filter=str(filter_expr)
)

自定义向量架构

from langchain_aws.vectorstores import ValkeyVectorStore

vectorstore = ValkeyVectorStore(
    embedding=embeddings,
    valkey_url="valkey://localhost:6379",
    index_name="my_index",
    vector_schema={
        "name": "content_vector",
        "algorithm": "HNSW",  # or "FLAT"
        "dims": 1536,
        "distance_metric": "COSINE",  # or "L2", "IP"
        "datatype": "FLOAT32",
    }
)

API 参考

有关详细的 API 文档,请参阅 ValkeyVectorStore.