Korvus 使用教程

Korvus 使用教程

korvus Korvus is a search SDK that unifies the entire RAG pipeline in a single database query. Built on top of Postgres with bindings for Python, JavaScript, Rust and C. korvus 项目地址: https://gitcode.com/gh_mirrors/kor/korvus

1. 项目介绍

Korvus 是一个开源的搜索软件开发工具包(SDK),旨在通过单一数据库查询统一整个 RAG(Retrieval-Augmented Generation)管道。基于 PostgreSQL,Korvus 提供了 Python、JavaScript、Rust 和 C 语言的绑定,实现了高性能、可定制的搜索功能,同时最小化了基础设施的复杂性。

2. 项目快速启动

环境准备

在使用 Korvus 之前,您需要一个安装了 pgml 和 pgvector 扩展的 PostgreSQL 数据库。您可以选择以下两种方式之一:

  • 自托管:自行设置数据库并安装 pgml 和 pgvector。具体步骤请参考官方的自托管指南。
  • 托管服务:使用预装了 pgml 和 pgvector 的托管 PostgreSQL 服务。

安装 Korvus

通过以下命令安装 Korvus:

pip install korvus

初始化数据库连接

设置环境变量 KORVUS_DATABASE_URL 为您的数据库连接字符串:

export KORVUS_DATABASE_URL="your_database_connection_string"

创建集合和管道

在 Python 中创建一个集合和管道:

from korvus import Collection, Pipeline
import asyncio

collection = Collection("korvus-demo-v0")
pipeline = Pipeline(
    "v1",
    {
        "text": {
            "splitter": {
                "model": "recursive_character"
            },
            "semantic_search": {
                "model": "Alibaba-NLP/gte-base-en-v1.5"
            }
        }
    }
)

async def add_pipeline():
    await collection.add_pipeline(pipeline)

asyncio.run(add_pipeline())

插入文档

插入文档到集合中:

async def upsert_documents():
    documents = [
        {"id": "1", "text": "Korvus 是非常快速且易于使用的。"},
        {"id": "2", "text": "番茄在汉堡上非常好。"},
    ]
    await collection.upsert_documents(documents)

asyncio.run(upsert_documents())

执行 RAG 查询

执行一个 RAG 查询:

async def rag():
    query = "Korvus 快吗?"
    print(f"查询响应:
{query}
")
    results = await collection.rag(
        {
            "CONTEXT": {
                "vector_search": {
                    "query": {
                        "fields": {
                            "text": {
                                "query": query
                            }
                        }
                    },
                    "document": {
                        "keys": ["id"]
                    },
                    "limit": 1
                },
                "aggregate": {
                    "join": "\n"
                }
            },
            "chat": {
                "model": "meta-llama/Meta-Llama-3-8B-Instruct",
                "messages": [
                    {
                        "role": "system",
                        "content": "你是一个友好且乐于助人的聊天机器人"
                    },
                    {
                        "role": "user",
                        "content": f"给定上下文
:{{CONTEXT}}
\n
回答问题:
{query}
"
                    }
                ],
                "max_tokens": 100
            }
        },
        pipeline
    )
    print(results)

asyncio.run(rag())

3. 应用案例和最佳实践

  • 单次查询优化:尽可能在一个查询中完成所有操作,减少数据库往返次数,提高性能。
  • 文档嵌入:使用高效的嵌入模型来提高搜索的相关性和速度。
  • 管道管理:合理管理和配置管道,以适应不同的搜索场景和需求。

4. 典型生态项目

  • PostgresML:一个基于 PostgreSQL 的机器学习平台,提供了 pgml 扩展,用于在 PostgreSQL 中进行机器学习操作。
  • pgvector:一个用于向量化搜索的 PostgreSQL 扩展,与 Korvus 密切配合,提供高效的搜索能力。

以上就是关于 Korvus 的使用教程,希望对您有所帮助。

korvus Korvus is a search SDK that unifies the entire RAG pipeline in a single database query. Built on top of Postgres with bindings for Python, JavaScript, Rust and C. korvus 项目地址: https://gitcode.com/gh_mirrors/kor/korvus

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯海莎Eliot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值