JackMatthewRimmer / rust-rag-toolchain

Library for doing RAG
MIT License
22 stars 2 forks source link

Rust RAG Toolchain

Overview

rust-rag-toolchain is a Rust native library designed to empower developers with seamless access to common Retrieval Augmented Generation (RAG) workflows. It provides functionalities for embedding generation, storage, and retrieval, allowing developers to build AI applications with their own data.

Features

Getting Started

Prerequisites

Installation

Add the latest version of rust-rag-toolchain to your Cargo.toml file.

Structure

The library is structured into several modules, each responsible for a specific functionality:

Code Examples


    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());
    }

Contributing

Contributions are welcome! If you have ideas for improvements or find any issues, please open an issue or submit a pull request.

Support

For any questions or assistance, feel free to contact the maintainers:

Please raise an issue and I will get back to you quickly

Enjoy leveraging the power of retrieval augmented generation and embedding generation with rust-rag-toolchain!