567-labs--instructor
97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
217 行
6.1 KiB
Python
217 行
6.1 KiB
Python
import instructor
|
|
from openai import OpenAI, AsyncOpenAI
|
|
from pydantic import BaseModel
|
|
import pytest
|
|
from collections.abc import Iterable
|
|
from itertools import product
|
|
|
|
models = ["gpt-4.1-nano"]
|
|
|
|
|
|
class UserProfile(BaseModel):
|
|
name: str
|
|
age: int
|
|
bio: str
|
|
|
|
|
|
response_modes = [
|
|
instructor.Mode.RESPONSES_TOOLS,
|
|
instructor.Mode.RESPONSES_TOOLS_WITH_INBUILT_TOOLS,
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
def test_basic_response_methods(client: OpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(client, mode=mode)
|
|
|
|
# Test create
|
|
profile = instructor_client.responses.create(
|
|
model=model,
|
|
input="Generate a profile for a user named John who is 30 years old",
|
|
response_model=UserProfile,
|
|
)
|
|
assert isinstance(profile, UserProfile)
|
|
assert profile.name == "John"
|
|
assert profile.age == 30
|
|
|
|
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
def test_create_iterable_from_create(client: OpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(client, mode=mode)
|
|
|
|
# Test create
|
|
profiles = instructor_client.responses.create(
|
|
model=model,
|
|
input="Generate three fake profiles",
|
|
response_model=Iterable[UserProfile],
|
|
)
|
|
|
|
count = 0
|
|
for profile in profiles:
|
|
assert isinstance(profile, UserProfile)
|
|
count += 1
|
|
|
|
assert count >= 3
|
|
|
|
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
def test_create_with_completion(client: OpenAI, mode, model):
|
|
from openai.types.responses import Response
|
|
|
|
instructor_client = instructor.from_openai(client, mode=mode)
|
|
|
|
# Test create
|
|
response, completion = instructor_client.responses.create_with_completion(
|
|
model=model,
|
|
input="Generate a profile for a user named John who is 30 years old",
|
|
response_model=UserProfile,
|
|
)
|
|
assert isinstance(response, UserProfile)
|
|
assert response.name == "John"
|
|
assert response.age == 30
|
|
assert isinstance(completion, Response)
|
|
|
|
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
def test_create_iterable(client: OpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(client, mode=mode)
|
|
|
|
# Test create
|
|
users = instructor_client.responses.create_iterable(
|
|
model=model,
|
|
input="generate three fake profiles",
|
|
response_model=UserProfile,
|
|
)
|
|
|
|
count = 0
|
|
for user in users:
|
|
assert isinstance(user, UserProfile)
|
|
count += 1
|
|
|
|
assert count == 3
|
|
|
|
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
def test_create_partial(client: OpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(client, mode=mode)
|
|
|
|
# Test create
|
|
resp = instructor_client.responses.create_partial(
|
|
model=model,
|
|
input="Generate a fake profile",
|
|
response_model=UserProfile,
|
|
)
|
|
|
|
prev = None
|
|
update_count = 0
|
|
for user in resp:
|
|
assert isinstance(user, UserProfile)
|
|
if user != prev:
|
|
update_count += 1
|
|
prev = user
|
|
|
|
assert update_count >= 1
|
|
|
|
|
|
# ASYNC TESTS
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
async def test_basic_response_methods_async(client: AsyncOpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(client, mode=mode)
|
|
|
|
# Test create
|
|
profile = instructor_client.responses.create(
|
|
model=model,
|
|
input="Generate a profile for a user named John who is 30 years old",
|
|
response_model=UserProfile,
|
|
)
|
|
assert isinstance(profile, UserProfile)
|
|
assert profile.name == "John"
|
|
assert profile.age == 30
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
async def test_create_iterable_from_create_async(aclient: AsyncOpenAI, mode, model):
|
|
instructor_client: instructor.AsyncInstructor = instructor.from_openai(
|
|
aclient, mode=mode
|
|
)
|
|
|
|
# Test create
|
|
profiles = instructor_client.responses.create(
|
|
model=model,
|
|
input="Generate three fake profiles",
|
|
response_model=Iterable[UserProfile],
|
|
)
|
|
|
|
count = 0
|
|
async for profile in await profiles:
|
|
assert isinstance(profile, UserProfile)
|
|
count += 1
|
|
|
|
assert count >= 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
async def test_create_with_completion_async(aclient: AsyncOpenAI, mode, model):
|
|
from openai.types.responses import Response
|
|
|
|
instructor_client = instructor.from_openai(aclient, mode=mode)
|
|
|
|
# Test create
|
|
response, completion = await instructor_client.responses.create_with_completion(
|
|
model=model,
|
|
input="Generate a profile for a user named John who is 30 years old",
|
|
response_model=UserProfile,
|
|
)
|
|
assert isinstance(response, UserProfile)
|
|
assert response.name == "John"
|
|
assert response.age == 30
|
|
assert isinstance(completion, Response)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
async def test_create_iterable_async(aclient: AsyncOpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(aclient, mode=mode)
|
|
|
|
# Test create
|
|
users = await instructor_client.responses.create_iterable(
|
|
model=model,
|
|
input="generate three fake profiles",
|
|
response_model=UserProfile,
|
|
)
|
|
|
|
count = 0
|
|
async for user in users:
|
|
assert isinstance(user, UserProfile)
|
|
count += 1
|
|
|
|
assert count == 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("model, mode", product(models, response_modes))
|
|
async def test_create_partial_async(aclient: AsyncOpenAI, mode, model):
|
|
instructor_client = instructor.from_openai(aclient, mode=mode)
|
|
|
|
# Test create
|
|
resp = instructor_client.responses.create_partial(
|
|
model=model,
|
|
input="Generate a fake profile",
|
|
response_model=UserProfile,
|
|
)
|
|
|
|
prev = None
|
|
update_count = 0
|
|
async for user in resp:
|
|
assert isinstance(user, UserProfile)
|
|
if user != prev:
|
|
update_count += 1
|
|
prev = user
|
|
|
|
assert update_count >= 1
|