mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
add asyncio test
This commit is contained in:
@@ -75,3 +75,33 @@ def test_runmodel_validator():
|
||||
assert hasattr(
|
||||
model, "_raw_response"
|
||||
), "The raw response should be available from OpenAI"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_runmodel_validator():
|
||||
aclient = instructor.apatch(AsyncOpenAI())
|
||||
from pydantic import field_validator
|
||||
|
||||
class UserExtract(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
@field_validator("name")
|
||||
@classmethod
|
||||
def validate_name(cls, v):
|
||||
if v.upper() != v:
|
||||
raise ValueError("Name should be uppercase")
|
||||
return v
|
||||
|
||||
model = await aclient.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserExtract,
|
||||
max_retries=2,
|
||||
messages=[
|
||||
{"role": "user", "content": "Extract jason is 25 years old"},
|
||||
],
|
||||
)
|
||||
assert isinstance(model, UserExtract), "Should be instance of UserExtract"
|
||||
assert model.name == "JASON"
|
||||
assert hasattr(
|
||||
model, "_raw_response"
|
||||
), "The raw response should be available from OpenAI"
|
||||
|
||||
Reference in New Issue
Block a user