Skip to content

Commit

Permalink
web: fix report prompt logging for OpenAI models (#315)
Browse files Browse the repository at this point in the history
Signed-off-by: David Korczynski <[email protected]>
  • Loading branch information
DavidKorczynski authored Jun 13, 2024
1 parent 0bed2b5 commit 0c006de
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion report/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,35 @@ def get_results(benchmark) -> tuple[list[evaluator.Result], list[str]]:
return results, targets


def prepare_prompt_for_html_text(raw_prompt_content: str) -> str:
"""Converts a raw prompt file into presentable HTML text."""
try:
structured_prompt = json.loads(raw_prompt_content)
if isinstance(structured_prompt, list) and structured_prompt:
html_presentable_content = ''
for elem in structured_prompt:
if isinstance(elem, dict) and 'content' in elem:
html_presentable_content += f'\n{elem["content"]}'
logging.debug('Converted structured prompt to raw text.')
return html_presentable_content
except json.decoder.JSONDecodeError:
logging.debug('Using raw prompt text.')
pass

# If execution goes here it the input was not a structured prompt but just
# raw text, which is then returned.
return raw_prompt_content


def get_prompt(benchmark) -> Optional[str]:
root_dir = os.path.join(RESULTS_DIR, benchmark)
for name in os.listdir(root_dir):
if re.match(r'^prompt.*txt$', name):
with open(os.path.join(root_dir, name)) as f:
return f.read()
content = f.read()

# Prepare prompt text for HTML.
return prepare_prompt_for_html_text(content)

return None

Expand Down Expand Up @@ -328,6 +351,9 @@ def get_fixed_target(path):
with open(os.path.join(path, name)) as f:
fixer_prompt = f.read()

# Prepare prompt for being used in HTML.
fixer_prompt = prepare_prompt_for_html_text(fixer_prompt)

if name.endswith('.rawoutput'):
with open(os.path.join(path, name)) as f:
code = f.read()
Expand Down

0 comments on commit 0c006de

Please sign in to comment.