fabricatio 0.2.5.dev4__cp312-cp312-win_amd64.whl → 0.2.6.dev0__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.
fabricatio/journal.py CHANGED
@@ -18,6 +18,7 @@ logger.add(
18
18
  )
19
19
  logger.add(sys.stderr, level=configs.debug.log_level)
20
20
 
21
+ __all__ = ["logger"]
21
22
  if __name__ == "__main__":
22
23
  logger.debug("This is a trace message.")
23
24
  logger.info("This is an information message.")
@@ -3,7 +3,7 @@
3
3
  import traceback
4
4
  from abc import abstractmethod
5
5
  from asyncio import Queue, create_task
6
- from typing import Any, Dict, Self, Tuple, Type, Union, Unpack, final
6
+ from typing import Any, Dict, Self, Tuple, Type, Union, final
7
7
 
8
8
  from fabricatio.capabilities.review import Review
9
9
  from fabricatio.capabilities.task import HandleTask, ProposeTask
@@ -37,7 +37,7 @@ class Action(HandleTask, ProposeTask, Review):
37
37
  self.description = self.description or self.__class__.__doc__ or ""
38
38
 
39
39
  @abstractmethod
40
- async def _execute(self, **cxt: Unpack) -> Any:
40
+ async def _execute(self, **cxt) -> Any:
41
41
  """Execute the action with the provided arguments.
42
42
 
43
43
  Args:
@@ -75,7 +75,7 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
75
75
  _context: Queue[Dict[str, Any]] = PrivateAttr(default_factory=lambda: Queue(maxsize=1))
76
76
  """ The context dictionary to be used for workflow execution."""
77
77
 
78
- _instances: Tuple[Action, ...] = PrivateAttr(...)
78
+ _instances: Tuple[Action, ...] = PrivateAttr(default_factory=tuple)
79
79
  """ The instances of the workflow steps."""
80
80
 
81
81
  steps: Tuple[Union[Type[Action], Action], ...] = Field(...)
@@ -18,7 +18,7 @@ class Event(BaseModel):
18
18
  """ The segments of the namespaces."""
19
19
 
20
20
  @classmethod
21
- def instantiate_from(cls, event: EventLike) -> Self:
21
+ def instantiate_from(cls, event: EventLike) -> "Event":
22
22
  """Create an Event instance from a string or list of strings or an Event instance.
23
23
 
24
24
  Args:
@@ -35,7 +35,7 @@ class Event(BaseModel):
35
35
  return cls(segments=event)
36
36
 
37
37
  @classmethod
38
- def quick_instantiate(cls, event: EventLike) -> Self:
38
+ def quick_instantiate(cls, event: EventLike) -> "Event":
39
39
  """Create an Event instance from a string or list of strings or an Event instance and push a wildcard and pending segment.
40
40
 
41
41
  Args:
@@ -59,7 +59,7 @@ class Event(BaseModel):
59
59
 
60
60
  def clone(self) -> Self:
61
61
  """Clone the event."""
62
- return Event(segments=list(self.segments))
62
+ return self.__class__(segments=list(self.segments))
63
63
 
64
64
  def push(self, segment: str) -> Self:
65
65
  """Push a segment to the event."""
@@ -113,6 +113,8 @@ class Event(BaseModel):
113
113
  """Return the hash of the event, using the collapsed string."""
114
114
  return hash(self.collapse())
115
115
 
116
- def __eq__(self, other: str | List[str] | Self) -> bool:
116
+ def __eq__(self, other: object) -> bool:
117
117
  """Check if the event is equal to another event or a string."""
118
+ if not isinstance(other, (str , list , Event)):
119
+ return False
118
120
  return self.collapse() == Event.instantiate_from(other).collapse()
@@ -9,23 +9,23 @@ from pydantic import Field
9
9
  class Equation(Base):
10
10
  """Structured representation of mathematical equations (including their physical or conceptual meanings)."""
11
11
 
12
- description: str = Field(...)
12
+ description: str
13
13
  """A concise explanation of the equation's meaning, purpose, and relevance in the context of the research."""
