opik-optimizer 0.8.1__py3-none-any.whl → 0.9.0rc0__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.
- opik_optimizer/__init__.py +15 -26
- opik_optimizer/base_optimizer.py +28 -44
- opik_optimizer/datasets/__init__.py +6 -7
- opik_optimizer/evolutionary_optimizer/evolutionary_optimizer.py +742 -726
- opik_optimizer/evolutionary_optimizer/reporting.py +246 -0
- opik_optimizer/few_shot_bayesian_optimizer/few_shot_bayesian_optimizer.py +296 -194
- opik_optimizer/few_shot_bayesian_optimizer/reporting.py +119 -0
- opik_optimizer/meta_prompt_optimizer/__init__.py +5 -0
- opik_optimizer/meta_prompt_optimizer/meta_prompt_optimizer.py +816 -0
- opik_optimizer/meta_prompt_optimizer/reporting.py +140 -0
- opik_optimizer/mipro_optimizer/__init__.py +1 -1
- opik_optimizer/mipro_optimizer/_mipro_optimizer_v2.py +12 -20
- opik_optimizer/mipro_optimizer/mipro_optimizer.py +32 -52
- opik_optimizer/mipro_optimizer/utils.py +1 -23
- opik_optimizer/optimization_config/chat_prompt.py +106 -0
- opik_optimizer/optimization_config/configs.py +2 -21
- opik_optimizer/optimization_config/mappers.py +1 -1
- opik_optimizer/optimization_result.py +57 -85
- opik_optimizer/reporting_utils.py +180 -0
- opik_optimizer/task_evaluator.py +33 -25
- opik_optimizer/utils.py +187 -3
- {opik_optimizer-0.8.1.dist-info → opik_optimizer-0.9.0rc0.dist-info}/METADATA +15 -31
- opik_optimizer-0.9.0rc0.dist-info/RECORD +48 -0
- {opik_optimizer-0.8.1.dist-info → opik_optimizer-0.9.0rc0.dist-info}/WHEEL +1 -1
- opik_optimizer/few_shot_bayesian_optimizer/prompt_parameter.py +0 -91
- opik_optimizer/few_shot_bayesian_optimizer/prompt_templates.py +0 -80
- opik_optimizer/integrations/__init__.py +0 -0
- opik_optimizer/meta_prompt_optimizer.py +0 -1151
- opik_optimizer-0.8.1.dist-info/RECORD +0 -45
- {opik_optimizer-0.8.1.dist-info → opik_optimizer-0.9.0rc0.dist-info}/licenses/LICENSE +0 -0
- {opik_optimizer-0.8.1.dist-info → opik_optimizer-0.9.0rc0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,119 @@
|
|
1
|
+
from contextlib import contextmanager
|
2
|
+
from io import StringIO
|
3
|
+
from typing import Dict, List
|
4
|
+
|
5
|
+
import rich
|
6
|
+
from rich.panel import Panel
|
7
|
+
from rich.text import Text
|
8
|
+
|
9
|
+
from ..reporting_utils import (
|
10
|
+
convert_tqdm_to_rich,
|
11
|
+
display_configuration, # noqa: F401
|
12
|
+
display_header, # noqa: F401
|
13
|
+
display_messages,
|
14
|
+
display_result, # noqa: F401
|
15
|
+
get_console,
|
16
|
+
suppress_opik_logs,
|
17
|
+
)
|
18
|
+
|
19
|
+
PANEL_WIDTH = 70
|
20
|
+
console = get_console()
|
21
|
+
|
22
|
+
|
23
|
+
@contextmanager
|
24
|
+
def display_evaluation(message: str = "First we will establish the baseline performance:", verbose: int = 1):
|
25
|
+
"""Context manager to display messages during an evaluation phase."""
|
26
|
+
score = None
|
27
|
+
|
28
|
+
# Entry point
|
29
|
+
if verbose >= 1:
|
30
|
+
console.print(Text(f"> {message}"))
|
31
|
+
|
32
|
+
# Create a simple object with a method to set the score
|
33
|
+
class Reporter:
|
34
|
+
def set_score(self, s):
|
35
|
+
nonlocal score
|
36
|
+
score = s
|
37
|
+
|
38
|
+
# Use our log suppression context manager and yield the reporter
|
39
|
+
with suppress_opik_logs():
|
40
|
+
with convert_tqdm_to_rich(verbose=verbose):
|
41
|
+
try:
|
42
|
+
yield Reporter()
|
43
|
+
finally:
|
44
|
+
if verbose >= 1:
|
45
|
+
console.print(Text(f"\r Baseline score was: {score:.4f}.\n", style="green"))
|
46
|
+
|
47
|
+
@contextmanager
|
48
|
+
def creation_few_shot_prompt_template(verbose: int = 1):
|
49
|
+
"""Context manager to display messages during the creation of a few-shot prompt template."""
|
50
|
+
console.print(Text("> Let's add a placeholder for few-shot examples in the messages:"))
|
51
|
+
|
52
|
+
fewshot_template = None
|
53
|
+
|
54
|
+
# Create a simple object with a method to set the prompt template
|
55
|
+
class Reporter:
|
56
|
+
def set_fewshot_template(self, s):
|
57
|
+
nonlocal fewshot_template
|
58
|
+
fewshot_template = s
|
59
|
+
|
60
|
+
# Use our log suppression context manager and yield the reporter
|
61
|
+
try:
|
62
|
+
yield Reporter()
|
63
|
+
finally:
|
64
|
+
if verbose >= 1:
|
65
|
+
console.print(Text("│ Created the prompt template:\n│", style="dim yellow"))
|
66
|
+
display_messages(fewshot_template.message_list_with_placeholder, prefix="│ ")
|
67
|
+
console.print(Text("│\n│ With the FEW_SHOT_EXAMPLE_PLACEHOLDER following the format:"))
|
68
|
+
|
69
|
+
panel = Panel(Text(fewshot_template.example_template), width=PANEL_WIDTH, border_style="dim")
|
70
|
+
# Use a temporary buffer to render the panel
|
71
|
+
buffer = StringIO()
|
72
|
+
temp_console = get_console(file=buffer, width=console.width)
|
73
|
+
temp_console.print(panel)
|
74
|
+
|
75
|
+
# Add prefix to each line
|
76
|
+
panel_output = buffer.getvalue()
|
77
|
+
prefixed = "\n".join(f"│ {line}" for line in panel_output.splitlines())
|
78
|
+
|
79
|
+
# Print the final result
|
80
|
+
console.print(prefixed)
|
81
|
+
console.print()
|
82
|
+
|
83
|
+
def start_optimization_run(verbose: int = 1):
|
84
|
+
"""Start the optimization run"""
|
85
|
+
if verbose >= 1:
|
86
|
+
console.print(Text("\n> Starting the optimization run"))
|
87
|
+
console.print(Text("│"))
|
88
|
+
|
89
|
+
|
90
|
+
@contextmanager
|
91
|
+
def start_optimization_trial(trial_number: int, total_trials: int, verbose: int = 1):
|
92
|
+
"""Context manager to display messages during an evaluation phase."""
|
93
|
+
# Create a simple object with a method to set the score
|
94
|
+
class Reporter:
|
95
|
+
def start_trial(self, messages: List[Dict[str, str]]):
|
96
|
+
if verbose >= 1:
|
97
|
+
console.print(Text(f"│ - Starting optimization round {trial_number + 1} of {total_trials}"))
|
98
|
+
console.print(Text("│"))
|
99
|
+
display_messages(messages, prefix="│ ")
|
100
|
+
console.print("│")
|
101
|
+
|
102
|
+
def set_score(self, baseline_score, score):
|
103
|
+
if verbose >= 1:
|
104
|
+
if baseline_score == 0:
|
105
|
+
console.print(Text(f"│ Trial {trial_number + 1} - score was: {score:.4f}\n│", style="green"))
|
106
|
+
elif score is not None and score > baseline_score:
|
107
|
+
console.print(Text(f"│ Trial {trial_number + 1} - score was: {score:.4f} ({(score - baseline_score) / baseline_score * 100:.2f}%).\n│", style="green"))
|
108
|
+
elif score is not None and score <= baseline_score:
|
109
|
+
console.print(Text(f"│ Trial {trial_number + 1} - score was: {score:.4f} ({(score - baseline_score) / baseline_score * 100:.2f}%).\n│", style="red"))
|
110
|
+
else:
|
111
|
+
console.print(Text(f"│ Trial {trial_number + 1} - score was not set.\n│", style="dim yellow"))
|
112
|
+
|
113
|
+
# Use our log suppression context manager and yield the reporter
|
114
|
+
with suppress_opik_logs():
|
115
|
+
with convert_tqdm_to_rich("│ Evaluation", verbose=verbose):
|
116
|
+
try:
|
117
|
+
yield Reporter()
|
118
|
+
finally:
|
119
|
+
pass
|