flashrag-dev 0.1.4.dev20250208__tar.gz → 0.1.4.dev20250209__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 (61) hide show
  1. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/PKG-INFO +1 -1
  2. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/evaluator/evaluator.py +1 -1
  3. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/evaluator/metrics.py +95 -0
  4. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/openai_generator.py +49 -41
  5. flashrag_dev-0.1.4.dev20250209/flashrag/version.py +1 -0
  6. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag_dev.egg-info/PKG-INFO +1 -1
  7. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag_dev.egg-info/requires.txt +6 -0
  8. flashrag_dev-0.1.4.dev20250208/flashrag/version.py +0 -1
  9. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/LICENSE +0 -0
  10. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/README.md +0 -0
  11. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/__init__.py +0 -0
  12. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/config/__init__.py +0 -0
  13. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/config/basic_config.yaml +0 -0
  14. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/config/config.py +0 -0
  15. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/dataset/__init__.py +0 -0
  16. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/dataset/dataset.py +0 -0
  17. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/dataset/utils.py +0 -0
  18. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/evaluator/__init__.py +0 -0
  19. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/evaluator/_bleu.py +0 -0
  20. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/evaluator/utils.py +0 -0
  21. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/__init__.py +0 -0
  22. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/fid.py +0 -0
  23. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/generator.py +0 -0
  24. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/multimodal_generator.py +0 -0
  25. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/stop_word_criteria.py +0 -0
  26. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/generator/utils.py +0 -0
  27. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/judger/__init__.py +0 -0
  28. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/judger/judger.py +0 -0
  29. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/pipeline/__init__.py +0 -0
  30. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/pipeline/active_pipeline.py +0 -0
  31. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/pipeline/branching_pipeline.py +0 -0
  32. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/pipeline/mm_pipeline.py +0 -0
  33. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/pipeline/pipeline.py +0 -0
  34. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/pipeline/replug_utils.py +0 -0
  35. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/prompt/__init__.py +0 -0
  36. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/prompt/base_prompt.py +0 -0
  37. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/prompt/mm_prompt.py +0 -0
  38. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/prompt/selfask_examplars.py +0 -0
  39. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/prompt/trace_examplars.py +0 -0
  40. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/refiner/__init__.py +0 -0
  41. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/refiner/kg_refiner.py +0 -0
  42. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/refiner/llmlingua_compressor.py +0 -0
  43. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/refiner/refiner.py +0 -0
  44. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/refiner/selective_context_compressor.py +0 -0
  45. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/__init__.py +0 -0
  46. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/__main__.py +0 -0
  47. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/encoder.py +0 -0
  48. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/index_builder.py +0 -0
  49. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/reranker.py +0 -0
  50. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/retriever.py +0 -0
  51. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/retriever/utils.py +0 -0
  52. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/utils/__init__.py +0 -0
  53. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/utils/constants.py +0 -0
  54. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/utils/pred_parse.py +0 -0
  55. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag/utils/utils.py +0 -0
  56. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag_dev.egg-info/SOURCES.txt +0 -0
  57. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag_dev.egg-info/dependency_links.txt +0 -0
  58. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/flashrag_dev.egg-info/top_level.txt +0 -0
  59. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/pyproject.toml +0 -0
  60. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/setup.cfg +0 -0
  61. {flashrag_dev-0.1.4.dev20250208 → flashrag_dev-0.1.4.dev20250209}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: flashrag_dev
3
- Version: 0.1.4.dev20250208
3
+ Version: 0.1.4.dev20250209
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
@@ -15,7 +15,7 @@ class Evaluator:
15
15
 
16
16
  self.avaliable_metrics = self._collect_metrics()
17
17
 
18
- self.metric_class = {}
18
+ self.metric_class = {}
19
19
  for metric in self.metrics:
20
20
  if metric in self.avaliable_metrics:
21
21
  self.metric_class[metric] = self.avaliable_metrics[metric](self.config)
@@ -307,6 +307,8 @@ class Rouge_Score(BaseMetric):
307
307
  return output
308
308
 
309
309
 
310
+
311
+
310
312
  class Rouge_1(Rouge_Score):
311
313
  metric_name = "rouge-1"
312
314
 
@@ -364,6 +366,99 @@ class Rouge_L(Rouge_Score):
364
366
  return {"rouge-l": score}, metric_score_list
365
367
 
366
368
 
