LarFii commited on
Commit
2678ed8
·
1 Parent(s): 4c55f30
examples/lightrag_hf_demo.py CHANGED
@@ -3,6 +3,7 @@ import sys
3
 
4
  from lightrag import LightRAG, QueryParam
5
  from lightrag.llm import hf_model_complete, hf_embedding
 
6
  from transformers import AutoModel,AutoTokenizer
7
 
8
  WORKING_DIR = "./dickens"
@@ -14,9 +15,13 @@ rag = LightRAG(
14
  working_dir=WORKING_DIR,
15
  llm_model_func=hf_model_complete,
16
  llm_model_name='meta-llama/Llama-3.1-8B-Instruct',
17
- embedding_func=hf_embedding,
18
- tokenizer=AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2"),
19
- embed_model=AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
 
 
 
 
20
  )
21
 
22
 
 
3
 
4
  from lightrag import LightRAG, QueryParam
5
  from lightrag.llm import hf_model_complete, hf_embedding
6
+ from lightrag.utils import EmbeddingFunc
7
  from transformers import AutoModel,AutoTokenizer
8
 
9
  WORKING_DIR = "./dickens"
 
15
  working_dir=WORKING_DIR,
16
  llm_model_func=hf_model_complete,
17
  llm_model_name='meta-llama/Llama-3.1-8B-Instruct',
18
+ embedding_func=EmbeddingFunc(
19
+ tokenizer=AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2"),
20
+ embed_model=AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2"),
21
+ embedding_dim=384,
22
+ max_token_size=5000,
23
+ func=hf_embedding
24
+ ),
25
  )
26
 
27
 
examples/lightrag_openai_demo.py CHANGED
@@ -5,15 +5,15 @@ from lightrag import LightRAG, QueryParam
5
  from lightrag.llm import gpt_4o_mini_complete, gpt_4o_complete
6
  from transformers import AutoModel,AutoTokenizer
7
 
8
- WORKING_DIR = "./dickens"
9
 
10
  if not os.path.exists(WORKING_DIR):
11
  os.mkdir(WORKING_DIR)
12
 
13
  rag = LightRAG(
14
  working_dir=WORKING_DIR,
15
- llm_model_func=gpt_4o_complete
16
- # llm_model_func=gpt_4o_mini_complete
17
  )
18
 
19
 
 
5
  from lightrag.llm import gpt_4o_mini_complete, gpt_4o_complete
6
  from transformers import AutoModel,AutoTokenizer
7
 
8
+ WORKING_DIR = "/home/zrguo/code/myrag/agriculture"
9
 
10
  if not os.path.exists(WORKING_DIR):
11
  os.mkdir(WORKING_DIR)
12
 
13
  rag = LightRAG(
14
  working_dir=WORKING_DIR,
15
+ llm_model_func=gpt_4o_mini_complete
16
+ # llm_model_func=gpt_4o_complete
17
  )
18
 
19
 
lightrag/lightrag.py CHANGED
@@ -76,12 +76,8 @@ class LightRAG:
76
  }
77
  )
78
 
79
- # text embedding
80
- tokenizer: Any = None
81
- embed_model: Any = None
82
-
83
  # embedding_func: EmbeddingFunc = field(default_factory=lambda:hf_embedding)
84
- embedding_func: EmbeddingFunc = field(default_factory=lambda:openai_embedding)#
85
  embedding_batch_num: int = 32
86
  embedding_func_max_async: int = 16
87
 
@@ -103,13 +99,6 @@ class LightRAG:
103
  convert_response_to_json_func: callable = convert_response_to_json
104
 
105
  def __post_init__(self):
106
- if callable(self.embedding_func) and self.embedding_func.__name__ == 'hf_embedding':
107
- if self.tokenizer is None:
108
- self.tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
109
- if self.embed_model is None:
110
- self.embed_model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
111
-
112
-
113
  log_file = os.path.join(self.working_dir, "lightrag.log")
114
  set_logger(log_file)
115
  logger.info(f"Logger initialized for working directory: {self.working_dir}")
@@ -139,10 +128,9 @@ class LightRAG:
139
  self.chunk_entity_relation_graph = self.graph_storage_cls(
140
  namespace="chunk_entity_relation", global_config=asdict(self)
141
  )
 
142
  self.embedding_func = limit_async_func_call(self.embedding_func_max_async)(
143
- lambda texts: self.embedding_func(texts, self.tokenizer, self.embed_model)
144
- if callable(self.embedding_func) and self.embedding_func.__name__ == 'hf_embedding'
145
- else self.embedding_func(texts)
146
  )
147
 
148
  self.entities_vdb = (
 
76
  }
77
  )
78
 
 
 
 
 
79
  # embedding_func: EmbeddingFunc = field(default_factory=lambda:hf_embedding)
80
+ embedding_func: EmbeddingFunc = field(default_factory=lambda:openai_embedding)
81
  embedding_batch_num: int = 32
82
  embedding_func_max_async: int = 16
83
 
 
99
  convert_response_to_json_func: callable = convert_response_to_json
100
 
101
  def __post_init__(self):
 
 
 
 
 
 
 
102
  log_file = os.path.join(self.working_dir, "lightrag.log")
103
  set_logger(log_file)
104
  logger.info(f"Logger initialized for working directory: {self.working_dir}")
 
128
  self.chunk_entity_relation_graph = self.graph_storage_cls(
129
  namespace="chunk_entity_relation", global_config=asdict(self)
130
  )
131
+
132
  self.embedding_func = limit_async_func_call(self.embedding_func_max_async)(
133
+ self.embedding_func
 
 
134
  )
135
 
136
  self.entities_vdb = (