14
14
 
15
- latex_code: str = Field(...)
15
+ latex_code: str
16
16
  """The LaTeX code used to represent the equation in a publication-ready format."""
17
17
 
18
18
 
19
19
  class Figure(Base):
20
20
  """Structured representation of figures (including their academic significance and explanatory captions)."""
21
21
 
22
- description: str = Field(...)
22
+ description: str
23
23
  """A detailed explanation of the figure's content and its role in conveying key insights."""
24
24
 
25
- figure_caption: str = Field(...)
25
+ figure_caption: str
26
26
  """The caption accompanying the figure, summarizing its main points and academic value."""
27
27
 
28
- figure_path: str = Field(...)
28
+ figure_path: str
29
29
  """The file path to the figure"""
30
30
 
31
31
 
@@ -34,7 +34,7 @@ class Highlightings(Base):
34
34
 
35
35
  # Academic Achievements Showcase
36
36
  highlighted_equations: List[Equation] = Field(default_factory=list)
37
- """Core mathematical equations that represent breakthroughs in the field, accompanied by explanations of their physical or conceptual significance."""
37
+ """Core mathematical equations that represent breakthroughs in the field, accompanied by explanations of their physical or conceptual significance,Should always be in LaTeX format wrapped in $ or $$ signs."""
38
38
 
39
39
  highlighted_algorithms: List[str] = Field(default_factory=list)
40
40
  """Pseudocode for key algorithms, annotated to highlight innovative components."""
@@ -53,39 +53,42 @@ class ArticleEssence(ProposedAble, Display, PrepareVectorization):
53
53
  title: str = Field(...)
54
54
  """The full title of the paper, including any subtitles if applicable."""
55
55
 
56
- authors: List[str] = Field(default_factory=list)
56
+ authors: List[str]
57
57
  """A list of the paper's authors, typically in the order of contribution."""
58
58
 
59
- keywords: List[str] = Field(default_factory=list)
59
+ keywords: List[str]
60
60
  """A list of keywords that summarize the paper's focus and facilitate indexing."""
61
61
 
62
- publication_year: int = Field(None)
62
+ publication_year: int
63
63
  """The year in which the paper was published."""
64
64
 
65
65
  # Core Content Elements
66
- domain: List[str] = Field(default_factory=list)
66
+ highlightings: Highlightings = Field(default_factory=Highlightings)
67
+ """A collection of highlighted elements in the paper, including equations, algorithms, figures, and tables."""
68
+
69
+ domain: List[str]
67
70
  """The research domains or fields addressed by the paper (e.g., ['Natural Language Processing', 'Computer Vision'])."""
68
71
 
69
72
  abstract: str = Field(...)
70
73
  """A structured abstract that outlines the research problem, methodology, and conclusions in three distinct sections."""
71
74
 
72
- core_contributions: List[str] = Field(default_factory=list)
75
+ core_contributions: List[str]
73
76
  """Key academic contributions that distinguish the paper from prior work in the field."""
74
77
 
75
- technical_novelty: List[str] = Field(default_factory=list)
78
+ technical_novelty: List[str]
76
79
  """Specific technical innovations introduced by the research, listed as individual points."""
77
80
 
78
81
  # Academic Discussion Dimensions
79
- research_problem: str = Field("")
82
+ research_problems: List[str]
80
83
  """A clearly defined research question or problem addressed by the study."""
81
84
 
82
- limitations: List[str] = Field(default_factory=list)
85
+ limitations: List[str]
83
86
  """An analysis of the methodological or experimental limitations of the research."""
84
87
 
85
- future_work: List[str] = Field(default_factory=list)
88
+ future_work: List[str]
86
89
  """Suggestions for potential directions or topics for follow-up studies."""
