fabricatio 0.2.6.dev4__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.6.dev6__cp312-cp312-manylinux_2_34_x86_64.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.
@@ -93,10 +93,11 @@ class CorrectProposal(Action):
93
93
  f"{task_input.briefing}\nExtract the path of file, which contains the article briefing that I need to read."
94
94
  )
95
95
 
96
- rprint(article_proposal.display())
97
96
  ret = None
98
97
  while await confirm("Do you want to correct the Proposal?").ask_async():
99
- topic = await text("What is the topic of the proposal?").ask_async()
98
+ rprint(article_proposal.display())
99
+ while not (topic := await text("What is the topic of the proposal reviewing?").ask_async()):
100
+ ...
100
101
  ret = await self.correct_obj(
101
102
  article_proposal,
102
103
  safe_text_read(input_path),
@@ -115,11 +116,13 @@ class CorrectOutline(Action):
115
116
  self,
116
117
  article_outline: ArticleOutline,
117
118
  article_proposal: ArticleProposal,
119
+
118
120
  **_,
119
121
  ) -> Optional[str]:
120
- rprint(article_outline.finalized_dump())
121
122
  ret = None
122
123
  while await confirm("Do you want to correct the outline?").ask_async():
123
- topic = await text("What is the topic of the outline?").ask_async()
124
+ rprint(article_outline.finalized_dump())
125
+ while not (topic := await text("What is the topic of the outline reviewing?").ask_async()):
126
+ ...
124
127
  ret = await self.correct_obj(article_outline, article_proposal.display(), topic=topic)
125
128
  return ret or article_outline
@@ -21,7 +21,7 @@ from fabricatio.models.kwargs_types import (
21
21
  LLMKwargs,
22
22
  )
23
23
  from fabricatio.models.usages import EmbeddingUsage
24
- from fabricatio.models.utils import MilvusData
24
+ from fabricatio.models.utils import MilvusData, ok
25
25
  from more_itertools.recipes import flatten, unique
26
26
  from pydantic import Field, PrivateAttr
27
27
 
@@ -60,7 +60,7 @@ class RAG(EmbeddingUsage):
60
60
  ) -> Self:
61
61
  """Initialize the Milvus client."""
62
62
  self._client = create_client(
63
- uri=milvus_uri or (self.milvus_uri or configs.rag.milvus_uri).unicode_string(),
63
+ uri=milvus_uri or ok(self.milvus_uri or configs.rag.milvus_uri).unicode_string(),
64
64
  token=milvus_token
65
65
  or (token.get_secret_value() if (token := (self.milvus_token or configs.rag.milvus_token)) else ""),
66
66
  timeout=milvus_timeout or self.milvus_timeout,
@@ -315,7 +315,7 @@ class RAG(EmbeddingUsage):
315
315
  **kwargs,
316
316
  )
317
317
 
318
- async def arefined_query(self, question: List[str] | str, **kwargs: Unpack[ChooseKwargs]) -> List[str]:
318
+ async def arefined_query(self, question: List[str] | str, **kwargs: Unpack[ChooseKwargs]) -> Optional[List[str]]:
319
319
  """Refines the given question using a template.
320
320
 
321
321
  Args:
@@ -84,7 +84,7 @@ class HandleTask(WithBriefing, ToolBoxUsage):
84
84
  **self.prepend(cast(Dict[str, Any], kwargs)),
85
85
  )
86
86
 
87
- async def handle_fin_grind(
87
+ async def handle_fine_grind(
88
88
  self,
89
89
  task: Task,
90
90
  data: Dict[str, Any],
@@ -110,4 +110,4 @@ class HandleTask(WithBriefing, ToolBoxUsage):
110
110
 
111
111
  async def handle(self, task: Task, data: Dict[str, Any], **kwargs: Unpack[ValidateKwargs]) -> Optional[Tuple]:
112
112
  """Asynchronously handles a task based on a given task object and parameters."""
113
- return await self.handle_fin_grind(task, data, **kwargs)
113
+ return await self.handle_fine_grind(task, data, **kwargs)
fabricatio/config.py CHANGED
@@ -48,37 +48,37 @@ class LLMConfig(BaseModel):
48
48
  """
