wisent 0.1.1__py3-none-any.whl → 0.5.2__py3-none-any.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.
Potentially problematic release.
This version of wisent might be problematic. Click here for more details.
- wisent/__init__.py +1 -8
- wisent/benchmarks/__init__.py +0 -0
- wisent/benchmarks/coding/__init__.py +0 -0
- wisent/benchmarks/coding/metrics/__init__.py +0 -0
- wisent/benchmarks/coding/metrics/core/__init__.py +0 -0
- wisent/benchmarks/coding/metrics/core/atoms.py +36 -0
- wisent/benchmarks/coding/metrics/evaluator.py +275 -0
- wisent/benchmarks/coding/metrics/passk.py +66 -0
- wisent/benchmarks/coding/output_sanitizer/__init__.py +0 -0
- wisent/benchmarks/coding/output_sanitizer/core/__init__.py +0 -0
- wisent/benchmarks/coding/output_sanitizer/core/atoms.py +27 -0
- wisent/benchmarks/coding/output_sanitizer/cpp_sanitizer.py +62 -0
- wisent/benchmarks/coding/output_sanitizer/java_sanitizer.py +78 -0
- wisent/benchmarks/coding/output_sanitizer/python_sanitizer.py +94 -0
- wisent/benchmarks/coding/output_sanitizer/utils.py +107 -0
- wisent/benchmarks/coding/providers/__init__.py +18 -0
- wisent/benchmarks/coding/providers/core/__init__.py +0 -0
- wisent/benchmarks/coding/providers/core/atoms.py +31 -0
- wisent/benchmarks/coding/providers/livecodebench/__init__.py +0 -0
- wisent/benchmarks/coding/providers/livecodebench/provider.py +53 -0
- wisent/benchmarks/coding/safe_docker/__init__.py +0 -0
- wisent/benchmarks/coding/safe_docker/core/__init__.py +0 -0
- wisent/benchmarks/coding/safe_docker/core/atoms.py +105 -0
- wisent/benchmarks/coding/safe_docker/core/runtime.py +118 -0
- wisent/benchmarks/coding/safe_docker/entrypoint.py +123 -0
- wisent/benchmarks/coding/safe_docker/recipes.py +60 -0
- wisent/classifiers/__init__.py +0 -0
- wisent/classifiers/core/__init__.py +0 -0
- wisent/classifiers/core/atoms.py +747 -0
- wisent/classifiers/models/__init__.py +0 -0
- wisent/classifiers/models/logistic.py +29 -0
- wisent/classifiers/models/mlp.py +47 -0
- wisent/cli/__init__.py +0 -0
- wisent/cli/classifiers/__init__.py +0 -0
- wisent/cli/classifiers/classifier_rotator.py +137 -0
- wisent/cli/cli_logger.py +142 -0
- wisent/cli/data_loaders/__init__.py +0 -0
- wisent/cli/data_loaders/data_loader_rotator.py +96 -0
- wisent/cli/evaluators/__init__.py +0 -0
- wisent/cli/evaluators/evaluator_rotator.py +148 -0
- wisent/cli/steering_methods/__init__.py +0 -0
- wisent/cli/steering_methods/steering_rotator.py +110 -0
- wisent/cli/wisent_cli/__init__.py +0 -0
- wisent/cli/wisent_cli/commands/__init__.py +0 -0
- wisent/cli/wisent_cli/commands/help_cmd.py +52 -0
- wisent/cli/wisent_cli/commands/listing.py +154 -0
- wisent/cli/wisent_cli/commands/train_cmd.py +322 -0
- wisent/cli/wisent_cli/main.py +93 -0
- wisent/cli/wisent_cli/shell.py +80 -0
- wisent/cli/wisent_cli/ui.py +69 -0
- wisent/cli/wisent_cli/util/__init__.py +0 -0
- wisent/cli/wisent_cli/util/aggregations.py +43 -0
- wisent/cli/wisent_cli/util/parsing.py +126 -0
- wisent/cli/wisent_cli/version.py +4 -0
- wisent/core/__init__.py +27 -0
- wisent/core/activations/__init__.py +0 -0
- wisent/core/activations/activations_collector.py +338 -0
- wisent/core/activations/core/__init__.py +0 -0
- wisent/core/activations/core/atoms.py +216 -0
- wisent/core/agent/__init__.py +18 -0
- wisent/core/agent/budget.py +638 -0
- wisent/core/agent/device_benchmarks.py +685 -0
- wisent/core/agent/diagnose/__init__.py +55 -0
- wisent/core/agent/diagnose/agent_classifier_decision.py +641 -0
- wisent/core/agent/diagnose/classifier_marketplace.py +554 -0
- wisent/core/agent/diagnose/create_classifier.py +1154 -0
- wisent/core/agent/diagnose/response_diagnostics.py +268 -0
- wisent/core/agent/diagnose/select_classifiers.py +506 -0
- wisent/core/agent/diagnose/synthetic_classifier_option.py +754 -0
- wisent/core/agent/diagnose/tasks/__init__.py +33 -0
- wisent/core/agent/diagnose/tasks/task_manager.py +1456 -0
- wisent/core/agent/diagnose/tasks/task_relevance.py +94 -0
- wisent/core/agent/diagnose/tasks/task_selector.py +151 -0
- wisent/core/agent/diagnose/test_synthetic_classifier.py +71 -0
- wisent/core/agent/diagnose.py +242 -0
- wisent/core/agent/steer.py +212 -0
- wisent/core/agent/timeout.py +134 -0
- wisent/core/autonomous_agent.py +1234 -0
- wisent/core/bigcode_integration.py +583 -0
- wisent/core/contrastive_pairs/__init__.py +15 -0
- wisent/core/contrastive_pairs/core/__init__.py +0 -0
- wisent/core/contrastive_pairs/core/atoms.py +45 -0
- wisent/core/contrastive_pairs/core/buliders.py +59 -0
- wisent/core/contrastive_pairs/core/pair.py +178 -0
- wisent/core/contrastive_pairs/core/response.py +152 -0
- wisent/core/contrastive_pairs/core/serialization.py +300 -0
- wisent/core/contrastive_pairs/core/set.py +133 -0
- wisent/core/contrastive_pairs/diagnostics/__init__.py +45 -0
- wisent/core/contrastive_pairs/diagnostics/activations.py +53 -0
- wisent/core/contrastive_pairs/diagnostics/base.py +73 -0
- wisent/core/contrastive_pairs/diagnostics/control_vectors.py +169 -0
- wisent/core/contrastive_pairs/diagnostics/coverage.py +79 -0
- wisent/core/contrastive_pairs/diagnostics/divergence.py +98 -0
- wisent/core/contrastive_pairs/diagnostics/duplicates.py +116 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/__init__.py +0 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/atoms.py +238 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/lm_extractor_manifest.py +8 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/lm_extractor_registry.py +132 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/lm_task_extractors/__init__.py +0 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/lm_task_extractors/winogrande.py +115 -0
- wisent/core/contrastive_pairs/lm_eval_pairs/lm_task_pairs_generation.py +50 -0
- wisent/core/data_loaders/__init__.py +0 -0
- wisent/core/data_loaders/core/__init__.py +0 -0
- wisent/core/data_loaders/core/atoms.py +98 -0
- wisent/core/data_loaders/loaders/__init__.py +0 -0
- wisent/core/data_loaders/loaders/custom.py +120 -0
- wisent/core/data_loaders/loaders/lm_loader.py +218 -0
- wisent/core/detection_handling.py +257 -0
- wisent/core/download_full_benchmarks.py +1386 -0
- wisent/core/evaluators/__init__.py +0 -0
- wisent/core/evaluators/oracles/__init__.py +0 -0
- wisent/core/evaluators/oracles/interactive.py +73 -0
- wisent/core/evaluators/oracles/nlp_evaluator.py +440 -0
- wisent/core/evaluators/oracles/user_specified.py +67 -0
- wisent/core/hyperparameter_optimizer.py +429 -0
- wisent/core/lm_eval_harness_ground_truth.py +1396 -0
- wisent/core/log_likelihoods_evaluator.py +321 -0
- wisent/core/managed_cached_benchmarks.py +595 -0
- wisent/core/mixed_benchmark_sampler.py +364 -0
- wisent/core/model_config_manager.py +330 -0
- wisent/core/model_persistence.py +317 -0
- wisent/core/models/__init__.py +0 -0
- wisent/core/models/core/__init__.py +0 -0
- wisent/core/models/core/atoms.py +460 -0
- wisent/core/models/wisent_model.py +727 -0
- wisent/core/multi_steering.py +316 -0
- wisent/core/optuna/__init__.py +57 -0
- wisent/core/optuna/classifier/__init__.py +25 -0
- wisent/core/optuna/classifier/activation_generator.py +349 -0
- wisent/core/optuna/classifier/classifier_cache.py +509 -0
- wisent/core/optuna/classifier/optuna_classifier_optimizer.py +606 -0
- wisent/core/optuna/steering/__init__.py +0 -0
- wisent/core/optuna/steering/bigcode_evaluator_wrapper.py +188 -0
- wisent/core/optuna/steering/data_utils.py +342 -0
- wisent/core/optuna/steering/metrics.py +474 -0
- wisent/core/optuna/steering/optuna_pipeline.py +1738 -0
- wisent/core/optuna/steering/steering_optimization.py +1111 -0
- wisent/core/parser.py +1668 -0
- wisent/core/prompts/__init__.py +0 -0
- wisent/core/prompts/core/__init__.py +0 -0
- wisent/core/prompts/core/atom.py +57 -0
- wisent/core/prompts/core/prompt_formater.py +157 -0
- wisent/core/prompts/prompt_stratiegies/__init__.py +0 -0
- wisent/core/prompts/prompt_stratiegies/direct_completion.py +24 -0
- wisent/core/prompts/prompt_stratiegies/instruction_following.py +24 -0
- wisent/core/prompts/prompt_stratiegies/multiple_choice.py +29 -0
- wisent/core/prompts/prompt_stratiegies/role_playing.py +31 -0
- wisent/core/representation.py +5 -0
- wisent/core/sample_size_optimizer.py +648 -0
- wisent/core/sample_size_optimizer_v2.py +355 -0
- wisent/core/save_results.py +277 -0
- wisent/core/steering.py +652 -0
- wisent/core/steering_method.py +26 -0
- wisent/core/steering_methods/__init__.py +0 -0
- wisent/core/steering_methods/core/__init__.py +0 -0
- wisent/core/steering_methods/core/atoms.py +153 -0
- wisent/core/steering_methods/methods/__init__.py +0 -0
- wisent/core/steering_methods/methods/caa.py +44 -0
- wisent/core/steering_optimizer.py +1297 -0
- wisent/core/task_interface.py +132 -0
- wisent/core/task_selector.py +189 -0
- wisent/core/tasks/__init__.py +175 -0
- wisent/core/tasks/aime_task.py +141 -0
- wisent/core/tasks/file_task.py +211 -0
- wisent/core/tasks/hle_task.py +180 -0
- wisent/core/tasks/hmmt_task.py +119 -0
- wisent/core/tasks/livecodebench_task.py +201 -0
- wisent/core/tasks/livemathbench_task.py +158 -0
- wisent/core/tasks/lm_eval_task.py +455 -0
- wisent/core/tasks/math500_task.py +84 -0
- wisent/core/tasks/polymath_task.py +146 -0
- wisent/core/tasks/supergpqa_task.py +220 -0
- wisent/core/time_estimator.py +149 -0
- wisent/core/timing_calibration.py +174 -0
- wisent/core/tracking/__init__.py +54 -0
- wisent/core/tracking/latency.py +618 -0
- wisent/core/tracking/memory.py +359 -0
- wisent/core/trainers/__init__.py +0 -0
- wisent/core/trainers/core/__init__.py +11 -0
- wisent/core/trainers/core/atoms.py +45 -0
- wisent/core/trainers/steering_trainer.py +271 -0
- wisent/core/user_model_config.py +158 -0
- wisent/opti/__init__.py +0 -0
- wisent/opti/core/__init__.py +0 -0
- wisent/opti/core/atoms.py +175 -0
- wisent/opti/methods/__init__.py +0 -0
- wisent/opti/methods/opti_classificator.py +172 -0
- wisent/opti/methods/opti_steering.py +138 -0
- wisent/synthetic/__init__.py +0 -0
- wisent/synthetic/cleaners/__init__.py +0 -0
- wisent/synthetic/cleaners/core/__init__.py +0 -0
- wisent/synthetic/cleaners/core/atoms.py +58 -0
- wisent/synthetic/cleaners/deduper_cleaner.py +53 -0
- wisent/synthetic/cleaners/methods/__init__.py +0 -0
- wisent/synthetic/cleaners/methods/base_dedupers.py +320 -0
- wisent/synthetic/cleaners/methods/base_refusalers.py +286 -0
- wisent/synthetic/cleaners/methods/core/__init__.py +0 -0
- wisent/synthetic/cleaners/methods/core/atoms.py +47 -0
- wisent/synthetic/cleaners/pairs_cleaner.py +90 -0
- wisent/synthetic/cleaners/refusaler_cleaner.py +133 -0
- wisent/synthetic/db_instructions/__init__.py +0 -0
- wisent/synthetic/db_instructions/core/__init__.py +0 -0
- wisent/synthetic/db_instructions/core/atoms.py +25 -0
- wisent/synthetic/db_instructions/mini_dp.py +37 -0
- wisent/synthetic/generators/__init__.py +0 -0
- wisent/synthetic/generators/core/__init__.py +0 -0
- wisent/synthetic/generators/core/atoms.py +73 -0
- wisent/synthetic/generators/diversities/__init__.py +0 -0
- wisent/synthetic/generators/diversities/core/__init__.py +0 -0
- wisent/synthetic/generators/diversities/core/core.py +68 -0
- wisent/synthetic/generators/diversities/methods/__init__.py +0 -0
- wisent/synthetic/generators/diversities/methods/fast_diversity.py +249 -0
- wisent/synthetic/generators/pairs_generator.py +179 -0
- wisent-0.5.2.dist-info/METADATA +67 -0
- wisent-0.5.2.dist-info/RECORD +218 -0
- {wisent-0.1.1.dist-info → wisent-0.5.2.dist-info}/WHEEL +1 -1
- {wisent-0.1.1.dist-info → wisent-0.5.2.dist-info/licenses}/LICENSE +2 -2
- wisent/activations/__init__.py +0 -9
- wisent/activations/client.py +0 -97
- wisent/activations/extractor.py +0 -251
- wisent/activations/models.py +0 -95
- wisent/client.py +0 -45
- wisent/control_vector/__init__.py +0 -9
- wisent/control_vector/client.py +0 -85
- wisent/control_vector/manager.py +0 -168
- wisent/control_vector/models.py +0 -70
- wisent/inference/__init__.py +0 -9
- wisent/inference/client.py +0 -103
- wisent/inference/inferencer.py +0 -250
- wisent/inference/models.py +0 -66
- wisent/utils/__init__.py +0 -3
- wisent/utils/auth.py +0 -30
- wisent/utils/http.py +0 -228
- wisent/version.py +0 -3
- wisent-0.1.1.dist-info/METADATA +0 -142
- wisent-0.1.1.dist-info/RECORD +0 -23
- {wisent-0.1.1.dist-info → wisent-0.5.2.dist-info}/top_level.txt +0 -0
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TypedDict
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"ChatMessage",
|
|
9
|
+
"PromptPair",
|
|
10
|
+
"PromptStrategy",
|
|
11
|
+
"UnknownStrategyError",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
class ChatMessage(TypedDict):
|
|
15
|
+
"""A single chat message compatible with common chat model APIs."""
|
|
16
|
+
role: str
|
|
17
|
+
content: str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class PromptPair:
|
|
22
|
+
"""A pair of prompts for positive and negative training/eval cases."""
|
|
23
|
+
positive: list[ChatMessage]
|
|
24
|
+
negative: list[ChatMessage]
|
|
25
|
+
|
|
26
|
+
class PromptStrategy(ABC):
|
|
27
|
+
"""Abstract strategy for building a PromptPair from QA text.
|
|
28
|
+
|
|
29
|
+
Subclasses MUST define a unique, non-empty class attribute:
|
|
30
|
+
strategy_key: str
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
strategy_key: str
|
|
34
|
+
|
|
35
|
+
def __init_subclass__(cls, **kwargs): # type: ignore[override]
|
|
36
|
+
super().__init_subclass__(**kwargs)
|
|
37
|
+
key = getattr(cls, "strategy_key", None)
|
|
38
|
+
if not isinstance(key, str) or not key.strip():
|
|
39
|
+
raise TypeError(
|
|
40
|
+
f"{cls.__name__} must define a non-empty class attribute "
|
|
41
|
+
"'strategy_key: str'."
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
@abstractmethod
|
|
45
|
+
def build(
|
|
46
|
+
self,
|
|
47
|
+
question: str,
|
|
48
|
+
correct_answer: str,
|
|
49
|
+
incorrect_answer: str,
|
|
50
|
+
) -> PromptPair:
|
|
51
|
+
"""Construct positive/negative prompts from the given QA trio."""
|
|
52
|
+
raise NotImplementedError
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class UnknownStrategyError(ValueError):
|
|
56
|
+
"""Raised when a requested strategy key does not exist."""
|
|
57
|
+
pass
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import inspect
|
|
5
|
+
import pkgutil
|
|
6
|
+
from typing import Iterable, Type
|
|
7
|
+
|
|
8
|
+
from wisent.core.prompts.core.atom import PromptPair, PromptStrategy, UnknownStrategyError
|
|
9
|
+
|
|
10
|
+
__all__ = ["PromptFormatter", "StrategyKey", "UnknownStrategyError"]
|
|
11
|
+
|
|
12
|
+
StrategyKey = str | type[PromptStrategy]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class PromptFormatter:
|
|
16
|
+
"""Main entry point for building prompt pairs via discovered strategies.
|
|
17
|
+
|
|
18
|
+
atributes:
|
|
19
|
+
_registry: Maps strategy_key to PromptStrategy subclass.
|
|
20
|
+
|
|
21
|
+
methods:
|
|
22
|
+
format:
|
|
23
|
+
Build a PromptPair using a discovered strategy.
|
|
24
|
+
available:
|
|
25
|
+
Return the available strategy keys.
|
|
26
|
+
refresh:
|
|
27
|
+
Rescan the 'strategies' package for newly added strategy files.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self) -> None:
|
|
31
|
+
self._registry: dict[str, Type[PromptStrategy]] = {}
|
|
32
|
+
self._discover_and_load()
|
|
33
|
+
|
|
34
|
+
def format(
|
|
35
|
+
self,
|
|
36
|
+
strategy: StrategyKey,
|
|
37
|
+
question: str,
|
|
38
|
+
correct_answer: str,
|
|
39
|
+
incorrect_answer: str,
|
|
40
|
+
) -> PromptPair:
|
|
41
|
+
"""Build a PromptPair using a discovered strategy.
|
|
42
|
+
|
|
43
|
+
arguments:
|
|
44
|
+
strategy: The strategy key (string) or PromptStrategy subclass.
|
|
45
|
+
question: The question text.
|
|
46
|
+
correct_answer: The correct answer text.
|
|
47
|
+
incorrect_answer: The incorrect answer text.
|
|
48
|
+
|
|
49
|
+
returns:
|
|
50
|
+
A PromptPair constructed by the specified strategy.
|
|
51
|
+
|
|
52
|
+
raises:
|
|
53
|
+
UnknownStrategyError: If the strategy key is not found.
|
|
54
|
+
TypeError: If the strategy argument is not a string or PromptStrategy subclass.
|
|
55
|
+
|
|
56
|
+
example:
|
|
57
|
+
>>> formatter = PromptFormatter()
|
|
58
|
+
>>> pair = formatter.format(
|
|
59
|
+
... strategy="multiple_choice",
|
|
60
|
+
... question="What is 2+2?",
|
|
61
|
+
... correct_answer="4",
|
|
62
|
+
... incorrect_answer="5"
|
|
63
|
+
... )
|
|
64
|
+
>>> print(pair)
|
|
65
|
+
PromptPair(
|
|
66
|
+
positive=[{'role': 'user', 'content': 'Which is better: What is 2+2? A. 5 B. 4'}, {'role': 'assistant', 'content': 'B'}],
|
|
67
|
+
negative=[{'role': 'user', 'content': 'Which is better: What is 2+2? A. 5 B. 4'}, {'role': 'assistant', 'content': 'A'}])
|
|
68
|
+
"""
|
|
69
|
+
key = self._normalize_key(strategy)
|
|
70
|
+
try:
|
|
71
|
+
strategy_cls = self._registry[key]
|
|
72
|
+
except KeyError as exc:
|
|
73
|
+
raise UnknownStrategyError(f"Unknown strategy: {strategy!r}") from exc
|
|
74
|
+
|
|
75
|
+
pair = strategy_cls().build(question, correct_answer, incorrect_answer)
|
|
76
|
+
return pair
|
|
77
|
+
|
|
78
|
+
def available(self) -> Iterable[str]:
|
|
79
|
+
"""Return the available strategy keys.
|
|
80
|
+
|
|
81
|
+
returns:
|
|
82
|
+
An iterable of available strategy keys.
|
|
83
|
+
|
|
84
|
+
example:
|
|
85
|
+
>>> formatter = PromptFormatter()
|
|
86
|
+
>>> print(formatter.available())
|
|
87
|
+
('multiple_choice', 'role_playing', 'instruction_following')
|
|
88
|
+
"""
|
|
89
|
+
return tuple(sorted(self._registry.keys()))
|
|
90
|
+
|
|
91
|
+
def refresh(self) -> None:
|
|
92
|
+
"""Rescan the 'strategies' package for newly added strategy files."""
|
|
93
|
+
self._registry.clear()
|
|
94
|
+
self._discover_and_load()
|
|
95
|
+
|
|
96
|
+
@staticmethod
|
|
97
|
+
def _normalize_key(key: StrategyKey) -> str:
|
|
98
|
+
if isinstance(key, str):
|
|
99
|
+
return key
|
|
100
|
+
if inspect.isclass(key) and issubclass(key, PromptStrategy):
|
|
101
|
+
return key.strategy_key
|
|
102
|
+
raise TypeError(
|
|
103
|
+
"strategy must be a string key or a PromptStrategy subclass."
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def _discover_and_load(self) -> None:
|
|
107
|
+
"""Import all modules in the 'strategies' package and collect strategies.
|
|
108
|
+
|
|
109
|
+
raises:
|
|
110
|
+
RuntimeError: If the 'strategies' package is not found or no strategies are discovered.
|
|
111
|
+
"""
|
|
112
|
+
try:
|
|
113
|
+
import wisent_guard.core.prompts.prompt_stratiegies as strategies_pkg
|
|
114
|
+
except ModuleNotFoundError as exc:
|
|
115
|
+
raise RuntimeError(
|
|
116
|
+
"The 'strategies' package was not found. "
|
|
117
|
+
"Create a 'strategies' directory with an empty __init__.py."
|
|
118
|
+
) from exc
|
|
119
|
+
|
|
120
|
+
import wisent_guard.core.prompts.prompt_stratiegies as strategies_pkg
|
|
121
|
+
|
|
122
|
+
for module_info in pkgutil.iter_modules(strategies_pkg.__path__):
|
|
123
|
+
name = module_info.name
|
|
124
|
+
if name.startswith("_"):
|
|
125
|
+
# Skip private/dunder modules.
|
|
126
|
+
continue
|
|
127
|
+
|
|
128
|
+
module = importlib.import_module(f"strategies.{name}")
|
|
129
|
+
self._register_strategies_from_module(module)
|
|
130
|
+
|
|
131
|
+
if not self._registry:
|
|
132
|
+
raise RuntimeError(
|
|
133
|
+
"No strategies found. Add at least one file in 'strategies/' "
|
|
134
|
+
"defining a PromptStrategy subclass with a unique 'strategy_key'."
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def _register_strategies_from_module(self, module) -> None:
|
|
138
|
+
'''
|
|
139
|
+
Inspect the given module for PromptStrategy subclasses and register them.
|
|
140
|
+
|
|
141
|
+
arguments:
|
|
142
|
+
module: The imported module to inspect.
|
|
143
|
+
|
|
144
|
+
raises:
|
|
145
|
+
RuntimeError: If duplicate strategy_key values are found.
|
|
146
|
+
'''
|
|
147
|
+
for _, obj in inspect.getmembers(module, inspect.isclass):
|
|
148
|
+
if not issubclass(obj, PromptStrategy) or obj is PromptStrategy:
|
|
149
|
+
continue
|
|
150
|
+
key = obj.strategy_key
|
|
151
|
+
if key in self._registry:
|
|
152
|
+
existing = self._registry[key].__module__
|
|
153
|
+
raise RuntimeError(
|
|
154
|
+
f"Duplicate strategy_key '{key}' found in module "
|
|
155
|
+
f"'{module.__name__}' (already defined in '{existing}')."
|
|
156
|
+
)
|
|
157
|
+
self._registry[key] = obj
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from wisent.core.prompts.core.atom import ChatMessage, PromptPair, PromptStrategy
|
|
4
|
+
|
|
5
|
+
__all__ = ["DirectCompletionStrategy"]
|
|
6
|
+
|
|
7
|
+
class DirectCompletionStrategy(PromptStrategy):
|
|
8
|
+
"""Direct completion strategy: question from user, answer from assistant."""
|
|
9
|
+
|
|
10
|
+
def build(
|
|
11
|
+
self,
|
|
12
|
+
question: str,
|
|
13
|
+
correct_answer: str,
|
|
14
|
+
incorrect_answer: str,
|
|
15
|
+
) -> PromptPair:
|
|
16
|
+
pos: list[ChatMessage] = [
|
|
17
|
+
{"role": "user", "content": question},
|
|
18
|
+
{"role": "assistant", "content": correct_answer},
|
|
19
|
+
]
|
|
20
|
+
neg: list[ChatMessage] = [
|
|
21
|
+
{"role": "user", "content": question},
|
|
22
|
+
{"role": "assistant", "content": incorrect_answer},
|
|
23
|
+
]
|
|
24
|
+
return PromptPair(positive=pos, negative=neg)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from wisent.core.prompts.core.atom import ChatMessage, PromptPair, PromptStrategy
|
|
4
|
+
|
|
5
|
+
__all__ = ["InstructionFollowingStrategy"]
|
|
6
|
+
|
|
7
|
+
class InstructionFollowingStrategy(PromptStrategy):
|
|
8
|
+
"""Plain instruction/QA style: question from user, answer from assistant."""
|
|
9
|
+
|
|
10
|
+
def build(
|
|
11
|
+
self,
|
|
12
|
+
question: str,
|
|
13
|
+
correct_answer: str,
|
|
14
|
+
incorrect_answer: str,
|
|
15
|
+
) -> PromptPair:
|
|
16
|
+
pos: list[ChatMessage] = [
|
|
17
|
+
{"role": "user", "content": question},
|
|
18
|
+
{"role": "assistant", "content": correct_answer},
|
|
19
|
+
]
|
|
20
|
+
neg: list[ChatMessage] = [
|
|
21
|
+
{"role": "user", "content": question},
|
|
22
|
+
{"role": "assistant", "content": incorrect_answer},
|
|
23
|
+
]
|
|
24
|
+
return PromptPair(positive=pos, negative=neg)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from wisent.core.prompts.core.atom import ChatMessage, PromptPair, PromptStrategy
|
|
4
|
+
|
|
5
|
+
__all__ = ["MultipleChoiceStrategy"]
|
|
6
|
+
|
|
7
|
+
class MultipleChoiceStrategy(PromptStrategy):
|
|
8
|
+
"""Formats a multiple-choice prompt with options A/B."""
|
|
9
|
+
|
|
10
|
+
def build(
|
|
11
|
+
self,
|
|
12
|
+
question: str,
|
|
13
|
+
correct_answer: str,
|
|
14
|
+
incorrect_answer: str,
|
|
15
|
+
) -> PromptPair:
|
|
16
|
+
mc_q = (
|
|
17
|
+
f"Which is better: {question} "
|
|
18
|
+
f"A. {incorrect_answer} B. {correct_answer}"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
pos: list[ChatMessage] = [
|
|
22
|
+
{"role": "user", "content": mc_q},
|
|
23
|
+
{"role": "assistant", "content": "B"},
|
|
24
|
+
]
|
|
25
|
+
neg: list[ChatMessage] = [
|
|
26
|
+
{"role": "user", "content": mc_q},
|
|
27
|
+
{"role": "assistant", "content": "A"},
|
|
28
|
+
]
|
|
29
|
+
return PromptPair(positive=pos, negative=neg)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from wisent.core.prompts.core.atom import ChatMessage, PromptPair, PromptStrategy
|
|
4
|
+
|
|
5
|
+
__all__ = ["RolePlayingStrategy"]
|
|
6
|
+
|
|
7
|
+
class RolePlayingStrategy(PromptStrategy):
|
|
8
|
+
"""Asks the model to behave like someone who gives a specific answer."""
|
|
9
|
+
|
|
10
|
+
def build(
|
|
11
|
+
self,
|
|
12
|
+
question: str,
|
|
13
|
+
correct_answer: str,
|
|
14
|
+
incorrect_answer: str,
|
|
15
|
+
) -> PromptPair:
|
|
16
|
+
positive_instruction = (
|
|
17
|
+
f"Behave like a person that would answer {question} with {correct_answer}"
|
|
18
|
+
)
|
|
19
|
+
negative_instruction = (
|
|
20
|
+
f"Behave like a person that would answer {question} with {incorrect_answer}"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
pos: list[ChatMessage] = [
|
|
24
|
+
{"role": "user", "content": positive_instruction},
|
|
25
|
+
{"role": "assistant", "content": correct_answer},
|
|
26
|
+
]
|
|
27
|
+
neg: list[ChatMessage] = [
|
|
28
|
+
{"role": "user", "content": negative_instruction},
|
|
29
|
+
{"role": "assistant", "content": incorrect_answer},
|
|
30
|
+
]
|
|
31
|
+
return PromptPair(positive=pos, negative=neg)
|