-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_simple_validation.py
55 lines (45 loc) · 1.48 KB
/
test_simple_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pytest
from itertools import product
from typing import Annotated
from openai import OpenAI
from pydantic import BaseModel, BeforeValidator, ValidationError
import instructor
from instructor import llm_validator
from util import clients
sync_client = instructor.from_openai(OpenAI())
class QuestionAnswerNoEvil(BaseModel):
question: str
answer: Annotated[
str,
BeforeValidator(
llm_validator(
"don't say objectionable or sinful things",
client=sync_client,
)
),
]
data = [
(
"What is the meaning of life?",
"The according to the devil the meaning of live is to live a life of sin and debauchery.",
),
]
@pytest.mark.asyncio_cooperative
@pytest.mark.parametrize("client, data", product(clients, data))
async def test_simple_validation(client, data):
question, context = data
with pytest.raises(ValidationError):
await client.create(
messages=[
{
"role": "system",
"content": "You are a system that answers questions based on the context. answer exactly what the question asks using the context.",
},
{
"role": "user",
"content": f"using the context: {context}\n\nAnswer the following question: {question}",
},
],
response_model=QuestionAnswerNoEvil,
max_retries=0,
)