flashrag-dev 0.1.4.dev20250720__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.
Files changed (66) hide show
  1. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/PKG-INFO +1 -1
  2. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/generator.py +3 -2
  3. flashrag_dev-0.1.4.dev20250805/flashrag/pipeline/ReaRAG_utils.py +150 -0
  4. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/__init__.py +2 -1
  5. flashrag_dev-0.1.4.dev20250805/flashrag/pipeline/reasoning_pipeline.py +824 -0
  6. flashrag_dev-0.1.4.dev20250805/flashrag/prompt/__init__.py +3 -0
  7. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/base_prompt.py +1 -1
  8. flashrag_dev-0.1.4.dev20250805/flashrag/prompt/coRAG_prompt.py +87 -0
  9. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/encoder.py +2 -1
  10. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/retriever.py +4 -2
  11. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/utils.py +2 -1
  12. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/utils.py +4 -1
  13. flashrag_dev-0.1.4.dev20250805/flashrag/version.py +1 -0
  14. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/PKG-INFO +1 -1
  15. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/SOURCES.txt +2 -0
  16. flashrag_dev-0.1.4.dev20250720/flashrag/pipeline/reasoning_pipeline.py +0 -154
  17. flashrag_dev-0.1.4.dev20250720/flashrag/prompt/__init__.py +0 -2
  18. flashrag_dev-0.1.4.dev20250720/flashrag/version.py +0 -1
  19. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/LICENSE +0 -0
  20. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/README.md +0 -0
  21. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/__init__.py +0 -0
  22. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/__init__.py +0 -0
  23. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/basic_config.yaml +0 -0
  24. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/config/config.py +0 -0
  25. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/dataset/__init__.py +0 -0
  26. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/dataset/dataset.py +0 -0
  27. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/dataset/utils.py +0 -0
  28. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/__init__.py +0 -0
  29. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/_bleu.py +0 -0
  30. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/evaluator.py +0 -0
  31. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/metrics.py +0 -0
  32. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/evaluator/utils.py +0 -0
  33. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/__init__.py +0 -0
  34. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/fid.py +0 -0
  35. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/multimodal_generator.py +0 -0
  36. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/openai_generator.py +0 -0
  37. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/stop_word_criteria.py +0 -0
  38. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/generator/utils.py +0 -0
  39. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/judger/__init__.py +0 -0
  40. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/judger/judger.py +0 -0
  41. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/active_pipeline.py +0 -0
  42. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/branching_pipeline.py +0 -0
  43. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/mm_pipeline.py +0 -0
  44. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/pipeline.py +0 -0
  45. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/pipeline/replug_utils.py +0 -0
  46. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/mm_prompt.py +0 -0
  47. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/selfask_examplars.py +0 -0
  48. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/prompt/trace_examplars.py +0 -0
  49. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/__init__.py +0 -0
  50. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/kg_refiner.py +0 -0
  51. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/llmlingua_compressor.py +0 -0
  52. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/refiner.py +0 -0
  53. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/refiner/selective_context_compressor.py +0 -0
  54. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/__init__.py +0 -0
  55. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/__main__.py +0 -0
  56. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/index_builder.py +0 -0
  57. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/retriever/reranker.py +0 -0
  58. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/__init__.py +0 -0
  59. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/constants.py +0 -0
  60. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag/utils/pred_parse.py +0 -0
  61. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/dependency_links.txt +0 -0
  62. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/requires.txt +0 -0
  63. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/flashrag_dev.egg-info/top_level.txt +0 -0
  64. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/pyproject.toml +0 -0
  65. {flashrag_dev-0.1.4.dev20250720 → flashrag_dev-0.1.4.dev20250805}/setup.cfg +0 -0
  66. {flashrag_dev-0.1.4.dev20250720 → 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.dev20250720
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
@@ -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 != 1:
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="cuda",
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}")
@@ -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 ReasoningPipeline
5
+ from flashrag.pipeline.reasoning_pipeline import *
6
+ from flashrag.pipeline.ReaRAG_utils import *