fabricatio 0.3.13__cp312-cp312-win_amd64.whl → 0.3.14__cp312-cp312-win_amd64.whl

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 (48) hide show
  1. fabricatio/__init__.py +6 -13
  2. fabricatio/actions/article.py +87 -50
  3. fabricatio/actions/article_rag.py +59 -68
  4. fabricatio/actions/output.py +58 -24
  5. fabricatio/actions/rag.py +2 -3
  6. fabricatio/capabilities/advanced_judge.py +4 -7
  7. fabricatio/capabilities/advanced_rag.py +2 -1
  8. fabricatio/capabilities/censor.py +5 -4
  9. fabricatio/capabilities/check.py +27 -27
  10. fabricatio/capabilities/correct.py +22 -22
  11. fabricatio/capabilities/extract.py +33 -33
  12. fabricatio/capabilities/persist.py +103 -0
  13. fabricatio/capabilities/propose.py +2 -2
  14. fabricatio/capabilities/rag.py +11 -10
  15. fabricatio/capabilities/rating.py +66 -70
  16. fabricatio/capabilities/review.py +12 -11
  17. fabricatio/capabilities/task.py +19 -18
  18. fabricatio/decorators.py +11 -9
  19. fabricatio/{core.py → emitter.py} +17 -19
  20. fabricatio/journal.py +2 -4
  21. fabricatio/models/action.py +15 -32
  22. fabricatio/models/extra/aricle_rag.py +13 -8
  23. fabricatio/models/extra/article_base.py +57 -25
  24. fabricatio/models/extra/article_essence.py +2 -1
  25. fabricatio/models/extra/article_main.py +24 -22
  26. fabricatio/models/extra/article_outline.py +2 -1
  27. fabricatio/models/extra/article_proposal.py +1 -1
  28. fabricatio/models/extra/rag.py +2 -2
  29. fabricatio/models/extra/rule.py +2 -1
  30. fabricatio/models/generic.py +55 -137
  31. fabricatio/models/kwargs_types.py +1 -54
  32. fabricatio/models/role.py +49 -28
  33. fabricatio/models/task.py +3 -4
  34. fabricatio/models/tool.py +6 -7
  35. fabricatio/models/usages.py +146 -149
  36. fabricatio/parser.py +59 -99
  37. fabricatio/rust.cp312-win_amd64.pyd +0 -0
  38. fabricatio/rust.pyi +58 -81
  39. fabricatio/utils.py +63 -162
  40. fabricatio-0.3.14.data/scripts/tdown.exe +0 -0
  41. fabricatio-0.3.14.data/scripts/ttm.exe +0 -0
  42. {fabricatio-0.3.13.dist-info → fabricatio-0.3.14.dist-info}/METADATA +10 -13
  43. fabricatio-0.3.14.dist-info/RECORD +64 -0
  44. {fabricatio-0.3.13.dist-info → fabricatio-0.3.14.dist-info}/WHEEL +1 -1
  45. fabricatio-0.3.13.data/scripts/tdown.exe +0 -0
  46. fabricatio-0.3.13.data/scripts/ttm.exe +0 -0
  47. fabricatio-0.3.13.dist-info/RECORD +0 -63
  48. {fabricatio-0.3.13.dist-info → fabricatio-0.3.14.dist-info}/licenses/LICENSE +0 -0
@@ -1,5 +1,6 @@
1
1
  """The Capabilities module for advanced judging."""
2
2
 
3
+ from abc import ABC
3
4
  from typing import Optional, Unpack
4
5
 
5
6
  from fabricatio.capabilities.propose import Propose
@@ -7,17 +8,13 @@ from fabricatio.models.extra.advanced_judge import JudgeMent
7
8
  from fabricatio.models.kwargs_types import ValidateKwargs
8
9
 
9
10
 
10
- class AdvancedJudge(Propose):
11
+ class AdvancedJudge(Propose, ABC):
11
12
  """A class that judges the evidence and makes a final decision."""
13
+
12
14
  async def evidently_judge(
13
15
  self,
14
16
  prompt: str,
15
17
  **kwargs: Unpack[ValidateKwargs[JudgeMent]],
16
18
  ) -> Optional[JudgeMent]:
17
19
  """Judge the evidence and make a final decision."""
