dot Redis 8 已发布,它是开源的

了解更多

使用 Superlinked 和 Redis 构建 GenAI 应用

使用 Superlinked 和 Redis 构建更智能、更快速的 GenAI 应用

众所周知,GenAI 应用可以提升运营效率、增强客户体验并降低成本。但许多企业技术团队却停留在概念验证 (POC) 阶段,难以将这些高性能应用推向生产环境。

这正是 Redis 极速向量数据库和 Superlinked 向量计算框架大显身手的地方。对于复杂数据,这可能正是您一直在寻找的解决方案。

克服 GenAI 推荐系统的挑战

实际数据很复杂,而大型语言模型 (LLM) 在理解和处理价格、日期、产品类别和流行度指标等结构化数据类型方面表现极差。这些数据类型对于跨多种用例实现高质量的信息检索至关重要,尤其是在推荐系统和个性化语义搜索中。

仅使用非结构化数据的推荐系统在小规模应用时可能表现良好,但在生产环境中往往性能不佳。客户希望获得与他们近期行为、类别和价格范围相匹配的快速推荐。为了实现这一点,技术团队通常会开发和训练自定义嵌入和重新排序模型,这对大多数企业技术团队来说是一项具有挑战性的工作。这就是为什么大多数 GenAI 驱动的推荐系统会停留在 POC 阶段,并且很少兑现其承诺。

Build GenAI apps with Superlinked and Redis

Redis 和 Superlinked 是一个成功的组合 

Superlinked 的向量计算框架让数据科学团队能够创建自定义向量嵌入,将结构化和非结构化数据结合在一个统一的向量空间中。这与用户洞察相符,并能在享受预训练模型便利的同时,实现自定义模型的性能

Redis 极速的向量数据库基于内存存储,非常适合需要快速响应的面向客户的用例,例如实时推荐系统。用户更能接受与其当前兴趣相符的推荐,而不是在他们已经离开之后才收到的推荐。

Superlinked 的框架还简化了生产部署流程,节省了数据和机器学习工程资源的时间。数据科学家可以在 Python notebook 中构建和测试他们的解决方案,并将其直接编译到云服务。这种从开发到部署的无缝过渡加快了产品上市时间,并确保了强大、可扩展的解决方案。

查看这家大型电商平台如何实现 4 倍 AOV 增长

一家大型电商平台面临产品推荐互动率低的问题。问题源于其不断变化的产品目录以及依赖静态、非个性化的推荐方法,导致错失收入机会。

利用 Superlinked 从结构化和非结构化产品数据生成向量嵌入,并利用 Redis 实时交付向量搜索结果,该客户在几周内就成功上线了一个实时个性化推荐系统。该系统能够秒级考虑用户的浏览行为,向用户呈现高度相关且及时的推荐,使得被推荐用户的平均订单价值 (AOV) 提升了 4 倍。

开始使用 Superlinked 和 Redis

下面是使用 Superlinked 构建您的第一个简单应用的逐步指南,该应用使用 Redis 作为向量数据库和搜索解决方案。这个语义搜索应用允许用户在产品评论数据库中执行自由文本搜索,并演示了如何将评论的非结构化文本与作为数值嵌入在同一向量空间中的产品星级评分相结合,从而获得更高质量和更相关的结果。

您可以尝试一个完整的应用示例,并一如既往地参考官方 README 获取最新详情。掌握此示例后,可以探索其他 notebooks,包括电商推荐系统的示例。

在 Python notebook 环境中进行实验

步骤 1:安装 Superlinked

%pip install superlinked

步骤 2:定义数据模式

# we are going to create 2 representations of the data
## 1. separate text and ranking for multimodal superlinked embeddings
## 2. full_review_as_text for LLM embedding of stringified review and rating
@schema

class Review:
    id: IdField
    review_text: String
    rating: Integer    
    full_review_as_text: String

步骤 3:创建空间以编码数据的不同部分

# Embed review data separately
review_text_space = TextSimilaritySpace(
    text=review.review_text, model="all-MiniLM-L6-v2")

rating_maximizer_space = NumberSpace(review.rating, min_value=1, 
    max_value=5, mode=Mode.MAXIMUM)

## Embed the full review as text
full_review_as_text_space = TextSimilaritySpace(
    text=review.full_review_as_text, model="all-MiniLM-L6-v2"

# Combine spaces as vector parts to an index.
## Create one for the stringified review 
naive_index = Index([full_review_as_text_space])

## and one for the structured multimodal embeddings
advanced_index = Index([review_text_space, rating_maximizer_space])

步骤 4:定义查询映射到每个索引类型

openai_config = OpenAIClientConfig(api_key=userdata.get("openai_api_key"),     model="gpt-4o")

# Define your query using dynamic parameters for query text and weights.
## first a query on the naive index - using natural language
naive_query = (
    Query(
        naive_index,
        weights={
            full_review_as_text_space: Param('full_review_as_text_weight')
        },
    )
    .find(review)
    .similar(full_review_as_text_space.text, Param("query_text"))
    .limit(Param('limit'))
    .with_natural_query(Param("natural_query"), openai_config)
)
## and another on the advanced multimodal index - also using natural language
superlinked_query = (
    Query(
        advanced_index,
        weights={
            review_text_space: Param('review_text_weight'),
            rating_maximizer_space: Param('rating_maximizer_weight'),
        },
    )
    .find(review)
    .similar(review_text_space.text, Param("query_text"))
    .limit(Param('limit'))
    .with_natural_query(Param("natural_query"), openai_config)
)

注意:Superlinked 支持两种方式设置查询不同部分的权重

  1. “自然语言查询”——使用 .with_natural_query——动态自动解析用户查询并设置权重
  2. 预定义权重——开发者可以根据业务逻辑或已知用户偏好为查询的每个部分设置权重

步骤 5:加载数据

# Run the app
source: InMemorySource = InMemorySource(review, parser=DataFrameParser(schema=review))
executor = InMemoryExecutor(sources=[source], indices=[naive_index, advanced_index]])
app = executor.run()