49
49
 
50
50
  model_config = ConfigDict(use_attribute_docstrings=True)
51
- api_endpoint: HttpUrl = Field(default=HttpUrl("https://api.openai.com"))
51
+ api_endpoint: Optional[HttpUrl] = Field(default=HttpUrl("https://api.openai.com"))
52
52
  """OpenAI API Endpoint."""
53
53
 
54
- api_key: SecretStr = Field(default=SecretStr(""))
54
+ api_key: Optional[SecretStr] = Field(default=SecretStr("sk-setyourkey"))
55
55
  """OpenAI API key. Empty by default for security reasons, should be set before use."""
56
56
 
57
- timeout: PositiveInt = Field(default=300)
57
+ timeout: Optional[PositiveInt] = Field(default=300)
58
58
  """The timeout of the LLM model in seconds. Default is 300 seconds as per request."""
59
59
 
60
- max_retries: PositiveInt = Field(default=3)
60
+ max_retries: Optional[PositiveInt] = Field(default=3)
61
61
  """The maximum number of retries. Default is 3 retries."""
62
62
 
63
- model: str = Field(default="gpt-3.5-turbo")
63
+ model: Optional[str] = Field(default="gpt-3.5-turbo")
64
64
  """The LLM model name. Set to 'gpt-3.5-turbo' as per request."""
65
65
 
66
- temperature: NonNegativeFloat = Field(default=1.0)
66
+ temperature: Optional[NonNegativeFloat] = Field(default=1.0)
67
67
  """The temperature of the LLM model. Controls randomness in generation. Set to 1.0 as per request."""
68
68
 
69
- stop_sign: str | List[str] = Field(default_factory=lambda: ["\n\n\n", "User:"])
69
+ stop_sign: Optional[str | List[str]] = Field(default=None)
70
70
  """The stop sign of the LLM model. No default stop sign specified."""
71
71
 
72
- top_p: NonNegativeFloat = Field(default=0.35)
72
+ top_p: Optional[NonNegativeFloat] = Field(default=0.35)
73
73
  """The top p of the LLM model. Controls diversity via nucleus sampling. Set to 0.35 as per request."""
74
74
 
75
- generation_count: PositiveInt = Field(default=1)
75
+ generation_count: Optional[PositiveInt] = Field(default=1)
76
76
  """The number of generations to generate. Default is 1."""
77
77
 
78
- stream: bool = Field(default=False)
78
+ stream: Optional[bool] = Field(default=False)
79
79
  """Whether to stream the LLM model's response. Default is False."""
80
80
 
81
- max_tokens: PositiveInt = Field(default=8192)
81
+ max_tokens: Optional[PositiveInt] = Field(default=None)
82
82
  """The maximum number of tokens to generate. Set to 8192 as per request."""
83
83
 
84
84
  rpm: Optional[PositiveInt] = Field(default=100)
@@ -93,7 +93,7 @@ class EmbeddingConfig(BaseModel):
93
93
 
94
94
  model_config = ConfigDict(use_attribute_docstrings=True)
95
95
 
96
- model: str = Field(default="text-embedding-ada-002")
96
+ model: Optional[str] = Field(default="text-embedding-ada-002")
97
97
  """The embedding model name. """
98
98
 
99
99
  dimensions: Optional[PositiveInt] = Field(default=None)
@@ -102,10 +102,10 @@ class EmbeddingConfig(BaseModel):
102
102
  timeout: Optional[PositiveInt] = Field(default=None)
103
103
  """The timeout of the embedding model in seconds."""
104
104
 
105
- max_sequence_length: PositiveInt = Field(default=8192)
105
+ max_sequence_length: Optional[PositiveInt] = Field(default=8192)
106
106
  """The maximum sequence length of the embedding model. Default is 8192 as per request."""
107
107
 
108
- caching: bool = Field(default=False)
108
+ caching: Optional[bool] = Field(default=False)
109
109
  """Whether to cache the embedding. Default is False."""
110
110
 
