yangdx commited on
Commit
0a1089d
·
1 Parent(s): ceab23f

Fix: top_k param handling error, unify top_k and cosine default value.

Browse files
.env.example CHANGED
@@ -32,8 +32,8 @@ MAX_EMBED_TOKENS=8192
32
  #HISTORY_TURNS=3
33
  #CHUNK_SIZE=1200
34
  #CHUNK_OVERLAP_SIZE=100
35
- #COSINE_THRESHOLD=0.4 # 0.2 while not running API server
36
- #TOP_K=50 # 60 while not running API server
37
 
38
  ### LLM Configuration (Use valid host. For local services, you can use host.docker.internal)
39
  ### Ollama example
 
32
  #HISTORY_TURNS=3
33
  #CHUNK_SIZE=1200
34
  #CHUNK_OVERLAP_SIZE=100
35
+ #COSINE_THRESHOLD=0.2
36
+ #TOP_K=60
37
 
38
  ### LLM Configuration (Use valid host. For local services, you can use host.docker.internal)
39
  ### Ollama example
lightrag/api/README.md CHANGED
@@ -103,7 +103,7 @@ After starting the lightrag-server, you can add an Ollama-type connection in the
103
 
104
  LightRAG can be configured using either command-line arguments or environment variables. When both are provided, command-line arguments take precedence over environment variables.
105
 
106
- For better performance, the API server's default values for TOP_K and COSINE_THRESHOLD are set to 50 and 0.4 respectively. If COSINE_THRESHOLD remains at its default value of 0.2 in LightRAG, many irrelevant entities and relations would be retrieved and sent to the LLM.
107
 
108
  ### Environment Variables
109
 
 
103
 
104
  LightRAG can be configured using either command-line arguments or environment variables. When both are provided, command-line arguments take precedence over environment variables.
105
 
106
+ Default `TOP_K` is set to `60`. Default `COSINE_THRESHOLD` are set to `0.2`.
107
 
108
  ### Environment Variables
109
 
lightrag/api/lightrag_server.py CHANGED
@@ -530,13 +530,13 @@ def parse_args() -> argparse.Namespace:
530
  parser.add_argument(
531
  "--top-k",
532
  type=int,
533
- default=get_env_value("TOP_K", 50, int),
534
- help="Number of most similar results to return (default: from env or 50)",
535
  )
536
  parser.add_argument(
537
  "--cosine-threshold",
538
  type=float,
539
- default=get_env_value("COSINE_THRESHOLD", 0.4, float),
540
  help="Cosine similarity threshold (default: from env or 0.4)",
541
  )
542
 
@@ -669,7 +669,13 @@ def get_api_key_dependency(api_key: Optional[str]):
669
  return api_key_auth
670
 
671
 
 
 
 
672
  def create_app(args):
 
 
 
673
  # Verify that bindings are correctly setup
674
  if args.llm_binding not in [
675
  "lollms",
@@ -1279,7 +1285,7 @@ def create_app(args):
1279
  mode=request.mode,
1280
  stream=request.stream,
1281
  only_need_context=request.only_need_context,
1282
- top_k=args.top_k,
1283
  ),
1284
  )
1285
 
@@ -1321,7 +1327,7 @@ def create_app(args):
1321
  mode=request.mode,
1322
  stream=True,
1323
  only_need_context=request.only_need_context,
1324
- top_k=args.top_k,
1325
  ),
1326
  )
1327
 
@@ -1611,7 +1617,7 @@ def create_app(args):
1611
  return await rag.get_graps(nodel_label=label, max_depth=100)
1612
 
1613
  # Add Ollama API routes
1614
- ollama_api = OllamaAPI(rag)
1615
  app.include_router(ollama_api.router, prefix="/api")
1616
 
1617
  @app.get("/documents", dependencies=[Depends(optional_api_key)])
 
530
  parser.add_argument(
531
  "--top-k",
532
  type=int,
533
+ default=get_env_value("TOP_K", 60, int),
534
+ help="Number of most similar results to return (default: from env or 60)",
535
  )
536
  parser.add_argument(
537
  "--cosine-threshold",
538
  type=float,
539
+ default=get_env_value("COSINE_THRESHOLD", 0.2, float),
540
  help="Cosine similarity threshold (default: from env or 0.4)",
541
  )
542
 
 
669
  return api_key_auth
670
 
671
 
672
+ # Global configuration
673
+ global_top_k = 60 # default value
674
+
675
  def create_app(args):
676
+ global global_top_k
677
+ global_top_k = args.top_k # save top_k from args
678
+
679
  # Verify that bindings are correctly setup
680
  if args.llm_binding not in [
681
  "lollms",
 
1285
  mode=request.mode,
1286
  stream=request.stream,
1287
  only_need_context=request.only_need_context,
1288
+ top_k=global_top_k,
1289
  ),
1290
  )
1291
 
 
1327
  mode=request.mode,
1328
  stream=True,
1329
  only_need_context=request.only_need_context,
1330
+ top_k=global_top_k,
1331
  ),
1332
  )
1333
 
 
1617
  return await rag.get_graps(nodel_label=label, max_depth=100)
1618
 
1619
  # Add Ollama API routes
1620
+ ollama_api = OllamaAPI(rag, top_k=args.top_k)
1621
  app.include_router(ollama_api.router, prefix="/api")
1622
 
1623
  @app.get("/documents", dependencies=[Depends(optional_api_key)])
lightrag/api/ollama_api.py CHANGED
@@ -148,9 +148,10 @@ def parse_query_mode(query: str) -> tuple[str, SearchMode]:
148
 
149
 
150
  class OllamaAPI:
151
- def __init__(self, rag: LightRAG):
152
  self.rag = rag
153
  self.ollama_server_infos = ollama_server_infos
 
154
  self.router = APIRouter()
155
  self.setup_routes()
156
 
@@ -381,7 +382,7 @@ class OllamaAPI:
381
  "stream": request.stream,
382
  "only_need_context": False,
383
  "conversation_history": conversation_history,
384
- "top_k": self.rag.args.top_k if hasattr(self.rag, "args") else 50,
385
  }
386
 
387
  if (
 
148
 
149
 
150
  class OllamaAPI:
151
+ def __init__(self, rag: LightRAG, top_k: int = 60):
152
  self.rag = rag
153
  self.ollama_server_infos = ollama_server_infos
154
+ self.top_k = top_k
155
  self.router = APIRouter()
156
  self.setup_routes()
157
 
 
382
  "stream": request.stream,
383
  "only_need_context": False,
384
  "conversation_history": conversation_history,
385
+ "top_k": self.top_k,
386
  }
387
 
388
  if (