18
- return await self.propose(
19
- JudgeMent,
20
- prompt,
21
- **kwargs
22
- )
23
-
20
+ return await self.propose(JudgeMent, prompt, **kwargs)
@@ -1,5 +1,6 @@
1
1
  """Advanced RAG (Retrieval Augmented Generation) model."""
2
2
 
3
+ from abc import ABC
3
4
  from typing import Optional, Unpack
4
5
 
5
6
  from fabricatio.capabilities.rag import RAG
@@ -10,7 +11,7 @@ from fabricatio.models.kwargs_types import ChooseKwargs
10
11
  from fabricatio.utils import fallback_kwargs
11
12
 
12
13
 
13
- class AdvancedRAG(RAG):
14
+ class AdvancedRAG(RAG, ABC):
14
15
  """A class representing the Advanced RAG (Retrieval Augmented Generation) model."""
15
16
 
16
17
  async def clued_search(
@@ -4,6 +4,7 @@ This module includes the Censor class which inherits from both Correct and Check
4
4
  It provides methods to censor objects and strings by first checking them against a ruleset and then correcting them if necessary.
5
5
  """
6
6
 
7
+ from abc import ABC
7
8
  from typing import Optional, Unpack
8
9
 
9
10
  from fabricatio.capabilities.check import Check
@@ -16,7 +17,7 @@ from fabricatio.models.kwargs_types import ReferencedKwargs
16
17
  from fabricatio.utils import override_kwargs
17
18
 
18
19
 
19
- class Censor(Correct, Check):
20
+ class Censor(Correct, Check, ABC):
20
21
  """Class to censor objects and strings based on provided rulesets.
21
22
 
22
23
  Inherits from both Correct and Check classes.
@@ -46,7 +47,7 @@ class Censor(Correct, Check):
46
47
  if not imp:
47
48
  logger.info(f"No improvement found for `{obj.__class__.__name__}`.")
48
49
  return obj
49
- logger.info(f'Generated {len(imp)} improvement(s) for `{obj.__class__.__name__}')
50
+ logger.info(f"Generated {len(imp)} improvement(s) for `{obj.__class__.__name__}")
50
51
  return await self.correct_obj(obj, Improvement.gather(*imp), **kwargs)
51
52
 
52
53
  async def censor_string(
@@ -72,7 +73,7 @@ class Censor(Correct, Check):
72
73
  if not imp:
73
74
  logger.info("No improvement found for string.")
74
75
  return input_text
75
- logger.info(f'Generated {len(imp)} improvement(s) for string.')
76
+ logger.info(f"Generated {len(imp)} improvement(s) for string.")
76
77
  return await self.correct_string(input_text, Improvement.gather(*imp), **kwargs)
77
78
 
78
79
  async def censor_obj_inplace[M: ProposedUpdateAble](
@@ -100,5 +101,5 @@ class Censor(Correct, Check):
100
101
  if not imp:
101
102
  logger.info(f"No improvement found for `{obj.__class__.__name__}`.")
102
103
  return obj
103
- logger.info(f'Generated {len(imp)} improvement(s) for `{obj.__class__.__name__}')
104
+ logger.info(f"Generated {len(imp)} improvement(s) for `{obj.__class__.__name__}")
104
105
  return await self.correct_obj_inplace(obj, improvement=Improvement.gather(*imp), **kwargs)
@@ -1,10 +1,9 @@
1
1
  """A class that provides the capability to check strings and objects against rules and guidelines."""
2
2
 
3
+ from abc import ABC
3
4
  from asyncio import gather
4
5
  from typing import List, Optional, Unpack
5
6
 
6
- from fabricatio.rust import CONFIG, TEMPLATE_MANAGER, detect_language
7
-
8
7
  from fabricatio.capabilities.advanced_judge import AdvancedJudge
9
8
  from fabricatio.capabilities.propose import Propose
10
9
  from fabricatio.journal import logger
@@ -13,10 +12,11 @@ from fabricatio.models.extra.problem import Improvement
13
12
  from fabricatio.models.extra.rule import Rule, RuleSet
14
13
  from fabricatio.models.generic import Display, WithBriefing
15
14
  from fabricatio.models.kwargs_types import ValidateKwargs
15
+ from fabricatio.rust import CONFIG, TEMPLATE_MANAGER, detect_language
16
16
  from fabricatio.utils import override_kwargs
17
17
 
18
18
 
19
- class Check(AdvancedJudge, Propose):
19
+ class Check(AdvancedJudge, Propose, ABC):
20
20
  """Class for validating strings/objects against predefined rules and guidelines.
21
21
 
22
22
  This capability combines rule-based judgment and proposal generation to provide
@@ -24,7 +24,7 @@ class Check(AdvancedJudge, Propose):
24
24
  """
25
25
 
26
26
  async def draft_ruleset(
27
- self, ruleset_requirement: str, rule_count: int = 0, **kwargs: Unpack[ValidateKwargs[Rule]]
27
+ self, ruleset_requirement: str, rule_count: int = 0, **kwargs: Unpack[ValidateKwargs[Rule]]
28
28
  ) -> Optional[RuleSet]:
29
29
  """Generate rule set based on requirement description.
30
30
 
@@ -80,11 +80,11 @@ class Check(AdvancedJudge, Propose):
80
80
  return RuleSet(rules=rules, **ruleset_patch.as_kwargs())
81
81
 
82
82
  async def check_string_against_rule(
83
- self,
84
- input_text: str,
85
- rule: Rule,
86
- reference: str = "",
87
- **kwargs: Unpack[ValidateKwargs[Improvement]],
83
+ self,
84
+ input_text: str,
85
+ rule: Rule,
86
+ reference: str = "",
87
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
88
88
  ) -> Optional[Improvement]:
89
89
  """Validate text against specific rule.
90
90
 
@@ -103,9 +103,9 @@ class Check(AdvancedJudge, Propose):
103
103
  - Proposes Improvement only when violation is confirmed
104
104
  """
105
105
  if judge := await self.evidently_judge(
106
- f"# Content to exam\n{input_text}\n\n# Rule Must to follow\n{rule.display()}\nDoes `Content to exam` provided above violate the `{rule.name}` provided above?"
107
- f"should I take some measure to fix that violation? true for I do need, false for I don't need.",
108
- **override_kwargs(kwargs, default=None),
106
+ f"# Content to exam\n{input_text}\n\n# Rule Must to follow\n{rule.display()}\nDoes `Content to exam` provided above violate the `{rule.name}` provided above?"
107
+ f"should I take some measure to fix that violation? true for I do need, false for I don't need.",
108
+ **override_kwargs(kwargs, default=None),
109
109
  ):
110
110
  logger.info(f"Rule `{rule.name}` violated: \n{judge.display()}")
111
111
  return await self.propose(
@@ -119,11 +119,11 @@ class Check(AdvancedJudge, Propose):
119
119
  return None
120
120
 
121
121
  async def check_obj_against_rule[M: (Display, WithBriefing)](
122
- self,
123
- obj: M,
124
- rule: Rule,
125
- reference: str = "",
126
- **kwargs: Unpack[ValidateKwargs[Improvement]],
122
+ self,
123
+ obj: M,
124
+ rule: Rule,
125
+ reference: str = "",
126
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
127
127
  ) -> Optional[Improvement]:
128
128
  """Validate object against rule using text representation.
129
129
 
@@ -151,11 +151,11 @@ class Check(AdvancedJudge, Propose):
151
151
  return await self.check_string_against_rule(input_text, rule, reference, **kwargs)
152
152
 
153
153
  async def check_string(
154
- self,
155
- input_text: str,
156
- ruleset: RuleSet,
157
- reference: str = "",
158
- **kwargs: Unpack[ValidateKwargs[Improvement]],
154
+ self,
155
+ input_text: str,
156
+ ruleset: RuleSet,
157
+ reference: str = "",
158
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
159
159
  ) -> Optional[List[Improvement]]:
160
160
  """Validate text against full ruleset.
161
161
 
@@ -182,11 +182,11 @@ class Check(AdvancedJudge, Propose):
182
182
  return [imp for imp in imp_seq if imp]
183
183
 
184
184
  async def check_obj[M: (Display, WithBriefing)](
185
- self,
186
- obj: M,
187
- ruleset: RuleSet,
188
- reference: str = "",
189
- **kwargs: Unpack[ValidateKwargs[Improvement]],
185
+ self,
186
+ obj: M,
187
+ ruleset: RuleSet,
188
+ reference: str = "",
189
+ **kwargs: Unpack[ValidateKwargs[Improvement]],
190
190
  ) -> Optional[List[Improvement]]:
191
191
  """Validate object against full ruleset.
192
192
 
@@ -1,9 +1,8 @@
1
1
  """A module containing the Correct capability for reviewing, validating, and improving objects."""
2
2
 
3
+ from abc import ABC
3
4
  from asyncio import gather
4
- from typing import Optional, Unpack, cast
5
-
6
- from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
5
+ from typing import Optional, Type, Unpack, cast
7
6
 
8
7
  from fabricatio.capabilities.propose import Propose
9
8
  from fabricatio.capabilities.rating import Rating
@@ -15,14 +14,15 @@ from fabricatio.models.kwargs_types import (
15
14
  BestKwargs,
16
15
  ValidateKwargs,
17
16
  )
17
+ from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
18
18
  from fabricatio.utils import fallback_kwargs, ok, override_kwargs
19
19
 
20
20
 
21
- class Correct(Rating, Propose):
21
+ class Correct(Rating, Propose, ABC):
22
22
  """A class that provides the capability to correct objects."""
23
23
 
24
24
  async def decide_solution(
25
- self, problem_solutions: ProblemSolutions, **kwargs: Unpack[BestKwargs]
25
+ self, problem_solutions: ProblemSolutions, **kwargs: Unpack[BestKwargs]
26
26
  ) -> ProblemSolutions:
27
27
  """Decide the best solution from a list of problem solutions.
28
28
 
@@ -72,11 +72,11 @@ class Correct(Rating, Propose):
72
72
  return improvement
73
73
 
74
74
  async def fix_troubled_obj[M: SketchedAble](
75
- self,
76
- obj: M,
77
- problem_solutions: ProblemSolutions,
78
- reference: str = "",
79
- **kwargs: Unpack[ValidateKwargs[M]],
75
+ self,
76
+ obj: M,
77
+ problem_solutions: ProblemSolutions,
78
+ reference: str = "",
79
+ **kwargs: Unpack[ValidateKwargs[M]],
80
80
  ) -> Optional[M]:
81
81
  """Fix a troubled object based on problem solutions.
82
82
 
@@ -106,11 +106,11 @@ class Correct(Rating, Propose):
106
106
  )
107
107
 
108
108
  async def fix_troubled_string(
109
- self,
110
- input_text: str,
111
- problem_solutions: ProblemSolutions,
112
- reference: str = "",
113
- **kwargs: Unpack[ValidateKwargs[str]],
109
+ self,
110
+ input_text: str,
111
+ problem_solutions: ProblemSolutions,
112
+ reference: str = "",
113
+ **kwargs: Unpack[ValidateKwargs[str]],
114
114
  ) -> Optional[str]:
115
115
  """Fix a troubled string based on problem solutions.
116
116
 
@@ -140,11 +140,11 @@ class Correct(Rating, Propose):
140
140
  )
141
141
 
142
142
  async def correct_obj[M: SketchedAble](
143
- self,
144
- obj: M,
145
- improvement: Improvement,
146
- reference: str = "",
147
- **kwargs: Unpack[ValidateKwargs[M]],
143
+ self,
144
+ obj: M,
145
+ improvement: Improvement,
146
+ reference: str = "",
147
+ **kwargs: Unpack[ValidateKwargs[M]],
148
148
  ) -> Optional[M]:
149
149
  """Review and correct an object based on defined criteria and templates.
150
150
 
@@ -178,7 +178,7 @@ class Correct(Rating, Propose):
178
178
  return obj
179
179
 
180
180
  async def correct_string(
181
- self, input_text: str, improvement: Improvement, reference: str = "", **kwargs: Unpack[ValidateKwargs[str]]
181
+ self, input_text: str, improvement: Improvement, reference: str = "", **kwargs: Unpack[ValidateKwargs[str]]
182
182
  ) -> Optional[str]:
183
183
  """Review and correct a string based on defined criteria and templates.
184
184
 
@@ -210,7 +210,7 @@ class Correct(Rating, Propose):
210
210
  return input_text
211
211
 
212
212
  async def correct_obj_inplace[M: ProposedUpdateAble](
213
- self, obj: M, **kwargs: Unpack[CorrectKwargs[M]]
213
+ self, obj: M, **kwargs: Unpack[CorrectKwargs[M]]
214
214
  ) -> Optional[M]:
215
215
  """Correct an object in place based on defined criteria and templates.
216
216
 
@@ -1,65 +1,65 @@
1
1
  """A module that provide capabilities for extracting information from a given source to a model."""
2
2
 
3
+ from abc import ABC
3
4
  from typing import List, Optional, Type, Unpack, overload
4
5
 
5
- from fabricatio.rust import CONFIG
6
-
7
6
  from fabricatio import TEMPLATE_MANAGER
8
7
  from fabricatio.capabilities.propose import Propose
9
8
  from fabricatio.models.generic import ProposedAble
10
9
  from fabricatio.models.kwargs_types import ValidateKwargs
10
+ from fabricatio.rust import CONFIG
11
11
 
12
12
 
13
- class Extract(Propose):
13
+ class Extract(Propose, ABC):
14
14
  """A class that extract information from a given source to a model."""
15
15
 
16
16
  @overload
17
17
  async def extract[M: ProposedAble](
18
- self,
19
- cls: Type[M],
20
- source: str,
21
- extract_requirement: Optional[str] = None,
22
- align_language: bool = True,
23
- **kwargs: Unpack[ValidateKwargs[M]],
18
+ self,
19
+ cls: Type[M],
20
+ source: str,
21
+ extract_requirement: Optional[str] = None,
22
+ align_language: bool = True,
23
+ **kwargs: Unpack[ValidateKwargs[M]],
24
24
  ) -> M: ...
25
25
 
26
26
  @overload
27
27
  async def extract[M: ProposedAble](
28
- self,
29
- cls: Type[M],
30
- source: str,
31
- extract_requirement: Optional[str] = None,
32
- align_language: bool = True,
33
- **kwargs: Unpack[ValidateKwargs[None]],
28
+ self,
29
+ cls: Type[M],
30
+ source: str,
31
+ extract_requirement: Optional[str] = None,
32
+ align_language: bool = True,
33
+ **kwargs: Unpack[ValidateKwargs[None]],
34
34
  ) -> Optional[M]: ...
35
35
 
36
36
  @overload
37
37
  async def extract[M: ProposedAble](
38
- self,
39
- cls: Type[M],
40
- source: List[str],
41
- extract_requirement: Optional[str] = None,
42
- align_language: bool = True,
43
- **kwargs: Unpack[ValidateKwargs[M]],
38
+ self,
39
+ cls: Type[M],
40
+ source: List[str],
41
+ extract_requirement: Optional[str] = None,
42
+ align_language: bool = True,
43
+ **kwargs: Unpack[ValidateKwargs[M]],
44
44
  ) -> List[M]: ...
45
45
 
46
46
  @overload
47
47
  async def extract[M: ProposedAble](
48
- self,
49
- cls: Type[M],
50
- source: List[str],
51
- extract_requirement: Optional[str] = None,
52
- align_language: bool = True,
53
- **kwargs: Unpack[ValidateKwargs[None]],
48
+ self,
49
+ cls: Type[M],
50
+ source: List[str],
51
+ extract_requirement: Optional[str] = None,
52
+ align_language: bool = True,
53
+ **kwargs: Unpack[ValidateKwargs[None]],
54
54
  ) -> List[Optional[M]]: ...
55
55
 
56
56
  async def extract[M: ProposedAble](
57
- self,
58
- cls: Type[M],
59
- source: List[str] | str,
60
- extract_requirement: Optional[str] = None,
61
- align_language: bool = True,
62
- **kwargs: Unpack[ValidateKwargs[Optional[M]]],
57
+ self,
58
+ cls: Type[M],
59
+ source: List[str] | str,
60
+ extract_requirement: Optional[str] = None,
61
+ align_language: bool = True,
62
+ **kwargs: Unpack[ValidateKwargs[Optional[M]]],
63
63
  ) -> M | List[M] | Optional[M] | List[Optional[M]]:
64
64
  """Extract information from a given source to a model."""
65
65
  return await self.propose(
@@ -0,0 +1,103 @@
1
+ """Persistence capabilities for model instances."""
2
+
3
+ from abc import ABC
4
+ from datetime import datetime
5
+ from pathlib import Path
6
+ from typing import Optional, Self
7
+
8
+ from loguru import logger
9
+
10
+ from fabricatio.fs import safe_text_read
11
+ from fabricatio.models.generic import Base
12
+ from fabricatio.rust import blake3_hash
13
+
14
+
15
+ class PersistentAble(Base, ABC):
16
+ """Class providing file persistence capabilities.
17
+
18
+ Enables saving model instances to disk with timestamped filenames and loading from persisted files.
19
+ Implements basic versioning through filename hashing and timestamping.
20
+ """
21
+
22
+ def persist(self, path: str | Path) -> Self:
23
+ """Save model instance to disk with versioned filename.
24
+
25
+ Args:
26
+ path (str | Path): Target directory or file path. If directory, filename is auto-generated.
27
+
28
+ Returns:
29
+ Self: Current instance for method chaining
30
+
31
+ Notes:
32
+ - Filename format: <ClassName>_<YYYYMMDD_HHMMSS>_<6-char_hash>.json
33
+ - Hash generated from JSON content ensures uniqueness
34
+ """
35
+ p = Path(path)
36
+ out = self.model_dump_json(indent=1, by_alias=True)
37
+
38
+ # Generate a timestamp in the format YYYYMMDD_HHMMSS
39
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
40
+
41
+ # Generate the hash
42
+ file_hash = blake3_hash(out.encode())[:6]
43
+
44
+ # Construct the file name with timestamp and hash
45
+ file_name = f"{self.__class__.__name__}_{timestamp}_{file_hash}.json"
46
+
47
+ if p.is_dir():
48
+ p.joinpath(file_name).write_text(out, encoding="utf-8")
49
+ else:
50
+ p.mkdir(exist_ok=True, parents=True)
51
+ p.write_text(out, encoding="utf-8")
52
+
53
+ logger.info(f"Persisted `{self.__class__.__name__}` to {p.as_posix()}")
54
+ return self
55
+
56
+ @classmethod
57
+ def from_latest_persistent(cls, dir_path: str | Path) -> Optional[Self]:
58
+ """Load most recent persisted instance from directory.
59
+
60
+ Args:
61
+ dir_path (str | Path): Directory containing persisted files
62
+
63
+ Returns:
64
+ Self: Most recently modified instance
65
+
66
+ Raises:
67
+ NotADirectoryError: If path is not a valid directory
68
+ FileNotFoundError: If no matching files found
69
+ """
70
+ dir_path = Path(dir_path)
71
+ if not dir_path.is_dir():
72
+ return None
73
+
74
+ pattern = f"{cls.__name__}_*.json"
75
+ files = list(dir_path.glob(pattern))
76
+
77
+ if not files:
78
+ return None
79
+
80
+ def _get_timestamp(file_path: Path) -> datetime:
81
+ stem = file_path.stem
82
+ parts = stem.split("_")
83
+ return datetime.strptime(f"{parts[1]}_{parts[2]}", "%Y%m%d_%H%M%S")
84
+
85
+ files.sort(key=lambda f: _get_timestamp(f), reverse=True)
86
+
87
+ return cls.from_persistent(files.pop(0))
88
+
89
+ @classmethod
90
+ def from_persistent(cls, path: str | Path) -> Self:
91
+ """Load an instance from a specific persisted file.
92
+
93
+ Args:
94
+ path (str | Path): Path to the JSON file.
95
+
96
+ Returns:
97
+ Self: The loaded instance from the file.
98
+
99
+ Raises:
100
+ FileNotFoundError: If the specified file does not exist.
101
+ ValueError: If the file content is invalid for the model.
102
+ """
103
+ return cls.model_validate_json(safe_text_read(path))
@@ -1,5 +1,5 @@
1
1
  """A module for the task capabilities of the Fabricatio library."""
2
-
2
+ from abc import ABC
3
3
  from typing import List, Optional, Type, Unpack, overload
4
4
 
5
5
  from fabricatio.models.generic import ProposedAble
@@ -7,7 +7,7 @@ from fabricatio.models.kwargs_types import ValidateKwargs
7
7
  from fabricatio.models.usages import LLMUsage
8
8
 
9
9
 
10
- class Propose(LLMUsage):
10
+ class Propose(LLMUsage,ABC):
11
11
  """A class that proposes an Obj based on a prompt."""
12
12
 
13
13
  @overload
@@ -1,5 +1,7 @@
1
1
  """A module for the RAG (Retrieval Augmented Generation) model."""
2
2
 
3
+ from abc import ABC
4
+
3
5
  try:
4
6
  from pymilvus import MilvusClient
5
7
  except ImportError as e:
@@ -10,7 +12,6 @@ from functools import lru_cache
10
12
  from operator import itemgetter
11
13
  from typing import List, Optional, Self, Type, Unpack
12
14
 
13
- from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
14
15
  from more_itertools.recipes import flatten, unique
15
16
  from pydantic import Field, PrivateAttr
16
17
 
@@ -19,6 +20,7 @@ from fabricatio.models.adv_kwargs_types import CollectionConfigKwargs, FetchKwar
19
20
  from fabricatio.models.extra.rag import MilvusDataBase
20
21
  from fabricatio.models.kwargs_types import ChooseKwargs
21
22
  from fabricatio.models.usages import EmbeddingUsage
23
+ from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
22
24
  from fabricatio.utils import ok
23
25
 
24
26
 
@@ -32,7 +34,7 @@ def create_client(uri: str, token: str = "", timeout: Optional[float] = None) ->
32
34
  )
33
35
 
34
36
 
35
- class RAG(EmbeddingUsage):
37
+ class RAG(EmbeddingUsage, ABC):
36
38
  """A class representing the RAG (Retrieval Augmented Generation) model."""
37
39
 
38
40
  target_collection: Optional[str] = Field(default=None)
@@ -56,7 +58,7 @@ class RAG(EmbeddingUsage):
56
58
  ) -> Self:
57
59
  """Initialize the Milvus client."""
58
60
  self._client = create_client(
59
- uri=milvus_uri or ok(self.milvus_uri or CONFIG.rag.milvus_uri).unicode_string(),
61
+ uri=milvus_uri or ok(self.milvus_uri or CONFIG.rag.milvus_uri),
60
62
  token=milvus_token
61
63
  or (token.get_secret_value() if (token := (self.milvus_token or CONFIG.rag.milvus_token)) else ""),
62
64
  timeout=milvus_timeout or self.milvus_timeout or CONFIG.rag.milvus_timeout,
@@ -162,7 +164,7 @@ class RAG(EmbeddingUsage):
162
164
  result_per_query (int): The maximum number of results to return per query. Defaults to 10.
163
165
  tei_endpoint (str): the endpoint of the TEI api.
164
166
  reranker_threshold (float): The threshold used to filtered low relativity document.
165
- filter_expr (str): filter_expression parsed into pymilvus search.
167
+ filter_expr (str) : The filter expression used to filter out unwanted documents.
166
168
 
167
169
  Returns:
168
170
  List[D]: A list of document objects created from the fetched data.
@@ -177,9 +179,9 @@ class RAG(EmbeddingUsage):
177
179
  limit=result_per_query,
178
180
  )
179
181
  if tei_endpoint is not None:
180
- from fabricatio.utils import RerankerAPI
182
+ from fabricatio.rust import TEIClient
181
183
 
182
- reranker = RerankerAPI(base_url=tei_endpoint)
184
+ reranker = TEIClient(base_url=tei_endpoint)
183
185
 
184
186
  retrieved_id = set()
185
187
  raw_result = []
@@ -190,10 +192,9 @@ class RAG(EmbeddingUsage):
190
192
  retrieved_id.update(res["id"] for res in g)
191
193
  if not models:
192
194
  continue
193
- rank_scores = await reranker.arerank(q, [m.prepare_vectorization() for m in models], truncate=True)
194
- raw_result.extend(
195
- (models[s["index"]], s["score"]) for s in rank_scores if s["score"] > reranker_threshold
196
- )
195
+ rank_scores = await reranker.arerank(q, [m.prepare_vectorization() for m in models], truncate=True,
196
+ truncation_direction="Left")
197
+ raw_result.extend((models[idx], scr) for (idx, scr) in rank_scores if scr > reranker_threshold)
197
198
 
198
199
  raw_result_sorted = sorted(raw_result, key=lambda x: x[1], reverse=True)
199
200
  return [r[0] for r in raw_result_sorted]