111
111
  api_endpoint: Optional[HttpUrl] = None
@@ -148,13 +148,13 @@ class DebugConfig(BaseModel):
148
148
  log_level: Literal["DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"] = Field(default="INFO")
149
149
  """The log level of the application."""
150
150
 
151
- log_file: FilePath = Field(default=Path(rf"{ROAMING_DIR}\fabricatio.log"))
151
+ log_file: FilePath = Field(default=Path(rf"{ROAMING_DIR}\fabricatio.log"), frozen=True)
152
152
  """The log file of the application."""
153
153
 
154
- rotation: int = Field(default=1)
154
+ rotation: int = Field(default=1, frozen=True)
155
155
  """The rotation of the log file. in weeks."""
156
156
 
157
- retention: int = Field(default=2)
157
+ retention: int = Field(default=2, frozen=True)
158
158
  """The retention of the log file. in weeks."""
159
159
 
160
160
  streaming_visible: bool = Field(default=False)
@@ -232,6 +232,9 @@ class TemplateConfig(BaseModel):
232
232
  correct_template: str = Field(default="correct")
233
233
  """The name of the correct template which will be used to correct a string."""
234
234
 
235
+ co_validation_template: str = Field(default="co_validation")
236
+ """The name of the co-validation template which will be used to co-validate a string."""
237
+
235
238
 
236
239
  class MagikaConfig(BaseModel):
237
240
  """Magika configuration class."""
@@ -272,7 +275,7 @@ class RagConfig(BaseModel):
272
275
 
273
276
  model_config = ConfigDict(use_attribute_docstrings=True)
274
277
 
275
- milvus_uri: HttpUrl = Field(default=HttpUrl("http://localhost:19530"))
278
+ milvus_uri: Optional[HttpUrl] = Field(default=HttpUrl("http://localhost:19530"))
276
279
  """The URI of the Milvus server."""
277
280
  milvus_timeout: Optional[PositiveFloat] = Field(default=None)
278
281
  """The timeout of the Milvus server."""
@@ -1,4 +1,8 @@
1
- """Module that contains the classes for actions and workflows."""
1
+ """Module that contains the classes for actions and workflows.
2
+
3
+ This module defines the Action and WorkFlow classes, which are used for
4
+ creating and executing sequences of actions in a task-based context.
5
+ """
2
6
 
3
7
  import traceback
4
8
  from abc import abstractmethod
@@ -15,20 +19,27 @@ from pydantic import Field, PrivateAttr
15
19
 
16
20
 
17
21
  class Action(HandleTask, ProposeTask, Correct):
18
- """Class that represents an action to be executed in a workflow."""
22
+ """Class that represents an action to be executed in a workflow.
23
+
24
+ Actions are the atomic units of work in a workflow. Each action performs
25
+ a specific operation and can modify the shared context data.
26
+ """
19
27
 
20
28
  name: str = Field(default="")
21
29
  """The name of the action."""
30
+
22
31
  description: str = Field(default="")
23
32
  """The description of the action."""
33
+
24
34
  personality: str = Field(default="")
25
- """The personality of whom the action belongs to."""
35
+ """The personality traits or context for the action executor."""
36
+
26
37
  output_key: str = Field(default="")
27
- """The key of the output data."""
38
+ """The key used to store this action's output in the context dictionary."""
28
39
 
29
40
  @final
30
41
  def model_post_init(self, __context: Any) -> None:
31
- """Initialize the action by setting the name if not provided.
42
+ """Initialize the action by setting default name and description if not provided.
32
43
 
33
44
  Args:
34
45
  __context: The context to be used for initialization.
@@ -38,121 +49,170 @@ class Action(HandleTask, ProposeTask, Correct):
38
49
 
39
50
  @abstractmethod
40
51
  async def _execute(self, **cxt) -> Any:
41
- """Execute the action with the provided arguments.
52
+ """Execute the action logic with the provided context arguments.
53
+
54
+ This method must be implemented by subclasses to define the actual behavior.
42
55
 
43
56
  Args:
44
57
  **cxt: The context dictionary containing input and output data.