369
+
370
+ class ZH_Rouge_Score(BaseMetric):
371
+ metric_name = "zh_rouge_score"
372
+ cached_scores = {}
373
+
374
+ def __init__(self, config):
375
+ super().__init__(config)
376
+ from rouge_chinese import Rouge
377
+
378
+ self.scorer = Rouge()
379
+
380
+ def calculate_rouge(self, pred, golden_answers):
381
+ import jieba
382
+ if (pred, tuple(golden_answers)) in self.cached_scores:
383
+ return self.cached_scores[(pred, tuple(golden_answers))]
384
+ output = {}
385
+ pred = ' '.join(jieba.cut(pred))
386
+ for answer in golden_answers:
387
+ answer = ' '.join(jieba.cut(answer))
388
+ scores = self.scorer.get_scores(pred, answer)
389
+ for key in ["rouge-1", "rouge-2", "rouge-l"]:
390
+ if key not in output:
391
+ output[key] = []
392
+ output[key].append(scores[0][key]["f"])
393
+ for k, v in output.items():
394
+ output[k] = max(v)
395
+
396
+ self.cached_scores[(pred, tuple(golden_answers))] = output
397
+ return output
398
+
399
+
400
+
401
+
402
+ class ZH_Rouge_1(ZH_Rouge_Score):
403
+ metric_name = "zh_rouge-1"
404
+
405
+ def __init__(self, config):
406
+ super().__init__(config)
407
+
408
+
409
+ def calculate_metric(self, data):
410
+ golden_answers_list = self.get_dataset_answer(data)
411
+ pred_list = data.pred
412
+
413
+ metric_score_list = [
414
+ self.calculate_rouge(pred, golden_answers)["rouge-1"]
415
+ for pred, golden_answers in zip(pred_list, golden_answers_list)
416
+ ]
417
+ score = sum(metric_score_list) / len(metric_score_list)
418
+
419
+ return {"zh_rouge-1": score}, metric_score_list
420
+
421
+
422
+ class ZH_Rouge_2(ZH_Rouge_Score):
423
+ metric_name = "zh_rouge-2"
424
+
425
+ def __init__(self, config):
426
+ super().__init__(config)
427
+
428
+ def calculate_metric(self, data):
429
+ golden_answers_list = self.get_dataset_answer(data)
430
+ pred_list = data.pred
431
+
432
+ metric_score_list = [
433
+ self.calculate_rouge(pred, golden_answers)["rouge-2"]
434
+ for pred, golden_answers in zip(pred_list, golden_answers_list)
435
+ ]
436
+ score = sum(metric_score_list) / len(metric_score_list)
437
+
438
+ return {"zh_rouge-2": score}, metric_score_list
439
+
440
+
441
+ class ZH_Rouge_L(ZH_Rouge_Score):
442
+ metric_name = "zh_rouge-l"
443
+
444
+ def __init__(self, config):
445
+ super().__init__(config)
446
+
447
+ def calculate_metric(self, data):
448
+ golden_answers_list = self.get_dataset_answer(data)
449
+ pred_list = data.pred
450
+
451
+ metric_score_list = [
452
+ self.calculate_rouge(pred, golden_answers)["rouge-l"]
453
+ for pred, golden_answers in zip(pred_list, golden_answers_list)
454
+ ]
455
+ score = sum(metric_score_list) / len(metric_score_list)
456
+
457
+ return {"zh_rouge-l": score}, metric_score_list
458
+
459
+
460
+
461
+
367
462
  class BLEU(BaseMetric):
368
463
  metric_name = "bleu"
369
464
 
@@ -4,11 +4,20 @@ from copy import deepcopy
4
4
  import warnings
5
5
  from tqdm import tqdm
6
6
  import numpy as np
7
-
7
+ import threading
8
8
  import asyncio
9
9
  from openai import AsyncOpenAI, AsyncAzureOpenAI
10
10
  import tiktoken
11
11
 
12
+ _background_loop = None
13
+
14
+ def get_background_loop():
15
+ global _background_loop
16
+ if _background_loop is None:
17
+ _background_loop = asyncio.new_event_loop()
18
+ t = threading.Thread(target=lambda: _background_loop.run_forever(), daemon=True)
19
+ t.start()
20
+ return _background_loop
12
21
 
13
22
  class OpenaiGenerator:
14
23
  """Class for api-based openai models"""
@@ -54,32 +63,35 @@ class OpenaiGenerator:
54
63
  def update_additional_setting(self):
55
64
  pass
56
65
 
57
- async def get_response(self, input: List, **params):
58
- response = await self.client.chat.completions.create(model=self.model_name, messages=input, **params)
66
+ async def _get_response(self, messages: List, **params):
67
+ response = await self.client.chat.completions.create(
68
+ model=self.model_name, messages=messages, **params
69
+ )
70
+ if not response.choices:
71
+ raise ValueError("No choices returned from API.")
59
72
  return response.choices[0]
60
73
 
