From 5bde05ed533db1a9f4f948a76c29940b0f09efd2 Mon Sep 17 00:00:00 2001 From: jin <52519003+jin38324@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:19:28 +0800 Subject: [PATCH] add LightRAG init parameters in readme also fix some error --- README.md | 29 ++++++++++++++++++++ examples/lightrag_api_oracle_demo..py | 3 ++- examples/lightrag_oracle_demo.py | 4 +-- lightrag/llm.py | 2 +- lightrag/operate.py | 38 +++++++++++++-------------- 5 files changed, 53 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 6d5af135..ad0b66ef 100644 --- a/README.md +++ b/README.md @@ -511,6 +511,35 @@ if __name__ == "__main__": +### LightRAG init parameters + +| **Parameter** | **Type** | **Explanation** | **Default** | +| --- | --- | --- | --- | +| **working\_dir** | `str` | Directory where the cache will be stored | `lightrag_cache+timestamp` | +| **kv\_storage** | `str` | Storage type for documents and text chunks. Supported types: `JsonKVStorage`, `OracleKVStorage` | `JsonKVStorage` | +| **vector\_storage** | `str` | Storage type for embedding vectors. Supported types: `NanoVectorDBStorage`, `OracleVectorDBStorage` | `NanoVectorDBStorage` | +| **graph\_storage** | `str` | Storage type for graph edges and nodes. Supported types: `NetworkXStorage`, `Neo4JStorage`, `OracleGraphStorage` | `NetworkXStorage` | +| **log\_level** | | Log level for application runtime | `logging.DEBUG` | +| **chunk\_token\_size** | `int` | Maximum token size per chunk when splitting documents | `1200` | +| **chunk\_overlap\_token\_size** | `int` | Overlap token size between two chunks when splitting documents | `100` | +| **tiktoken\_model\_name** | `str` | Model name for the Tiktoken encoder used to calculate token numbers | `gpt-4o-mini` | +| **entity\_extract\_max\_gleaning** | `int` | Number of loops in the entity extraction process, appending history messages | `1` | +| **entity\_summary\_to\_max\_tokens** | `int` | Maximum token size for each entity summary | `500` | +| **node\_embedding\_algorithm** | `str` | Algorithm for node embedding (currently not used) | `node2vec` | +| **node2vec\_params** | `dict` | Parameters for node embedding | `{"dimensions": 1536,"num_walks": 10,"walk_length": 40,"window_size": 2,"iterations": 3,"random_seed": 3,}` | +| **embedding\_func** | `EmbeddingFunc` | Function to generate embedding vectors from text | `openai_embedding` | +| **embedding\_batch\_num** | `int` | Maximum batch size for embedding processes (multiple texts sent per batch) | `32` | +| **embedding\_func\_max\_async** | `int` | Maximum number of concurrent asynchronous embedding processes | `16` | +| **llm\_model\_func** | `callable` | Function for LLM generation | `gpt_4o_mini_complete` | +| **llm\_model\_name** | `str` | LLM model name for generation | `meta-llama/Llama-3.2-1B-Instruct` | +| **llm\_model\_max\_token\_size** | `int` | Maximum token size for LLM generation (affects entity relation summaries) | `32768` | +| **llm\_model\_max\_async** | `int` | Maximum number of concurrent asynchronous LLM processes | `16` | +| **llm\_model\_kwargs** | `dict` | Additional parameters for LLM generation | | +| **vector\_db\_storage\_cls\_kwargs** | `dict` | Additional parameters for vector database (currently not used) | | +| **enable\_llm\_cache** | `bool` | If `TRUE`, stores LLM results in cache; repeated prompts return cached responses | `TRUE` | +| **addon\_params** | `dict` | Additional parameters, e.g., `{"example_number": 1, "language": "Simplified Chinese"}`: sets example limit and output language | `example_number: all examples, language: English` | +| **convert\_response\_to\_json\_func** | `callable` | Not used | `convert_response_to_json` | + ## API Server Implementation LightRAG also provides a FastAPI-based server implementation for RESTful API access to RAG operations. This allows you to run LightRAG as a service and interact with it through HTTP requests. diff --git a/examples/lightrag_api_oracle_demo..py b/examples/lightrag_api_oracle_demo..py index 8aaa2cf5..774ef61f 100644 --- a/examples/lightrag_api_oracle_demo..py +++ b/examples/lightrag_api_oracle_demo..py @@ -81,7 +81,7 @@ async def get_embedding_dim(): async def init(): # Detect embedding dimension - embedding_dimension = 1024 # await get_embedding_dim() + embedding_dimension = await get_embedding_dim() print(f"Detected embedding dimension: {embedding_dimension}") # Create Oracle DB connection # The `config` parameter is the connection configuration of Oracle DB @@ -105,6 +105,7 @@ async def init(): await oracle_db.check_tables() # Initialize LightRAG # We use Oracle DB as the KV/vector/graph storage + # You can add `addon_params={"example_number": 1, "language": "Simplfied Chinese"}` to control the prompt rag = LightRAG( enable_llm_cache=False, working_dir=WORKING_DIR, diff --git a/examples/lightrag_oracle_demo.py b/examples/lightrag_oracle_demo.py index 630c1fd8..02fb569d 100644 --- a/examples/lightrag_oracle_demo.py +++ b/examples/lightrag_oracle_demo.py @@ -84,6 +84,7 @@ async def main(): # Initialize LightRAG # We use Oracle DB as the KV/vector/graph storage + # You can add `addon_params={"example_number": 1, "language": "Simplfied Chinese"}` to control the prompt rag = LightRAG( enable_llm_cache=False, working_dir=WORKING_DIR, @@ -96,8 +97,7 @@ async def main(): ), graph_storage="OracleGraphStorage", kv_storage="OracleKVStorage", - vector_storage="OracleVectorDBStorage", - addon_params={"example_number": 1, "language": "Simplfied Chinese"}, + vector_storage="OracleVectorDBStorage" ) # Setthe KV/vector/graph storage's `db` property, so all operation will use same connection pool diff --git a/lightrag/llm.py b/lightrag/llm.py index d3729941..6a191a0f 100644 --- a/lightrag/llm.py +++ b/lightrag/llm.py @@ -72,7 +72,7 @@ async def openai_complete_if_cache( content = response.choices[0].message.content if r"\u" in content: content = content.encode("utf-8").decode("unicode_escape") - print(content) + # print(content) if hashing_kv is not None: await hashing_kv.upsert( {args_hash: {"return": response.choices[0].message.content, "model": model}} diff --git a/lightrag/operate.py b/lightrag/operate.py index c4740e70..4265ebcb 100644 --- a/lightrag/operate.py +++ b/lightrag/operate.py @@ -571,19 +571,19 @@ async def _build_query_context( hl_text_units_context, ) return f""" -# -----Entities----- -# ```csv -# {entities_context} -# ``` -# -----Relationships----- -# ```csv -# {relations_context} -# ``` -# -----Sources----- -# ```csv -# {text_units_context} -# ``` -# """ +-----Entities----- +```csv +{entities_context} +``` +-----Relationships----- +```csv +{relations_context} +``` +-----Sources----- +```csv +{text_units_context} +``` +""" async def _get_node_data( @@ -593,18 +593,18 @@ async def _get_node_data( text_chunks_db: BaseKVStorage[TextChunkSchema], query_param: QueryParam, ): - # 获取相似的实体 + # get similar entities results = await entities_vdb.query(query, top_k=query_param.top_k) if not len(results): return None - # 获取实体信息 + # get entity information node_datas = await asyncio.gather( *[knowledge_graph_inst.get_node(r["entity_name"]) for r in results] ) if not all([n is not None for n in node_datas]): logger.warning("Some nodes are missing, maybe the storage is damaged") - # 获取实体的度 + # get entity degree node_degrees = await asyncio.gather( *[knowledge_graph_inst.node_degree(r["entity_name"]) for r in results] ) @@ -613,11 +613,11 @@ async def _get_node_data( for k, n, d in zip(results, node_datas, node_degrees) if n is not None ] # what is this text_chunks_db doing. dont remember it in airvx. check the diagram. - # 根据实体获取文本片段 + # get entitytext chunk use_text_units = await _find_most_related_text_unit_from_entities( node_datas, query_param, text_chunks_db, knowledge_graph_inst ) - # 获取关联的边 + # get relate edges use_relations = await _find_most_related_edges_from_entities( node_datas, query_param, knowledge_graph_inst ) @@ -625,7 +625,7 @@ async def _get_node_data( f"Local query uses {len(node_datas)} entites, {len(use_relations)} relations, {len(use_text_units)} text units" ) - # 构建提示词 + # build prompt entites_section_list = [["id", "entity", "type", "description", "rank"]] for i, n in enumerate(node_datas): entites_section_list.append(