From 7500e2ef37596ebc1d2afdb467106833919d3fad Mon Sep 17 00:00:00 2001 From: Uche Ogbuji Date: Mon, 24 Jun 2024 11:16:06 -0600 Subject: [PATCH] Fix tests --- pylib/llm_wrapper.py | 6 ++---- test/test_ogbujipt.py | 6 +++--- test/test_word_loom.py | 24 +++++++++++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pylib/llm_wrapper.py b/pylib/llm_wrapper.py index c449e1b..9490ef7 100644 --- a/pylib/llm_wrapper.py +++ b/pylib/llm_wrapper.py @@ -17,7 +17,6 @@ import concurrent.futures from functools import partial from typing import List -import warnings from amara3 import iri @@ -72,13 +71,12 @@ def from_openai_chat(response): c['message'] = llm_response(c['message']) rc1 = resp['choices'][0] # No response message content if a tool call is invoked - if 'tool_calls' in rc1['message']: - # Why the hell does OpenAI have these arguments properties as plain text? Seems like a massive layering violation + if rc1.get('message', {}).get('tool_calls'): + # WTH does OpenAI have these arguments properties as plain text? Seems a massive layering violation for tc in rc1['message']['tool_calls']: tc['function']['arguments_obj'] = json.loads(tc['function']['arguments']) else: resp['first_choice_text'] = rc1['text'] if 'text' in rc1 else rc1['message']['content'] - print('GRIPPO', f'from_openai_chat: {rc1 =}') else: resp['first_choice_text'] = resp['content'] return resp diff --git a/test/test_ogbujipt.py b/test/test_ogbujipt.py index 2cc1ede..f8b460a 100644 --- a/test/test_ogbujipt.py +++ b/test/test_ogbujipt.py @@ -12,14 +12,14 @@ # import pytest -from ogbujipt.llm_wrapper import openai_chat_api +from ogbujipt.llm_wrapper import llm_response #, openai_chat_api def test_oapi_first_choice_text(OPENAI_TEXT_RESPONSE_OBJECT): - text1 = openai_chat_api.first_choice_text(OPENAI_TEXT_RESPONSE_OBJECT) + text1 = llm_response.from_openai_chat(OPENAI_TEXT_RESPONSE_OBJECT).first_choice_text assert text1 == '…is an exceptional employee who has made significant contributions to our company.' def test_oapi_first_choice_message(OPENAI_MSG_RESPONSE_OBJECT): - msg1 = openai_chat_api.first_choice_message(OPENAI_MSG_RESPONSE_OBJECT) + msg1 = llm_response.from_openai_chat(OPENAI_MSG_RESPONSE_OBJECT).first_choice_text assert msg1 == '…is an exceptional employee who has made significant contributions to our company.' diff --git a/test/test_word_loom.py b/test/test_word_loom.py index b22dcfe..5034a87 100644 --- a/test/test_word_loom.py +++ b/test/test_word_loom.py @@ -12,7 +12,7 @@ import os import sys -# import pkgutil +from itertools import chain import pytest @@ -49,9 +49,19 @@ def test_load_fp_vs_str(SAMPLE_TOML_STR, SAMPLE_TOML_FP): def test_sample_texts_check(SAMPLE_TOML_STR): # print(SAMPLE_TOML) loom = word_loom.load(SAMPLE_TOML_STR) - assert list(sorted(loom.keys())) == ['davinci3_instruct_system', 'hello_translated', 'i18n_context', 'write_i18n_advocacy'] - assert list(sorted([v[:20] for v in loom.values()])) == ['Hello', 'Internationalization', 'Obey the instruction', '{davinci3_instruct_s'] - assert [v.markers or [] for v in loom.values()] == [[], [], ['davinci3_instruct_system', 'i18n_context'], []] + # default language text is also a key + assert len(loom.keys()) == 8 + for k in ['davinci3_instruct_system', 'hello_translated', 'i18n_context', 'write_i18n_advocacy']: + assert k in loom.keys() + assert 'Hello' in loom.keys() + + # loom_dlt = set([v[:20] for v in loom.values()]) + + assert len(set(loom.values())) == 4 + for k in ['Hello', 'Internationalization', 'Obey the instruction', '{davinci3_instruct_s']: + assert k in [v[:20] for v in loom.values()] + + assert [v.markers or [] for v in loom.values()] == [[], [], [], [], ['davinci3_instruct_system', 'i18n_context'], ['davinci3_instruct_system', 'i18n_context'], [], []] assert loom['davinci3_instruct_system'].lang == 'en' # Default language is English @@ -59,9 +69,9 @@ def test_sample_texts_check(SAMPLE_TOML_STR): assert loom1 == loom loom = word_loom.load(SAMPLE_TOML_STR, lang='fr') - assert list(sorted(loom.keys())) == ['goodbye_translated', 'hardcoded_food', 'translate_request'] - assert list(sorted([v[:20] for v in loom.values()])) == ['Adieu', 'Comment dit-on en an', 'pomme de terre'] - assert [v.markers or [] for v in loom.values()] == [['hardcoded_food'], [], []] + assert list(sorted(loom.keys())) == ['Adieu', 'Comment dit-on en anglais: {hardcoded_food}?', 'goodbye_translated', 'hardcoded_food', 'pomme de terre', 'translate_request'] + assert list(sorted(set([v[:20] for v in loom.values()]))) == ['Adieu', 'Comment dit-on en an', 'pomme de terre'] + assert [v.markers or [] for v in loom.values()] == [['hardcoded_food'], ['hardcoded_food'], [], [], [], []] assert loom['hardcoded_food'].lang == 'fr'