mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-08-13 05:09:03 +08:00
Log llm response on exception (#6750)
### What problem does this PR solve? Log llm response on exception ### Type of change - [x] Refactoring
This commit is contained in:
parent
724a36fcdb
commit
e7a2a4b7ff
@ -56,7 +56,8 @@ class Extractor:
|
|||||||
response = self._llm.chat(system_msg[0]["content"], hist, conf)
|
response = self._llm.chat(system_msg[0]["content"], hist, conf)
|
||||||
response = re.sub(r"<think>.*</think>", "", response, flags=re.DOTALL)
|
response = re.sub(r"<think>.*</think>", "", response, flags=re.DOTALL)
|
||||||
if response.find("**ERROR**") >= 0:
|
if response.find("**ERROR**") >= 0:
|
||||||
raise Exception(response)
|
logging.warning(f"Extractor._chat got error. response: {response}")
|
||||||
|
return ""
|
||||||
set_llm_cache(self._llm.llm_name, system, response, history, gen_conf)
|
set_llm_cache(self._llm.llm_name, system, response, history, gen_conf)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ class GraphExtractor(Extractor):
|
|||||||
self._tuple_delimiter_key: DEFAULT_TUPLE_DELIMITER,
|
self._tuple_delimiter_key: DEFAULT_TUPLE_DELIMITER,
|
||||||
self._record_delimiter_key: DEFAULT_RECORD_DELIMITER,
|
self._record_delimiter_key: DEFAULT_RECORD_DELIMITER,
|
||||||
self._completion_delimiter_key: DEFAULT_COMPLETION_DELIMITER,
|
self._completion_delimiter_key: DEFAULT_COMPLETION_DELIMITER,
|
||||||
self._entity_types_key: entity_types,
|
self._entity_types_key: ",".join(entity_types),
|
||||||
}
|
}
|
||||||
|
|
||||||
async def _process_single_content(self, chunk_key_dp: tuple[str, str], chunk_seq: int, num_chunks: int, out_results):
|
async def _process_single_content(self, chunk_key_dp: tuple[str, str], chunk_seq: int, num_chunks: int, out_results):
|
||||||
|
@ -72,41 +72,51 @@ async def run_graphrag(
|
|||||||
if not subgraph:
|
if not subgraph:
|
||||||
return
|
return
|
||||||
|
|
||||||
subgraph_nodes = set(subgraph.nodes())
|
graphrag_task_lock = RedisDistributedLock(f"graphrag_task_{kb_id}", lock_value=doc_id, timeout=3600)
|
||||||
new_graph = await merge_subgraph(
|
while True:
|
||||||
tenant_id,
|
if graphrag_task_lock.acquire():
|
||||||
kb_id,
|
break
|
||||||
doc_id,
|
callback(msg=f"merge_subgraph {doc_id} is waiting graphrag_task_lock")
|
||||||
subgraph,
|
await trio.sleep(20)
|
||||||
embedding_model,
|
|
||||||
callback,
|
|
||||||
)
|
|
||||||
assert new_graph is not None
|
|
||||||
|
|
||||||
if not with_resolution or not with_community:
|
try:
|
||||||
return
|
subgraph_nodes = set(subgraph.nodes())
|
||||||
|
new_graph = await merge_subgraph(
|
||||||
if with_resolution:
|
|
||||||
await resolve_entities(
|
|
||||||
new_graph,
|
|
||||||
subgraph_nodes,
|
|
||||||
tenant_id,
|
tenant_id,
|
||||||
kb_id,
|
kb_id,
|
||||||
doc_id,
|
doc_id,
|
||||||
chat_model,
|
subgraph,
|
||||||
embedding_model,
|
|
||||||
callback,
|
|
||||||
)
|
|
||||||
if with_community:
|
|
||||||
await extract_community(
|
|
||||||
new_graph,
|
|
||||||
tenant_id,
|
|
||||||
kb_id,
|
|
||||||
doc_id,
|
|
||||||
chat_model,
|
|
||||||
embedding_model,
|
embedding_model,
|
||||||
callback,
|
callback,
|
||||||
)
|
)
|
||||||
|
assert new_graph is not None
|
||||||
|
|
||||||
|
if not with_resolution or not with_community:
|
||||||
|
return
|
||||||
|
|
||||||
|
if with_resolution:
|
||||||
|
await resolve_entities(
|
||||||
|
new_graph,
|
||||||
|
subgraph_nodes,
|
||||||
|
tenant_id,
|
||||||
|
kb_id,
|
||||||
|
doc_id,
|
||||||
|
chat_model,
|
||||||
|
embedding_model,
|
||||||
|
callback,
|
||||||
|
)
|
||||||
|
if with_community:
|
||||||
|
await extract_community(
|
||||||
|
new_graph,
|
||||||
|
tenant_id,
|
||||||
|
kb_id,
|
||||||
|
doc_id,
|
||||||
|
chat_model,
|
||||||
|
embedding_model,
|
||||||
|
callback,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
graphrag_task_lock.release()
|
||||||
now = trio.current_time()
|
now = trio.current_time()
|
||||||
callback(msg=f"GraphRAG for doc {doc_id} done in {now - start:.2f} seconds.")
|
callback(msg=f"GraphRAG for doc {doc_id} done in {now - start:.2f} seconds.")
|
||||||
return
|
return
|
||||||
@ -191,13 +201,6 @@ async def merge_subgraph(
|
|||||||
embedding_model,
|
embedding_model,
|
||||||
callback,
|
callback,
|
||||||
):
|
):
|
||||||
graphrag_task_lock = RedisDistributedLock(f"graphrag_task_{kb_id}", lock_value=doc_id, timeout=600)
|
|
||||||
while True:
|
|
||||||
if graphrag_task_lock.acquire():
|
|
||||||
break
|
|
||||||
callback(msg=f"merge_subgraph {doc_id} is waiting graphrag_task_lock")
|
|
||||||
await trio.sleep(10)
|
|
||||||
|
|
||||||
start = trio.current_time()
|
start = trio.current_time()
|
||||||
change = GraphChange()
|
change = GraphChange()
|
||||||
old_graph = await get_graph(tenant_id, kb_id)
|
old_graph = await get_graph(tenant_id, kb_id)
|
||||||
@ -214,7 +217,6 @@ async def merge_subgraph(
|
|||||||
new_graph.nodes[node_name]["pagerank"] = pagerank
|
new_graph.nodes[node_name]["pagerank"] = pagerank
|
||||||
|
|
||||||
await set_graph(tenant_id, kb_id, embedding_model, new_graph, change, callback)
|
await set_graph(tenant_id, kb_id, embedding_model, new_graph, change, callback)
|
||||||
graphrag_task_lock.release()
|
|
||||||
now = trio.current_time()
|
now = trio.current_time()
|
||||||
callback(
|
callback(
|
||||||
msg=f"merging subgraph for doc {doc_id} into the global graph done in {now - start:.2f} seconds."
|
msg=f"merging subgraph for doc {doc_id} into the global graph done in {now - start:.2f} seconds."
|
||||||
@ -232,13 +234,6 @@ async def resolve_entities(
|
|||||||
embed_bdl,
|
embed_bdl,
|
||||||
callback,
|
callback,
|
||||||
):
|
):
|
||||||
graphrag_task_lock = RedisDistributedLock(f"graphrag_task_{kb_id}", lock_value=doc_id, timeout=600)
|
|
||||||
while True:
|
|
||||||
if graphrag_task_lock.acquire():
|
|
||||||
break
|
|
||||||
callback(msg=f"resolve_entities {doc_id} is waiting graphrag_task_lock")
|
|
||||||
await trio.sleep(10)
|
|
||||||
|
|
||||||
start = trio.current_time()
|
start = trio.current_time()
|
||||||
er = EntityResolution(
|
er = EntityResolution(
|
||||||
llm_bdl,
|
llm_bdl,
|
||||||
@ -250,7 +245,6 @@ async def resolve_entities(
|
|||||||
callback(msg="Graph resolution updated pagerank.")
|
callback(msg="Graph resolution updated pagerank.")
|
||||||
|
|
||||||
await set_graph(tenant_id, kb_id, embed_bdl, graph, change, callback)
|
await set_graph(tenant_id, kb_id, embed_bdl, graph, change, callback)
|
||||||
graphrag_task_lock.release()
|
|
||||||
now = trio.current_time()
|
now = trio.current_time()
|
||||||
callback(msg=f"Graph resolution done in {now - start:.2f}s.")
|
callback(msg=f"Graph resolution done in {now - start:.2f}s.")
|
||||||
|
|
||||||
@ -264,13 +258,6 @@ async def extract_community(
|
|||||||
embed_bdl,
|
embed_bdl,
|
||||||
callback,
|
callback,
|
||||||
):
|
):
|
||||||
graphrag_task_lock = RedisDistributedLock(f"graphrag_task_{kb_id}", lock_value=doc_id, timeout=600)
|
|
||||||
while True:
|
|
||||||
if graphrag_task_lock.acquire():
|
|
||||||
break
|
|
||||||
callback(msg=f"extract_community {doc_id} is waiting graphrag_task_lock")
|
|
||||||
await trio.sleep(10)
|
|
||||||
|
|
||||||
start = trio.current_time()
|
start = trio.current_time()
|
||||||
ext = CommunityReportsExtractor(
|
ext = CommunityReportsExtractor(
|
||||||
llm_bdl,
|
llm_bdl,
|
||||||
@ -326,7 +313,6 @@ async def extract_community(
|
|||||||
error_message = f"Insert chunk error: {doc_store_result}, please check log file and Elasticsearch/Infinity status!"
|
error_message = f"Insert chunk error: {doc_store_result}, please check log file and Elasticsearch/Infinity status!"
|
||||||
raise Exception(error_message)
|
raise Exception(error_message)
|
||||||
|
|
||||||
graphrag_task_lock.release()
|
|
||||||
now = trio.current_time()
|
now = trio.current_time()
|
||||||
callback(
|
callback(
|
||||||
msg=f"Graph indexed {len(cr.structured_output)} communities in {now - start:.2f}s."
|
msg=f"Graph indexed {len(cr.structured_output)} communities in {now - start:.2f}s."
|
||||||
|
@ -82,7 +82,7 @@ class Base(ABC):
|
|||||||
return ERROR_CONNECTION
|
return ERROR_CONNECTION
|
||||||
elif "quota" in error_str or "capacity" in error_str or "credit" in error_str or "billing" in error_str or "limit" in error_str and "rate" not in error_str:
|
elif "quota" in error_str or "capacity" in error_str or "credit" in error_str or "billing" in error_str or "limit" in error_str and "rate" not in error_str:
|
||||||
return ERROR_QUOTA
|
return ERROR_QUOTA
|
||||||
elif "filter" in error_str or "content" in error_str or "policy" in error_str or "blocked" in error_str or "safety" in error_str:
|
elif "filter" in error_str or "content" in error_str or "policy" in error_str or "blocked" in error_str or "safety" in error_str or "inappropriate" in error_str:
|
||||||
return ERROR_CONTENT_FILTER
|
return ERROR_CONTENT_FILTER
|
||||||
elif "model" in error_str or "not found" in error_str or "does not exist" in error_str or "not available" in error_str:
|
elif "model" in error_str or "not found" in error_str or "does not exist" in error_str or "not available" in error_str:
|
||||||
return ERROR_MODEL
|
return ERROR_MODEL
|
||||||
@ -110,6 +110,7 @@ class Base(ABC):
|
|||||||
ans += LENGTH_NOTIFICATION_EN
|
ans += LENGTH_NOTIFICATION_EN
|
||||||
return ans, self.total_token_count(response)
|
return ans, self.total_token_count(response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logging.exception("chat_model.Base.chat got exception")
|
||||||
# Classify the error
|
# Classify the error
|
||||||
error_code = self._classify_error(e)
|
error_code = self._classify_error(e)
|
||||||
|
|
||||||
@ -124,7 +125,7 @@ class Base(ABC):
|
|||||||
# For non-rate limit errors or the last attempt, return an error message
|
# For non-rate limit errors or the last attempt, return an error message
|
||||||
if attempt == self.max_retries - 1:
|
if attempt == self.max_retries - 1:
|
||||||
error_code = ERROR_MAX_RETRIES
|
error_code = ERROR_MAX_RETRIES
|
||||||
return f"{ERROR_PREFIX}: {error_code} - {str(e)}", 0
|
return f"{ERROR_PREFIX}: {error_code} - {str(e)}. response: {response}", 0
|
||||||
|
|
||||||
def chat_streamly(self, system, history, gen_conf):
|
def chat_streamly(self, system, history, gen_conf):
|
||||||
if system:
|
if system:
|
||||||
|
@ -318,4 +318,4 @@ class RedisDistributedLock:
|
|||||||
return self.lock.acquire(token=self.lock_value)
|
return self.lock.acquire(token=self.lock_value)
|
||||||
|
|
||||||
def release(self):
|
def release(self):
|
||||||
return self.lock.release()
|
REDIS_CONN.delete_if_equal(self.lock_key, self.lock_value)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user