mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-08-13 01:39:02 +08:00
Fix raptor issue (#3737)
### What problem does this PR solve? #3732 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
parent
a0c0a957b4
commit
27cd765d6f
@ -33,7 +33,7 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
self._prompt = prompt
|
self._prompt = prompt
|
||||||
self._max_token = max_token
|
self._max_token = max_token
|
||||||
|
|
||||||
def _get_optimal_clusters(self, embeddings: np.ndarray, random_state:int):
|
def _get_optimal_clusters(self, embeddings: np.ndarray, random_state: int):
|
||||||
max_clusters = min(self._max_cluster, len(embeddings))
|
max_clusters = min(self._max_cluster, len(embeddings))
|
||||||
n_clusters = np.arange(1, max_clusters)
|
n_clusters = np.arange(1, max_clusters)
|
||||||
bics = []
|
bics = []
|
||||||
@ -44,7 +44,7 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
optimal_clusters = n_clusters[np.argmin(bics)]
|
optimal_clusters = n_clusters[np.argmin(bics)]
|
||||||
return optimal_clusters
|
return optimal_clusters
|
||||||
|
|
||||||
def __call__(self, chunks: tuple[str, np.ndarray], random_state, callback=None):
|
def __call__(self, chunks, random_state, callback=None):
|
||||||
layers = [(0, len(chunks))]
|
layers = [(0, len(chunks))]
|
||||||
start, end = 0, len(chunks)
|
start, end = 0, len(chunks)
|
||||||
if len(chunks) <= 1: return
|
if len(chunks) <= 1: return
|
||||||
@ -54,13 +54,15 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
nonlocal chunks
|
nonlocal chunks
|
||||||
try:
|
try:
|
||||||
texts = [chunks[i][0] for i in ck_idx]
|
texts = [chunks[i][0] for i in ck_idx]
|
||||||
len_per_chunk = int((self._llm_model.max_length - self._max_token)/len(texts))
|
len_per_chunk = int((self._llm_model.max_length - self._max_token) / len(texts))
|
||||||
cluster_content = "\n".join([truncate(t, max(1, len_per_chunk)) for t in texts])
|
cluster_content = "\n".join([truncate(t, max(1, len_per_chunk)) for t in texts])
|
||||||
cnt = self._llm_model.chat("You're a helpful assistant.",
|
cnt = self._llm_model.chat("You're a helpful assistant.",
|
||||||
[{"role": "user", "content": self._prompt.format(cluster_content=cluster_content)}],
|
[{"role": "user",
|
||||||
{"temperature": 0.3, "max_tokens": self._max_token}
|
"content": self._prompt.format(cluster_content=cluster_content)}],
|
||||||
)
|
{"temperature": 0.3, "max_tokens": self._max_token}
|
||||||
cnt = re.sub("(······\n由于长度的原因,回答被截断了,要继续吗?|For the content length reason, it stopped, continue?)", "", cnt)
|
)
|
||||||
|
cnt = re.sub("(······\n由于长度的原因,回答被截断了,要继续吗?|For the content length reason, it stopped, continue?)", "",
|
||||||
|
cnt)
|
||||||
logging.debug(f"SUM: {cnt}")
|
logging.debug(f"SUM: {cnt}")
|
||||||
embds, _ = self._embd_model.encode([cnt])
|
embds, _ = self._embd_model.encode([cnt])
|
||||||
with lock:
|
with lock:
|
||||||
@ -74,10 +76,10 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
while end - start > 1:
|
while end - start > 1:
|
||||||
embeddings = [embd for _, embd in chunks[start: end]]
|
embeddings = [embd for _, embd in chunks[start: end]]
|
||||||
if len(embeddings) == 2:
|
if len(embeddings) == 2:
|
||||||
summarize([start, start+1], Lock())
|
summarize([start, start + 1], Lock())
|
||||||
if callback:
|
if callback:
|
||||||
callback(msg="Cluster one layer: {} -> {}".format(end-start, len(chunks)-end))
|
callback(msg="Cluster one layer: {} -> {}".format(end - start, len(chunks) - end))
|
||||||
labels.extend([0,0])
|
labels.extend([0, 0])
|
||||||
layers.append((end, len(chunks)))
|
layers.append((end, len(chunks)))
|
||||||
start = end
|
start = end
|
||||||
end = len(chunks)
|
end = len(chunks)
|
||||||
@ -85,7 +87,7 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
|
|
||||||
n_neighbors = int((len(embeddings) - 1) ** 0.8)
|
n_neighbors = int((len(embeddings) - 1) ** 0.8)
|
||||||
reduced_embeddings = umap.UMAP(
|
reduced_embeddings = umap.UMAP(
|
||||||
n_neighbors=max(2, n_neighbors), n_components=min(12, len(embeddings)-2), metric="cosine"
|
n_neighbors=max(2, n_neighbors), n_components=min(12, len(embeddings) - 2), metric="cosine"
|
||||||
).fit_transform(embeddings)
|
).fit_transform(embeddings)
|
||||||
n_clusters = self._get_optimal_clusters(reduced_embeddings, random_state)
|
n_clusters = self._get_optimal_clusters(reduced_embeddings, random_state)
|
||||||
if n_clusters == 1:
|
if n_clusters == 1:
|
||||||
@ -100,7 +102,7 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
with ThreadPoolExecutor(max_workers=12) as executor:
|
with ThreadPoolExecutor(max_workers=12) as executor:
|
||||||
threads = []
|
threads = []
|
||||||
for c in range(n_clusters):
|
for c in range(n_clusters):
|
||||||
ck_idx = [i+start for i in range(len(lbls)) if lbls[i] == c]
|
ck_idx = [i + start for i in range(len(lbls)) if lbls[i] == c]
|
||||||
threads.append(executor.submit(summarize, ck_idx, lock))
|
threads.append(executor.submit(summarize, ck_idx, lock))
|
||||||
wait(threads, return_when=ALL_COMPLETED)
|
wait(threads, return_when=ALL_COMPLETED)
|
||||||
logging.debug(str([t.result() for t in threads]))
|
logging.debug(str([t.result() for t in threads]))
|
||||||
@ -109,7 +111,9 @@ class RecursiveAbstractiveProcessing4TreeOrganizedRetrieval:
|
|||||||
labels.extend(lbls)
|
labels.extend(lbls)
|
||||||
layers.append((end, len(chunks)))
|
layers.append((end, len(chunks)))
|
||||||
if callback:
|
if callback:
|
||||||
callback(msg="Cluster one layer: {} -> {}".format(end-start, len(chunks)-end))
|
callback(msg="Cluster one layer: {} -> {}".format(end - start, len(chunks) - end))
|
||||||
start = end
|
start = end
|
||||||
end = len(chunks)
|
end = len(chunks)
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
|
||||||
|
@ -344,7 +344,7 @@ def run_raptor(row, chat_mdl, embd_mdl, callback=None):
|
|||||||
row["parser_config"]["raptor"]["threshold"]
|
row["parser_config"]["raptor"]["threshold"]
|
||||||
)
|
)
|
||||||
original_length = len(chunks)
|
original_length = len(chunks)
|
||||||
raptor(chunks, row["parser_config"]["raptor"]["random_seed"], callback)
|
chunks = raptor(chunks, row["parser_config"]["raptor"]["random_seed"], callback)
|
||||||
doc = {
|
doc = {
|
||||||
"doc_id": row["doc_id"],
|
"doc_id": row["doc_id"],
|
||||||
"kb_id": [str(row["kb_id"])],
|
"kb_id": [str(row["kb_id"])],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user