Skip to content

Commit ef80323

Browse files
committed
further streamlining
1 parent c0b85ad commit ef80323

File tree

2 files changed

+29
-44
lines changed

2 files changed

+29
-44
lines changed

codeflash/api/aiservice.py

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,22 @@
77
from typing import TYPE_CHECKING, Any
88

99
import requests
10-
from pydantic.dataclasses import dataclass
1110
from pydantic.json import pydantic_encoder
1211

1312
from codeflash.cli_cmds.console import console, logger
1413
from codeflash.code_utils.env_utils import get_codeflash_api_key, is_LSP_enabled
1514
from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name
1615
from codeflash.models.ExperimentMetadata import ExperimentMetadata
17-
from codeflash.models.models import OptimizedCandidate
16+
from codeflash.models.models import AIServiceRefinerRequest, OptimizedCandidate
1817
from codeflash.telemetry.posthog_cf import ph
1918
from codeflash.version import __version__ as codeflash_version
2019

2120
if TYPE_CHECKING:
2221
from pathlib import Path
2322

2423
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
25-
26-
27-
@dataclass(frozen=True)
28-
class AIServiceRefinerRequest:
29-
optimization_id: str
30-
original_source_code: str
31-
read_only_dependency_code: str
32-
original_code_runtime: str
33-
optimized_source_code: str
34-
optimized_explanation: str
35-
optimized_code_runtime: str
36-
speedup: str
37-
trace_id: str
38-
fto_name: str
39-
original_line_profiler_results: str
40-
optimized_line_profiler_results: str
41-
experiment_metadata: ExperimentMetadata | None
24+
from codeflash.models.ExperimentMetadata import ExperimentMetadata
25+
from codeflash.models.models import AIServiceRefinerRequest
4226

4327

4428
class AiServiceClient:
@@ -238,7 +222,16 @@ def optimize_python_code_line_profiler( # noqa: D417
238222
return []
239223

240224
def optimize_python_code_refinement(self, request: list[AIServiceRefinerRequest]) -> list[OptimizedCandidate]:
241-
git_repo_owner, git_repo_name = safe_get_repo_owner_and_name()
225+
"""Optimize the given python code for performance by making a request to the Django endpoint.
226+
227+
Args:
228+
request: A list of optimization candidate details for refinement
229+
230+
Returns:
231+
-------
232+
- List[OptimizationCandidate]: A list of Optimization Candidates.
233+
234+
"""
242235
payload = [
243236
{
244237
"optimization_id": opt.optimization_id,
@@ -251,34 +244,10 @@ def optimize_python_code_refinement(self, request: list[AIServiceRefinerRequest]
251244
"optimized_line_profiler_results": opt.optimized_line_profiler_results,
252245
"optimized_code_runtime": opt.optimized_code_runtime,
253246
"speedup": opt.speedup,
254-
"python_version": platform.python_version(),
255-
"experiment_metadata": opt.experiment_metadata,
256-
"codeflash_version": codeflash_version,
257-
"lsp_mode": is_LSP_enabled(),
258-
# needed for tracking the refinement behavior
259247
"trace_id": opt.trace_id,
260-
"function_to_optimize": opt.fto_name,
261-
"repo_owner": git_repo_owner,
262-
"repo_name": git_repo_name,
263248
}
264249
for opt in request
265250
]
266-
"""Optimize the given python code for performance by making a request to the Django endpoint.
267-
268-
Parameters
269-
----------
270-
- source_code (str): The python code to optimize.
271-
- dependency_code (str): The dependency code used as read-only context for the optimization
272-
- trace_id (str): Trace id of optimization run
273-
- num_candidates (int): Number of optimization variants to generate. Default is 10.
274-
- experiment_metadata (Optional[ExperimentalMetadata, None]): Any available experiment metadata for this optimization
275-
276-
Returns
277-
-------
278-
- List[OptimizationCandidate]: A list of Optimization Candidates.
279-
280-
"""
281-
282251
logger.info(f"Refining {len(request)} optimizations…")
283252
console.rule()
284253
try:

codeflash/models/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
from codeflash.code_utils.env_utils import is_end_to_end
2828
from codeflash.verification.comparator import comparator
2929

30+
31+
@dataclass(frozen=True)
32+
class AIServiceRefinerRequest:
33+
optimization_id: str
34+
original_source_code: str
35+
read_only_dependency_code: str
36+
original_code_runtime: str
37+
optimized_source_code: str
38+
optimized_explanation: str
39+
optimized_code_runtime: str
40+
speedup: str
41+
trace_id: str
42+
original_line_profiler_results: str
43+
optimized_line_profiler_results: str
44+
45+
3046
# If the method spam is in the class Ham, which is at the top level of the module eggs in the package foo, the fully
3147
# qualified name of the method is foo.eggs.Ham.spam, its qualified name is Ham.spam, and its name is spam. The full name
3248
# of the module is foo.eggs.

0 commit comments

Comments
 (0)