61
- async def get_batch_response(self, input_list: List[List], batch_size, **params):
62
- total_input = [self.get_response(input, **params) for input in input_list]
63
- all_result = []
64
- for idx in tqdm(range(0, len(input_list), batch_size), desc="Generation process: "):
65
- batch_input = total_input[idx : idx + batch_size]
66
- batch_result = await asyncio.gather(*batch_input)
67
- all_result.extend(batch_result)
68
-
69
- return all_result
70
-
71
- def generate(self, input_list: List[List], batch_size=None, return_scores=False, **params) -> List[str]:
72
- # deal with single input
73
- if len(input_list) == 1 and isinstance(input_list[0], dict):
74
+ async def _get_batch_response(self, input_list: List[List], batch_size, **params):
75
+ tasks = [self._get_response(messages, **params) for messages in input_list]
76
+ all_results = []
77
+ for idx in tqdm(range(0, len(tasks), batch_size), desc="Generation process: "):
78
+ batch_tasks = tasks[idx: idx + batch_size]
79
+ batch_results = await asyncio.gather(*batch_tasks)
80
+ all_results.extend(batch_results)
81
+ return all_results
82
+
83
+ async def _generate_async(self, input_list: List, batch_size=None, return_scores=False, **params) -> List[str]:
84
+ if isinstance(input_list, dict):
85
+ input_list = [[input_list]]
86
+ elif isinstance(input_list[0], dict):
74
87
  input_list = [input_list]
88
+
75
89
  if batch_size is None:
76
90
  batch_size = self.batch_size
77
91
 
78
- # deal with generation params
79
92
  generation_params = deepcopy(self.generation_params)
80
93
  generation_params.update(params)
81
- if "do_sample" in generation_params:
82
- generation_params.pop("do_sample")
94
+ generation_params.pop("do_sample", None)
83
95
 
84
96
  max_tokens = params.pop("max_tokens", None) or params.pop("max_new_tokens", None)
85
97
  if max_tokens is not None:
@@ -91,30 +103,26 @@ class OpenaiGenerator:
91
103
  generation_params.pop("max_new_tokens", None)
92
104
 
93
105
  if return_scores:
94
- if generation_params.get("logprobs") is not None:
95
- generation_params["logprobs"] = True
96
- warnings.warn("Set logprobs to True to get generation scores.")
97
- else:
98
- generation_params["logprobs"] = True
99
-
100
- if generation_params.get("n") is not None:
101
- generation_params["n"] = 1
102
- warnings.warn("Set n to 1. It can minimize costs.")
103
- else:
104
- generation_params["n"] = 1
106
+ generation_params["logprobs"] = True
107
+ warnings.warn("Set logprobs to True to get generation scores.")
108
+
105
109
 
106
- loop = asyncio.get_event_loop()
107
- result = loop.run_until_complete(self.get_batch_response(input_list, batch_size, **generation_params))
110
+ results = await self._get_batch_response(input_list, batch_size, **generation_params)
108
111
 
109
- # parse result into response text and logprob
112
+ response_texts = []
110
113
  scores = []
111
- response_text = []
112
- for res in result:
113
- response_text.append(res.message.content)
114
+ for res in results:
115
+ response_texts.append(res.message.content)
114
116
  if return_scores:
115
- score = np.exp(list(map(lambda x: x.logprob, res.logprobs.content)))
117
+ score = np.exp([item.logprob for item in res.logprobs.content])
116
118
  scores.append(score)
117
- if return_scores:
118
- return response_text, scores
119
- else:
120
- return response_text
119
+ return (response_texts, scores) if return_scores else response_texts
120
+
121
+ # ----------------- 同步包装接口 -----------------
122
+ def generate(self, input_list: List, batch_size=None, return_scores=False, **params) -> List[str]:
123
+ loop = get_background_loop()
124
+ future = asyncio.run_coroutine_threadsafe(
125
+ self._generate_async(input_list, batch_size=batch_size, return_scores=return_scores, **params),
126
+ loop
127
+ )
128
+ return future.result()
@@ -0,0 +1 @@
1
+ __version__ = "0.1.4dev20250209"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: flashrag-dev
3
- Version: 0.1.4.dev20250208
3
+ Version: 0.1.4.dev20250209
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
@@ -18,6 +18,8 @@ fschat
18
18
  streamlit
19
19
  chonkie>=0.4.0
20
20
  gradio==5.11.0
21
+ rouge-chinese
22
+ jieba
21
23
 
22
24
  [core]
23
25
  datasets
@@ -40,6 +42,8 @@ fschat
40
42
  streamlit
41
43
  chonkie>=0.4.0
42
44
  gradio==5.11.0
45
+ rouge-chinese
46
+ jieba
43
47
 
44
48
  [full]
45
49
  datasets
@@ -62,6 +66,8 @@ fschat
62
66
  streamlit
63
67
  chonkie>=0.4.0
64
68
  gradio==5.11.0
69
+ rouge-chinese
70
+ jieba
65
71
  pyserini
66
72
  sentence-transformers>=3.0.1
67
73
  vllm==0.5.5
@@ -1 +0,0 @@
1
- __version__ = "0.1.4dev20250208"