87
90
 
88
- impact_analysis: str = Field("")
91
+ impact_analysis: List[str]
89
92
  """An assessment of the paper's potential influence on the development of the field."""
90
93
 
91
94
  def _prepare_vectorization_inner(self) -> str:
@@ -2,7 +2,7 @@
2
2
 
3
3
  from abc import abstractmethod
4
4
  from pathlib import Path
5
- from typing import Callable, Iterable, List, Optional, Self, Union, final
5
+ from typing import Any, Callable, Dict, Iterable, List, Optional, Self, Union, final, overload
6
6
 
7
7
  import orjson
8
8
  from fabricatio._rust import blake3_hash
@@ -67,6 +67,18 @@ class WithBriefing(Named, Described):
67
67
  """
68
68
  return f"{self.name}: {self.description}" if self.description else self.name
69
69
 
70
+ def prepend[D: Dict[str, Any]](self, kwargs: D) -> D:
71
+ """Prepend the briefing to the system message in the kwargs.
72
+
73
+ Args:
74
+ kwargs (Dict[str, Any]): The keyword arguments to modify.
75
+
76
+ Returns:
77
+ Dict[str, Any]: The modified keyword arguments.
78
+ """
79
+ kwargs["system_message"] = f"# your personal briefing: \n{self.briefing}\n" + kwargs.get("system_message", "")
80
+ return kwargs
81
+
70
82
 
71
83
  class WithFormatedJsonSchema(Base):
72
84
  """Class that provides a formatted JSON schema of the model."""
@@ -88,7 +100,15 @@ class CreateJsonObjPrompt(WithFormatedJsonSchema):
88
100
  """Class that provides a prompt for creating a JSON object."""
89
101
 
90
102
  @classmethod
91
- def create_json_prompt(cls, requirement: str) -> str:
103
+ @overload
104
+ def create_json_prompt(cls, requirement: List[str]) -> List[str]: ...
105
+
106
+ @classmethod
107
+ @overload
108
+ def create_json_prompt(cls, requirement: str) -> str: ...
109
+
110
+ @classmethod
111
+ def create_json_prompt(cls, requirement: str | List[str]) -> str | List[str]:
92
112
  """Create the prompt for creating a JSON object with given requirement.
93
113
 
94
114
  Args:
@@ -97,10 +117,18 @@ class CreateJsonObjPrompt(WithFormatedJsonSchema):
97
117
  Returns:
98
118
  str: The prompt for creating a JSON object with given requirement.
99
119
  """
100
- return template_manager.render_template(
101
- configs.templates.create_json_obj_template,
102
- {"requirement": requirement, "json_schema": cls.formated_json_schema()},
103
- )
120
+ if isinstance(requirement, str):
121
+ return template_manager.render_template(
122
+ configs.templates.create_json_obj_template,
123
+ {"requirement": requirement, "json_schema": cls.formated_json_schema()},
124
+ )
125
+ return [
126
+ template_manager.render_template(
127
+ configs.templates.create_json_obj_template,
128
+ {"requirement": r, "json_schema": cls.formated_json_schema()},
129
+ )
130
+ for r in requirement
131
+ ]
104
132
 
105
133
 
106
134
  class InstantiateFromString(Base):
@@ -295,6 +323,12 @@ class ScopedConfig(Base):
295
323
  llm_max_tokens: Optional[PositiveInt] = None
296
324
  """The maximum number of tokens to generate."""
297
325
 
326
+ llm_tpm: Optional[PositiveInt] = None
327
+ """The tokens per minute of the LLM model."""
328
+
329
+ llm_rpm: Optional[PositiveInt] = None
330
+ """The requests per minute of the LLM model."""
331
+
298
332
  embedding_api_endpoint: Optional[HttpUrl] = None
299
333
  """The OpenAI API endpoint."""
300
334
 
@@ -361,3 +395,4 @@ class ScopedConfig(Base):
361
395
  for attr_name in ScopedConfig.model_fields:
362
396
  if (attr := getattr(self, attr_name)) is not None and getattr(other, attr_name) is None:
363
397
  setattr(other, attr_name, attr)
398
+ return self
@@ -1,92 +1,90 @@
1
1
  """This module contains the types for the keyword arguments of the methods in the models module."""
2
2
 
3
- from typing import Any, List, NotRequired, Set, TypedDict
3
+ from typing import Any, TypedDict
4
4
 
5
5
  from litellm.caching.caching import CacheMode
6
6
  from litellm.types.caching import CachingSupportedCallTypes
7
- from pydantic import NonNegativeFloat, NonNegativeInt, PositiveInt
8
7
 
9
8
 
10
- class CollectionSimpleConfigKwargs(TypedDict):
9
+ class CollectionSimpleConfigKwargs(TypedDict, total=False):
11
10
  """Configuration parameters for a vector collection.