# Download dataset
data = pd.read_json("https://storage.googleapis.com/superlinked-preview-test-data/amazon_dataset_1000.jsonl",lines=True)

# Ingest data to the framework.
source.put([data])

步骤 6:使用不同的表示方式运行实验,直到对结果满意

# query that is based on the LLM embedded reviews
naive_positive_results = app.query(
    naive_query,
    natural_query='High rated quality products',
    limit=10)
naive_positive_results.to_pandas()


# query based on multimodal Superlinked embeddings
superlinked_positive_results = app.query(
    superlinked_query,
    natural_query='High rated quality products',
    limit=10)
superlinked_positive_results.to_pandas()

亲眼看看

请注意,使用自定义嵌入创建多模态向量如何提高结果质量。比较下表中查询“高质量高评分产品”(High rated quality products)的前三个结果,您将看到 LLM 如何“误解”了查询并在第三位检索出了一个明显错误的结果。

搜索“高质量高评分产品”
仅基于文本的结果基于文本和评分的结果
评论文本评分评论文本 评分
好产品 好产品
5
好产品 好产品
5
这笔交易太划算了!这个价格能买到这么多产品。有很多小样,但有些产品的价值超乎想象。我喜欢尝试很多我通常不会考虑的东西。
5
这笔交易太划算了!这个价格能买到这么多产品。有很多小样,但有些产品的价值超乎想象。我喜欢尝试很多我通常不会考虑的东西。5
便宜货 不好
1
好产品 效果很好5

准备部署了吗? 

步骤 7:安装 Superlinked 服务器(请参考服务器的 README 获取最新说明)

# Clone the repository
git clone https://github.com/superlinked/superlinked

cd <repo-directory>/server
./tools/init-venv.sh
cd runner
source "$(poetry env info --path)/bin/activate"
cd ..

# Make sure you have your docker engine running and activate the virtual environment
./tools/deploy.py up

步骤 8:连接到您的 Redis 实例(请参考 Redis 文档 获取说明)

from superlinked.framework.dsl.storage.redis_vector_database import RedisVectorDatabase

vector_database = RedisVectorDatabase(
# Your Redis URL without port or any extra fields 
    "<your_redis_host>",
# Port number, which should be an integer
    12315,     
# The params can be found here: https://redis.readthedocs.io/en/stable/connections.html 
    username="<username>",
    password="<password>"
)

步骤 9:配置 Superlinked 服务器 – 添加 notebook 中的配置(步骤 2、3)并附加部署设置

# Copy your configuration to app.py
# ...

# Create a data source to bulk load your production data.
config = DataLoaderConfig("https://storage.googleapis.com/superlinked-sample-datasets/amazon_dataset_ext_1000.jsonl", DataFormat.JSON, pandas_read_kwargs={"lines": True, "chunksize": 100})
source = DataLoaderSource(review, config)

executor = RestExecutor(
    # Add your data source 
    sources=[source],
    # Add the indices that contains your configuration 
    indices=[index],
    # Create a REST endpoint for your query.
    queries=[RestQuery(RestDescriptor("naive_query"), naive_query),      
            RestQuery(RestDescriptor("superlinked_query"), superlinked_query)],

    # Connect to Redis 
    vector_database=RedisVectorDatabase()
)

SuperlinkedRegistry.register(executor)

步骤 10:测试您的部署

# Trigger the data load 
curl -X POST 'http://localhost:8080/data-loader/review/run'

# Check the status of the loader 
curl -X GET 'http://localhost:8080/data-loader/review/status'

# Send your first query
curl -X POST \
    'http://localhost:8080/api/v1/search/superlinked_query' \
    --header 'Accept: */*' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "natural_query": "High rated quality products",


        "limit": 10
    }'

恭喜您。您已经学会了如何构建第一个将数值数据和非结构化数据结合在同一嵌入空间中以提供高质量结果的解决方案。我们很高兴看到您将使用 Superlinked 和 Redis 构建出令人惊叹的应用——请随时与我们分享您的成果

结论 

正如 Redis 云销售与合作关系高级副总裁 Ash Vijayakanthan 所言:“Redis 正与 Superlinked 合作,让向量数据库更容易应用于复杂数据,以支持诸如 LLMs 的 RAG(检索增强生成)和电商推荐系统等用例。”这是一种合作关系,有助于企业克服从概念验证到生产的障碍,并充分发挥其 GenAI 应用的潜力。

相关资源

解决方案

Redis 向量数据库

构建响应速度超出预期、包含复杂处理且延迟低的实时应用。

解决方案

Superlinked

Superlinked 是一个用于信息检索和特征工程系统的计算框架,专注于将复杂数据转换为向量嵌入。

解决方案

Superlinked 示例

用于向量驱动应用的计算工具。