fabricatio 0.2.1.dev4__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.2__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.
@@ -12,7 +12,7 @@ from fabricatio.models.generic import WithBriefing
12
12
  from fabricatio.models.kwargs_types import GenerateKwargs, ValidateKwargs
13
13
  from fabricatio.models.usages import LLMUsage
14
14
  from fabricatio.parser import JsonCapture
15
- from more_itertools import flatten
15
+ from more_itertools import flatten, windowed
16
16
  from pydantic import NonNegativeInt, PositiveInt
17
17
 
18
18
 
@@ -275,3 +275,81 @@ class GiveRating(WithBriefing, LLMUsage):
275
275
  validator=_criteria_validator,
276
276
  **kwargs,
277
277
  )
278
+
279
+ async def drafting_rating_weights_klee(
280
+ self,
281
+ topic: str,
282
+ criteria: Set[str],
283
+ **kwargs: Unpack[ValidateKwargs],
284
+ ) -> Dict[str, float]:
285
+ """Drafts rating weights for a given topic and criteria using the Klee method.
286
+
287
+ Args:
288
+ topic (str): The topic for the rating weights.
289
+ criteria (Set[str]): A set of criteria for the rating weights.
290
+ **kwargs (Unpack[ValidateKwargs]): Additional keyword arguments for the LLM usage.
291
+
292
+ Returns:
293
+ Dict[str, float]: A dictionary representing the drafted rating weights for each criterion.
294
+ """
295
+ if len(criteria) < 2: # noqa: PLR2004
296
+ raise ValueError("At least two criteria are required to draft rating weights")
297
+
298
+ def _validator(resp: str) -> float | None:
299
+ if (cap := JsonCapture.convert_with(resp, orjson.loads)) is not None and isinstance(cap, float):
300
+ return cap
301
+ return None
302
+
303
+ criteria = list(criteria) # freeze the order
304
+ windows = windowed(criteria, 2)
305
+
306
+ # get the importance multiplier indicating how important is second criterion compared to the first one
307
+ relative_weights = await self.aask_validate_batch(
308
+ questions=[
309
+ template_manager.render_template(
310
+ configs.templates.draft_rating_weights_klee_template,
311
+ {
312
+ "topic": topic,
313
+ "first": pair[0],
314
+ "second": pair[1],
315
+ },
316
+ )
317
+ for pair in windows
318
+ ],
319
+ validator=_validator,
320
+ **GenerateKwargs(system_message=f"# your personal briefing: \n{self.briefing}", **kwargs),
321
+ )
322
+ weights = [1]
323
+ for rw in relative_weights:
324
+ weights.append(weights[-1] * rw)
325
+ total = sum(weights)
326
+ return dict(zip(criteria, [w / total for w in weights], strict=True))
327
+
328
+ async def composite_score(
329
+ self,
330
+ topic: str,
331
+ to_rate: List[str],
332
+ reasons_count: PositiveInt = 2,
333
+ criteria_count: PositiveInt = 5,
334
+ **kwargs: Unpack[ValidateKwargs],
335
+ ) -> List[float]:
336
+ """Calculates the composite scores for a list of items based on a given topic and criteria.
337
+
338
+ Args:
339
+ topic (str): The topic for the rating.
340
+ to_rate (List[str]): A list of strings to be rated.
341
+ reasons_count (PositiveInt, optional): The number of reasons to extract from each pair of examples. Defaults to 2.
342
+ criteria_count (PositiveInt, optional): The number of criteria to draft. Defaults to 5.
343
+ **kwargs (Unpack[ValidateKwargs]): Additional keyword arguments for the LLM usage.
344
+
345
+ Returns:
346
+ List[float]: A list of composite scores for the items.
347
+ """
348
+ criteria = await self.draft_rating_criteria_from_examples(
349
+ topic, to_rate, reasons_count, criteria_count, **kwargs
350
+ )
351
+ weights = await self.drafting_rating_weights_klee(topic, criteria, **kwargs)
352
+ logger.info(f"Criteria: {criteria}\nWeights: {weights}")
353
+ ratings_seq = await self.rate(to_rate, topic, criteria, **kwargs)
354
+
355
+ return [sum(ratings[c] * weights[c] for c in criteria) for ratings in ratings_seq]
fabricatio/config.py CHANGED
@@ -172,6 +172,9 @@ class TemplateConfig(BaseModel):
172
172
  extract_criteria_from_reasons_template: str = Field(default="extract_criteria_from_reasons")
173
173
  """The name of the extract criteria from reasons template which will be used to extract criteria from reasons."""
174
174
 
175
+ draft_rating_weights_klee_template: str = Field(default="draft_rating_weights_klee")
176
+ """The name of the draft rating weights klee template which will be used to draft rating weights with Klee method."""
177
+
175
178
 
176
179
  class MagikaConfig(BaseModel):
