Fixes for Pydantic 1, including matrix test - refs #169

Also refs #147
This commit is contained in:
Simon Willison 2023-08-19 20:59:30 -07:00
parent 740a8e8344
commit 14a91efbad
4 changed files with 8 additions and 13 deletions

View file

@ -11,6 +11,7 @@ jobs:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
pydantic: ["1.10.2", ">=2.0.0"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
@ -22,6 +23,7 @@ jobs:
- name: Install dependencies
run: |
pip install -e '.[test]'
pip install pydantic==${{ matrix.pydantic }}
- name: Run tests
run: |
pytest

View file

@ -868,7 +868,7 @@ def load_template(name):
return Template(name=name, prompt=loaded)
loaded["name"] = name
try:
return Template.model_validate(loaded)
return Template(**loaded)
except pydantic.ValidationError as ex:
msg = "A validation error occurred:\n"
msg += render_errors(ex.errors())

View file

@ -143,7 +143,7 @@ class Response(ABC):
"prompt_json": self._prompt_json,
"options_json": {
key: value
for key, value in self.prompt.options.model_dump().items()
for key, value in dict(self.prompt.options).items()
if value is not None
},
"response": self.text(),

View file

@ -51,14 +51,7 @@ def test_openai_options_min_max(mocked_models):
for option, [min_val, max_val] in options.items():
result = runner.invoke(cli, ["-m", "chatgpt", "-o", option, "-10"])
assert result.exit_code == 1
assert (
result.output
== f"Error: {option}\n Input should be greater than or equal to {min_val}\n"
)
result = runner.invoke(cli, ["-m", "chatgpt", "-o", option, "10"])
assert result.exit_code == 1
assert (
result.output
== f"Error: {option}\n Input should be less than or equal to {max_val}\n"
)
assert f"greater than or equal to {min_val}" in result.output
result2 = runner.invoke(cli, ["-m", "chatgpt", "-o", option, "10"])
assert result2.exit_code == 1
assert f"less than or equal to {max_val}" in result2.output