This implementation creates a personal knowledge assistant by integrating LangChain for retrieval-augmented generation with a Gradio-based user interface. The system allows users to query a custom knowledge base while optionally enabling contextual retrieval to enhance response accuracy and reduce model hallucinations.
Environment Setup
Python 3.8.5 or higher is required for compatibility with LangChain dependencies. Install the necessary packages using pip:
pip install flask chromadb gradio fastapi langchain FlagEmbedding
FlagEmbedding instals PyTorch and Trensformer dependencies by default, which may require extended installation time. The default installation uses CPU-based PyTorch; for improved performance, consider installing GPU-enabled PyTorch separately before adding FlagEmbedding.
Knowledge Base Configuration
Creating the Knowledge Base
Prepare text documents in UTF-8 encoding to serve as your knowledge source. These files should contain domain-specific information that the assistant will reference during queries. Organize documents in a dedicated directory accessible to the application.
Vector Model Setup
The BGE (BAAI General Embedding) model provides Chinese language embeddings through FlagEmbedding. Download the pre-trained weights from Hugging Face:
git lfs install
git clone https://huggingface.co/BAAI/bge-large-zh
Store the downloaded model directory locally and reference its path in the application configuration.
Large Language Model Integration
This implementation uses the iFlyTek Spark API as the primary LLM. Register for a free account at xinghuo.xfyun.cn too obtain authentication credentials:
- APPID
- APISecret
- APIKey
These credentials enable access to the Spark 2.0 API for text generation tasks.
Application Implementation
The project structure consists of several key components:
- SparkApi.py: Handles direct API communication with iFlyTek Spark (no modifications needed)
- SparkGPT.py: Wrapper class for Spark API integration (requires credential updates)
- prompt_utils.py: Manages prompt templates and formatting (no modifications needed)
- gradio_demo.py: Main application interface (requires path configuration)
- GPT4.png: Interface avatar image (no modifications needed)
Update authentication credentials in SparkGPT.py:
appid = "YOUR_APPID"
api_secret = "YOUR_APISECRET"
api_key = "YOUR_APIKEY"
Configure local paths in gradio_demo.py:
BGE_MODEL_PATH = "/path/to/bge-large-zh"
FILE_PATH = "/path/to/knowledge/files"
Execution
Launch the application by running:
python gradio_demo.py
The Gradio interface will provide a local URL where users can interact with the knowledge assistant. The interface includes a toggle for enabling/disabling retrieval augmentation, allowing comparison between standard LLM responses and context-enhanced answers.