From 9754499e814b88b70b309a46f7dfe4aa328bf8a3 Mon Sep 17 00:00:00 2001 From: Lu Yao Chen Date: Sun, 19 Mar 2023 17:17:18 -0700 Subject: [PATCH 1/6] simple send req non chatgpt test --- .gitignore | 3 ++- models/openai_model.py | 16 +++++++++------- tests/__init__.py | 0 tests/test_requests.py | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_requests.py diff --git a/.gitignore b/.gitignore index 5d320ce..6f7812f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ usage.txt /dalleimages /indexes /audiotemp -/pickles \ No newline at end of file +/pickles +.idea \ No newline at end of file diff --git a/models/openai_model.py b/models/openai_model.py index f31a11b..b8cfced 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -633,10 +633,14 @@ class Model: else: await self.usage_service.update_usage(tokens_used) except Exception as e: - raise ValueError( - "The API returned an invalid response: " - + str(response["error"]["message"]) - ) from e + traceback.print_exc() + if 'error' in response: + raise ValueError( + "The API returned an invalid response: " + + str(response["error"]["message"]) + ) from e + else: + raise ValueError("The API returned an invalid response") from e @backoff.on_exception( backoff.expo, @@ -1010,8 +1014,6 @@ class Model: stop=None, custom_api_key=None, is_chatgpt_request=False, - ) -> ( - Tuple[dict, bool] ): # The response, and a boolean indicating whether or not the context limit was reached. # Validate that all the parameters are in a good state before we send the request @@ -1057,7 +1059,7 @@ class Model: headers=headers, ) as resp: response = await resp.json() - # print(f"Payload -> {payload}") + print(f"Payload -> {payload}") # Parse the total tokens used for this request and response pair from the response await self.valid_text_request( response, model=self.model if model is None else model diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_requests.py b/tests/test_requests.py new file mode 100644 index 0000000..6226bba --- /dev/null +++ b/tests/test_requests.py @@ -0,0 +1,21 @@ +from pathlib import Path + +import pytest +from models.openai_model import Models, Model +from transformers import GPT2TokenizerFast +import asyncio + +from services.usage_service import UsageService +import os + +# Non-ChatGPT +@pytest.mark.asyncio +async def test_send_req(): + + usage_service = UsageService(Path("../tests")) + model = Model(usage_service) + prompt = 'how many hours are in a day?' + tokens = len(GPT2TokenizerFast.from_pretrained("gpt2")(prompt)["input_ids"]) + # tokens = 60 + res = await model.send_request(prompt, tokens) + assert '24' in res['choices'][0]['text'] From ab5ac691eb94501eec692538f6cae2af44f61a42 Mon Sep 17 00:00:00 2001 From: Lu Yao Chen Date: Sun, 19 Mar 2023 17:21:56 -0700 Subject: [PATCH 2/6] tomol --- pyproject.toml | 3 ++- requirements.txt | 1 + requirements_base.txt | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d9ddcdc..2d2ce0c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,8 @@ dependencies = [ "langchain==0.0.115", "unidecode==1.3.6", "tqdm==4.64.1", -"docx2txt==0.8" +"docx2txt==0.8", +"pytest-asyncio==0.21.0" ] dynamic = ["version"] diff --git a/requirements.txt b/requirements.txt index 05da893..754cf34 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,3 +24,4 @@ openai-whisper unidecode==1.3.6 tqdm==4.64.1 docx2txt==0.8 +pytest-asyncio==0.21.0 \ No newline at end of file diff --git a/requirements_base.txt b/requirements_base.txt index fcac1c7..d2992dd 100644 --- a/requirements_base.txt +++ b/requirements_base.txt @@ -21,4 +21,5 @@ python-pptx==0.6.21 langchain==0.0.115 unidecode==1.3.6 tqdm==4.64.1 -docx2txt==0.8 \ No newline at end of file +docx2txt==0.8 +pytest-asyncio==0.21.0 \ No newline at end of file From d78a7d2fad7eecde26c022206daef8b3548d2699 Mon Sep 17 00:00:00 2001 From: Lu Yao Chen Date: Thu, 23 Mar 2023 14:34:25 -0700 Subject: [PATCH 3/6] chatgpt send request and fix logic for max tokens --- models/openai_model.py | 2 +- tests/test_requests.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/models/openai_model.py b/models/openai_model.py index b8cfced..659e4fc 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -1018,7 +1018,7 @@ class Model: # Validate that all the parameters are in a good state before we send the request if not max_tokens_override: - if model: + if model and model not in Models.GPT4_MODELS and model not in Models.CHATGPT_MODELS: max_tokens_override = Models.get_max_tokens(model) - tokens print(f"The prompt about to be sent is {prompt}") diff --git a/tests/test_requests.py b/tests/test_requests.py index 6226bba..14ef128 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -16,6 +16,16 @@ async def test_send_req(): model = Model(usage_service) prompt = 'how many hours are in a day?' tokens = len(GPT2TokenizerFast.from_pretrained("gpt2")(prompt)["input_ids"]) - # tokens = 60 res = await model.send_request(prompt, tokens) assert '24' in res['choices'][0]['text'] + + +# ChatGPT version +@pytest.mark.asyncio +async def test_send_req_gpt(): + + usage_service = UsageService(Path("../tests")) + model = Model(usage_service) + prompt = 'how many hours are in a day?' + res = await model.send_request(prompt, None, is_chatgpt_request=True, model="gpt-3.5-turbo") + assert '24' in res['choices'][0]['message']['content'] \ No newline at end of file From d85cf50c113e69c1a54eeb0735e5746a1d6ea909 Mon Sep 17 00:00:00 2001 From: Lu Yao Chen Date: Thu, 23 Mar 2023 15:08:52 -0700 Subject: [PATCH 4/6] fixes --- gpt3discord.py | 2 +- requirements.txt | 4 +++- tests/test_requests.py | 30 +++++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/gpt3discord.py b/gpt3discord.py index cd8b45b..fc4fcc0 100644 --- a/gpt3discord.py +++ b/gpt3discord.py @@ -33,7 +33,7 @@ from services.environment_service import EnvService from models.openai_model import Model -__version__ = "11.1.1" +__version__ = "11.1.5" PID_FILE = Path("bot.pid") diff --git a/requirements.txt b/requirements.txt index 754cf34..9980544 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,4 +24,6 @@ openai-whisper unidecode==1.3.6 tqdm==4.64.1 docx2txt==0.8 -pytest-asyncio==0.21.0 \ No newline at end of file +pytest-asyncio==0.21.0 +aiohttp~=3.8.4 +pytest~=7.2.2 \ No newline at end of file diff --git a/tests/test_requests.py b/tests/test_requests.py index 14ef128..aec41fd 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1,14 +1,13 @@ from pathlib import Path import pytest -from models.openai_model import Models, Model +from models.openai_model import Model from transformers import GPT2TokenizerFast -import asyncio from services.usage_service import UsageService -import os -# Non-ChatGPT + +# Non-ChatGPT -> TODO: make generic test and loop through text models @pytest.mark.asyncio async def test_send_req(): @@ -28,4 +27,25 @@ async def test_send_req_gpt(): model = Model(usage_service) prompt = 'how many hours are in a day?' res = await model.send_request(prompt, None, is_chatgpt_request=True, model="gpt-3.5-turbo") - assert '24' in res['choices'][0]['message']['content'] \ No newline at end of file + assert '24' in res['choices'][0]['message']['content'] + + +# GPT4 version +@pytest.mark.asyncio +async def test_send_req_gpt4(): + usage_service = UsageService(Path("../tests")) + model = Model(usage_service) + prompt = 'how many hours are in a day?' + res = await model.send_request(prompt, None, is_chatgpt_request=True, model="gpt-4") + assert '24' in res['choices'][0]['message']['content'] + + +# Edit request -> currently broken due to endpoint +# @pytest.mark.asyncio +# async def test_send_edit_req(): +# usage_service = UsageService(Path("../tests")) +# model = Model(usage_service) +# text = 'how many hours are in a day?' +# res = await model.send_edit_request(text, codex=True) +# assert '24' in res['choices'][0]['text'] + From 6bc39b13b4560a3bb615f714b78a6a92cf110663 Mon Sep 17 00:00:00 2001 From: Lu Yao Chen Date: Thu, 23 Mar 2023 15:12:49 -0700 Subject: [PATCH 5/6] req --- pyproject.toml | 3 ++- requirements_base.txt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2d2ce0c..ad2da4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,8 @@ dependencies = [ "unidecode==1.3.6", "tqdm==4.64.1", "docx2txt==0.8", -"pytest-asyncio==0.21.0" +"pytest-asyncio==0.21.0", +"pytest~=7.2.2" ] dynamic = ["version"] diff --git a/requirements_base.txt b/requirements_base.txt index d2992dd..a5c2173 100644 --- a/requirements_base.txt +++ b/requirements_base.txt @@ -22,4 +22,5 @@ langchain==0.0.115 unidecode==1.3.6 tqdm==4.64.1 docx2txt==0.8 -pytest-asyncio==0.21.0 \ No newline at end of file +pytest-asyncio==0.21.0 +pytest~=7.2.2 \ No newline at end of file From 509291e7c509bd9760723d34996df2055e5fa9bf Mon Sep 17 00:00:00 2001 From: Lu Yao Chen Date: Thu, 23 Mar 2023 15:15:35 -0700 Subject: [PATCH 6/6] rm aio --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9980544..b424c55 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,5 +25,4 @@ unidecode==1.3.6 tqdm==4.64.1 docx2txt==0.8 pytest-asyncio==0.21.0 -aiohttp~=3.8.4 pytest~=7.2.2 \ No newline at end of file