Skip to content

Commit 33c8258

Browse files
committed
encoding as a separate function, can be replaced later for more accurate calculation
1 parent 14de7b4 commit 33c8258

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
from codeflash.cli_cmds.console import logger
1212

13+
def encode_str(s: str) -> str:
14+
return s[:int(0.75 * len(s))]
1315

1416
def get_qualified_name(module_name: str, full_qualified_name: str) -> str:
1517
if not full_qualified_name:

codeflash/context/code_context_extractor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from codeflash.cli_cmds.console import logger
1414
from codeflash.code_utils.code_extractor import add_needed_imports_from_module, find_preexisting_objects
15-
from codeflash.code_utils.code_utils import get_qualified_name, path_belongs_to_site_packages
15+
from codeflash.code_utils.code_utils import get_qualified_name, path_belongs_to_site_packages, encode_str
1616
from codeflash.context.unused_definition_remover import remove_unused_definitions_by_function_names
1717
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
1818
from codeflash.models.models import (
@@ -72,7 +72,7 @@ def get_code_optimization_context(
7272
)
7373

7474
# Handle token limits
75-
final_read_writable_tokens = len(final_read_writable_code)*0.75
75+
final_read_writable_tokens = len(encode_str(final_read_writable_code))
7676
if final_read_writable_tokens > optim_token_limit:
7777
raise ValueError("Read-writable code has exceeded token limit, cannot proceed")
7878

@@ -85,7 +85,7 @@ def get_code_optimization_context(
8585
)
8686
read_only_context_code = read_only_code_markdown.markdown
8787

88-
read_only_code_markdown_tokens = len(read_only_context_code)*0.75
88+
read_only_code_markdown_tokens = len(encode_str(read_only_context_code))
8989
total_tokens = final_read_writable_tokens + read_only_code_markdown_tokens
9090
if total_tokens > optim_token_limit:
9191
logger.debug("Code context has exceeded token limit, removing docstrings from read-only code")
@@ -94,7 +94,7 @@ def get_code_optimization_context(
9494
helpers_of_fto_dict, helpers_of_helpers_dict, project_root_path, remove_docstrings=True
9595
)
9696
read_only_context_code = read_only_code_no_docstring_markdown.markdown
97-
read_only_code_no_docstring_markdown_tokens = len(read_only_context_code)*0.75
97+
read_only_code_no_docstring_markdown_tokens = len(encode_str(read_only_context_code))
9898
total_tokens = final_read_writable_tokens + read_only_code_no_docstring_markdown_tokens
9999
if total_tokens > optim_token_limit:
100100
logger.debug("Code context has exceeded token limit, removing read-only code")
@@ -109,7 +109,7 @@ def get_code_optimization_context(
109109
code_context_type=CodeContextType.TESTGEN,
110110
)
111111
testgen_context_code = testgen_code_markdown.code
112-
testgen_context_code_tokens = len(testgen_context_code)*0.75
112+
testgen_context_code_tokens = len(encode_str(testgen_context_code))
113113
if testgen_context_code_tokens > testgen_token_limit:
114114
testgen_code_markdown = extract_code_string_context_from_files(
115115
helpers_of_fto_dict,
@@ -119,7 +119,7 @@ def get_code_optimization_context(
119119
code_context_type=CodeContextType.TESTGEN,
120120
)
121121
testgen_context_code = testgen_code_markdown.code
122-
testgen_context_code_tokens = len(testgen_context_code)*0.75
122+
testgen_context_code_tokens = len(encode_str(testgen_context_code))
123123
if testgen_context_code_tokens > testgen_token_limit:
124124
raise ValueError("Testgen code context has exceeded token limit, cannot proceed")
125125

0 commit comments

Comments
 (0)