Rename API key to AI_KEY because it's the OpenAI key. And the API_KEY is now the CF API key.

This commit is contained in:
afik.cohen 2023-12-04 16:51:20 -08:00
parent 369175295c
commit 63b6efaa87
14 changed files with 105 additions and 51 deletions

View file

@ -8,6 +8,7 @@ jobs:
bubble-sort-optimization:
runs-on: ubuntu-latest
env:
CODEFLASH_AI_KEY: ${{ secrets.CODEFLASH_AI_KEY }}
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
steps:
- uses: actions/checkout@v3

View file

@ -10,6 +10,7 @@ jobs:
runs-on: ubuntu-latest
env:
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
CODEFLASH_AI_KEY: ${{ secrets.CODEFLASH_AI_KEY }}
PR_NUMBER: ${{ github.event.number }}
steps:
- uses: actions/checkout@v3

View file

@ -5,15 +5,25 @@
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
<env name="CODEFLASH_API_KEY" value="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" />
<env name="PR_NUMBER" value="30" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="Poetry (CodeFlash)" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH=".env" />
</ENTRIES>
</EXTENSION>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/codeflash/main.py" />
<option name="PARAMETERS" value="--file code_to_optimize/bubble_sort.py --function sorter --test-framework unittest --test-root code_to_optimize/tests/unittest" />
<option name="SHOW_COMMAND_LINE" value="false" />

View file

@ -12,6 +12,17 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH=".env" />
</ENTRIES>
</EXTENSION>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/codeflash/main.py" />
<option name="PARAMETERS" value="--file code_to_optimize/bubble_sort.py --function sorter --test-framework unittest --test-root code_to_optimize/tests/unittest --use-cached-tests" />
<option name="SHOW_COMMAND_LINE" value="false" />

View file

@ -12,6 +12,17 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH=".env" />
</ENTRIES>
</EXTENSION>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/codeflash/main.py" />
<option name="PARAMETERS" value="--file code_to_optimize/bubble_sort_deps.py --function sorter_deps --test-framework unittest --test-root code_to_optimize/tests/unittest --use-cached-tests" />
<option name="SHOW_COMMAND_LINE" value="false" />

View file

@ -12,6 +12,17 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH=".env" />
</ENTRIES>
</EXTENSION>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/codeflash/main.py" />
<option name="PARAMETERS" value="--file code_to_optimize/bubble_sort_in_class.py --function BubbleSortClass.sorter --test-framework unittest --test-root code_to_optimize/tests/unittest" />
<option name="SHOW_COMMAND_LINE" value="false" />

View file

@ -12,6 +12,17 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH=".env" />
</ENTRIES>
</EXTENSION>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/codeflash/main.py" />
<option name="PARAMETERS" value="--file code_to_optimize/use_cosine_similarity_from_other_file.py --function use_cosine_similarity --test-framework unittest --test-root code_to_optimize/tests/unittest --use-cached-tests" />
<option name="SHOW_COMMAND_LINE" value="false" />

View file

@ -2,15 +2,15 @@ import os
from typing import Optional
def get_codeflash_api_key() -> str:
api_key = os.environ.get("CODEFLASH_API_KEY")
if not api_key:
raise EnvironmentError(f"CODEFLASH_API_KEY not found in environment variables")
return api_key
def get_codeflash_ai_key() -> str:
ai_key = os.environ.get("CODEFLASH_AI_KEY")
if not ai_key:
raise EnvironmentError(f"CODEFLASH_AI_KEY not found in environment variables")
return ai_key
def ensure_codeflash_api_key() -> bool:
get_codeflash_api_key()
def ensure_codeflash_ai_key() -> bool:
get_codeflash_ai_key()
return True
@ -35,6 +35,6 @@ def ensure_pr_number() -> bool:
return True
def get_codeflash_token() -> Optional[str]:
token = os.environ.get("CODEFLASH_TOKEN_KEY")
return token
def get_codeflash_api_key() -> Optional[str]:
api_key = os.environ.get("CODEFLASH_API_KEY")
return api_key

View file

