MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

ThinkRAG:本地知识库智能问答系统的深度解析

项目背景与痛点

在当前的信息爆炸时代,知识分散在各个地方,企业内部的知识库往往难以进行有效管理与利用。同时,大型语言模型的使用门槛较高,往往需要昂贵的云计算资源。ThinkRAG 的出现,正是为了填补这一空白,旨在为用户提供一个运行于本地设备上的检索增强生成系统。通过这一系统,用户可以在笔记本电脑上实现知识库的智能问答功能,从而降低使用成本并提升数据安全性,实现了知识库的自管理和高效利用。

核心技术揭秘

ThinkRAG 的核心在于将大型语言模型的检索增强生成技术(RAG)与本地化部署相结合,提供了一种实用且低成本的智能问答解决方案。系统依赖于清晰的组件拆解与优化的数据流处理,使得整体架构简洁高效。

ThinkRAG架构流程图

graph TD A[用户请求] --> B[获取本地知识库] B --> C[预处理文档] C --> D{"文档检索"} D -->|"匹配文档"| E[提取特征] D -->|"无匹配"| F[生成答案] E --> G[生成答案] G --> H[发送回答] F --> H

架构与实现原理:

  • 本地知识库获取:系统从本地保存的知识库开始,对各类文档进行抽样处理。
  • 文档预处理:对文档进行预处理,包括分词、去除停用词等,便于后续特征提取与检索。
  • 文档检索:基于预处理的文档,系统采用高效检索算法,识别出与用户提问最相关的文档片段。
  • 特征提取:从匹配的文档片段中提取关键特征,用于增强语言模型预测能力。
  • 答案生成:结合特征提取的结果,最终生成高质量的问答答案。

功能亮点与差异

相比同类竞品,ThinkRAG 拥有以下几项优势功能:

  1. 本地化与安全:无需依赖外部云服务,充分利用本地计算资源,确保知识库数据的安全。
  2. 易于部署与使用:ThinkRAG 通过优化代码库与简单化配置,大大降低了部署门槛。
  3. 高性能与低功耗:系统经过深度优化,能在典型笔记本硬件上流畅运行,既减少能耗又有效处理大型数据集。

应用场景与落地建议

对于那些对数据安全有高度要求或有限的云计算资源的用户,ThinkRAG 的确是一个绝佳的选择。这适用于多种行业,如:

  • 教育行业:帮助教师与学生快速找到电子教科书与资源中的关键信息。
  • 企业文档管理:方便员工查询企业内部的操作手册及其他文档,提高工作效率。
  • 个人知识管理:为个人用户提供一个本地化知识存储和查询工具,有效管理个人积累的知识。

使用时需注意,对本地知识库进行定期更新维护,优化索引以提升响应速度。

综合评价

ThinkRAG 在实现大型语言模型应用的本地化上作出了卓越贡献。其核心优势在于低功耗、高可用性和强大的本地安全性。不过,系统对知识库效率优化的高度依赖值得改进,尤其是在大型数据集场景下操作表现上,还有一定的提升空间。对于需要定制化解决方案的用户,ThinkRAG 已经搭建了一个良好的基础框架,只需要度身定做相关部分,即可满足各类业务需求。

简要分析

热度分
478
价值分
228
活跃状态
活跃

语言占比

Python

README

English | 简体中文

License: MIT support: Ollama support: LlamaIndex

Table of Contents

ThinkRAG

ThinkRAG is a LLM RAG system that can be easily deployed on a laptop to implement Q&A with local knowledge base.

This system is built on LlamaIndex and Streamlit, and has been optimized for Chinese users in various fields such as model selection and text processing.

Key Features

ThinkRAG is a LLM application developed for professionals, researchers, students, and other knowledge workers, which can be used directly on a laptop with all knowledge and data stored locally on the computer.