12
11
 
13
12
  These arguments are typically used when configuring connections to vector databases.
14
13
  """
15
14
 
16
- dimension: NotRequired[int]
17
- timeout: NotRequired[float]
15
+ dimension: int|None
16
+ timeout: float
18
17
 
19
18
 
20
- class FetchKwargs(TypedDict):
19
+ class FetchKwargs(TypedDict, total=False):
21
20
  """Arguments for fetching data from vector collections.
22
21
 
23
22
  Controls how data is retrieved from vector databases, including filtering
24
23
  and result limiting parameters.
25
24
  """
26
25
 
27
- collection_name: NotRequired[str]
28
- similarity_threshold: NotRequired[float]
29
- result_per_query: NotRequired[int]
26
+ collection_name: str|None
27
+ similarity_threshold: float
28
+ result_per_query: int
30
29
 
31
30
 
32
- class EmbeddingKwargs(TypedDict):
31
+ class EmbeddingKwargs(TypedDict, total=False):
33
32
  """Configuration parameters for text embedding operations.
34
33
 
35
34
  These settings control the behavior of embedding models that convert text
36
35
  to vector representations.
37
36
  """
38
37
 
39
- model: NotRequired[str]
40
- dimensions: NotRequired[int]
41
- timeout: NotRequired[PositiveInt]
42
- caching: NotRequired[bool]
38
+ model: str
39
+ dimensions: int
40
+ timeout: int
41
+ caching: bool
43
42
 
44
43
 
45
- class LLMKwargs(TypedDict):
44
+ class LLMKwargs(TypedDict, total=False):
46
45
  """Configuration parameters for language model inference.
47
46
 
48
47
  These arguments control the behavior of large language model calls,
49
48
  including generation parameters and caching options.
50
49
  """
51
50
 
52
- model: NotRequired[str]
53
- temperature: NotRequired[NonNegativeFloat]
54
- stop: NotRequired[str | List[str]]
55
- top_p: NotRequired[NonNegativeFloat]
56
- max_tokens: NotRequired[PositiveInt]
57
- stream: NotRequired[bool]
58
- timeout: NotRequired[PositiveInt]
59
- max_retries: NotRequired[PositiveInt]
60
- no_cache: NotRequired[bool] # If use cache in this call
61
- no_store: NotRequired[bool] # If store the response of this call to cache
62
- cache_ttl: NotRequired[int] # how long the stored cache is alive, in seconds
63
- s_maxage: NotRequired[int] # max accepted age of cached response, in seconds
64
-
65
-
66
- class ValidateKwargs[T](LLMKwargs):
67
- """Arguments for content validation operations.
51
+ model: str
52
+ temperature: float
53
+ stop: str | list[str]
54
+ top_p: float
55
+ max_tokens: int
56
+ stream: bool
57
+ timeout: int
58
+ max_retries: int
59
+ no_cache: bool # if the req uses cache in this call
60
+ no_store: bool # If store the response of this call to cache
61
+ cache_ttl: int # how long the stored cache is alive, in seconds
62
+ s_maxage: int # max accepted age of cached response, in seconds
63
+
64
+
65
+ class GenerateKwargs(LLMKwargs, total=False):
66
+ """Arguments for content generation operations.
68
67
 
69
- Extends LLMKwargs with additional parameters specific to validation tasks,
70
- such as limiting the number of validation attempts.
68
+ Extends LLMKwargs with additional parameters specific to generation tasks,
69
+ such as the number of generated items and the system message.
71
70
  """
