Skip to content

Commit 3ee6e90

Browse files
committed
divertsity
1 parent 9b2aa3c commit 3ee6e90

3 files changed

Lines changed: 45 additions & 11 deletions

File tree

codeflash/code_utils/config_consts.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@
3232
MAX_N_CANDIDATES = 5
3333
MAX_N_CANDIDATES_LP = 6
3434

35+
# Multi-model diversity configuration
36+
# Each tuple is (model_name, num_calls) where each call returns 1 candidate
37+
# Standard mode: 3 GPT-4.1 + 2 Claude Sonnet = 5 candidates
38+
MODEL_DISTRIBUTION: list[tuple[str, int]] = [
39+
("gpt-4.1", 3),
40+
("claude-sonnet-4-5", 2),
41+
]
42+
43+
# LSP mode: fewer candidates for faster response
44+
MODEL_DISTRIBUTION_LSP: list[tuple[str, int]] = [
45+
("gpt-4.1", 2),
46+
("claude-sonnet-4-5", 1),
47+
]
48+
49+
# Line profiler mode: 6 candidates total
50+
MODEL_DISTRIBUTION_LP: list[tuple[str, int]] = [
51+
("gpt-4.1", 4),
52+
("claude-sonnet-4-5", 2),
53+
]
54+
55+
# Line profiler LSP mode
56+
MODEL_DISTRIBUTION_LP_LSP: list[tuple[str, int]] = [
57+
("gpt-4.1", 2),
58+
("claude-sonnet-4-5", 1),
59+
]
60+
3561
try:
3662
from codeflash.lsp.helpers import is_LSP_enabled
3763

@@ -43,5 +69,7 @@
4369
N_CANDIDATES_LP_EFFECTIVE = min(N_CANDIDATES_LP_LSP if _IS_LSP_ENABLED else N_CANDIDATES_LP, MAX_N_CANDIDATES_LP)
4470
N_TESTS_TO_GENERATE_EFFECTIVE = N_TESTS_TO_GENERATE_LSP if _IS_LSP_ENABLED else N_TESTS_TO_GENERATE
4571
TOTAL_LOOPING_TIME_EFFECTIVE = TOTAL_LOOPING_TIME_LSP if _IS_LSP_ENABLED else TOTAL_LOOPING_TIME
72+
MODEL_DISTRIBUTION_EFFECTIVE = MODEL_DISTRIBUTION_LSP if _IS_LSP_ENABLED else MODEL_DISTRIBUTION
73+
MODEL_DISTRIBUTION_LP_EFFECTIVE = MODEL_DISTRIBUTION_LP_LSP if _IS_LSP_ENABLED else MODEL_DISTRIBUTION_LP
4674

4775
MAX_CONTEXT_LEN_REVIEW = 1000

codeflash/models/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ class OptimizedCandidate:
464464
optimization_id: str
465465
source: OptimizedCandidateSource
466466
parent_id: str | None = None
467+
model: str | None = None # Which LLM model generated this candidate
467468

468469

469470
@dataclass(frozen=True)

codeflash/optimization/function_optimizer.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
COVERAGE_THRESHOLD,
4747
INDIVIDUAL_TESTCASE_TIMEOUT,
4848
MAX_REPAIRS_PER_TRACE,
49+
MODEL_DISTRIBUTION_EFFECTIVE,
50+
MODEL_DISTRIBUTION_LP_EFFECTIVE,
4951
N_CANDIDATES_EFFECTIVE,
5052
N_CANDIDATES_LP_EFFECTIVE,
5153
N_TESTS_TO_GENERATE_EFFECTIVE,
@@ -921,18 +923,20 @@ def determine_best_candidate(
921923
ai_service_client = self.aiservice_client if exp_type == "EXP0" else self.local_aiservice_client
922924
assert ai_service_client is not None, "AI service client must be set for optimization"
923925

926+
# Use multi-model approach for line profiler optimization
924927
future_line_profile_results = self.executor.submit(
925-
ai_service_client.optimize_python_code_line_profiler,
928+
ai_service_client.optimize_python_code_line_profiler_multi_model,
926929
source_code=code_context.read_writable_code.markdown,
927930
dependency_code=code_context.read_only_context_code,
928-
trace_id=self.get_trace_id(exp_type),
931+
base_trace_id=self.get_trace_id(exp_type),
929932
line_profiler_results=original_code_baseline.line_profile_results["str_out"],
930-
num_candidates=N_CANDIDATES_LP_EFFECTIVE,
933+
model_distribution=MODEL_DISTRIBUTION_LP_EFFECTIVE,
931934
experiment_metadata=ExperimentMetadata(
932935
id=self.experiment_id, group="control" if exp_type == "EXP0" else "experiment"
933936
)
934937
if self.experiment_id
935938
else None,
939+
executor=self.executor,
936940
)
937941

938942
processor = CandidateProcessor(
@@ -1353,17 +1357,17 @@ def generate_optimizations(
13531357
read_only_context_code: str,
13541358
run_experiment: bool = False, # noqa: FBT001, FBT002
13551359
) -> Result[tuple[OptimizationSet, str], str]:
1356-
"""Generate optimization candidates for the function."""
1357-
n_candidates = N_CANDIDATES_EFFECTIVE
1358-
1360+
"""Generate optimization candidates for the function using multiple models in parallel."""
1361+
# Use multi-model approach for diversity
13591362
future_optimization_candidates = self.executor.submit(
1360-
self.aiservice_client.optimize_python_code,
1363+
self.aiservice_client.optimize_python_code_multi_model,
13611364
read_writable_code.markdown,
13621365
read_only_context_code,
13631366
self.function_trace_id[:-4] + "EXP0" if run_experiment else self.function_trace_id,
1364-
n_candidates,
1367+
MODEL_DISTRIBUTION_EFFECTIVE,
13651368
ExperimentMetadata(id=self.experiment_id, group="control") if run_experiment else None,
13661369
is_async=self.function_to_optimize.is_async,
1370+
executor=self.executor,
13671371
)
13681372

13691373
future_references = self.executor.submit(
@@ -1380,13 +1384,14 @@ def generate_optimizations(
13801384

13811385
if run_experiment:
13821386
future_candidates_exp = self.executor.submit(
1383-
self.local_aiservice_client.optimize_python_code,
1387+
self.local_aiservice_client.optimize_python_code_multi_model,
13841388
read_writable_code.markdown,
13851389
read_only_context_code,
13861390
self.function_trace_id[:-4] + "EXP1",
1387-
n_candidates,
1391+
MODEL_DISTRIBUTION_EFFECTIVE,
13881392
ExperimentMetadata(id=self.experiment_id, group="experiment"),
13891393
is_async=self.function_to_optimize.is_async,
1394+
executor=self.executor,
13901395
)
13911396
futures.append(future_candidates_exp)
13921397

@@ -1395,7 +1400,7 @@ def generate_optimizations(
13951400

13961401
# Retrieve results
13971402
candidates: list[OptimizedCandidate] = future_optimization_candidates.result()
1398-
logger.info(f"!lsp|Generated '{len(candidates)}' candidate optimizations.")
1403+
logger.info(f"!lsp|Generated '{len(candidates)}' candidate optimizations from multiple models.")
13991404

14001405
if not candidates:
14011406
return Failure(f"/!\\ NO OPTIMIZATIONS GENERATED for {self.function_to_optimize.function_name}")

0 commit comments

Comments
 (0)