From 70baa4b572c71ec309eee88ce41d36bc189a04e8 Mon Sep 17 00:00:00 2001 From: LarFii <834462287@qq.com> Date: Tue, 15 Oct 2024 19:55:30 +0800 Subject: [PATCH] update README.md --- README.md | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 562585e4..cc282c27 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ This repository hosts the code of LightRAG. The structure of this code is based ![请添加图片描述](https://i-blog.csdnimg.cn/direct/b2aaf634151b4706892693ffb43d9093.png) +## 🎉 News +- [x] [2024.10.15]🎯🎯📢📢LightRAG now supports Hugging Face models! + ## Install * Install from source @@ -35,17 +38,27 @@ pip install lightrag-hku ## Quick Start -* Set OpenAI API key in environment: `export OPENAI_API_KEY="sk-...".` -* Download the demo text "A Christmas Carol by Charles Dickens" +* Set OpenAI API key in environment if using OpenAI models: `export OPENAI_API_KEY="sk-...".` +* Download the demo text "A Christmas Carol by Charles Dickens": ```bash curl https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt > ./book.txt ``` -Use the below python snippet: +Use the below Python snippet to initialize LightRAG and perform queries: ```python from lightrag import LightRAG, QueryParam +from lightrag.llm import gpt_4o_mini_complete, gpt_4o_complete -rag = LightRAG(working_dir="./dickens") +WORKING_DIR = "./dickens" + +if not os.path.exists(WORKING_DIR): + os.mkdir(WORKING_DIR) + +rag = LightRAG( + working_dir=WORKING_DIR, + llm_model_func=gpt_4o_mini_complete # Use gpt_4o_mini_complete LLM model + # llm_model_func=gpt_4o_complete # Optionally, use a stronger model +) with open("./book.txt") as f: rag.insert(f.read()) @@ -62,13 +75,31 @@ print(rag.query("What are the top themes in this story?", param=QueryParam(mode= # Perform hybrid search print(rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))) ``` -Batch Insert +### Using Hugging Face Models +If you want to use Hugging Face models, you only need to set LightRAG as follows: ```python +from lightrag.llm import hf_model_complete, hf_embedding +from transformers import AutoModel, AutoTokenizer + +# Initialize LightRAG with Hugging Face model +rag = LightRAG( + working_dir=WORKING_DIR, + llm_model_func=hf_model_complete, # Use Hugging Face complete model for text generation + llm_model_name='meta-llama/Llama-3.1-8B-Instruct', # Model name from Hugging Face + embedding_func=hf_embedding, # Use Hugging Face embedding function + tokenizer=AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2"), + embed_model=AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2") +) +``` +### Batch Insert +```python +# Batch Insert: Insert multiple texts at once rag.insert(["TEXT1", "TEXT2",...]) ``` -Incremental Insert +### Incremental Insert ```python +# Incremental Insert: Insert new documents into an existing LightRAG instance rag = LightRAG(working_dir="./dickens") with open("./newText.txt") as f: