Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Nov 20, 2023
1 parent 56cc9ed commit 469abec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/learn-async/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

client = instructor.apatch(AsyncOpenAI())


class Timer:
def __init__(self, name):
self.name = name
Expand Down Expand Up @@ -112,7 +113,6 @@ async def rate_limited_extract_person(text: str) -> Person:
print("asyncio.as_completed (rate limited):", all_persons)



if __name__ == "__main__":
asyncio.run(main())
"""
Expand All @@ -123,4 +123,4 @@ async def rate_limited_extract_person(text: str) -> Person:
asyncio.gather (rate limited) took 3.04 seconds
asyncio.as_completed (rate limited) took 3.26 seconds
"""
"""
7 changes: 4 additions & 3 deletions examples/validators/llm_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ class QuestionAnswer(BaseModel):
"""




class QuestionAnswerNoEvil(BaseModel):
question: str
answer: Annotated[
str,
BeforeValidator(llm_validator("don't say objectionable things", openai_client=client))
BeforeValidator(
llm_validator("don't say objectionable things", openai_client=client)
),
]


try:
qa = QuestionAnswerNoEvil(
question="What is the meaning of life?",
Expand Down
2 changes: 2 additions & 0 deletions tests/openai/test_multitask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

client = instructor.patch(OpenAI())


class User(BaseModel):
name: str
age: int


def test_multi_user():
def stream_extract(input: str, cls) -> Iterable[User]:
MultiUser = instructor.MultiTask(cls)
Expand Down
20 changes: 12 additions & 8 deletions tests/openai/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def validate_name(cls, v):
if v.upper() != v:
raise ValueError("Name should be uppercase")
return v

def test_runmodel_validator():


def test_runmodel_validator():
model = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=UserExtract,
Expand Down Expand Up @@ -69,40 +69,44 @@ async def test_async_runmodel():
{"role": "user", "content": "Extract jason is 25 years old"},
],
)
assert isinstance(model, UserExtractSimple), "Should be instance of UserExtractSimple"
assert isinstance(
model, UserExtractSimple
), "Should be instance of UserExtractSimple"
assert model.name.lower() == "jason"
assert hasattr(
model, "_raw_response"
), "The raw response should be available from OpenAI"


def test_runmodel():

model = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=UserExtractSimple,
messages=[
{"role": "user", "content": "Extract jason is 25 years old"},
],
)
assert isinstance(model, UserExtractSimple), "Should be instance of UserExtractSimple"
assert isinstance(
model, UserExtractSimple
), "Should be instance of UserExtractSimple"
assert model.name.lower() == "jason"
assert hasattr(
model, "_raw_response"
), "The raw response should be available from OpenAI"


def test_runmodel_validator_error():

class QuestionAnswerNoEvil(BaseModel):
question: str
answer: Annotated[
str,
BeforeValidator(llm_validator("don't say objectionable things", openai_client=client))
BeforeValidator(
llm_validator("don't say objectionable things", openai_client=client)
),
]

with pytest.raises(ValidationError):
QuestionAnswerNoEvil(
question="What is the meaning of life?",
answer="The meaning of life is to be evil and steal",
)
)
5 changes: 4 additions & 1 deletion tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ def wrapped_function():

assert is_async(wrapped_function) is True


def test_override_docs():
assert "response_model" in OVERRIDE_DOCS, "response_model should be in OVERRIDE_DOCS"
assert (
"response_model" in OVERRIDE_DOCS
), "response_model should be in OVERRIDE_DOCS"

0 comments on commit 469abec

Please sign in to comment.