### Installing GoMate: Using Pip (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This command shows the primary method for installing the GoMate library and its required dependencies using the pip package manager, ensuring all necessary components are available for use. ```shell pip install gomate ``` -------------------------------- ### Installing GoMate: Installing Source Dependencies (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md After cloning the source code, this command installs the project's dependencies in 'editable' mode. This allows for local development, where changes to the source code are immediately reflected without reinstallation. ```shell pip install -e . ``` -------------------------------- ### Installing GoMate: Cloning Source Code (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This command illustrates how to download the GoMate project's source code directly from its GitHub repository using Git. This method is suitable for users who prefer to install from source or contribute to the project. ```shell git clone https://github.com/gomate-community/GoMate.git ``` -------------------------------- ### Installing Qdrant Python Client Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This command installs the `qdrant-client` Python package using pip, which is necessary to interact with the Qdrant vector database programmatically from Python applications. ```Bash pip install qdrant-client ``` -------------------------------- ### Testing Hybrid Retriever (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This example illustrates how to perform a retrieval query using the `HybridRetriever` with a sample query. It demonstrates fetching the top-k relevant documents and printing their text content along with their corresponding relevance scores. ```python query = "支付宝" results = hybrid_retriever.retrieve(query, top_k=10) print(len(results)) for result in results: print(f"Text: {result['text']}, Score: {result['score']}") ``` -------------------------------- ### Installing GoMate: Creating Conda Environment (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This snippet demonstrates how to create a new Conda environment named 'gomate' with Python 3.9 and activate it. This is an optional but recommended first step to isolate GoMate's dependencies. ```shell conda create -n gomate python=3.9 conda activate gomate ``` -------------------------------- ### Example Qdrant Search Result Output Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This JSON snippet shows an example of the output returned by a Qdrant search query. It includes the ID, version, similarity score, and indicates that payload and vector fields are null in this specific output format, which might vary based on query parameters. ```JSON [ { "id": 4, "version": 0, "score": 1.362, "payload": null, "vector": null }, { "id": 1, "version": 0, "score": 1.273, "payload": null, "vector": null }, { "id": 3, "version": 0, "score": 1.208, "payload": null, "vector": null } ] ``` -------------------------------- ### Starting Qdrant Container Service Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This command starts a Qdrant container named `qdrant_server` in detached mode. It mounts a local volume for data persistence and maps port 6333 for external access, allowing the Qdrant service to run in the background. ```Bash docker run -d \ --name qdrant_server \ -v $(pwd)/qdrant_storage:/qdrant/storage \ -p 6333:6333 \ qdrant/qdrant ``` -------------------------------- ### Configuring Hybrid Retriever (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This code configures a `HybridRetriever` by defining parameters for both `BM25RetrieverConfig` and `DenseRetrieverConfig`, including index paths, model names, and relative weights for each retrieval method. This setup determines how the hybrid retriever combines scores from different retrieval approaches. ```python bm25_config = BM25RetrieverConfig( method='lucene', index_path='indexs/description_bm25.index', k1=1.6, b=0.7 ) bm25_config.validate() print(bm25_config.log_config()) dense_config = DenseRetrieverConfig( model_name_or_path=embedding_model_path, dim=1024, index_path='indexs/dense_cache' ) config_info = dense_config.log_config() print(config_info) hybrid_config = HybridRetrieverConfig( bm25_config=bm25_config, dense_config=dense_config, bm25_weight=0.7, dense_weight=0.3 ) hybrid_retriever = HybridRetriever(config=hybrid_config) ``` -------------------------------- ### Viewing Qdrant Container Logs Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This command displays the real-time logs of the `qdrant_server` container, which is useful for monitoring its startup process and identifying any potential issues during operation. ```Bash docker logs qdrant_server ``` -------------------------------- ### Initializing Qdrant Python Client Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This Python snippet imports the `QdrantClient` class and initializes a client instance, connecting to the Qdrant service running locally at `http://localhost:6333`. This client object is used for all subsequent database operations. ```Python from qdrant_client import QdrantClient client = QdrantClient(url="http://localhost:6333") ``` -------------------------------- ### Loading Retriever Index (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This line of code shows how to load a previously built and saved retriever index. This step is crucial for reusing existing indexes, significantly optimizing performance by bypassing the time-consuming index construction phase. ```python hybrid_retriever.load_index() ``` -------------------------------- ### Creating Python Wheel Distribution with setup.py Source: https://github.com/gomate-community/trustrag/blob/main/docs/pypi.md This command uses `setup.py` to create a wheel (`.whl`) distribution file for the Python package. Wheels are pre-built distributions that can be installed without needing to run `setup.py install` or compile extensions, making installation faster. ```Shell python setup.py bdist_wheel ``` -------------------------------- ### Creating Python Source Distribution with setup.py Source: https://github.com/gomate-community/trustrag/blob/main/docs/pypi.md This command uses `setup.py` to create a source distribution (`.tar.gz` or `.zip`) of the Python package. Source distributions contain the source code and metadata, allowing users to build and install the package from scratch. ```Shell python setup.py sdist ``` -------------------------------- ### Building and Saving Retriever Index (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This snippet demonstrates how to build the search index for the configured `HybridRetriever` from a given corpus of texts. After construction, the index is saved to disk, allowing for efficient retrieval operations in subsequent runs without needing to rebuild. ```python hybrid_retriever.build_from_texts(corpus) hybrid_retriever.save_index() ``` -------------------------------- ### Creating Qdrant Collection with Python Client Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This Python code demonstrates how to create a new collection named `test_collection` using the `qdrant-client`. It configures the collection to store 4-dimensional vectors and uses `Distance.DOT` for similarity calculations. ```Python from qdrant_client.models import Distance, VectorParams client.create_collection( collection_name="test_collection", vectors_config=VectorParams(size=4, distance=Distance.DOT), ) ``` -------------------------------- ### GoMate: Module Directory Structure (Text) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This snippet outlines the hierarchical structure of the GoMate project's modules, detailing the purpose of each core component such as citation, document parsing, generator, judger, prompt, refiner, reranker, retrieval, and rewriter, which collectively form the RAG framework. ```text ├── applications ├── modules | ├── citation:答案与证据引用 | ├── document:文档解析与切块,支持多种文档类型 | ├── generator:生成器 | ├── judger:文档选择 | ├── prompt:提示语 | ├── refiner:信息总结 | ├── reranker:排序模块 | ├── retrieval:检索模块 | └── rewriter:改写模块 ``` -------------------------------- ### Pulling Qdrant Docker Image Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This command pulls the official Qdrant Docker image from Docker Hub, which is the first step to running Qdrant as a containerized service. It ensures the necessary image is available locally for deployment. ```Bash docker pull qdrant/qdrant ``` -------------------------------- ### Importing GoMate Modules and Dependencies (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This code block demonstrates the essential Python imports required to utilize various functionalities within the GoMate framework. It includes standard libraries like `pickle`, `pandas`, and `tqdm`, alongside specific GoMate modules for document processing, generation, reranking, and hybrid retrieval. ```python import pickle import pandas as pd from tqdm import tqdm from trustrag.modules.document.chunk import TextChunker from trustrag.modules.document.txt_parser import TextParser from trustrag.modules.document.utils import PROJECT_BASE from trustrag.modules.generator.llm import GLM4Chat from trustrag.modules.reranker.bge_reranker import BgeRerankerConfig, BgeReranker from trustrag.modules.retrieval.bm25s_retriever import BM25RetrieverConfig from trustrag.modules.retrieval.dense_retriever import DenseRetrieverConfig from trustrag.modules.retrieval.hybrid_retriever import HybridRetriever, HybridRetrieverConfig ``` -------------------------------- ### Installing TrustRAG from Source (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet demonstrates how to install TrustRAG in editable mode from its source directory using 'pip install -e .', allowing for local development and modifications to the library. ```shell pip install -e . ``` -------------------------------- ### Document Parsing and Chunking (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This function illustrates the process of parsing a text document (`corpus.txt`) using `TextParser` and subsequently chunking its content into smaller, manageable sentences using `TextChunker`. The resulting chunks are then serialized and saved as a pickle file for efficient retrieval later. ```python def generate_chunks(): tp = TextParser()# 代表txt格式解析 tc = TextChunker() paragraphs = tp.parse(r'H:/2024-Xfyun-RAG/data/corpus.txt', encoding="utf-8") print(len(paragraphs)) chunks = [] for content in tqdm(paragraphs): chunk = tc.chunk_sentences([content], chunk_size=1024) chunks.append(chunk) with open(f'{PROJECT_BASE}/output/chunks.pkl', 'wb') as f: pickle.dump(chunks, f) ``` -------------------------------- ### Checking Loaded Dataset Size - Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/multimodal_rag.md This simple Python snippet calculates and outputs the total number of examples loaded into the `examples` list. It serves as a quick verification step to confirm that the dataset has been successfully loaded and to understand its size before proceeding with visualization or further processing. ```Python len(examples) ``` -------------------------------- ### Configuring BGE Reranker (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This code configures the `BgeReranker` by specifying the path to its pre-trained model. The reranker is used to re-order the initial set of retrieved documents, improving their relevance to the query and enhancing the overall quality of the RAG system's output. ```python reranker_config = BgeRerankerConfig( model_name_or_path=reranker_model_path ) bge_reranker = BgeReranker(reranker_config) ``` -------------------------------- ### Configuring GLM4 Chat Generator (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This snippet initializes the `GLM4Chat` generator by providing the path to the large language model. This component is responsible for generating coherent and contextually relevant answers based on the information retrieved and reranked by previous steps in the RAG pipeline. ```python glm4_chat = GLM4Chat(llm_model_path) ``` -------------------------------- ### Installing TrustRAG via Pip (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet shows how to install the TrustRAG library and its dependencies directly using the 'pip' package manager, which is the standard method for installing Python packages. ```shell pip install trustrag ``` -------------------------------- ### Running Search Query with Qdrant Python Client Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This Python code performs a similarity search on the `test_collection` using a specified query vector. It retrieves the top 3 most similar points and prints the search results, demonstrating how to find relevant data programmatically. ```Python search_result = client.query_points( collection_name="test_collection", query=[0.2, 0.1, 0.9, 0.7], limit=3 ).points print(search_result) ``` -------------------------------- ### Cloning TrustRAG Source Code (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet illustrates how to download the TrustRAG project's source code from its GitHub repository using the 'git clone' command, a prerequisite for installing from source. ```shell git clone https://github.com/gomate-community/TrustRAG.git ``` -------------------------------- ### Running TrustRAG Application from Shell Source: https://github.com/gomate-community/trustrag/blob/main/README.md This shell command executes the `app.py` script, which is responsible for starting the `TrustRAG` application. It typically launches the backend server, making the application accessible via a web browser. This is the standard way to run the application after configuration. ```shell python app.py ``` -------------------------------- ### Listing Installed Python Packages Source: https://github.com/gomate-community/trustrag/blob/main/notebooks/rag实战2-基于Mineru进行PDF内容解析.ipynb This snippet lists all Python packages installed in the current environment, useful for verifying dependencies or debugging environment issues. ```Python !pip list ``` -------------------------------- ### Example Usage of Markdown Content Merging Function Source: https://github.com/gomate-community/trustrag/blob/main/docs/parser.md This snippet demonstrates how to call the `merge_title_content` function with a `data` list and then iterate through the returned `merged_data` to print each merged title and its corresponding content. It serves as an example of how to use the previously defined function. ```Python # 假设 data 是已经加载的文档列表 merged_data = merge_title_content(data) # 输出合并后的标题和内容 for item in merged_data: print(f"一级标题: {item['title']}") print(f"段落内容: {item['content']}\n") print("==="*10) ``` -------------------------------- ### Implementing Retrieval-Augmented Generation (RAG) (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/quickstart.md This comprehensive code block demonstrates a full RAG pipeline: it reads questions from a CSV, retrieves relevant documents using the hybrid retriever, reranks them with the BGE reranker, and then generates answers using the GLM4 chat model. Finally, the generated answers are appended to the DataFrame and saved to a new CSV file. ```python # ====================检索问答========================= test = pd.read_csv(test_path) answers = [] for question in tqdm(test['question'], total=len(test)): search_docs = hybrid_retriever.retrieve(question, top_k=10) search_docs = bge_reranker.rerank( query=question, documents=[doc['text'] for idx, doc in enumerate(search_docs)] ) # print(search_docs) content = '\n'.join([f'信息[{idx}]:' + doc['text'] for idx, doc in enumerate(search_docs)]) answer = glm4_chat.chat(prompt=question, content=content) answers.append(answer[0]) print(question) print(answer[0]) print("************************************/n") test['answer'] = answers test[['answer']].to_csv(f'{PROJECT_BASE}/output/gomate_baseline.csv', index=False) ``` -------------------------------- ### Deploying DeepSeek-R1-Distill-Qwen-32B Model Service with vLLM Source: https://github.com/gomate-community/trustrag/blob/main/docs/vllm.md This command deploys the DeepSeek-R1-Distill-Qwen-32B model service using vLLM, configuring GPU devices, model path, served name, maximum model length, GPU memory utilization, tensor parallelism, host, and port. It requires a pre-trained model and vLLM installed. ```bash CUDA_VISIBLE_DEVICES=4,5,6,7 python -m vllm.entrypoints.openai.api_server \ --model /data/users/searchgpt/pretrained_models/DeepSeek-R1-Distill-Qwen-32B \ --served-model-name DeepSeek-R1-Distill-Qwen-32B \ --max-model-len=8192 \ --gpu-memory-utilization=0.9 \ --tensor-parallel-size=4 \ --host 0.0.0.0 \ --port 8000 \ ``` -------------------------------- ### Executing Image and Caption Visualization - Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/multimodal_rag.md This snippet calls the `visualize_images_and_captions` function, passing the loaded `examples` list and specifying `num_images=5`. This action triggers the display of the first five images from the COCO-CN dataset along with their associated Chinese captions, demonstrating the successful loading and visualization of the multimodal data. ```Python visualize_images_and_captions(examples, num_images=5) ``` -------------------------------- ### Running DeepResearch CLI Tool in Bash Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet provides the necessary bash commands to set up and run the DeepResearch CLI tool. It involves navigating to the module directory, copying an example environment file for configuration (e.g., LLM API and search settings), and then executing the main pipeline script. ```bash cd trustrag/modules/deepsearch cp .env.example .env #Configure LLM API and search python pipeline.py ``` -------------------------------- ### Building Filter and Searching Qdrant (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This code block constructs a Qdrant filter to narrow down search results to documents where the 'city' field matches 'Berlin'. It then performs a vector search for items related to 'vacations' within the Qdrant database, applying the 'custom_filter', and limits the results to 5. Finally, it iterates through and prints each search result. ```Python # Build a filter for city and category conditions = [ {"key": "city", "match": "Berlin"}, ] custom_filter = qdrant_engine.build_filter(conditions); # Search for startups related to "vacations" in Berlin results = qdrant_engine.search(text="vacations", query_filter=custom_filter, limit=5); for result in results: print(result) ``` -------------------------------- ### Generating Embeddings and Uploading to Qdrant (Python) Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This snippet generates vector embeddings for the 'description' field of each document in the 'documents' list using 'qdrant_engine.embedding_generator'. It then prints the shape of the resulting vectors and uploads both the generated vectors and their corresponding document payloads to the Qdrant database via 'qdrant_engine.upload_vectors'. ```Python vectors = qdrant_engine.embedding_generator.generate_embedding([doc["description"] for doc in documents]) print(vectors.shape) payload = [doc for doc in documents] # Upload vectors and payload qdrant_engine.upload_vectors(vectors=vectors, payload=payload) ``` -------------------------------- ### Creating Qdrant Collection via REST API Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This JSON payload defines a request to create a new collection named `star_charts` in Qdrant. It specifies that vectors in this collection will be 4-dimensional and use the Dot Product as the similarity distance metric. ```JSON { "vectors": { "size": 4, "distance": "Dot" } } ``` -------------------------------- ### Sample Qdrant Filtered Query Result - JSON Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This JSON snippet illustrates the structure of a single point returned from a Qdrant query that includes a filter. It shows the point's `id`, `version`, `score`, and importantly, the `payload` containing the 'city' field that matched the filter condition. ```json [ { "id": 2, "version": 0, "score": 0.871, "payload": { "city": "London" }, "vector": null } ] ``` -------------------------------- ### Testing Hybrid Retriever with a Query - Python Source: https://github.com/gomate-community/trustrag/blob/main/README_zh.md This snippet provides an example of how to perform a retrieval operation using the configured `hybrid_retriever`. It takes a query, retrieves the top-k relevant documents, and then iterates through the results to print each document's text and its corresponding score. ```Python query = "支付宝" results = hybrid_retriever.retrieve(query, top_k=10) print(len(results)) # Output results for result in results: print(f"Text: {result['text']}, Score: {result['score']}") ``` -------------------------------- ### Running Docker Container with GPU Support (Bash) Source: https://github.com/gomate-community/trustrag/blob/main/docs/docker.md This command starts a Docker container named 'test' from the 'trustrag:0.1' image. It runs in detached mode (`-d`), allocates a pseudo-TTY (`-t`), keeps stdin open (`-i`), maps all available GPUs (`--gpus=all`), and executes `/bin/bash` inside the container. ```bash docker run -itd --gpus=all --name=test trustrag:0.1 /bin/bash ``` -------------------------------- ### Deploying MySQL with Docker (Bash) Source: https://github.com/gomate-community/trustrag/blob/main/docs/mysql.md This Bash script automates the deployment of a MySQL Docker container. It first stops and removes any existing container named 'mysql' to ensure a clean setup, then launches a new MySQL container. The container is configured to map port 3306, restart automatically, persist data to a local volume, and set the root password. ```Bash docker stop mysql docker rm -f mysql docker run --name mysql \ -p 3306:3306 \ --restart always \ -v G:/Ubuntu_WSL/rag-middlewares/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql:latest ``` -------------------------------- ### Configuring and Using DenseRetriever in Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/retrieval.md This snippet illustrates the setup and usage of the DenseRetriever for vector-based document retrieval. It involves configuring the embedding model path, vector dimension, and index directory, then building the retriever from a corpus loaded from a JSON file, and finally performing a retrieval query and saving the index. Dependencies include `pandas`, `tqdm`, and `trustrag.modules.retrieval.dense_retriever`. ```python import pandas as pd from tqdm import tqdm from trustrag.modules.retrieval.dense_retriever import DenseRetriever, DenseRetrieverConfig if __name__ == '__main__': retriever_config = DenseRetrieverConfig( model_name_or_path="/data/users/searchgpt/pretrained_models/bge-large-zh-v1.5", dim=1024, index_dir='/data/users/searchgpt/yq/GoMate/examples/retrievers/dense_cache' ) config_info = retriever_config.log_config() print(config_info) retriever = DenseRetriever(config=retriever_config) data = pd.read_json('/data/users/searchgpt/yq/GoMate/data/docs/zh_refine.json', lines=True)[:5] print(data) print(data.columns) corpus = [] for documents in tqdm(data['positive'], total=len(data)): for document in documents: # retriever.add_text(document) corpus.append(document) for documents in tqdm(data['negative'], total=len(data)): for document in documents: # retriever.add_text(document) corpus.append(document) print("len(corpus)", len(corpus)) retriever.build_from_texts(corpus) result = retriever.retrieve("RCEP具体包括哪些国家") print(result) retriever.save_index() ``` -------------------------------- ### Adding Points with Payload using Qdrant Python Client Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This Python snippet uses the `client.upsert` method to add multiple vector points to the `test_collection`. Each `PointStruct` includes an ID, a vector, and a `payload` dictionary containing associated city information, with `wait=True` ensuring the operation completes before returning. ```Python from qdrant_client.models import PointStruct operation_info = client.upsert( collection_name="test_collection", wait=True, points=[ PointStruct(id=1, vector=[0.05, 0.61, 0.76, 0.74], payload={"city": "Berlin"}), PointStruct(id=2, vector=[0.19, 0.81, 0.75, 0.11], payload={"city": "London"}), PointStruct(id=3, vector=[0.36, 0.55, 0.47, 0.94], payload={"city": "Moscow"}), PointStruct(id=4, vector=[0.18, 0.01, 0.85, 0.80], payload={"city": "New York"}), PointStruct(id=5, vector=[0.24, 0.18, 0.22, 0.44], payload={"city": "Beijing"}), PointStruct(id=6, vector=[0.35, 0.08, 0.11, 0.44], payload={"city": "Mumbai"}), ] ) print(operation_info) ``` -------------------------------- ### Building Python Project with setup.py Source: https://github.com/gomate-community/trustrag/blob/main/docs/pypi.md This command executes the `build` command from `setup.py`, which compiles any C extensions, copies data files, and prepares the project for packaging. It's a preliminary step before creating distribution archives. ```Shell python setup.py build ``` -------------------------------- ### Creating Conda Environment for TrustRAG (Shell) Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet demonstrates how to create and activate a dedicated Conda environment named 'trustrag' with Python 3.9, which is an optional but recommended step before installing TrustRAG dependencies. ```shell conda create -n trustrag python=3.9 conda activate trustrag ``` -------------------------------- ### Reading Markdown File Content Source: https://github.com/gomate-community/trustrag/blob/main/docs/parser.md This snippet demonstrates how to open and read the entire content of a Markdown file into a string variable. It uses `utf-8` encoding to ensure proper handling of various characters. ```Python with open(markdown_path,"r",encoding="utf-8") as f: content=f.read() ``` -------------------------------- ### Configuring TrustRAG Application Parameters Source: https://github.com/gomate-community/trustrag/blob/main/README.md This configuration snippet demonstrates how to set up the `TrustRAG` application's parameters, including paths for documents and LLM models, and configurations for the dense retriever and BGE reranker. It initializes `ApplicationConfig`, `DenseRetrieverConfig`, and `BgeRerankerConfig` objects, then assigns them to the main application configuration before initializing the vector store. This block is crucial for customizing model and data locations. ```text # Modify to your own configuration!!! app_config = ApplicationConfig() app_config.docs_path = "./docs/" app_config.llm_model_path = "/data/users/searchgpt/pretrained_models/chatglm3-6b/" retriever_config = DenseRetrieverConfig( model_name_or_path="/data/users/searchgpt/pretrained_models/bge-large-zh-v1.5", dim=1024, index_dir='/data/users/searchgpt/yq/TrustRAG/examples/retrievers/dense_cache' ) rerank_config = BgeRerankerConfig( model_name_or_path="/data/users/searchgpt/pretrained_models/bge-reranker-large" ) app_config.retriever_config = retriever_config app_config.rerank_config = rerank_config application = RagApplication(app_config) application.init_vector_store() ``` -------------------------------- ### Running an Ollama Model Source: https://github.com/gomate-community/trustrag/blob/main/docs/ollama.md This command initiates an interactive session with a specified Ollama model. It allows users to send prompts and receive responses directly from the model running on the local Ollama server. ```bash ollama run deepseek-r1:1.5b ``` -------------------------------- ### Searching Points in Qdrant Collection via REST API Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This JSON payload defines a search query against the `star_charts` collection. It specifies a query vector, a limit of 3 results, and requests that the associated `payload` data be returned with each matching point. ```JSON { "vector": [0.2, 0.1, 0.9, 0.7], "limit": 3, "with_payload": true } ``` -------------------------------- ### Listing Available Ollama Models Source: https://github.com/gomate-community/trustrag/blob/main/docs/ollama.md This command displays a list of all models currently available and managed by the Ollama server. It provides an overview of downloaded and created models, including their sizes and last modified dates. ```bash ollama list ``` -------------------------------- ### Example Usage of Title-Content Merging Function in Python Source: https://github.com/gomate-community/trustrag/blob/main/notebooks/rag实战1-markdown文件解析切片.ipynb This snippet demonstrates how to use the `merge_title_content` function with a hypothetical `data` list, then iterates through the `merged_data` to print the extracted titles and their corresponding merged content, providing a clear output format. ```python merged_data = merge_title_content(data) # 输出合并后的标题和内容 for item in merged_data: print(f"一级标题: {item['title']}") print(f"段落内容: {item['content']}\n") print("==="*10) ``` -------------------------------- ### Initializing PairWise-Reranker with Qwen2-7B-Instruct in Python Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet demonstrates the initialization of a PairWise-Reranker, utilizing the `qwen2-7B-instruct` model. Pairwise methods compare two documents against a query to determine which is more relevant, often used in 'allpair' or 'bubblesort' strategies. ```python from trustrag.modules.reranker.llm_reranker import LLMRerankerConfig, PairWiseReranker reranker_config = LLMRerankerConfig( model_name_or_path="qwen2-7B-instruct" ) llm_reranker = PairWiseReranker(reranker_config) ``` -------------------------------- ### Generating Text via Ollama API with cURL Source: https://github.com/gomate-community/trustrag/blob/main/docs/ollama.md This cURL command demonstrates how to interact with the Ollama API's `/api/generate` endpoint to get a text completion. It sends a JSON payload specifying the model, prompt, and stream option, receiving the generated response. ```bash curl http://localhost:11434/api/generate -d '{ "model": "deepseek-r1:1.5b", "prompt": "怎么学习机器学习?", "stream": false }' ``` -------------------------------- ### Configuring BM25 and Dense Retrievers - Python Source: https://github.com/gomate-community/trustrag/blob/main/README_zh.md This snippet demonstrates how to initialize `BM25RetrieverConfig` and `DenseRetrieverConfig` with specific parameters. It sets up the method, index path, k1, and b for BM25, and the model name, dimension, and index path for the dense retriever. It also shows how to validate and log these configurations. ```Python bm25_config = BM25RetrieverConfig( method='lucene', index_path='indexs/description_bm25.index', k1=1.6, b=0.7 ) bm25_config.validate() print(bm25_config.log_config()) dense_config = DenseRetrieverConfig( model_name_or_path=embedding_model_path, dim=1024, index_path='indexs/dense_cache' ) config_info = dense_config.log_config() print(config_info) ``` -------------------------------- ### Running Xinference Docker Container Source: https://github.com/gomate-community/trustrag/blob/main/docs/xinference.md This command initiates the Xinference service as a Docker container. It maps port 9997, sets the XINFERENCE_HOME environment variable, mounts the current working directory as a data volume, and enables GPU access for accelerated processing. ```Shell docker run --name xinference -d -p 9997:9997 -e XINFERENCE_HOME=/data -v $(pwd):/data --gpus all xprobe/xinference:latest xinference-local -H 0.0.0.0 ``` -------------------------------- ### Initializing Multimodal Retriever - Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/multimodal_rag.md This snippet demonstrates how to configure and initialize the `MultimodalRetriever` class. It sets up parameters like the model name, index path, batch size, embedding dimension, and download root for the underlying embedding model. ```Python # 初始化配置 config = MultimodalRetrieverConfig( model_name='ViT-B-16', index_path='./index', batch_size=32, dim=512, download_root="data/chinese-clip-vit-base-patch16/" ) # 创建检索器示例 retriever = MultimodalRetriever(config) ``` -------------------------------- ### Loading Pre-built Retriever Index (Python) Source: https://github.com/gomate-community/trustrag/blob/main/README.md This snippet shows how to load an already built retriever index from disk, allowing users to skip the index building process if an index is already available. This is useful for resuming work or deploying pre-indexed systems. ```python hybrid_retriever.load_index() ``` -------------------------------- ### Filtering Qdrant Points by Field - Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This Python snippet demonstrates how to query points in a Qdrant collection using a filter. It constructs a `Filter` object with a `FieldCondition` to match points where the 'city' payload field has the value 'London', returning the top 3 results with their payloads. ```python from qdrant_client.models import Filter, FieldCondition, MatchValue search_result = client.query_points( collection_name="test_collection", query=[0.2, 0.1, 0.9, 0.7], query_filter=Filter( must=[FieldCondition(key="city", match=MatchValue(value="London"))] ), with_payload=True, limit=3, ).points print(search_result) ``` -------------------------------- ### Implementing HybridRetriever with BM25 and Dense Methods in Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/retrieval.md This snippet demonstrates how to set up and use the HybridRetriever, which combines BM25 and Dense retrieval methods. It involves configuring both BM25RetrieverConfig and DenseRetrieverConfig, then creating a HybridRetrieverConfig with specified weights, building the hybrid retriever from a parsed document corpus, and performing a combined retrieval query. Dependencies include `trustrag.modules.document.common_parser`, `trustrag.modules.retrieval.bm25s_retriever`, `trustrag.modules.retrieval.dense_retriever`, and `trustrag.modules.retrieval.hybrid_retriever`. ```python from trustrag.modules.document.common_parser import CommonParser from trustrag.modules.retrieval.bm25s_retriever import BM25RetrieverConfig from trustrag.modules.retrieval.dense_retriever import DenseRetrieverConfig from trustrag.modules.retrieval.hybrid_retriever import HybridRetriever, HybridRetrieverConfig if __name__ == '__main__': # BM25 and Dense Retriever configurations bm25_config = BM25RetrieverConfig( method='lucene', index_path='indexs/description_bm25.index', k1=1.6, b=0.7 ) bm25_config.validate() print(bm25_config.log_config()) dense_config = DenseRetrieverConfig( model_name_or_path="/data/users/searchgpt/pretrained_models/bge-large-zh-v1.5", dim=1024, index_path='/data/users/searchgpt/yq/GoMate/examples/retrievers/dense_cache' ) config_info = dense_config.log_config() print(config_info) # Hybrid Retriever configuration hybrid_config = HybridRetrieverConfig( bm25_config=bm25_config, dense_config=dense_config, bm25_weight=0.5, dense_weight=0.5 ) hybrid_retriever = HybridRetriever(config=hybrid_config) # Corpus corpus = [] # Files to be parsed new_files = [ r'/data/users/searchgpt/yq/GoMate_dev/data/docs/伊朗.txt', r'/data/users/searchgpt/yq/GoMate_dev/data/docs/伊朗总统罹难事件.txt', r'/data/users/searchgpt/yq/GoMate_dev/data ``` -------------------------------- ### Adding Points to Qdrant Collection via REST API Source: https://github.com/gomate-community/trustrag/blob/main/docs/qdrant.md This JSON payload represents a request to insert multiple vector points into the `star_charts` collection. Each point includes a unique ID, a 4-dimensional vector, and a `payload` containing associated metadata like the `colony` name. ```JSON { "points": [ { "id": 1, "vector": [0.05, 0.61, 0.76, 0.74], "payload": { "colony": "Mars" } }, { "id": 2, "vector": [0.19, 0.81, 0.75, 0.11], "payload": { "colony": "Jupiter" } }, { "id": 3, "vector": [0.36, 0.55, 0.47, 0.94], "payload": { "colony": "Venus" } }, { "id": 4, "vector": [0.18, 0.01, 0.85, 0.80], "payload": { "colony": "Moon" } }, { "id": 5, "vector": [0.24, 0.18, 0.22, 0.44], "payload": { "colony": "Pluto" } } ] } ``` -------------------------------- ### Creating Ollama Model from Modelfile Source: https://github.com/gomate-community/trustrag/blob/main/docs/ollama.md This command uses the `ollama create` utility to build a new Ollama model from a `Modelfile`. It requires the `Modelfile` to be present in the current directory and assigns a specified name and tag to the newly created model. ```bash ollama create deepseek-r1:1.5b ``` -------------------------------- ### Parsing and Merging Markdown Documents Source: https://github.com/gomate-community/trustrag/blob/main/docs/parser.md This Python snippet shows the sequential steps of parsing raw Markdown content into a list of document objects and then merging their titles and content using a custom function. It assumes `parse_markdown_to_documents` and `merge_title_content` functions are defined elsewhere. ```Python parsed_documents = parse_markdown_to_documents(content) merge_title_content(parsed_documents) ``` -------------------------------- ### Pushing Docker Image to Alibaba Cloud Registry (Bash) Source: https://github.com/gomate-community/trustrag/blob/main/docs/docker.md These commands facilitate pushing a Docker image to an Alibaba Cloud Container Registry. It involves logging in with a specified username, tagging the local image with the registry's full path and version, and then pushing the tagged image to the remote repository. ```bash docker login --username=11859*****@qq.com registry.cn-beijing.aliyuncs.com docker tag [ImageId] registry.cn-beijing.aliyuncs.com/quincyqiang/trustrag:[镜像版本号] docker push registry.cn-beijing.aliyuncs.com/quincyqiang/trustrag:[镜像版本号] ``` -------------------------------- ### Initializing and Using BM25Retriever in Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/retrieval.md This snippet demonstrates how to configure, build, and use the BM25Retriever for document retrieval. It shows parsing documents from files, extending a corpus, configuring BM25 parameters like method and index path, building the retriever from the corpus, and performing a query. Dependencies include `trustrag.modules.document.common_parser`, `trustrag.modules.document.utils`, and `trustrag.modules.retrieval.bm25s_retriever`. ```python from trustrag.modules.document.common_parser import CommonParser from trustrag.modules.document.utils import PROJECT_BASE from trustrag.modules.retrieval.bm25s_retriever import BM25RetrieverConfig, BM25Retriever if __name__ == '__main__': corpus = [] new_files = [ f'{PROJECT_BASE}/data/docs/伊朗.txt', f'{PROJECT_BASE}/data/docs/伊朗总统罹难事件.txt', f'{PROJECT_BASE}/data/docs/伊朗总统莱希及多位高级官员遇难的直升机事故.txt', f'{PROJECT_BASE}/data/docs/伊朗问题.txt', f'{PROJECT_BASE}/data/docs/汽车操作手册.pdf', # r'H:\2024-Xfyun-RAG\data\corpus.txt' ] parser = CommonParser() for filename in new_files: chunks = parser.parse(filename) corpus.extend(chunks) bm25_config = BM25RetrieverConfig(method='lucene', index_path='indexs/description_bm25.index', k1=1.6, b=0.7) bm25_config.validate() print(bm25_config.log_config()) bm25_retriever = BM25Retriever(bm25_config) bm25_retriever.build_from_texts(corpus) # bm25_retriever.load_index() query = "伊朗总统莱希" search_docs = bm25_retriever.retrieve(query) print(search_docs) ``` -------------------------------- ### Displaying Ollama Model Details Source: https://github.com/gomate-community/trustrag/blob/main/docs/ollama.md This command provides detailed information about a specific Ollama model, including its Modelfile content, parameters, and other metadata. It's useful for inspecting model configurations. ```bash ollama show deepseek-r1:1.5b ``` -------------------------------- ### Initializing MultimodalRAG Instance in Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/multimodal_rag.md This snippet demonstrates how to initialize the `MultimodalRAG` class, providing the ZhipuAI API key, a `MultimodalRetrieverConfig` object, and specifying the `top_k` parameter for retrieval. `your_key` and `config` are placeholders for actual values that must be defined externally. ```python # 初始化 rag = MultimodalRAG( api_key=your_key, retriever_config=config, top_k=1 ) ``` -------------------------------- ### Building Docker Image from Context (Bash) Source: https://github.com/gomate-community/trustrag/blob/main/docs/docker.md This command builds a Docker image named 'trustrag' with tag '0.1' using the Dockerfile located in the current directory. The '.' indicates that the build context is the current directory. ```bash docker build -t trustrag:0.1 . ``` -------------------------------- ### Loading COCO-CN Dataset from JSONL File - Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/multimodal_rag.md This Python snippet reads a JSONL file named 'test.jsonl', line by line, parsing each line as a JSON object and appending it to the `examples` list. This process effectively loads the COCO-CN dataset, which contains image metadata and captions, into memory for further processing and visualization. ```Python examples=[] with open("data/test.jsonl","r",encoding="utf-8") as f: for line in f.readlines(): # print(line) examples.append(json.loads(line.strip())) examples[0].keys() ``` -------------------------------- ### Initializing SetWise-Reranker with Qwen2-7B-Instruct in Python Source: https://github.com/gomate-community/trustrag/blob/main/README.md This code initializes a SetWise-Reranker, configured with the `qwen2-7B-instruct` model. The setwise likelihood method prompts LLMs to identify the most relevant document within a set, reranking based on this likelihood. ```python from trustrag.modules.reranker.llm_reranker import LLMRerankerConfig, SetWiseReranker reranker_config = LLMRerankerConfig( model_name_or_path="qwen2-7B-instruct" ) llm_reranker = SetWiseReranker(reranker_config) ``` -------------------------------- ### Generating Text Completions with OpenAI Python Client Source: https://github.com/gomate-community/trustrag/blob/main/docs/vllm.md This Python script uses the openai library to interact with the DeepSeek-R1-Distill-Qwen-32B model for text completion. It configures the client with the service base URL and a placeholder API key, then sends a chat completion request with a user message. The response's content is then printed. ```python from openai import OpenAI client = OpenAI( base_url="http://127.0.0.1:8000/v1", api_key="sk-xxx", # 随便填写,只是为了通过接口参数校验 ) completion = client.chat.completions.create( model="DeepSeek-R1-Distill-Qwen-32B", messages=[ {"role": "user", "content": "请介绍下中科院计算所"} ] ) print(completion.choices[0].message) ``` -------------------------------- ### Performing Pure Text Query with MultimodalRAG in Python Source: https://github.com/gomate-community/trustrag/blob/main/docs/multimodal_rag.md This example shows how to perform a text-only query using the `chat` method of the `MultimodalRAG` instance. It demonstrates passing a text query and receiving both retrieval results and the AI's generated response, showcasing the system's ability to answer questions based on textual input. ```python # 纯文本查询 query_text="描述一下飞机的样子" retrieve_results,response = rag.chat("描述一下飞机的样子") retrieve_results,response ```