7 个版本
0.1.7 | 2024 年 8 月 5 日 |
---|---|
0.1.6 | 2024 年 7 月 4 日 |
0.1.5 | 2024 年 5 月 14 日 |
0.1.3 | 2024 年 4 月 20 日 |
0.1.1 | 2024 年 3 月 1 日 |
#108 in 机器学习
每月 125 次下载
145KB
2.5K SLoC
Rust RAG 工具链
概述
rust-rag-toolchain
是一个旨在赋予开发者无缝访问常见检索增强生成 (RAG) 工作流的 Rust 原生库。它提供了嵌入生成、存储和检索的功能,允许开发者使用自己的数据构建 AI 应用程序。
功能
-
检索增强生成 (RAG): 通过基于您的知识库提供更好的提示补全来增强您的 AI 应用程序。参见 BasicRagChain !
-
PG 矢量支持 能够存储和检索启用 pg_vector 的 postgres 数据库!
-
OpenAI 支持 能够通过 OpenAI 生成聊天补全和嵌入,以用于您的 AI 工作流程
-
令牌分块 能够根据令牌大小对文本进行分块。
入门指南
先决条件
- 已安装 Rust 编程语言。有关安装说明,请访问 rust-lang.org。您还需要一个异步运行时;我们推荐 Tokio。
安装
将 rust-rag-toolchain
的最新版本添加到您的 Cargo.toml
文件中。
结构
该库分为几个模块,每个模块负责特定的功能
-
chains
: 此模块包含 RAG 工作流的实现。 -
chunkers
: 此模块提供将数据分解为可管理块的工具。 -
clients
:此模块包含与各种服务交互的客户端实现。 -
common
:此模块包含跨库使用的通用实用工具和辅助函数。 -
loaders
:此模块提供加载数据和处理数据的功能。 -
retrievers
:此模块包含从各种来源检索数据的实现。 -
stores
:此模块提供存储和管理数据的功能。
代码示例
const SYSTEM_MESSAGE: &'static str =
"You are to give straight forward answers using the supporting information you are provided";
#[tokio::main]
async fn main() {
// Initialize the PostgresVectorStore
let store: PostgresVectorStore =
PostgresVectorStore::try_new("embeddings", TextEmbeddingAda002)
.await
.unwrap();
// Create a new embedding client
let embedding_client: OpenAIEmbeddingClient =
OpenAIEmbeddingClient::try_new(TextEmbeddingAda002).unwrap();
// Convert our store into a retriever
let retriever: PostgresVectorRetriever<OpenAIEmbeddingClient> =
store.as_retriever(embedding_client, DistanceFunction::Cosine);
// Create a new chat client
let chat_client: OpenAIChatCompletionClient =
OpenAIChatCompletionClient::try_new(Gpt3Point5Turbo).unwrap();
// Define our system prompt
let system_prompt: PromptMessage = PromptMessage::SystemMessage(SYSTEM_MESSAGE.into());
// Create a new BasicRAGChain with over our open ai chat client and postgres vector retriever
let chain: BasicRAGChain<OpenAIChatCompletionClient, PostgresVectorRetriever<_>> =
BasicRAGChain::builder()
.system_prompt(system_prompt)
.chat_client(chat_client)
.retriever(retriever)
.build();
// Define our user prompt
let user_message: PromptMessage =
PromptMessage::HumanMessage("what kind of alcohol does Morwenna drink".into());
// Invoke the chain. Under the hood this will retrieve some similar text from
// the retriever and then use the chat client to generate a response.
let response = chain
.invoke_chain(user_message, NonZeroU32::new(2).unwrap())
.await
.unwrap();
println!("{}", response.content());
}
贡献
欢迎贡献!如果您有改进的想法或发现任何问题,请打开一个问题或提交一个pull请求。
支持
对于任何问题或帮助,请随时联系维护者
请提出一个问题,我将尽快回复您
享受使用rust-rag-toolchain
利用检索增强生成和嵌入生成的力量!
依赖项
~27–42MB
~534K SLoC