ThinkRAG has the following features:

  • Complete application of the LlamaIndex framework
  • Development mode supports local file storage without the need to install any databases
  • No GPU support is required to run on a laptop
  • Supports locally deployed models and offline use

Specifically, ThinkRAG has also made a lot of customizations and optimizations for Chinese users:

  • Uses Spacy text splitter for better handling of Chinese characters
  • Employs Chinese title enhancement features
  • Uses Chinese prompt templates for Q&A and refinement processes
  • Default support for China LLM service provider such as DeepSeek, Moonshot and ZhiPu
  • Uses bilingual embedding models, such as bge-large-zh-v1.5 from BAAI

Model Support

ThinkRAG can use all models supported by the LlamaIndex data framework. For model list information, please refer to relevant documentation.

ThinkRAG is committed to creating an application system that is directly usable, useful, and easy to use.

Therefore, we have made careful selections and trade-offs in various models, components, and technologies.

Firstly, using large models, ThinkRAG supports the OpenAI API and all compatible LLM APIs, including LLM service providers in China, such as:

  • DeepSeek
  • Moonshot
  • ZhiPu
  • …

If you want to deploy LLMs locally, ThinkRAG chooses Ollama, which is easy to use. We can download models to run locally through Ollama.

Currently, Ollama supports the local deployment of almost all large language models, including Llama, Gemma, GLM, Mistral, Phi, Llava, etc. For details, please visit the Ollama official website.

The system also uses embedding models and reranking models, which can support most models from Hugging Face. Currently, ThinkRAG mainly selects the BGE series models from BAAI. Chinese users can visit the mirror website to learn about and download them.

Known Issues

Currently, there are issues from Windows users that have not been reproduced or resolved. Please use ThinkRAG on Linux or MacOS systems.

Due to incompatibility between llama_index and the latest ollama 0.4, please install ollama 0.3.3, which is reflected in the requirements.txt file.

Quick Start

Step 1 Download and Installation

After downloading the code from Github, use pip to install the required components.

pip3 install -r requirements.txt

If you want to run the system offline, please first download Ollama from the official website. Then, use the Ollama command to download LLMs such as DeepSeek, Qwen, and Gemma.

Then, download the embedding model (BAAI/bge-large-zh-v1.5) and reranking model (BAAI/bge-reranker-base) from Hugging Face to the localmodels directory.

For specific steps, please refer to the document in the docs directory: HowToDownloadModels.md

Step 2 System Configuration

For better performance, it is recommended to use commercial LLM APIs.

First, obtain the API key from the LLM service provider and configure the following environment variables.

OPENAI_API_KEY = ""
DEEPSEEK_API_KEY = ""
MOONSHOT_API_KEY = ""
ZHIPU_API_KEY = ""

You can skip this step and configure the API keys through the application interface after the system is running.

If you choose to use one or more LLM APIs, please delete the unused service providers in the config.py configuration file.

Of course, you can also add other service providers compatible with the OpenAI API in the configuration file.

ThinkRAG runs in development mode by default. In this mode, the system uses local file storage, and you do not need to install any databases.

If you want to switch to production mode, you can configure the environment variables as follows.

THINKRAG_ENV = production

In production mode, the system uses vector databases Chroma or LanceDB, and key-value databases Redis.

If you do not have Redis installed, it is recommended to install it through Docker or use an existing Redis instance. Please configure the parameters of the Redis instance in the config.py file.

Step 3 Running the System

Now, you are ready to run ThinkRAG.

Please run the following command in the directory containing the app.py file.

streamlit run app.py

The system will run and automatically open the following URL in the browser to display the application interface.

http://localhost:8501/

The first run may take a moment. If you have not downloaded the embedding model from Hugging Face in advance, the system will automatically download the model, which will take a longer time.

User Guide

1. System Configuration

ThinkRAG supports configuration and selection of large models in the user interface, including: the Base URL and API key of the large model LLM API, and the specific model to be used, such as ZhiPu’s glm-4.

