Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update code snippets and text across multiple documentation files #450

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ except ValidationError as e:
"""
1 validation error for QuestionAnswer
answer
Assertion failed, The statement promotes objectionable behavior. [type=assertion_error, input_value='The meaning of life is to be evil and steal', input_type=str]
Assertion failed, The statement promotes objectionable behavior by encouraging evil and stealing, which goes against the rule of not saying objectionable things. [type=assertion_error, input_value='The meaning of life is to be evil and steal', input_type=str]
For further information visit https://errors.pydantic.dev/2.6/v/assertion_error
"""
```
Expand Down
4 changes: 2 additions & 2 deletions docs/concepts/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def extract(data) -> UserDetail:
start = time.perf_counter() # (1)
model = extract("Extract jason is 25 years old")
print(f"Time taken: {time.perf_counter() - start}")
#> Time taken: 0.41433916706591845
#> Time taken: 0.8392175831831992

start = time.perf_counter()
model = extract("Extract jason is 25 years old") # (2)
print(f"Time taken: {time.perf_counter() - start}")
#> Time taken: 1.7080456018447876e-06
#> Time taken: 8.33999365568161e-07
```

1. Using `time.perf_counter()` to measure the time taken to run the function is better than using `time.time()` because it's more accurate and less susceptible to system clock changes.
Expand Down
10 changes: 4 additions & 6 deletions docs/concepts/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ users = client.chat.completions.create(
)
for user in users:
print(user)
#> ('tasks', [User(name='Jason', age=10), User(name='John', age=30)])

#> name="Jason" "age"=10
#> name="John" "age"=10
#> name='Jason' age=10
#> name='John' age=30
```

## Streaming Tasks
Expand Down Expand Up @@ -159,8 +157,8 @@ async def print_iterable_results():
)
async for m in model:
print(m)
#> name='John Doe' age=32
#> name='Jane Smith' age=28
#> name='John Smith' age=30
#> name='Mary Jane' age=28


import asyncio
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/maybe.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ print(user2.model_dump_json(indent=2))
{
"result": null,
"error": false,
"message": "Unknown user"
"message": null
}
"""
```
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class SearchQuery(BaseModel):

def execute(self):
print(f"Searching for {self.query} of type {self.query_type}")
#> Searching for cat of type image
#> Searching for cat pictures of type image
return "Results for cat"


Expand Down
4 changes: 2 additions & 2 deletions docs/concepts/parallel.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ function_calls = client.chat.completions.create(

for fc in function_calls:
print(fc)
#> location='Toronto' units='metric'
#> location='Toronto' units='imperial'
#> location='Dallas' units='imperial'
#> query='super bowl winner'
#> query='who won the super bowl'
```

1. Set the mode to `PARALLEL_TOOLS` to enable parallel function calling.
Expand Down
142 changes: 33 additions & 109 deletions docs/concepts/partial.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,31 @@ for extraction in extraction_stream:
console.clear()
console.print(obj)

print(extraction.model_dump_json())
print(extraction.model_dump_json(indent=2))
"""
{"users":[{"name":"John Doe","email":"[email protected]","twitter":"@TechGuru44"},{"name":"Jane Smith","email":"[email protected]","twitter":"@DigitalDiva88"},{"name":"Alex Johnson","email":"[email protected]","twitter":"@CodeMaster2023"}],"date":"2024-03-15","location":"Grand Tech Arena located at 4521 Innovation Drive","budget":50000,"deadline":"2024-02-20"}
{
"users": [
{
"name": "John Doe",
"email": "[email protected]",
"twitter": "@TechGuru44"
},
{
"name": "Jane Smith",
"email": "[email protected]",
"twitter": "@DigitalDiva88"
},
{
"name": "Alex Johnson",
"email": "[email protected]",
"twitter": "@CodeMaster2023"
}
],
"date": "2024-03-15",
"location": "Grand Tech Arena located at 4521 Innovation Drive",
"budget": 50000,
"deadline": "2024-02-20"
}
"""
```

Expand All @@ -118,128 +140,30 @@ import instructor
from openai import AsyncOpenAI
from pydantic import BaseModel

client = instructor.apatch(AsyncOpenAI())
client = instructor.patch(AsyncOpenAI())


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


async def print_partial_results():
user = await client.chat.completions.create(
model="gpt-4-turbo-preview",
response_model=instructor.Partial[UserExtract],
response_model=instructor.Partial[User],
max_retries=2,
stream=True,
messages=[
{"role": "user", "content": "Jason Liu is 12 years old"},
{"role": "user", "content": "Jason is 12 years old"},
],
)
async for m in user:
print(m.model_dump_json(indent=2))
"""
{
"name": null,
"age": null
}
"""
"""
{
"name": "",
"age": null
}
"""
"""
{
"name": "Jason",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": 12
}
"""
"""
{
"name": "",
"age": null
}
"""
"""
{
"name": "Jason",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": 12
}
"""
"""
{
"name": "",
"age": null
}
"""
"""
{
"name": "Jason",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": 12
}
"""
"""
{
"name": "",
"age": null
}
"""
"""
{
"name": "Jason",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": null
}
"""
"""
{
"name": "Jason Liu",
"age": 12
}
"""
print(m)
#> name=None age=None
#> name='' age=None
#> name='Jason' age=None
#> name='Jason' age=12


import asyncio
Expand Down
25 changes: 15 additions & 10 deletions docs/concepts/raw_response.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ user: UserExtract = client.chat.completions.create(
print(user._raw_response)
"""
ChatCompletion(
id='chatcmpl-8pOAsSOIHAmngMBBki3BLN3p552L0',
id='chatcmpl-8u9bsrmmf5YjZyfCtQymoZV8LK1qg',
choices=[
Choice(
finish_reason='stop',
Expand All @@ -34,19 +34,24 @@ ChatCompletion(
message=ChatCompletionMessage(
content=None,
role='assistant',
function_call=FunctionCall(
arguments='{\n "name": "Jason",\n "age": 25\n}',
name='UserExtract',
),
tool_calls=None,
function_call=None,
tool_calls=[
ChatCompletionMessageToolCall(
id='call_O5rpXf47YgXiYrYWv45yZUeM',
function=Function(
arguments='{"name":"Jason","age":25}', name='UserExtract'
),
type='function',
)
],
),
)
],
created=1707258346,
model='gpt-3.5-turbo-0613',
created=1708394000,
model='gpt-3.5-turbo-0125',
object='chat.completion',
system_fingerprint=None,
usage=CompletionUsage(completion_tokens=16, prompt_tokens=73, total_tokens=89),
system_fingerprint='fp_69829325d0',
usage=CompletionUsage(completion_tokens=9, prompt_tokens=82, total_tokens=91),
)
"""
```
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/reask_validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ except ValidationError as e:
"""
1 validation error for QuestionAnswer
answer
Assertion failed, The statement promotes objectionable behavior. [type=assertion_error, input_value='The meaning of life is to be evil and steal', input_type=str]
Assertion failed, The statement promotes objectionable behavior by encouraging evil and theft. [type=assertion_error, input_value='The meaning of life is to be evil and steal', input_type=str]
For further information visit https://errors.pydantic.dev/2.6/v/assertion_error
"""
```
Expand Down
Loading
Loading