flashrag-dev 0.1.4.dev20250715__tar.gz → 0.1.4.dev20250805__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/PKG-INFO +27 -1
- flashrag_dev-0.1.4.dev20250715/flashrag_dev.egg-info/PKG-INFO → flashrag_dev-0.1.4.dev20250805/README.md +26 -17
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/basic_config.yaml +4 -1
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/generator.py +3 -2
- flashrag_dev-0.1.4.dev20250805/flashrag/pipeline/ReaRAG_utils.py +150 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/__init__.py +2 -1
- flashrag_dev-0.1.4.dev20250805/flashrag/pipeline/reasoning_pipeline.py +824 -0
- flashrag_dev-0.1.4.dev20250805/flashrag/prompt/__init__.py +3 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/base_prompt.py +1 -1
- flashrag_dev-0.1.4.dev20250805/flashrag/prompt/coRAG_prompt.py +87 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/encoder.py +2 -1
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/index_builder.py +296 -27
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/retriever.py +210 -1
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/utils.py +2 -1
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/utils.py +8 -3
- flashrag_dev-0.1.4.dev20250805/flashrag/version.py +1 -0
- flashrag_dev-0.1.4.dev20250715/README.md → flashrag_dev-0.1.4.dev20250805/flashrag_dev.egg-info/PKG-INFO +43 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/SOURCES.txt +2 -0
- flashrag_dev-0.1.4.dev20250715/flashrag/pipeline/reasoning_pipeline.py +0 -154
- flashrag_dev-0.1.4.dev20250715/flashrag/prompt/__init__.py +0 -2
- flashrag_dev-0.1.4.dev20250715/flashrag/version.py +0 -1
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/LICENSE +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/config.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/dataset/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/dataset/dataset.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/dataset/utils.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/_bleu.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/evaluator.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/metrics.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/utils.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/fid.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/multimodal_generator.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/openai_generator.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/stop_word_criteria.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/utils.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/judger/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/judger/judger.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/active_pipeline.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/branching_pipeline.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/mm_pipeline.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/pipeline.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/replug_utils.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/mm_prompt.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/selfask_examplars.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/trace_examplars.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/kg_refiner.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/llmlingua_compressor.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/refiner.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/selective_context_compressor.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/__main__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/reranker.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/__init__.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/constants.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/pred_parse.py +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/dependency_links.txt +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/requires.txt +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/top_level.txt +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/pyproject.toml +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/setup.cfg +0 -0
- {flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: flashrag_dev
|
|
3
|
-
Version: 0.1.4.
|
|
3
|
+
Version: 0.1.4.dev20250805
|
|
4
4
|
Summary: A library for efficient Retrieval-Augmented Generation research
|
|
5
5
|
Home-page: https://github.com/RUC-NLPIR/FlashRAG
|
|
6
6
|
Author: Jiajie Jin, Yutao Zhu, Chenghao Zhang, Xinyu Yang, Zhicheng Dou
|
|
@@ -278,6 +278,32 @@ python -m flashrag.retriever.index_builder \
|
|
|
278
278
|
--save_dir indexes/
|
|
279
279
|
```
|
|
280
280
|
|
|
281
|
+
### For Sparse Neural Retrieval Methods (SPLADE)
|
|
282
|
+
|
|
283
|
+
##### Install Seismic Index:
|
|
284
|
+
```bash
|
|
285
|
+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # Install Rust for compiling
|
|
286
|
+
pip install pyseismic-lsr # Install Seismic
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
##### Then build the index with Seismic:
|
|
290
|
+
```bash
|
|
291
|
+
python -m flashrag.retriever.index_builder \ # builder
|
|
292
|
+
--retrieval_method splade \ # Model name to trigger seismic index (splade only available)
|
|
293
|
+
--model_path retriever/splade-v3 \ # Local path or repository path are both supported.
|
|
294
|
+
--corpus_embedded_path data/ms_marco/ms_marco_embedded_corpus.jsonl \ # Use cached embedded corpus if corpus is already available in seismic expected format
|
|
295
|
+
--corpus_path data/ms_marco/ms_marco_corpus.jsonl \ # Corpus path in format {id, contents} jsonl file to be embedded if not already built
|
|
296
|
+
--save_dir indexes/ \ # save index directory
|
|
297
|
+
--use_fp16 \ # tell to use fp16 for splade model
|
|
298
|
+
--max_length 512 \ # max tokens for each document
|
|
299
|
+
--batch_size 4 \ # batch size for splade model (4-5 seems the best size for Tesla T4 16GB)
|
|
300
|
+
--n_postings 1000 \ # seismic number of posting lists
|
|
301
|
+
--centroid_fraction 0.2 \ # seismic centroids
|
|
302
|
+
--min_cluster_size 2 \ # seismic min cluster
|
|
303
|
+
--summary_energy 0.4 \ # seismic energy
|
|
304
|
+
--batched_indexing 10000000 # seismic batch
|
|
305
|
+
--nknn 32 # Optional parameter. Tell to seismic to use also knn graph. if not present seismic will work without knn graph
|
|
306
|
+
```
|
|
281
307
|
### Using the ready-made pipeline
|
|
282
308
|
|
|
283
309
|
You can use the pipeline class we have already built (as shown in [<u>pipelines</u>](#pipelines)) to implement the RAG process inside. In this case, you just need to configure the config and load the corresponding pipeline.
|
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: flashrag-dev
|
|
3
|
-
Version: 0.1.4.dev20250715
|
|
4
|
-
Summary: A library for efficient Retrieval-Augmented Generation research
|
|
5
|
-
Home-page: https://github.com/RUC-NLPIR/FlashRAG
|
|
6
|
-
Author: Jiajie Jin, Yutao Zhu, Chenghao Zhang, Xinyu Yang, Zhicheng Dou
|
|
7
|
-
Author-email: jinjiajie@ruc.edu.cn
|
|
8
|
-
License: MIT License
|
|
9
|
-
Requires-Python: >=3.9
|
|
10
|
-
Description-Content-Type: text/markdown
|
|
11
|
-
Provides-Extra: core
|
|
12
|
-
Provides-Extra: retriever
|
|
13
|
-
Provides-Extra: generator
|
|
14
|
-
Provides-Extra: multimodal
|
|
15
|
-
Provides-Extra: full
|
|
16
|
-
License-File: LICENSE
|
|
17
|
-
|
|
18
1
|
# <div align="center">⚡FlashRAG: A Python Toolkit for Efficient RAG Research<div>
|
|
19
2
|
\[ English | [中文](README_zh.md) \]
|
|
20
3
|
<div align="center">
|
|
@@ -278,6 +261,32 @@ python -m flashrag.retriever.index_builder \
|
|
|
278
261
|
--save_dir indexes/
|
|
279
262
|
```
|
|
280
263
|
|
|
264
|
+
### For Sparse Neural Retrieval Methods (SPLADE)
|
|
265
|
+
|
|
266
|
+
##### Install Seismic Index:
|
|
267
|
+
```bash
|
|
268
|
+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # Install Rust for compiling
|
|
269
|
+
pip install pyseismic-lsr # Install Seismic
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
##### Then build the index with Seismic:
|
|
273
|
+
```bash
|
|
274
|
+
python -m flashrag.retriever.index_builder \ # builder
|
|
275
|
+
--retrieval_method splade \ # Model name to trigger seismic index (splade only available)
|
|
276
|
+
--model_path retriever/splade-v3 \ # Local path or repository path are both supported.
|
|
277
|
+
--corpus_embedded_path data/ms_marco/ms_marco_embedded_corpus.jsonl \ # Use cached embedded corpus if corpus is already available in seismic expected format
|
|
278
|
+
--corpus_path data/ms_marco/ms_marco_corpus.jsonl \ # Corpus path in format {id, contents} jsonl file to be embedded if not already built
|
|
279
|
+
--save_dir indexes/ \ # save index directory
|
|
280
|
+
--use_fp16 \ # tell to use fp16 for splade model
|
|
281
|
+
--max_length 512 \ # max tokens for each document
|
|
282
|
+
--batch_size 4 \ # batch size for splade model (4-5 seems the best size for Tesla T4 16GB)
|
|
283
|
+
--n_postings 1000 \ # seismic number of posting lists
|
|
284
|
+
--centroid_fraction 0.2 \ # seismic centroids
|
|
285
|
+
--min_cluster_size 2 \ # seismic min cluster
|
|
286
|
+
--summary_energy 0.4 \ # seismic energy
|
|
287
|
+
--batched_indexing 10000000 # seismic batch
|
|
288
|
+
--nknn 32 # Optional parameter. Tell to seismic to use also knn graph. if not present seismic will work without knn graph
|
|
289
|
+
```
|
|
281
290
|
### Using the ready-made pipeline
|
|
282
291
|
|
|
283
292
|
You can use the pipeline class we have already built (as shown in [<u>pipelines</u>](#pipelines)) to implement the RAG process inside. In this case, you just need to configure the config and load the corresponding pipeline.
|
{flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/basic_config.yaml
RENAMED
|
@@ -68,7 +68,9 @@ bm25_backend: bm25s # pyserini, bm25s
|
|
|
68
68
|
use_sentence_transformer: False
|
|
69
69
|
silent_retrieval: True # whether to silent the retrieval process
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
seismic_query_cut: 10 # parameters for seismic. See seismic paper for full details
|
|
72
|
+
seismic_heap_factor: 0.8 # parameters for seismic. See seismic paper for full details
|
|
73
|
+
# -------------------------------------------------Reranker Settings------------------------------------------------#
|
|
72
74
|
use_reranker: False # whether to use reranker
|
|
73
75
|
rerank_model_name: ~ # same as retrieval_method
|
|
74
76
|
rerank_model_path: ~ # path to reranker model, path will be automatically find in `model2path`
|
|
@@ -117,6 +119,7 @@ gpu_memory_utilization: 0.85 # ratio of gpu's memory usage for generator
|
|
|
117
119
|
# -------------------------------------------------Evaluation Settings------------------------------------------------#
|
|
118
120
|
# Metrics to evaluate the result
|
|
119
121
|
metrics: ["em", "f1", "acc", "precision", "recall", "input_tokens"]
|
|
122
|
+
|
|
120
123
|
# Specify setting for metric, will be called within certain metrics
|
|
121
124
|
metric_setting:
|
|
122
125
|
retrieval_recall_topk: 5
|
{flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/generator.py
RENAMED
|
@@ -12,6 +12,7 @@ from transformers import (
|
|
|
12
12
|
AutoConfig,
|
|
13
13
|
)
|
|
14
14
|
from flashrag.generator.utils import resolve_max_tokens
|
|
15
|
+
from flashrag.utils import get_device
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
class BaseGenerator:
|
|
@@ -553,13 +554,13 @@ class FastChatGenerator(HFCausalLMGenerator):
|
|
|
553
554
|
max_gpu_memory = None
|
|
554
555
|
import torch
|
|
555
556
|
self.gpu_num = torch.cuda.device_count()
|
|
556
|
-
if self.gpu_num
|
|
557
|
+
if self.gpu_num > 1:
|
|
557
558
|
available_gpu_memory = get_gpu_memory()
|
|
558
559
|
max_gpu_memory = str(int(min(available_gpu_memory) * gpu_memory_utilization)) + "GiB"
|
|
559
560
|
|
|
560
561
|
model, tokenizer = load_model(
|
|
561
562
|
self.model_path,
|
|
562
|
-
device=
|
|
563
|
+
device=get_device(),
|
|
563
564
|
num_gpus=self.gpu_num,
|
|
564
565
|
max_gpu_memory=max_gpu_memory,
|
|
565
566
|
load_8bit=False,
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from termcolor import colored
|
|
3
|
+
|
|
4
|
+
class AgentUtilsBase():
|
|
5
|
+
def __init__(self):
|
|
6
|
+
pass
|
|
7
|
+
|
|
8
|
+
def truncate(self, tokenizer, tokenized_prompt, model_max_length):
|
|
9
|
+
half = int(model_max_length/2)
|
|
10
|
+
prompt = tokenizer.decode(tokenized_prompt[:half], skip_special_tokens=False) + \
|
|
11
|
+
tokenizer.decode(tokenized_prompt[-half:], skip_special_tokens=False)
|
|
12
|
+
|
|
13
|
+
return prompt
|
|
14
|
+
|
|
15
|
+
def preprocess_query(self, query):
|
|
16
|
+
if "'" in query and '"' in query:
|
|
17
|
+
query = query.replace("'", "\\'").replace('"', '\\"')
|
|
18
|
+
return query
|
|
19
|
+
|
|
20
|
+
def extract_code(self, text: str) -> str:
|
|
21
|
+
triple_match = re.search(r'```[^\n]*\n(.+?)```', text, re.DOTALL)
|
|
22
|
+
single_match = re.search(r'`([^`]*)`', text, re.DOTALL)
|
|
23
|
+
if triple_match:
|
|
24
|
+
return triple_match.group(1)
|
|
25
|
+
elif single_match:
|
|
26
|
+
return single_match.group(1)
|
|
27
|
+
return text
|
|
28
|
+
|
|
29
|
+
def postprocess_agent_response(self, response):
|
|
30
|
+
"""
|
|
31
|
+
Implement the postprocess_agent_response function
|
|
32
|
+
"""
|
|
33
|
+
raise NotImplementedError("postprocess_agent_response function must be implemented in a subclass")
|
|
34
|
+
|
|
35
|
+
class AgentUtils(AgentUtilsBase):
|
|
36
|
+
def __init__(self):
|
|
37
|
+
super().__init__()
|
|
38
|
+
|
|
39
|
+
def parse_reasoning_steps(self, text: str):
|
|
40
|
+
"""
|
|
41
|
+
Parse a string containing Thought/Action/Observation steps (including multi-line)
|
|
42
|
+
and return a list of dictionaries of the form:
|
|
43
|
+
|
|
44
|
+
[
|
|
45
|
+
{
|
|
46
|
+
"1": {
|
|
47
|
+
"Thought": "...",
|
|
48
|
+
"Action": "...", # Only content inside backticks (if present)
|
|
49
|
+
"Observation": "..."
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"2": {
|
|
54
|
+
"Thought": "...",
|
|
55
|
+
"Action": "...",
|
|
56
|
+
"Observation": "..."
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
...
|
|
60
|
+
]
|
|
61
|
+
"""
|
|
62
|
+
# Regex pattern to match lines that start with "Thought X:", "Action X:", or "Observation X:".
|
|
63
|
+
pattern = re.compile(r'^(Thought|Action|Observation)\s+(\d+):', re.MULTILINE)
|
|
64
|
+
|
|
65
|
+
# This dictionary will accumulate:
|
|
66
|
+
# data_dict[step_number] = {"Thought": ..., "Action": ..., "Observation": ...}
|
|
67
|
+
data_dict = {}
|
|
68
|
+
|
|
69
|
+
# We'll track the current label (Thought/Action/Observation) and step number
|
|
70
|
+
current_label = None
|
|
71
|
+
current_step = None
|
|
72
|
+
last_pos = 0
|
|
73
|
+
|
|
74
|
+
# Find all pattern occurrences in the text
|
|
75
|
+
matches = list(pattern.finditer(text))
|
|
76
|
+
|
|
77
|
+
for match in matches:
|
|
78
|
+
# If we already have a label in progress, we can record its content
|
|
79
|
+
if current_label is not None:
|
|
80
|
+
# Slice the text from the last match's end to the start of this new match
|
|
81
|
+
content = text[last_pos:match.start()].strip()
|
|
82
|
+
# Store that content in data_dict
|
|
83
|
+
data_dict[current_step][current_label] = content
|
|
84
|
+
|
|
85
|
+
# Extract the new label and step
|
|
86
|
+
label = match.group(1) # "Thought", "Action", or "Observation"
|
|
87
|
+
step = match.group(2) # e.g. "1", "2", "3"
|
|
88
|
+
|
|
89
|
+
# Ensure a dict for this step
|
|
90
|
+
if step not in data_dict:
|
|
91
|
+
data_dict[step] = {"Thought": None, "Action": None, "Observation": None}
|
|
92
|
+
|
|
93
|
+
# Update current label/step
|
|
94
|
+
current_label = label
|
|
95
|
+
current_step = step
|
|
96
|
+
# We'll slice from here next time
|
|
97
|
+
last_pos = match.end()
|
|
98
|
+
|
|
99
|
+
# Handle the final block after the last match
|
|
100
|
+
if current_label is not None:
|
|
101
|
+
content = text[last_pos:].strip()
|
|
102
|
+
data_dict[current_step][current_label] = content
|
|
103
|
+
|
|
104
|
+
# Post-process:
|
|
105
|
+
# - For each step, extract only the text inside triple backticks for "Action".
|
|
106
|
+
for step_number in data_dict:
|
|
107
|
+
action_text = data_dict[step_number]["Action"]
|
|
108
|
+
if action_text:
|
|
109
|
+
# Extract content inside triple backticks
|
|
110
|
+
data_dict[step_number]["Action"] = self.extract_code(action_text)
|
|
111
|
+
|
|
112
|
+
# Convert our dictionary to the desired list-of-dicts structure
|
|
113
|
+
structured_data = []
|
|
114
|
+
for step_number in sorted(data_dict.keys(), key=lambda x: int(x)):
|
|
115
|
+
structured_data.append({step_number: data_dict[step_number]})
|
|
116
|
+
|
|
117
|
+
return structured_data
|
|
118
|
+
|
|
119
|
+
def postprocess_agent_response(self, response):
|
|
120
|
+
"""
|
|
121
|
+
Extract Thought and Action, then extract the dict from Action.
|
|
122
|
+
|
|
123
|
+
Return:
|
|
124
|
+
- thought: str
|
|
125
|
+
- action: dict
|
|
126
|
+
- is_valid: bool
|
|
127
|
+
"""
|
|
128
|
+
parsed_codes = self.parse_reasoning_steps(response)
|
|
129
|
+
|
|
130
|
+
thoughts = []
|
|
131
|
+
actions = []
|
|
132
|
+
for steps in parsed_codes:
|
|
133
|
+
for step_idx, step in steps.items():
|
|
134
|
+
thought = f"Thought {step_idx}: {step['Thought']}"
|
|
135
|
+
action = eval(self.extract_code(step['Action']))
|
|
136
|
+
|
|
137
|
+
assert "function" in action, f"Action does not contain 'function' key: {action}"
|
|
138
|
+
assert "parameters" in action, f"Action does not contain 'parameters' key: {action}"
|
|
139
|
+
|
|
140
|
+
thoughts.append(thought)
|
|
141
|
+
actions.append(action)
|
|
142
|
+
|
|
143
|
+
return thoughts, actions
|
|
144
|
+
|
|
145
|
+
def print_code(codes):
|
|
146
|
+
for idx, step in enumerate(codes):
|
|
147
|
+
print(f"{colored(step['thought'], 'blue')}")
|
|
148
|
+
print(colored(f"Action {idx+1}:\n```\n{step['action']}\n```", 'red'))
|
|
149
|
+
print(colored(f"Observation {idx+1}: {step['observation']}", 'yellow'))
|
|
150
|
+
# print(f"{'-'*60}")
|
{flashrag_dev-0.1.4.dev20250715 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/__init__.py
RENAMED
|
@@ -2,4 +2,5 @@ from flashrag.pipeline.mm_pipeline import *
|
|
|
2
2
|
from flashrag.pipeline.pipeline import *
|
|
3
3
|
from flashrag.pipeline.branching_pipeline import REPLUGPipeline, SuRePipeline
|
|
4
4
|
from flashrag.pipeline.active_pipeline import IterativePipeline, SelfRAGPipeline, FLAREPipeline, SelfAskPipeline, IRCOTPipeline, RQRAGPipeline
|
|
5
|
-
from flashrag.pipeline.reasoning_pipeline import
|
|
5
|
+
from flashrag.pipeline.reasoning_pipeline import *
|
|
6
|
+
from flashrag.pipeline.ReaRAG_utils import *
|