Quivr RAG Framework

repository·main·Indexed 12 days ago

https://github.com/quivrhq/quivr

An opinionated Retrieval-Augmented Generation (RAG) framework designed as a 'second brain'. It allows developers to ingest various file types and interact with them using LLMs such as OpenAI, Anthropic, or local models via Ollama. The quivr-core package (v0.0.33) provides the RAG engine, featuring a Brain class for knowledge management, YAML-based workflow configuration, and a plugin architecture for custom data processors.

Tokens
11.3K
Snippets
37
Records
62
Agent score
98%

What's inside Quivr

  1. Overview of Quivr-Whisper

    main

    Quivr-Whisper is a web application designed for audio-based interaction with Quivr. It enables users to ask questions via audio input, which is then processed through the following workflow:

    1. Speech Transcription: Uses OpenAI's Whisper model to convert audio input into text.
    2. Intelligent Response: Queries the Quivr API using the transcribed text to retrieve an answer.
    3. Speech Synthesis: Uses OpenAI's text-to-speech capabilities to convert the Quivr response back into audio for playback.

    Key features include audio input, Whisper-based transcription, Quivr API integration, and automated speech synthesis.

  2. Overview of Quivr core features

    main

    Quivr is an opinionated RAG (Retrieval-Augmented Generation) framework designed to act as a 'second brain'. Key capabilities include:

    • LLM Agnostic: Works with OpenAI, Anthropic, Mistral, Gemma, and other providers.
    • Flexible Ingestion: Supports PDF, TXT, Markdown, and custom parsers.
    • Customizable RAG: Supports adding internet search and external tools.
    • Megaparse Integration: Can ingest files using Megaparse for enhanced parsing before using them with Quivr.
  3. What is a Brain in Quivr?

    main

    A Brain is the essential abstraction in Quivr for knowledge management. It acts as a container that:

    1. Stores knowledge extracted from uploaded files.
    2. Processes that knowledge using embedding models.
    3. Retrieves information via RAG (Retrieval-Augmented Generation) workflows to answer user queries.

    It serves as the interface between your raw data (files) and the generative AI models used to interact with that data.

  4. Manage conversation memory with ChatHistory

    main

    The ChatHistory class manages the storage of all interactions between a user and the LLM. In Quivr, a ChatHistory object is automatically instantiated within a Brain instance upon creation.

    Key behaviors:

    • Automatic Updates: Every time you call Brain.ask_streaming, both your input message and the LLM's response are appended to the history.
    • RAG Context: This history acts as a memory mechanism for the Retrieval-Augmented Generation (RAG) process, providing the LLM with context from previous turns in the conversation to improve response quality.
    • Inspection: You can inspect the state of the brain, including the current chat history and the total number of chats stored, by using the print_info() method.
    # Conceptual usage pattern
    # A ChatHistory object is created automatically when you instantiate a Brain
    from quivr_core.brain import Brain
    
    brain = Brain()
    
    # Interactions are automatically recorded in ChatHistory
    # via the ask_streaming method
    response = brain.ask_streaming("Hello, what is my second brain?")
    
    # Inspect the history and brain state
    brain.print_info()
  5. How the Quivr Chatbot works

    main

    The chatbot architecture combines Quivr and Chainlit:

    • Quivr: Acts as the intelligence layer. It creates a "brain" from the uploaded text file, which is used to retrieve and process information to answer questions.
    • Chainlit: Acts as the interface layer. It manages the web UI, file uploads, and the chat interaction loop.
  6. How the Voice Chatbot workflow works

    main

    The Voice Chatbot follows a three-stage lifecycle to enable interactive voice queries against uploaded documents:

    1. File Upload & Brain Creation:

      • The user uploads a .txt file.
      • The file is saved to the uploads directory.
      • Quivr processes the file to create a "brain" (a context-aware knowledge base).
      • The brain is cached and associated with a unique session ID for subsequent queries.
    2. Voice Question Processing (Speech-to-Text):

      • The user records a question via the microphone.
      • The audio file is sent to the server.
      • OpenAI's Whisper model transcribes the audio into text.
    3. Answer Generation & Delivery (Text-to-Speech):

      • The transcribed text is queried against the session's "brain".
      • Quivr retrieves relevant information and generates a text response.
      • The text response is converted into audio using OpenAI's text-to-speech model.
      • The final audio is returned to the user as a Base64-encoded string.
  7. Understand the Storage abstraction in quivr-core

    main

    In quivr-core, the Storage class acts as the file management system for a Brain. Every Brain holds a reference to a storage system that manages QuivrFile objects.

    Key Lifecycle & Behaviors:

    • Brain-Storage Connection: When you add files to a Brain, they are uploaded to the storage before any processing occurs.
    • File Management: The storage system is responsible for the lifecycle of files, including uploading, retrieving lists of files, and deleting files via their unique file IDs.
    • Extensibility: You can implement custom storage logic by subclassing the StorageBase class and passing your implementation to the Brain.
  8. How the Chatbot workflow works

    main

    The chatbot follows a specific lifecycle managed by Chainlit and Quivr:

    1. Chat Start (Initialization)

    • The system waits for a .txt file upload.
    • Upon upload, the file is processed by Quivr to create a "brain" (a context-aware knowledge base).
    • The user is notified once the "brain" is ready.

    2. On User Message (Querying)

    • The system retrieves the active "brain" from the current session.
    • The user's question is processed using Quivr's ask_streaming method.
    • Responses are streamed incrementally to the UI.
    • Relevant file excerpts (sources) are extracted and displayed alongside the answer.
  9. Use TransparentStorage for in-memory file management

    main
    TransparentStorage is a lightweight, flexible implementation designed for temporary file management. It manages files primarily in memory and does not require local file paths. This is ideal for use cases where persistent storage is not required and you only need to store and retrieve files during the Brain's immediate operation.
  10. How the Quivr Chatbot example works

    main

    The chatbot architecture relies on two main components:

    1. Quivr: Acts as the intelligence layer. It creates a "brain" from the uploaded text file, which serves as the knowledge base for answering questions.
    2. Chainlit: Provides the user interface (UI) layer, managing the web-based chat interactions and file upload handling.
  11. Understand the Voice Chatbot workflow

    main

    The Voice Chatbot operates through a specific lifecycle managed by Chainlit and Quivr:

    Chat Initialization

    1. The system waits for a .txt file upload.
    2. The file is processed into a Quivr "brain".
    3. The user is notified when the brain is ready.

    Message Processing

    1. Context Retrieval: The system extracts the active "brain" and queries it using the user's message.
    2. Streaming: Responses are streamed incrementally to the user for real-time feedback.
    3. Source Attribution: The chatbot displays relevant file sources used to generate the response.

    Audio Processing Loop

    1. Audio chunks are captured during user input.
    2. STT: Captured audio is converted to text via the OpenAI Whisper API.
    3. Query: The transcribed text is used to query the brain.
    4. TTS: The resulting text response is converted to audio for playback.