177
180
  """Magika configuration class."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fabricatio
3
- Version: 0.2.1.dev4
3
+ Version: 0.2.2
4
4
  Classifier: License :: OSI Approved :: MIT License
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python :: 3.12
@@ -1,6 +1,6 @@
1
- fabricatio-0.2.1.dev4.dist-info/METADATA,sha256=oHmnpsvVA2lu7hJEfGzSThR2xjXIbxk8A7-tMfQLYWo,11960
2
- fabricatio-0.2.1.dev4.dist-info/WHEEL,sha256=RIvmwLDYujv60MYBx2jxyP4vdn1DD7X0kBgz1TQvZuc,108
3
- fabricatio-0.2.1.dev4.dist-info/licenses/LICENSE,sha256=yDZaTLnOi03bi3Dk6f5IjhLUc5old2yOsihHWU0z-i0,1067
1
+ fabricatio-0.2.2.dist-info/METADATA,sha256=6jYTAggAa5xO9tsHL1XwqJCDKsHq37NcUPjcqxY31YM,11955
2
+ fabricatio-0.2.2.dist-info/WHEEL,sha256=RIvmwLDYujv60MYBx2jxyP4vdn1DD7X0kBgz1TQvZuc,108
3
+ fabricatio-0.2.2.dist-info/licenses/LICENSE,sha256=yDZaTLnOi03bi3Dk6f5IjhLUc5old2yOsihHWU0z-i0,1067
4
4
  fabricatio/decorators.py,sha256=cJHsxxbnMhc4SzPl4454CPLuDP3H0qbTrzV_U2rLPrs,6372
5
5
  fabricatio/core.py,sha256=apwXgI94DCWpGujGlsmXsTZQvJOQMB9llmuUo7ohd-4,5771
6
6
  fabricatio/models/generic.py,sha256=4j6DRNkHLNhx8U6aijf0Cz9HSahtEPQBh22i6d_PGTs,4981
@@ -20,7 +20,7 @@ fabricatio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
20
  fabricatio/fs/readers.py,sha256=lqMrM5Nt3l6QJmPDoBno2PSaN2BbFwfUjBqaghrbK3A,1002
21
21
  fabricatio/fs/curd.py,sha256=g74Pc2WPSkPlCXX0cWj2jyiDYTfkLwpwM480eyCkmc8,3851
22
22
  fabricatio/fs/__init__.py,sha256=bYE9r8uR0dtknzbg_YaGv_6Wwa27ntkQt0Tl7Kb3HFI,117
23
- fabricatio/config.py,sha256=ViWC9Htaq6tTbqzhLBxcTzaxNUINwOKD0Pk2myj0Svw,10973
23
+ fabricatio/config.py,sha256=KBA7TN138YrPPiqx873m_KV6MoIiiSA9VGtwLJJ2PZE,11185
24
24
  fabricatio/journal.py,sha256=bzxZay48ZWI0VIkkDXm4Wc_Cc9lBQYa2VGx3Hxy_PtA,753
25
25
  fabricatio/__init__.py,sha256=h0FgwSAdI2yhxYiuQi46uFvzqkK5LfJLt5XsHZ9rWIo,1063
26
26
  fabricatio/actions/communication.py,sha256=MrL_revv1zm1VXqjDMKZkg4PHmyMxYThAmmw2wF791k,414
@@ -28,9 +28,9 @@ fabricatio/actions/transmission.py,sha256=WM-kssiqzTLZuZSiqbRvBaC2qynd9kjrKXya4f
28
28
  fabricatio/actions/__init__.py,sha256=eLa_5ACZ-FqdrLtOfCHk5nQBxzhIs1kgMIXWmkm2P8Y,110
29
29
  fabricatio/_rust_instances.py,sha256=JAtO-vL8ihvduf1SHLNf0w7ZSVGCJeIv6zZ9Ekyy1hY,271
30
30
  fabricatio/parser.py,sha256=Q4laV79Svggl09UKa3lAw6NYPuDz1sP2F-6U4tsSvQw,3429
31
- fabricatio/capabilities/rating.py,sha256=-QAqcxUaJcu3_Xh_9VNgvOAue1AM6Sa50z855ppkKbc,11193
31
+ fabricatio/capabilities/rating.py,sha256=KLJlaufG74lHJ5hHI_tjhQgjWjHhO_38Q1ILW2cY9fA,14552
32
32
  fabricatio/capabilities/task.py,sha256=UnkeON3VCADXUBtUmQbMwl9TEwK0yQSED3EglqMMHQ4,5196
33
33
  fabricatio/_rust.pyi,sha256=clhcURuiB9zlFo4m3VyoWQ8Xs4tvg6KNHXpF-ok9h4o,1703
34
34
  fabricatio/_rust.cpython-312-x86_64-linux-gnu.so,sha256=zuOW9YLJrxii2VOvcedTt_WxWPw6FXWJQIMm8wNB_Yg,1339320
35
- fabricatio-0.2.1.dev4.data/scripts/tdown,sha256=P1enq0UfaeGcF3CRC51IYwzkEeUYxWi_C2vsOdAxgVA,4569728
36
- fabricatio-0.2.1.dev4.dist-info/RECORD,,
35
+ fabricatio-0.2.2.data/scripts/tdown,sha256=P1enq0UfaeGcF3CRC51IYwzkEeUYxWi_C2vsOdAxgVA,4569728
36
+ fabricatio-0.2.2.dist-info/RECORD,,