Skip to content

Commit df0b176

Browse files
committed
wip
1 parent d3e6427 commit df0b176

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

codeflash/api/aiservice.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,112 @@ def generate_regression_tests( # noqa: D417
513513
ph("cli-testgen-error-response", {"response_status_code": response.status_code, "error": response.text})
514514
return None
515515

516+
def get_optimization_impact(self, original_code, new_code, explanation, existing_tests_source, generated_original_test_source, function_trace_id, coverage_message, replay_tests, concolic_tests) -> list[OptimizedCandidate]:
517+
"""Optimize the given python code for performance by making a request to the Django endpoint.
518+
519+
Args:
520+
request: A list of optimization candidate details for refinement
521+
522+
Returns:
523+
-------
524+
- List[OptimizationCandidate]: A list of Optimization Candidates.
525+
526+
"""
527+
# """{
528+
# "original_code": original_code_combined,
529+
# "new_code": new_code_combined,
530+
# "explanation": new_explanation,
531+
# "existing_tests_source": existing_tests,
532+
# "generated_original_test_source": generated_tests_str,
533+
# "function_trace_id": self.function_trace_id[:-4] + exp_type
534+
# if self.experiment_id
535+
# else self.function_trace_id,
536+
# "coverage_message": coverage_message,
537+
# "replay_tests": replay_tests,
538+
# "concolic_tests": concolic_tests,
539+
# }"""
540+
# logger.info("Creating a new PR with the optimized code...")
541+
# console.rule()
542+
# owner, repo = get_repo_owner_and_name(git_repo, git_remote)
543+
# logger.info(f"Pushing to {git_remote} - Owner: {owner}, Repo: {repo}")
544+
# console.rule()
545+
# if not check_and_push_branch(git_repo, git_remote, wait_for_push=True):
546+
# logger.warning("⏭️ Branch is not pushed, skipping PR creation...")
547+
# return
548+
# relative_path = explanation.file_path.relative_to(root_dir).as_posix()
549+
# base_branch = get_current_branch()
550+
# build_file_changes = {
551+
# Path(p).relative_to(root_dir).as_posix(): FileDiffContent(
552+
# oldContent=original_code[p], newContent=new_code[p]
553+
# )
554+
# for p in original_code
555+
# }
556+
#
557+
# response = cfapi.create_pr(
558+
# owner=owner,
559+
# repo=repo,
560+
# base_branch=base_branch,
561+
# file_changes=build_file_changes,
562+
# pr_comment=PrComment(
563+
# optimization_explanation=explanation.explanation_message(),
564+
# best_runtime=explanation.best_runtime_ns,
565+
# original_runtime=explanation.original_runtime_ns,
566+
# function_name=explanation.function_name,
567+
# relative_file_path=relative_path,
568+
# speedup_x=explanation.speedup_x,
569+
# speedup_pct=explanation.speedup_pct,
570+
# winning_behavior_test_results=explanation.winning_behavior_test_results,
571+
# winning_benchmarking_test_results=explanation.winning_benchmarking_test_results,
572+
# benchmark_details=explanation.benchmark_details,
573+
# ),
574+
# existing_tests=existing_tests_source,
575+
# generated_tests=generated_original_test_source,
576+
# trace_id=function_trace_id,
577+
# coverage_message=coverage_message,
578+
# replay_tests=replay_tests,
579+
# concolic_tests=concolic_tests,
580+
# )
581+
# if response.ok:
582+
# pr_id = response.text
583+
# pr_url = github_pr_url(owner, repo, pr_id)
584+
# logger.info(f"Successfully created a new PR #{pr_id} with the optimized code: {pr_url}")
585+
# else:
586+
# logger.error(
587+
# f"Optimization was successful, but I failed to create a PR with the optimized code."
588+
# f" Response from server was: {response.text}"
589+
# )
590+
# console.rule()
591+
logger.info("!lsp|Computing Optimization Impact…")
592+
payload = {
593+
"original_code": original_code,
594+
"new_code": new_code,
595+
"existing_tests_source": existing_tests_source,
596+
"generated_original_test_source": generated_original_test_source,
597+
"function_trace_id": function_trace_id,
598+
"coverage_message": coverage_message,
599+
"replay_tests": replay_tests,
600+
"concolic_tests": concolic_tests,
601+
}
602+
console.rule()
603+
try:
604+
response = self.make_ai_service_request("/optimization_impact", payload=payload, timeout=600)
605+
except requests.exceptions.RequestException as e:
606+
logger.exception(f"Error generating optimization refinements: {e}")
607+
ph("cli-optimize-error-caught", {"error": str(e)})
608+
return ''
609+
610+
if response.status_code == 200:
611+
impact = response.json()["impact"]
612+
return impact
613+
try:
614+
error = response.json()["error"]
615+
except Exception:
616+
error = response.text
617+
logger.error(f"Error generating impact candidates: {response.status_code} - {error}")
618+
ph("cli-optimize-error-response", {"response_status_code": response.status_code, "error": error})
619+
console.rule()
620+
return []
621+
516622

517623
class LocalAiServiceClient(AiServiceClient):
518624
"""Client for interacting with the local AI service."""

codeflash/optimization/function_optimizer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,11 @@ def process_review(
14011401
}
14021402

14031403
raise_pr = not self.args.no_pr
1404+
# modify argument of staging vs pr based on the impact
1405+
opt_impact_response = self.aiservice_client.get_optimization_impact(**data)
1406+
if opt_impact_response in ['medium', 'low']:
1407+
raise_pr = False
1408+
self.args.staging_review = True
14041409

14051410
if raise_pr or self.args.staging_review:
14061411
data["root_dir"] = git_root_dir()

0 commit comments

Comments
 (0)