72
71
 
73
- default: NotRequired[T]
74
- max_validations: NotRequired[PositiveInt]
72
+ system_message: str
75
73
 
76
74
 
77
- # noinspection PyTypedDict
78
- class GenerateKwargs[T](ValidateKwargs[T]):
79
- """Arguments for content generation operations.
75
+ class ValidateKwargs[T](GenerateKwargs, total=False):
76
+ """Arguments for content validation operations.
80
77
 
81
- Extends ValidateKwargs with parameters specific to text generation,
82
- including system prompt configuration.
78
+ Extends LLMKwargs with additional parameters specific to validation tasks,
79
+ such as limiting the number of validation attempts.
83
80
  """
84
81
 
85
- system_message: NotRequired[str]
82
+ default: T
83
+ max_validations: int
86
84
 
87
85
 
88
86
  # noinspection PyTypedDict
89
- class ReviewKwargs[T](GenerateKwargs[T]):
87
+ class ReviewKwargs[T](ValidateKwargs[T], total=False):
90
88
  """Arguments for content review operations.
91
89
 
92
90
  Extends GenerateKwargs with parameters for evaluating content against
@@ -94,18 +92,18 @@ class ReviewKwargs[T](GenerateKwargs[T]):
94
92
  """
95
93
 
96
94
  topic: str
97
- criteria: NotRequired[Set[str]]
95
+ criteria: set[str]
98
96
 
99
97
 
100
98
  # noinspection PyTypedDict
101
- class ChooseKwargs[T](GenerateKwargs[T]):
99
+ class ChooseKwargs[T](ValidateKwargs[T], total=False):
102
100
  """Arguments for selection operations.
103
101
 
104
102
  Extends GenerateKwargs with parameters for selecting among options,
105
103
  such as the number of items to choose.
106
104
  """
107
105
 
108
- k: NotRequired[NonNegativeInt]
106
+ k: int
109
107
 
110
108
 
111
109
  class CacheKwargs(TypedDict, total=False):
@@ -115,35 +113,35 @@ class CacheKwargs(TypedDict, total=False):
115
113
  including in-memory, Redis, S3, and vector database caching options.
116
114
  """
117
115
 
118
- mode: NotRequired[CacheMode] # when default_on cache is always on, when default_off cache is opt in
119
- host: NotRequired[str]
120
- port: NotRequired[str]
121
- password: NotRequired[str]
122
- namespace: NotRequired[str]
123
- ttl: NotRequired[float]
124
- default_in_memory_ttl: NotRequired[float]
125
- default_in_redis_ttl: NotRequired[float]
126
- similarity_threshold: NotRequired[float]
127
- supported_call_types: NotRequired[List[CachingSupportedCallTypes]]
116
+ mode: CacheMode # when default_on cache is always on, when default_off cache is opt in
117
+ host: str
118
+ port: str
119
+ password: str
120
+ namespace: str
121
+ ttl: float
122
+ default_in_memory_ttl: float
123
+ default_in_redis_ttl: float
124
+ similarity_threshold: float
125
+ supported_call_types: list[CachingSupportedCallTypes]
128
126
  # s3 Bucket, boto3 configuration
