Skip to content

Commit

Permalink
Merge pull request #14 from alainzhiyanov/new_branch
Browse files Browse the repository at this point in the history
Fix ARGFILE bug
  • Loading branch information
carolemieux authored Aug 23, 2023
2 parents 360c228 + b92dda8 commit e6425e6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ isort:

.PHONY: black
black:
poetry run black .
poetry run black --exclude '/replication/' .

.PHONY: darglint
darglint:
Expand Down
10 changes: 10 additions & 0 deletions pynguin/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ class CodaMosaConfiguration:
model_name: str = ""
"""The OpenAI Model to use for completions"""

model_base_url: str = ""
"""The base url used to interact with the model.
Put together, model_base_url and model_relative_url describe
the url for the model"""

model_relative_url: str = ""
"""The relative url used to interact with the model.
Put together, model_base_url and model_relative_url describe
the url for the model"""

max_plateau_len: int = 25
"""The number of iterations to let go on before trying to do LLM Seeding"""

Expand Down
7 changes: 7 additions & 0 deletions pynguin/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def _setup_language_model_seeding(
model.languagemodel.complete_model = (
config.configuration.codamosa.model_name
)
model.languagemodel.model_base_url = (
config.configuration.codamosa.model_base_url
)
model.languagemodel.model_relative_url = (
config.configuration.codamosa.model_relative_url
)

else:
return True

Expand Down
81 changes: 70 additions & 11 deletions pynguin/languagemodels/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ def char_type(c):
return len(toks)


def _openai_api_legacy_request(self, function_header, context):
# TODO: remove this function as part of Issue #19
url = f"{self._model_base_url}/v1/engines/{self._complete_model}/completions"
payload = {
"prompt": context + "\n" + function_header,
"max_tokens": 200,
"temperature": self._temperature,
"stop": ["\n# Unit test for", "\ndef ", "\nclass "],
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self._authorization_key}",
}
return url, payload, headers


def _openai_api_request(self, function_header, context):
url = f"{self._model_base_url}{self._model_relative_url}"
payload = {
"model": self._complete_model,
"prompt": context + "\n" + function_header,
"max_tokens": 200,
"temperature": self._temperature,
"stop": ["\n# Unit test for", "\ndef ", "\nclass "],
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self._authorization_key}",
}
return url, payload, headers


class _OpenAILanguageModel:
"""
An interface for an OpenAI language model to generate/mutate tests as natural language.
Expand All @@ -87,6 +119,8 @@ def __init__(self):
self._test_src: str
self._authorization_key: str
self._complete_model: str
self._model_base_url: str
self._model_relative_url: str
self._edit_model: str
self._log_path: str = ""
# TODO(ANON): make configurable; adding a fudge factor
Expand Down Expand Up @@ -162,6 +196,34 @@ def edit_model(self) -> str:
def edit_model(self, edit_model: str):
self._edit_model = edit_model

@property
def model_base_url(self) -> str:
"""The base url used to interact with the model. Put together, model_base_url and model_relative_url describe
the url for the model
Returns:
The base url used to interact with the model
"""
return self._model_base_url

@model_base_url.setter
def model_base_url(self, model_base_url: str):
self._model_base_url = model_base_url

@property
def model_relative_url(self) -> str:
"""The relative url used to interact with the model. Put together, model_base_url and model_relative_url
describe the url for the model
Returns:
The relative url used to interact with the model
"""
return self._model_relative_url

@model_relative_url.setter
def model_relative_url(self, model_relative_url: str):
self._model_relative_url = model_relative_url

def _get_maximal_source_context(
self, start_line: int = -1, end_line: int = -1, used_tokens: int = 0
):
Expand Down Expand Up @@ -266,19 +328,16 @@ def _call_completion(
"""
context = self._get_maximal_source_context(context_start, context_end)

url = f"https://api.openai.com/v1/engines/{self.complete_model}/completions"
if self.model_base_url == "https://api.openai.com":
url, payload, headers = _openai_api_legacy_request(
self, function_header, context
)
else:
url, payload, headers = _openai_api_request(self, function_header, context)

# We want to stop the generation before it spits out a bunch of other tests,
# because that slows things down
payload = {
"prompt": context + "\n" + function_header,
"max_tokens": 200,
"temperature": self._temperature,
"stop": ["\n# Unit test for", "\ndef ", "\nclass "],
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self._authorization_key}",
}

time_start = time.time()
res = requests.post(url, data=json.dumps(payload), headers=headers)
self.time_calling_codex += time.time() - time_start
Expand Down
7 changes: 4 additions & 3 deletions replication/scripts/run_one.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# This file lets you run one instance of codamosa, etc.
# This file lets you run one instance of codamosa, etc.

SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null && pwd 2> /dev/null; )";

Expand All @@ -10,6 +10,7 @@ fi
MOD=$1
OUT_DIR=$2
ARGFILE=$3
ARGS=`cat $ARGFILE`
SEARCH_TIME=$4
PLAY_OPTION=$5 #"--auth" for querying codex with auth key or "--replay" with existing codex generations
PLAY_KEY=$6 # auth key file or codex generations file
Expand All @@ -19,10 +20,10 @@ if [[ ! -d $SCRIPT_DIR/../test-apps ]]; then
exit 1
fi

grep $1 $SCRIPT_DIR/../test-apps/good_modules.csv |
grep $1 $SCRIPT_DIR/../test-apps/good_modules.csv |
while IFS=, read -r TEST_DIR TEST_MOD
do
TEST_DIR=$(dirname $SCRIPT_DIR)/$TEST_DIR
TEST_DIR=$(dirname $SCRIPT_DIR)/$TEST_DIR
mkdir -p $OUT_DIR
if [ $PLAY_OPTION = "--auth" ]; then
PLAY_CONFIG="--authorization-key $(cat $PLAY_KEY)"
Expand Down

0 comments on commit e6425e6

Please sign in to comment.