45
58
 
46
59
  Returns:
47
- The result of the action execution.
60
+ Any: The result of the action execution.
48
61
  """
49
62
  pass
50
63
 
51
64
  @final
52
65
  async def act(self, cxt: Dict[str, Any]) -> Dict[str, Any]:
53
- """Perform the action by executing it and setting the output data.
66
+ """Perform the action and update the context with results.
54
67
 
55
68
  Args:
56
69
  cxt: The context dictionary containing input and output data.
70
+
71
+ Returns:
72
+ Dict[str, Any]: The updated context dictionary.
57
73
  """
58
74
  ret = await self._execute(**cxt)
75
+
59
76
  if self.output_key:
60
77
  logger.debug(f"Setting output: {self.output_key}")
61
78
  cxt[self.output_key] = ret
79
+
62
80
  return cxt
63
81
 
64
82
  @property
65
83
  def briefing(self) -> str:
66
- """Return a brief description of the action."""
84
+ """Return a formatted description of the action including personality context if available.
85
+
86
+ Returns:
87
+ str: Formatted briefing text with personality and action description.
88
+ """
67
89
  if self.personality:
68
90
  return f"## Your personality: \n{self.personality}\n# The action you are going to perform: \n{super().briefing}"
69
91
  return f"# The action you are going to perform: \n{super().briefing}"
70
92
 
71
93
 
72
94
  class WorkFlow(WithBriefing, ToolBoxUsage):
73
- """Class that represents a workflow to be executed in a task."""
95
+ """Class that represents a sequence of actions to be executed for a task.
96
+
97
+ A workflow manages the execution of multiple actions in sequence, passing
98
+ a shared context between them and handling task lifecycle events.
99
+ """
74
100
 
75
101
  _context: Queue[Dict[str, Any]] = PrivateAttr(default_factory=lambda: Queue(maxsize=1))
76
- """ The context dictionary to be used for workflow execution."""
102
+ """Queue for storing the workflow execution context."""
77
103
 
78
104
  _instances: Tuple[Action, ...] = PrivateAttr(default_factory=tuple)
79
- """ The instances of the workflow steps."""
105
+ """Instantiated action objects to be executed in this workflow."""
80
106
 
81
107
  steps: Tuple[Union[Type[Action], Action], ...] = Field(...)
82
- """ The steps to be executed in the workflow, actions or action classes."""
108
+ """The sequence of actions to be executed, can be action classes or instances."""
109
+
83
110
  task_input_key: str = Field(default="task_input")
84
- """ The key of the task input data."""
111
+ """Key used to store the input task in the context dictionary."""
112
+
85
113
  task_output_key: str = Field(default="task_output")
86
- """ The key of the task output data."""
114
+ """Key used to extract the final result from the context dictionary."""
115
+
87
116
  extra_init_context: Dict[str, Any] = Field(default_factory=dict, frozen=True)
88
- """ The extra context dictionary to be used for workflow initialization."""
117
+ """Additional initial context values to be included at workflow start."""
89
118
 
90
119
  def model_post_init(self, __context: Any) -> None:
91
- """Initialize the workflow by setting fallbacks for each step.
120
+ """Initialize the workflow by instantiating any action classes.
92
121
 
93
122
  Args:
94
123
  __context: The context to be used for initialization.
95
124
  """
96
- temp = []
97
- for step in self.steps:
98
- temp.append(step if isinstance(step, Action) else step())
99
- self._instances = tuple(temp)
125
+ # Convert any action classes to instances
126
+ self._instances = tuple(step if isinstance(step, Action) else step() for step in self.steps)
100
127
 
101
128
  def inject_personality(self, personality: str) -> Self:
102
- """Inject the personality of the workflow.
129
+ """Set the personality for all actions that don't have one defined.
103
130
 
104
131
  Args:
105
- personality: The personality to be injected.
132
+ personality: The personality text to inject.
106
133
 
107
134
  Returns:
108
- Self: The instance of the workflow with the injected personality.
135
+ Self: The workflow instance for method chaining.
109
136
  """