129
- s3_bucket_name: NotRequired[str]
130
- s3_region_name: NotRequired[str]
131
- s3_api_version: NotRequired[str]
132
- s3_use_ssl: NotRequired[bool]
133
- s3_verify: NotRequired[bool | str]
134
- s3_endpoint_url: NotRequired[str]
135
- s3_aws_access_key_id: NotRequired[str]
136
- s3_aws_secret_access_key: NotRequired[str]
137
- s3_aws_session_token: NotRequired[str]
138
- s3_config: NotRequired[Any]
139
- s3_path: NotRequired[str]
127
+ s3_bucket_name: str
128
+ s3_region_name: str
129
+ s3_api_version: str
130
+ s3_use_ssl: bool
131
+ s3_verify: bool | str
132
+ s3_endpoint_url: str
133
+ s3_aws_access_key_id: str
134
+ s3_aws_secret_access_key: str
135
+ s3_aws_session_token: str
136
+ s3_config: Any
137
+ s3_path: str
140
138
  redis_semantic_cache_use_async: bool
141
139
  redis_semantic_cache_embedding_model: str
142
- redis_flush_size: NotRequired[int]
143
- redis_startup_nodes: NotRequired[List]
140
+ redis_flush_size: int
141
+ redis_startup_nodes: list
144
142
  disk_cache_dir: Any
145
- qdrant_api_base: NotRequired[str]
146
- qdrant_api_key: NotRequired[str]
147
- qdrant_collection_name: NotRequired[str]
148
- qdrant_quantization_config: NotRequired[str]
143
+ qdrant_api_base: str
144
+ qdrant_api_key: str
145
+ qdrant_collection_name: str
146
+ qdrant_quantization_config: str
149
147
  qdrant_semantic_cache_embedding_model: str
fabricatio/models/tool.py CHANGED
@@ -4,7 +4,7 @@ from importlib.machinery import ModuleSpec
4
4
  from importlib.util import module_from_spec
5
5
  from inspect import iscoroutinefunction, signature
6
6
  from types import CodeType, ModuleType
7
- from typing import Any, Callable, Dict, List, Optional, Self, overload
7
+ from typing import Any, Callable, Dict, List, Optional, Self, cast, overload
8
8
 
9
9
  from fabricatio.config import configs
10
10
  from fabricatio.decorators import logging_execution_info, use_temp_module
@@ -136,7 +136,7 @@ class ToolExecutor(BaseModel):
136
136
 
137
137
  def inject_tools[M: ModuleType](self, module: Optional[M] = None) -> M:
138
138
  """Inject the tools into the provided module or default."""
139
- module = module or module_from_spec(spec=ModuleSpec(name=configs.toolbox.tool_module_name, loader=None))
139
+ module = module or cast(M, module_from_spec(spec=ModuleSpec(name=configs.toolbox.tool_module_name, loader=None)))
140
140
  for tool in self.candidates:
141
141
  logger.debug(f"Injecting tool: {tool.name}")
142
142
  setattr(module, tool.name, tool.invoke)
@@ -144,7 +144,7 @@ class ToolExecutor(BaseModel):
144
144
 
145
145
  def inject_data[M: ModuleType](self, module: Optional[M] = None) -> M:
146
146
  """Inject the data into the provided module or default."""
147
- module = module or module_from_spec(spec=ModuleSpec(name=configs.toolbox.data_module_name, loader=None))
147
+ module = module or cast(M,module_from_spec(spec=ModuleSpec(name=configs.toolbox.data_module_name, loader=None)))
148
148
  for key, value in self.data.items():
149
149
  logger.debug(f"Injecting data: {key}")
150
150
  setattr(module, key, value)
@@ -184,6 +184,6 @@ class ToolExecutor(BaseModel):
184
184
  tools = []
185
185
  while tool_name := recipe.pop(0):
186
186
  for toolbox in toolboxes:
187
- tools.append(toolbox[tool_name])
187
+ tools.append(toolbox.get(tool_name))
188
188
 
189
189
  return cls(candidates=tools)