fabricatio 0.2.13.dev3__cp312-cp312-win_amd64.whl → 0.3.14.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/__init__.py +5 -10
- fabricatio/actions/article.py +32 -34
- fabricatio/actions/article_rag.py +58 -58
- fabricatio/actions/output.py +22 -21
- fabricatio/actions/rag.py +4 -3
- fabricatio/capabilities/check.py +29 -30
- fabricatio/capabilities/correct.py +23 -23
- fabricatio/capabilities/extract.py +36 -32
- fabricatio/capabilities/rag.py +34 -35
- fabricatio/capabilities/rating.py +77 -72
- fabricatio/capabilities/review.py +12 -11
- fabricatio/capabilities/task.py +4 -5
- fabricatio/core.py +22 -24
- fabricatio/decorators.py +10 -10
- fabricatio/fs/__init__.py +1 -2
- fabricatio/journal.py +2 -9
- fabricatio/models/action.py +8 -22
- fabricatio/models/extra/aricle_rag.py +10 -9
- fabricatio/models/extra/article_base.py +5 -6
- fabricatio/models/extra/article_main.py +11 -10
- fabricatio/models/generic.py +33 -60
- fabricatio/models/kwargs_types.py +1 -46
- fabricatio/models/role.py +45 -25
- fabricatio/models/task.py +9 -9
- fabricatio/models/tool.py +5 -4
- fabricatio/models/usages.py +170 -161
- fabricatio/parser.py +2 -2
- fabricatio/rust.cp312-win_amd64.pyd +0 -0
- fabricatio/rust.pyi +435 -17
- fabricatio/utils.py +38 -4
- fabricatio-0.3.14.dev0.data/scripts/tdown.exe +0 -0
- {fabricatio-0.2.13.dev3.data → fabricatio-0.3.14.dev0.data}/scripts/ttm.exe +0 -0
- {fabricatio-0.2.13.dev3.dist-info → fabricatio-0.3.14.dev0.dist-info}/METADATA +1 -3
- fabricatio-0.3.14.dev0.dist-info/RECORD +63 -0
- fabricatio/config.py +0 -430
- fabricatio/constants.py +0 -20
- fabricatio/models/events.py +0 -120
- fabricatio/rust_instances.py +0 -10
- fabricatio-0.2.13.dev3.data/scripts/tdown.exe +0 -0
- fabricatio-0.2.13.dev3.dist-info/RECORD +0 -67
- {fabricatio-0.2.13.dev3.dist-info → fabricatio-0.3.14.dev0.dist-info}/WHEEL +0 -0
- {fabricatio-0.2.13.dev3.dist-info → fabricatio-0.3.14.dev0.dist-info}/licenses/LICENSE +0 -0
@@ -1,11 +1,12 @@
|
|
1
1
|
"""A module containing the Correct capability for reviewing, validating, and improving objects."""
|
2
2
|
|
3
3
|
from asyncio import gather
|
4
|
-
from typing import Optional,
|
4
|
+
from typing import Optional, Unpack, cast
|
5
|
+
|
6
|
+
from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
|
5
7
|
|
6
8
|
from fabricatio.capabilities.propose import Propose
|
7
9
|
from fabricatio.capabilities.rating import Rating
|
8
|
-
from fabricatio.config import configs
|
9
10
|
from fabricatio.journal import logger
|
10
11
|
from fabricatio.models.adv_kwargs_types import CorrectKwargs
|
11
12
|
from fabricatio.models.extra.problem import Improvement, ProblemSolutions
|
@@ -14,7 +15,6 @@ from fabricatio.models.kwargs_types import (
|
|
14
15
|
BestKwargs,
|
15
16
|
ValidateKwargs,
|
16
17
|
)
|
17
|
-
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
18
18
|
from fabricatio.utils import fallback_kwargs, ok, override_kwargs
|
19
19
|
|
20
20
|
|
@@ -22,7 +22,7 @@ class Correct(Rating, Propose):
|
|
22
22
|
"""A class that provides the capability to correct objects."""
|
23
23
|
|
24
24
|
async def decide_solution(
|
25
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
|
@@ -92,7 +92,7 @@ class Correct(Rating, Propose):
|
|
92
92
|
return await self.propose(
|
93
93
|
cast("Type[M]", obj.__class__),
|
94
94
|
TEMPLATE_MANAGER.render_template(
|
95
|
-
|
95
|
+
CONFIG.templates.fix_troubled_obj_template,
|
96
96
|
{
|
97
97
|
"problem": problem_solutions.problem.display(),
|
98
98
|
"solution": ok(
|
@@ -106,11 +106,11 @@ class Correct(Rating, Propose):
|
|
106
106
|
)
|
107
107
|
|
108
108
|
async def fix_troubled_string(
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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
|
|
@@ -125,7 +125,7 @@ class Correct(Rating, Propose):
|
|
125
125
|
"""
|
126
126
|
return await self.ageneric_string(
|
127
127
|
TEMPLATE_MANAGER.render_template(
|
128
|
-
|
128
|
+
CONFIG.templates.fix_troubled_string_template,
|
129
129
|
{
|
130
130
|
"problem": problem_solutions.problem.display(),
|
131
131
|
"solution": ok(
|
@@ -140,11 +140,11 @@ class Correct(Rating, Propose):
|
|
140
140
|
)
|
141
141
|
|
142
142
|
async def correct_obj[M: SketchedAble](
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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
|
-
|
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
|
-
|
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
|
|
@@ -2,9 +2,10 @@
|
|
2
2
|
|
3
3
|
from typing import List, Optional, Type, Unpack, overload
|
4
4
|
|
5
|
+
from fabricatio.rust import CONFIG
|
6
|
+
|
5
7
|
from fabricatio import TEMPLATE_MANAGER
|
6
8
|
from fabricatio.capabilities.propose import Propose
|
7
|
-
from fabricatio.config import configs
|
8
9
|
from fabricatio.models.generic import ProposedAble
|
9
10
|
from fabricatio.models.kwargs_types import ValidateKwargs
|
10
11
|
|
@@ -14,54 +15,57 @@ class Extract(Propose):
|
|
14
15
|
|
15
16
|
@overload
|
16
17
|
async def extract[M: ProposedAble](
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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]],
|
23
24
|
) -> M: ...
|
25
|
+
|
24
26
|
@overload
|
25
27
|
async def extract[M: ProposedAble](
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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]],
|
32
34
|
) -> Optional[M]: ...
|
33
35
|
|
34
36
|
@overload
|
35
37
|
async def extract[M: ProposedAble](
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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]],
|
42
44
|
) -> List[M]: ...
|
45
|
+
|
43
46
|
@overload
|
44
47
|
async def extract[M: ProposedAble](
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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]],
|
51
54
|
) -> List[Optional[M]]: ...
|
55
|
+
|
52
56
|
async def extract[M: ProposedAble](
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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]]],
|
59
63
|
) -> M | List[M] | Optional[M] | List[Optional[M]]:
|
60
64
|
"""Extract information from a given source to a model."""
|
61
65
|
return await self.propose(
|
62
66
|
cls,
|
63
67
|
prompt=TEMPLATE_MANAGER.render_template(
|
64
|
-
|
68
|
+
CONFIG.templates.extract_template,
|
65
69
|
[{"source": s, "extract_requirement": extract_requirement} for s in source]
|
66
70
|
if isinstance(source, list)
|
67
71
|
else {"source": source, "extract_requirement": extract_requirement, "align_language": align_language},
|
fabricatio/capabilities/rag.py
CHANGED
@@ -10,16 +10,15 @@ from functools import lru_cache
|
|
10
10
|
from operator import itemgetter
|
11
11
|
from typing import List, Optional, Self, Type, Unpack
|
12
12
|
|
13
|
+
from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
|
13
14
|
from more_itertools.recipes import flatten, unique
|
14
15
|
from pydantic import Field, PrivateAttr
|
15
16
|
|
16
|
-
from fabricatio.config import configs
|
17
17
|
from fabricatio.journal import logger
|
18
18
|
from fabricatio.models.adv_kwargs_types import CollectionConfigKwargs, FetchKwargs
|
19
19
|
from fabricatio.models.extra.rag import MilvusDataBase
|
20
20
|
from fabricatio.models.kwargs_types import ChooseKwargs
|
21
21
|
from fabricatio.models.usages import EmbeddingUsage
|
22
|
-
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
23
22
|
from fabricatio.utils import ok
|
24
23
|
|
25
24
|
|
@@ -50,17 +49,17 @@ class RAG(EmbeddingUsage):
|
|
50
49
|
return self._client
|
51
50
|
|
52
51
|
def init_client(
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
self,
|
53
|
+
milvus_uri: Optional[str] = None,
|
54
|
+
milvus_token: Optional[str] = None,
|
55
|
+
milvus_timeout: Optional[float] = None,
|
57
56
|
) -> Self:
|
58
57
|
"""Initialize the Milvus client."""
|
59
58
|
self._client = create_client(
|
60
|
-
uri=milvus_uri or ok(self.milvus_uri or
|
59
|
+
uri=milvus_uri or ok(self.milvus_uri or CONFIG.rag.milvus_uri).unicode_string(),
|
61
60
|
token=milvus_token
|
62
|
-
|
63
|
-
timeout=milvus_timeout or self.milvus_timeout or
|
61
|
+
or (token.get_secret_value() if (token := (self.milvus_token or CONFIG.rag.milvus_token)) else ""),
|
62
|
+
timeout=milvus_timeout or self.milvus_timeout or CONFIG.rag.milvus_timeout,
|
64
63
|
)
|
65
64
|
return self
|
66
65
|
|
@@ -73,7 +72,7 @@ class RAG(EmbeddingUsage):
|
|
73
72
|
return self
|
74
73
|
|
75
74
|
def view(
|
76
|
-
|
75
|
+
self, collection_name: Optional[str], create: bool = False, **kwargs: Unpack[CollectionConfigKwargs]
|
77
76
|
) -> Self:
|
78
77
|
"""View the specified collection.
|
79
78
|
|
@@ -86,9 +85,9 @@ class RAG(EmbeddingUsage):
|
|
86
85
|
kwargs["dimension"] = ok(
|
87
86
|
kwargs.get("dimension")
|
88
87
|
or self.milvus_dimensions
|
89
|
-
or
|
88
|
+
or CONFIG.rag.milvus_dimensions
|
90
89
|
or self.embedding_dimensions
|
91
|
-
or
|
90
|
+
or CONFIG.embedding.dimensions,
|
92
91
|
"`dimension` is not set at any level.",
|
93
92
|
)
|
94
93
|
self.client.create_collection(collection_name, auto_id=True, **kwargs)
|
@@ -115,7 +114,7 @@ class RAG(EmbeddingUsage):
|
|
115
114
|
return ok(self.target_collection, "No collection is being viewed. Have you called `self.view()`?")
|
116
115
|
|
117
116
|
async def add_document[D: MilvusDataBase](
|
118
|
-
|
117
|
+
self, data: List[D] | D, collection_name: Optional[str] = None, flush: bool = False
|
119
118
|
) -> Self:
|
120
119
|
"""Adds a document to the specified collection.
|
121
120
|
|
@@ -142,15 +141,15 @@ class RAG(EmbeddingUsage):
|
|
142
141
|
return self
|
143
142
|
|
144
143
|
async def afetch_document[D: MilvusDataBase](
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
144
|
+
self,
|
145
|
+
query: List[str],
|
146
|
+
document_model: Type[D],
|
147
|
+
collection_name: Optional[str] = None,
|
148
|
+
similarity_threshold: float = 0.37,
|
149
|
+
result_per_query: int = 10,
|
150
|
+
tei_endpoint: Optional[str] = None,
|
151
|
+
reranker_threshold: float = 0.7,
|
152
|
+
filter_expr: str = "",
|
154
153
|
) -> List[D]:
|
155
154
|
"""Asynchronously fetches documents from a Milvus database based on input vectors.
|
156
155
|
|
@@ -215,11 +214,11 @@ class RAG(EmbeddingUsage):
|
|
215
214
|
return document_model.from_sequence(resp)
|
216
215
|
|
217
216
|
async def aretrieve[D: MilvusDataBase](
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
217
|
+
self,
|
218
|
+
query: List[str] | str,
|
219
|
+
document_model: Type[D],
|
220
|
+
max_accepted: int = 20,
|
221
|
+
**kwargs: Unpack[FetchKwargs],
|
223
222
|
) -> List[D]:
|
224
223
|
"""Retrieve data from the collection.
|
225
224
|
|
@@ -236,15 +235,15 @@ class RAG(EmbeddingUsage):
|
|
236
235
|
query = [query]
|
237
236
|
|
238
237
|
return (
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
238
|
+
await self.afetch_document(
|
239
|
+
query=query,
|
240
|
+
document_model=document_model,
|
241
|
+
**kwargs,
|
242
|
+
)
|
243
|
+
)[:max_accepted]
|
245
244
|
|
246
245
|
async def arefined_query(
|
247
|
-
|
246
|
+
self, question: List[str] | str, **kwargs: Unpack[ChooseKwargs[Optional[List[str]]]]
|
248
247
|
) -> Optional[List[str]]:
|
249
248
|
"""Refines the given question using a template.
|
250
249
|
|
@@ -257,7 +256,7 @@ class RAG(EmbeddingUsage):
|
|
257
256
|
"""
|
258
257
|
return await self.alist_str(
|
259
258
|
TEMPLATE_MANAGER.render_template(
|
260
|
-
|
259
|
+
CONFIG.templates.refined_query_template,
|
261
260
|
{"question": [question] if isinstance(question, str) else question},
|
262
261
|
),
|
263
262
|
**kwargs,
|
@@ -4,16 +4,15 @@ from itertools import permutations
|
|
4
4
|
from random import sample
|
5
5
|
from typing import Dict, List, Optional, Set, Tuple, Union, Unpack, overload
|
6
6
|
|
7
|
+
from fabricatio.rust import CONFIG, TEMPLATE_MANAGER
|
7
8
|
from more_itertools import flatten, windowed
|
8
9
|
from pydantic import Field, NonNegativeInt, PositiveInt, create_model
|
9
10
|
|
10
11
|
from fabricatio.capabilities.propose import Propose
|
11
|
-
from fabricatio.config import configs
|
12
12
|
from fabricatio.journal import logger
|
13
13
|
from fabricatio.models.generic import Display, ProposedAble
|
14
14
|
from fabricatio.models.kwargs_types import CompositeScoreKwargs, ValidateKwargs
|
15
15
|
from fabricatio.parser import JsonCapture
|
16
|
-
from fabricatio.rust_instances import TEMPLATE_MANAGER
|
17
16
|
from fabricatio.utils import ok, override_kwargs
|
18
17
|
|
19
18
|
|
@@ -25,11 +24,11 @@ class Rating(Propose):
|
|
25
24
|
"""
|
26
25
|
|
27
26
|
async def rate_fine_grind(
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
self,
|
28
|
+
to_rate: str | List[str],
|
29
|
+
rating_manual: Dict[str, str],
|
30
|
+
score_range: Tuple[float, float],
|
31
|
+
**kwargs: Unpack[ValidateKwargs[Dict[str, float]]],
|
33
32
|
) -> Dict[str, float] | List[Dict[str, float]] | List[Optional[Dict[str, float]]] | None:
|
34
33
|
"""Rate a given string based on a rating manual and score range.
|
35
34
|
|
@@ -66,13 +65,13 @@ class Rating(Propose):
|
|
66
65
|
res = await self.propose(
|
67
66
|
model,
|
68
67
|
TEMPLATE_MANAGER.render_template(
|
69
|
-
|
68
|
+
CONFIG.templates.rate_fine_grind_template,
|
70
69
|
{"to_rate": to_rate, "min_score": min_score, "max_score": max_score},
|
71
70
|
)
|
72
71
|
if isinstance(to_rate, str)
|
73
72
|
else [
|
74
73
|
TEMPLATE_MANAGER.render_template(
|
75
|
-
|
74
|
+
CONFIG.templates.rate_fine_grind_template,
|
76
75
|
{"to_rate": t, "min_score": min_score, "max_score": max_score},
|
77
76
|
)
|
78
77
|
for t in to_rate
|
@@ -88,34 +87,36 @@ class Rating(Propose):
|
|
88
87
|
|
89
88
|
@overload
|
90
89
|
async def rate(
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
) -> Dict[str, float]:
|
90
|
+
self,
|
91
|
+
to_rate: str,
|
92
|
+
topic: str,
|
93
|
+
criteria: Set[str],
|
94
|
+
manual: Optional[Dict[str, str]] = None,
|
95
|
+
score_range: Tuple[float, float] = (0.0, 1.0),
|
96
|
+
**kwargs: Unpack[ValidateKwargs],
|
97
|
+
) -> Dict[str, float]:
|
98
|
+
...
|
99
99
|
|
100
100
|
@overload
|
101
101
|
async def rate(
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
) -> List[Dict[str, float]]:
|
102
|
+
self,
|
103
|
+
to_rate: List[str],
|
104
|
+
topic: str,
|
105
|
+
criteria: Set[str],
|
106
|
+
manual: Optional[Dict[str, str]] = None,
|
107
|
+
score_range: Tuple[float, float] = (0.0, 1.0),
|
108
|
+
**kwargs: Unpack[ValidateKwargs],
|
109
|
+
) -> List[Dict[str, float]]:
|
110
|
+
...
|
110
111
|
|
111
112
|
async def rate(
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
113
|
+
self,
|
114
|
+
to_rate: Union[str, List[str]],
|
115
|
+
topic: str,
|
116
|
+
criteria: Set[str],
|
117
|
+
manual: Optional[Dict[str, str]] = None,
|
118
|
+
score_range: Tuple[float, float] = (0.0, 1.0),
|
119
|
+
**kwargs: Unpack[ValidateKwargs],
|
119
120
|
) -> Dict[str, float] | List[Dict[str, float]] | List[Optional[Dict[str, float]]] | None:
|
120
121
|
"""Rate a given string or a sequence of strings based on a topic, criteria, and score range.
|
121
122
|
|
@@ -132,15 +133,15 @@ class Rating(Propose):
|
|
132
133
|
or a list of dictionaries with the ratings for each criterion if a sequence of strings is provided.
|
133
134
|
"""
|
134
135
|
manual = (
|
135
|
-
|
136
|
-
|
137
|
-
|
136
|
+
manual
|
137
|
+
or await self.draft_rating_manual(topic, criteria, **override_kwargs(kwargs, default=None))
|
138
|
+
or dict(zip(criteria, criteria, strict=True))
|
138
139
|
)
|
139
140
|
|
140
141
|
return await self.rate_fine_grind(to_rate, manual, score_range, **kwargs)
|
141
142
|
|
142
143
|
async def draft_rating_manual(
|
143
|
-
|
144
|
+
self, topic: str, criteria: Optional[Set[str]] = None, **kwargs: Unpack[ValidateKwargs[Dict[str, str]]]
|
144
145
|
) -> Optional[Dict[str, str]]:
|
145
146
|
"""Drafts a rating manual based on a topic and dimensions.
|
146
147
|
|
@@ -155,9 +156,9 @@ class Rating(Propose):
|
|
155
156
|
|
156
157
|
def _validator(response: str) -> Dict[str, str] | None:
|
157
158
|
if (
|
158
|
-
|
159
|
-
|
160
|
-
|
159
|
+
(json_data := JsonCapture.validate_with(response, target_type=dict, elements_type=str)) is not None
|
160
|
+
and json_data.keys() == criteria
|
161
|
+
and all(isinstance(v, str) for v in json_data.values())
|
161
162
|
):
|
162
163
|
return json_data
|
163
164
|
return None
|
@@ -171,7 +172,7 @@ class Rating(Propose):
|
|
171
172
|
return await self.aask_validate(
|
172
173
|
question=(
|
173
174
|
TEMPLATE_MANAGER.render_template(
|
174
|
-
|
175
|
+
CONFIG.templates.draft_rating_manual_template,
|
175
176
|
{
|
176
177
|
"topic": topic,
|
177
178
|
"criteria": list(criteria),
|
@@ -183,10 +184,10 @@ class Rating(Propose):
|
|
183
184
|
)
|
184
185
|
|
185
186
|
async def draft_rating_criteria(
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
187
|
+
self,
|
188
|
+
topic: str,
|
189
|
+
criteria_count: NonNegativeInt = 0,
|
190
|
+
**kwargs: Unpack[ValidateKwargs[Set[str]]],
|
190
191
|
) -> Optional[Set[str]]:
|
191
192
|
"""Drafts rating dimensions based on a topic.
|
192
193
|
|
@@ -201,7 +202,7 @@ class Rating(Propose):
|
|
201
202
|
return await self.aask_validate(
|
202
203
|
question=(
|
203
204
|
TEMPLATE_MANAGER.render_template(
|
204
|
-
|
205
|
+
CONFIG.templates.draft_rating_criteria_template,
|
205
206
|
{
|
206
207
|
"topic": topic,
|
207
208
|
"criteria_count": criteria_count,
|
@@ -215,13 +216,13 @@ class Rating(Propose):
|
|
215
216
|
)
|
216
217
|
|
217
218
|
async def draft_rating_criteria_from_examples(
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
219
|
+
self,
|
220
|
+
topic: str,
|
221
|
+
examples: List[str],
|
222
|
+
m: NonNegativeInt = 0,
|
223
|
+
reasons_count: PositiveInt = 2,
|
224
|
+
criteria_count: PositiveInt = 5,
|
225
|
+
**kwargs: Unpack[ValidateKwargs],
|
225
226
|
) -> Optional[Set[str]]:
|
226
227
|
"""Asynchronously drafts a set of rating criteria based on provided examples.
|
227
228
|
|
@@ -251,7 +252,7 @@ class Rating(Propose):
|
|
251
252
|
await self.aask_validate( # pyright: ignore [reportArgumentType]
|
252
253
|
question=[
|
253
254
|
TEMPLATE_MANAGER.render_template(
|
254
|
-
|
255
|
+
CONFIG.templates.extract_reasons_from_examples_template,
|
255
256
|
{
|
256
257
|
"topic": topic,
|
257
258
|
"first": pair[0],
|
@@ -271,7 +272,7 @@ class Rating(Propose):
|
|
271
272
|
return await self.aask_validate(
|
272
273
|
question=(
|
273
274
|
TEMPLATE_MANAGER.render_template(
|
274
|
-
|
275
|
+
CONFIG.templates.extract_criteria_from_reasons_template,
|
275
276
|
{
|
276
277
|
"topic": topic,
|
277
278
|
"reasons": list(reasons),
|
@@ -286,10 +287,10 @@ class Rating(Propose):
|
|
286
287
|
)
|
287
288
|
|
288
289
|
async def drafting_rating_weights_klee(
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
290
|
+
self,
|
291
|
+
topic: str,
|
292
|
+
criteria: Set[str],
|
293
|
+
**kwargs: Unpack[ValidateKwargs[float]],
|
293
294
|
) -> Dict[str, float]:
|
294
295
|
"""Drafts rating weights for a given topic and criteria using the Klee method.
|
295
296
|
|
@@ -311,7 +312,7 @@ class Rating(Propose):
|
|
311
312
|
relative_weights = await self.aask_validate(
|
312
313
|
question=[
|
313
314
|
TEMPLATE_MANAGER.render_template(
|
314
|
-
|
315
|
+
CONFIG.templates.draft_rating_weights_klee_template,
|
315
316
|
{
|
316
317
|
"topic": topic,
|
317
318
|
"first": pair[0],
|
@@ -332,14 +333,14 @@ class Rating(Propose):
|
|
332
333
|
return dict(zip(criteria_seq, [w / total for w in weights], strict=True))
|
333
334
|
|
334
335
|
async def composite_score(
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
336
|
+
self,
|
337
|
+
topic: str,
|
338
|
+
to_rate: List[str],
|
339
|
+
criteria: Optional[Set[str]] = None,
|
340
|
+
weights: Optional[Dict[str, float]] = None,
|
341
|
+
manual: Optional[Dict[str, str]] = None,
|
342
|
+
approx: bool = False,
|
343
|
+
**kwargs: Unpack[ValidateKwargs[List[Dict[str, float]]]],
|
343
344
|
) -> List[float]:
|
344
345
|
"""Calculates the composite scores for a list of items based on a given topic and criteria.
|
345
346
|
|
@@ -369,14 +370,17 @@ class Rating(Propose):
|
|
369
370
|
return [sum(ratings[c] * weights[c] for c in criteria) for ratings in ratings_seq]
|
370
371
|
|
371
372
|
@overload
|
372
|
-
async def best(self, candidates: List[str], k: int = 1, **kwargs: Unpack[CompositeScoreKwargs]) -> List[str]:
|
373
|
+
async def best(self, candidates: List[str], k: int = 1, **kwargs: Unpack[CompositeScoreKwargs]) -> List[str]:
|
374
|
+
...
|
375
|
+
|
373
376
|
@overload
|
374
377
|
async def best[T: Display](
|
375
|
-
|
376
|
-
) -> List[T]:
|
378
|
+
self, candidates: List[T], k: int = 1, **kwargs: Unpack[CompositeScoreKwargs]
|
379
|
+
) -> List[T]:
|
380
|
+
...
|
377
381
|
|
378
382
|
async def best[T: Display](
|
379
|
-
|
383
|
+
self, candidates: List[str] | List[T], k: int = 1, **kwargs: Unpack[CompositeScoreKwargs]
|
380
384
|
) -> Optional[List[str] | List[T]]:
|
381
385
|
"""Choose the best candidates from the list of candidates based on the composite score.
|
382
386
|
|
@@ -400,4 +404,5 @@ class Rating(Propose):
|
|
400
404
|
rating_seq = await self.composite_score(
|
401
405
|
to_rate=[c.display() if isinstance(c, Display) else c for c in candidates], **kwargs
|
402
406
|
)
|
403
|
-
return [a[0] for a in sorted(zip(candidates, rating_seq, strict=True), key=lambda x: x[1], reverse=True)[
|
407
|
+
return [a[0] for a in sorted(zip(candidates, rating_seq, strict=True), key=lambda x: x[1], reverse=True)[
|
408
|
+
:k]] # pyright: ignore [reportReturnType]
|