Skip to content

Commit 491a0d1

Browse files
committed
Revert "Revert "/unit_tests switch to devchat env""
1 parent 847576f commit 491a0d1

6 files changed

Lines changed: 30 additions & 12 deletions

File tree

unit_tests/assistants/directory_structure/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def __init__(
1414
) -> None:
1515
self._root_path = root_path
1616

17-
self._client = OpenAI()
1817
self._chat_language = chat_language
1918

2019
@property

unit_tests/assistants/directory_structure/relevant_file_finder.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
from pathlib import Path
33
from typing import Callable, List
44

5-
import tiktoken
65
from assistants.directory_structure.base import DirectoryStructureBase
76
from assistants.rerank_files import rerank_files
87
from openai_util import create_chat_completion_content
9-
from tools.directory_viewer import (
10-
ListViewer,
11-
)
8+
from tools.directory_viewer import ListViewer
9+
from tools.tiktoken_util import get_encoding
1210

1311

1412
class RelevantFileFinder(DirectoryStructureBase):
1513
model_name = "gpt-3.5-turbo-1106"
1614
dir_token_budget = 16000 * 0.95
17-
encoding: tiktoken.Encoding = tiktoken.encoding_for_model("gpt-3.5-turbo-1106")
15+
encoding = get_encoding("cl100k_base")
1816

1917
def _paginate_dir_structure(
2018
self, criteria: Callable[[Path], bool], style: str = "list"
@@ -85,7 +83,6 @@ def _find_relevant_files(self, objective: str, dir_structure_pages: List[str]) -
8583
user_msg = self._mk_message(objective, dir_structure)
8684

8785
response = create_chat_completion_content(
88-
client=self._client,
8986
model=self.model_name,
9087
messages=[
9188
{"role": "user", "content": user_msg},

unit_tests/command.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
description: Generate unit tests.
22
steps:
3-
- run: $command_python $command_path/main.py "$input"
3+
- run: $devchat_python $command_path/main.py "$input"

unit_tests/propose_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
from functools import partial
33
from typing import List
44

5-
import tiktoken
65
from model import FuncToTest, TokenBudgetExceededException
76
from openai_util import create_chat_completion_content
87
from prompts import PROPOSE_TEST_PROMPT
8+
from tools.tiktoken_util import get_encoding
99

1010
MODEL = "gpt-3.5-turbo-1106"
1111
# MODEL = "gpt-4-1106-preview"
12+
ENCODING = "cl100k_base"
1213
TOKEN_BUDGET = int(16000 * 0.9)
1314

1415

@@ -20,7 +21,7 @@ def _mk_user_msg(
2021
"""
2122
Create a user message to be sent to the model within the token budget.
2223
"""
23-
encoding: tiktoken.Encoding = tiktoken.encoding_for_model(MODEL)
24+
encoding = get_encoding(ENCODING)
2425

2526
func_content = f"function code\n```\n{func_to_test.func_content}\n```\n"
2627
class_content = ""

unit_tests/tools/tiktoken_util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import tiktoken
2+
3+
4+
def get_encoding(encoding_name: str):
5+
"""
6+
Get a tiktoken encoding by name.
7+
"""
8+
try:
9+
return tiktoken.get_encoding(encoding_name)
10+
except Exception:
11+
from tiktoken import registry
12+
from tiktoken.core import Encoding
13+
from tiktoken.registry import _find_constructors
14+
15+
def _get_encoding(name: str):
16+
_find_constructors()
17+
constructor = registry.ENCODING_CONSTRUCTORS[name]
18+
return Encoding(**constructor(), use_pure_python=True)
19+
20+
return _get_encoding(encoding_name)

unit_tests/write_tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from functools import partial
22
from typing import List, Optional
33

4-
import tiktoken
54
from model import FuncToTest, TokenBudgetExceededException
65
from openai_util import create_chat_completion_chunks
76
from prompts import WRITE_TESTS_PROMPT
87
from tools.file_util import retrieve_file_content
8+
from tools.tiktoken_util import get_encoding
99

1010
MODEL = "gpt-4-1106-preview"
11+
ENCODING = "cl100k_base"
1112
TOKEN_BUDGET = int(128000 * 0.9)
1213

1314

@@ -18,7 +19,7 @@ def _mk_write_tests_msg(
1819
chat_language: str,
1920
reference_files: Optional[List[str]] = None,
2021
) -> Optional[str]:
21-
encoding: tiktoken.Encoding = tiktoken.encoding_for_model(MODEL)
22+
encoding = get_encoding(ENCODING)
2223

2324
test_cases_str = ""
2425
for i, test_case in enumerate(test_cases, 1):

0 commit comments

Comments
 (0)