110
- for a in filter(lambda action: not action.personality, self._instances):
111
- a.personality = personality
137
+ for action in filter(lambda a: not a.personality, self._instances):
138
+ action.personality = personality
112
139
  return self
113
140
 
114
141
  async def serve(self, task: Task) -> None:
115
- """Serve the task by executing the workflow steps.
142
+ """Execute the workflow to fulfill the given task.
143
+
144
+ This method manages the complete lifecycle of processing a task through
145
+ the workflow's sequence of actions.
116
146
 
117
147
  Args:
118
- task: The task to be served.
148
+ task: The task to be processed.
119
149
  """
120
150
  await task.start()
121
151
  await self._init_context(task)
152
+
122
153
  current_action = None
123
154
  try:
155
+ # Process each action in sequence
124
156
  for step in self._instances:
125
- logger.debug(f"Executing step: {(current_action := step.name)}")
126
- act_task = create_task(step.act(await self._context.get()))
157
+ current_action = step.name
158
+ logger.debug(f"Executing step: {current_action}")
159
+
160
+ # Get current context and execute action
161
+ context = await self._context.get()
162
+ act_task = create_task(step.act(context))
163
+
164
+ # Handle task cancellation
127
165
  if task.is_cancelled():
128
166
  act_task.cancel(f"Cancelled by task: {task.name}")
129
167
  break
168
+
169
+ # Update context with modified values
130
170
  modified_ctx = await act_task
131
171
  await self._context.put(modified_ctx)
172
+
132
173
  logger.info(f"Finished executing workflow: {self.name}")
133
174
 
134
- if self.task_output_key not in (final_ctx := await self._context.get()):
175
+ # Get final context and extract result
176
+ final_ctx = await self._context.get()
177
+ result = final_ctx.get(self.task_output_key)
178
+
179
+ if self.task_output_key not in final_ctx:
135
180
  logger.warning(
136
- f"Task output key: {self.task_output_key} not found in the context, None will be returned. You can check if `Action.output_key` is set the same as `WorkFlow.task_output_key`."
181
+ f"Task output key: {self.task_output_key} not found in the context, None will be returned. "
182
+ f"You can check if `Action.output_key` is set the same as `WorkFlow.task_output_key`."
137
183
  )
138
184
 
139
- await task.finish(final_ctx.get(self.task_output_key, None))
185
+ await task.finish(result)
186
+
140
187
  except RuntimeError as e:
141
- logger.error(f"Error during task: {current_action} execution: {e}") # Log the exception
142
- logger.error(traceback.format_exc()) # Add this line to log the traceback
143
- await task.fail() # Mark the task as failed
188
+ logger.error(f"Error during task: {current_action} execution: {e}")
189
+ logger.error(traceback.format_exc())
190
+ await task.fail()
144
191
 
145
192
  async def _init_context[T](self, task: Task[T]) -> None:
146
- """Initialize the context dictionary for workflow execution."""
193
+ """Initialize the context dictionary for workflow execution.
194
+
195
+ Args:
196
+ task: The task being served by this workflow.
197
+ """
147
198
  logger.debug(f"Initializing context for workflow: {self.name}")
148
- await self._context.put({self.task_input_key: task, **dict(self.extra_init_context)})
199
+ initial_context = {self.task_input_key: task, **dict(self.extra_init_context)}
200
+ await self._context.put(initial_context)
149
201
 
150
202
  def steps_fallback_to_self(self) -> Self:
151
- """Set the fallback for each step to the workflow itself."""
203
+ """Configure all steps to use this workflow's configuration as fallback.
204
+
205
+ Returns:
206
+ Self: The workflow instance for method chaining.
207
+ """
152
208
  self.hold_to(self._instances)
153
209
  return self
154
210
 
155
211
  def steps_supply_tools_from_self(self) -> Self:
156
- """Supply the tools from the workflow to each step."""
212
+ """Provide this workflow's tools to all steps in the workflow.
213
+
214
+ Returns:
215
+ Self: The workflow instance for method chaining.
216
+ """
157
217
  self.provide_tools_to(self._instances)
158
218
  return self