@ -1,6 +1,6 @@
from pydantic.dataclasses import dataclass
from codeflash.code_utils.env_utils import get_codeflash_token
from codeflash.code_utils.env_utils import get_codeflash_api_key
from codeflash.github.api import make_cfapi_request
@ -11,7 +11,7 @@ class FileDiffContent:
def suggest_changes(
owner: str,
owner: str,
repo: str,
pr_number: int,
file_changes: dict[str, FileDiffContent],
@ -32,6 +32,6 @@ def suggest_changes(
},
"prComment": pr_comment,
},
auth_header={"Authorization": f"Bearer {get_codeflash_token()}"},
auth_header={"Authorization": f"Bearer {get_codeflash_api_key()}"},
)
return response

View file

@ -104,8 +104,8 @@ def parse_args() -> Namespace:
assert os.path.isdir(args.test_root), f"--test_root {args.test_root} must be a valid directory"
if env_utils.get_pr_number() is not None:
assert (
env_utils.get_codeflash_token()
), "When running in Github Actions Context, please also provide 'CODEFLASH_TOKEN_KEY'"
env_utils.get_codeflash_api_key()
), "When running in a Github Actions Context, please provide the 'CODEFLASH_API_KEY'."
if hasattr(args, "ignore_paths") and args.ignore_paths is not None:
for path in args.ignore_paths:
assert os.path.exists(
@ -134,7 +134,7 @@ class Optimizer:
def run(self):
logging.info("RUNNING THE OPTIMIZER")
env_utils.ensure_codeflash_api_key()
env_utils.ensure_codeflash_ai_key()
file_to_funcs_to_optimize, num_modified_functions = get_functions_to_optimize_by_file(
optimize_all=self.args.all,

View file

@ -4,7 +4,7 @@ import ast # used for detecting whether generated Python code is valid
import sys
from typing import List, Tuple
from codeflash.code_utils.env_utils import get_codeflash_api_key, get_codeflash_org_key
from codeflash.code_utils.env_utils import get_codeflash_ai_key, get_codeflash_org_key
from codeflash.verification import EXPLAIN_MODEL, PLAN_MODEL, EXECUTE_MODEL, LLM
if sys.version_info < (3, 9, 0):
@ -21,25 +21,24 @@ from codeflash.verification.gen_regression_tests import (
print_message_delta,
)
openai.api_key = get_codeflash_api_key()
openai.api_key = get_codeflash_ai_key()
openai.organization = get_codeflash_org_key()
def regression_tests_from_function_with_inspiration(
function_to_test: str, # Python function to test, as a string
function_name: str, # name of the function to test
existing_unit_test_path_and_function: List[
Tuple[str, str]
], # path to existing unit test file and function name that is testing the function
unit_test_package: str = "pytest", # unit testing package; use the name as it appears in the import statement
approx_min_cases_to_cover: int = 7, # minimum number of test case categories to cover (approximate)
print_text: bool = False, # optionally prints text; helpful for understanding the function & debugging
explain_model: LLM = EXPLAIN_MODEL, # model used to generate text plans in step 1
plan_model: LLM = PLAN_MODEL, # model used to generate text plans in steps 2 and 2b
execute_model: LLM = EXECUTE_MODEL, # model used to generate code in step 3
temperature: float = 0.4, # temperature = 0 can sometimes get stuck in repetitive loops, so we use 0.4
reruns_if_fail: int = 1, # if the output code cannot be parsed, this will re-run the function up to N times
function_to_test: str, # Python function to test, as a string
function_name: str, # name of the function to test
existing_unit_test_path_and_function: List[
Tuple[str, str]
], # path to existing unit test file and function name that is testing the function
unit_test_package: str = "pytest", # unit testing package; use the name as it appears in the import statement
approx_min_cases_to_cover: int = 7, # minimum number of test case categories to cover (approximate)
print_text: bool = False, # optionally prints text; helpful for understanding the function & debugging
explain_model: LLM = EXPLAIN_MODEL, # model used to generate text plans in step 1
plan_model: LLM = PLAN_MODEL, # model used to generate text plans in steps 2 and 2b
execute_model: LLM = EXECUTE_MODEL, # model used to generate code in step 3
temperature: float = 0.4, # temperature = 0 can sometimes get stuck in repetitive loops, so we use 0.4
reruns_if_fail: int = 1, # if the output code cannot be parsed, this will re-run the function up to N times
) -> str:
"""Returns a unit test for a given Python function, using a 3-step GPT prompt."""
# TODO: This step is exactly the same as the non-inspired test generator. Merge them into one to save on API calls
@ -127,7 +126,7 @@ def regression_tests_from_function_with_inspiration(
- Be easy to read and understand, with clean code and descriptive names
- Be deterministic, so that the tests always pass or fail in the same way
To test the function above, here {"are a few unit tests" if len(existing_unit_tests)>1 else "is a unit test"} that already exist:
To test the function above, here {"are a few unit tests" if len(existing_unit_tests) > 1 else "is a unit test"} that already exist:
{f'{chr(10)}'.join(existing_unit_tests)}

View file

@ -2,12 +2,11 @@
import ast # used for detecting whether generated Python code is valid
import logging
import os
import openai
from codeflash.code_utils.code_utils import ellipsis_in_ast
from codeflash.code_utils.env_utils import get_codeflash_api_key, get_codeflash_org_key
from codeflash.code_utils.env_utils import get_codeflash_ai_key, get_codeflash_org_key
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
from codeflash.verification import EXPLAIN_MODEL, PLAN_MODEL, EXECUTE_MODEL, LLM
@ -16,7 +15,7 @@ class TestGenerationFailedException(Exception):
pass
openai.api_key = get_codeflash_api_key()
openai.api_key = get_codeflash_ai_key()
openai.organization = get_codeflash_org_key()
color_prefix_by_role = {
"system": "\033[0m", # gray
@ -48,16 +47,16 @@ def print_message_delta(delta, color_prefix_by_role=color_prefix_by_role) -> Non
def regression_tests_from_function(
function_code: str, # Python function to test, as a string
function: FunctionToOptimize, # the function to test
unit_test_package: str = "pytest", # unit testing package; use the name as it appears in the import statement
approx_min_cases_to_cover: int = 7, # minimum number of test case categories to cover (approximate)
print_text: bool = False, # optionally prints text; helpful for understanding the function & debugging
explain_model: LLM = EXPLAIN_MODEL, # model used to generate text plans in step 1
plan_model: LLM = PLAN_MODEL, # model used to generate text plans in steps 2 and 2b
execute_model: LLM = EXECUTE_MODEL, # model used to generate code in step 3
temperature: float = 0.4, # temperature = 0 can sometimes get stuck in repetitive loops, so we use 0.4
reruns_if_fail: int = 1, # if the output code cannot be parsed, this will re-run the function up to N times
function_code: str, # Python function to test, as a string
function: FunctionToOptimize, # the function to test
unit_test_package: str = "pytest", # unit testing package; use the name as it appears in the import statement
approx_min_cases_to_cover: int = 7, # minimum number of test case categories to cover (approximate)
print_text: bool = False, # optionally prints text; helpful for understanding the function & debugging
explain_model: LLM = EXPLAIN_MODEL, # model used to generate text plans in step 1
plan_model: LLM = PLAN_MODEL, # model used to generate text plans in steps 2 and 2b
execute_model: LLM = EXECUTE_MODEL, # model used to generate code in step 3
temperature: float = 0.4, # temperature = 0 can sometimes get stuck in repetitive loops, so we use 0.4
reruns_if_fail: int = 1, # if the output code cannot be parsed, this will re-run the function up to N times
) -> str:
"""Returns a unit test for a given Python function, using a 3-step GPT prompt."""

View file

@ -1,7 +1,7 @@
import os
from tempfile import NamedTemporaryFile
os.environ["CODEFLASH_API_KEY"] = "test-key"
os.environ["CODEFLASH_AI_KEY"] = "test-key"
from codeflash.code_utils.code_replacer import replace_function_in_file

View file

@ -1,7 +1,7 @@
import os
import sys
os.environ["CODEFLASH_API_KEY"] = "test-key"
os.environ["CODEFLASH_AI_KEY"] = "test-key"
from codeflash.verification.verifier import merge_unit_tests