file_uploads

The system will automatically detect whether the API and key are available, and if available, display the current selected large model instance in green text at the bottom.

Similarly, the system can automatically obtain models downloaded by Ollama, and users can select the required models on the user interface.

file_uploads

If you have already downloaded the embedding model and reranking model to the local localmodels directory, you can switch and select the model to be used on the user interface, and set the parameters of the reranking model, such as Top N.

file_uploads

In the left navigation bar, click on Advanced Settings (Settings-Advanced), and you can also set the following parameters:

  • Top K
  • Temperature
  • System Prompt
  • Response Mode

By using different parameters, we can compare the output results of large models and find the most effective parameter combination.

2. Knowledge Base Management

ThinkRAG supports uploading various types of files such as PDF, DOCX, PPTX, and also supports uploading web page URLs.

file_uploads

Click the Browse files button, select the files on your computer, and then click the Load button to load, at which point all loaded files will be listed.

Then, click the Save button, and the system will process the files, including text splitting and embedding, and save them to the knowledge base.

file_uploads

Similarly, you can enter or paste the web page URL to obtain web page information, process it, and save it to the knowledge base.

The system supports knowledge base management.

file_uploads

As shown in the figure above, ThinkRAG can list all documents in the knowledge base in pages.

Select the document to be deleted, and the Delete selected documents button will appear, click the button to delete the document from the knowledge base.

3. Query

In the left navigation bar, click on Query, and the Q&A page will appear.

After entering the question, the system will search the knowledge base and provide an answer. In this process, the system will use hybrid retrieval and reranking technologies to obtain accurate content from the knowledge base.

For example, we have uploaded a Word document about business process management to the knowledge base.

Now enter the question: “What are the three characteristics of the process?”

file_uploads

As shown in the figure, the system took 2.49 seconds to provide an accurate answer: The process has the characteristics of goal orientation, repetition, and processuality. At the same time, the system also provided 2 reference documents retrieved from the knowledge base.

It can be seen that ThinkRAG fully and effectively implements the function of large model retrieval and enhanced generation based on the local knowledge base.

Architecture

ThinkRAG is developed using the LlamaIndex data framework, with Streamlit for the front end. The development mode and production mode of the system use different technical components, as shown in the table below:

Development Mode Production Mode
RAG Framework LlamaIndex LlamaIndex
Frontend Framework Streamlit Streamlit
Embedding Model BAAI/bge-small-zh-v1.5 BAAI/bge-large-zh-v1.5
Reranking Model BAAI/bge-reranker-base BAAI/bge-reranker-large
Text Splitter SentenceSplitter SpacyTextSplitter
Conversation Storage SimpleChatStore Redis
Document Storage SimpleDocumentStore Redis
Index Storage SimpleIndexStore Redis
Vector Storage SimpleVectorStore LanceDB

These technical components are designed according to the Frontend, Framework, LLM, Tools, Storage, and Infrastructure, which are six parts of the architecture.

As shown in the figure below:

file_uploads

Roadmap

ThinkRAG will continue to optimize core functions and continuously improve the efficiency and accuracy of retrieval, including:

  • Optimize the processing of documents and web pages, support multimodal knowledge bases and multimodal retrieval
  • Build a knowledge graph, enhance retrieval through the knowledge graph, and reason based on the graph
  • Process complex scenarios through intelligent agents, especially accurately calling other tools and data to complete tasks

At the same time, we will further improve the application architecture and enhance the user experience, mainly including:

  • Design: A user interface with a sense of design and excellent user experience
  • Frontend: Build a desktop client application based on Electron, React, Vite, etc., to provide users with an extremely simple way to download, install, and run
  • Backend: Provide interfaces through FastAPI, and improve overall performance and scalability through technologies such as message queues

Welcome to join the ThinkRAG open source project, and together create AI products that users love!

License

ThinkRAG uses the MIT License.

评论

暂无评论