mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
update everything
This commit is contained in:
@@ -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.44677383406087756
|
||||
#> Time taken: 0.41433916706591845
|
||||
|
||||
start = time.perf_counter()
|
||||
model = extract("Extract jason is 25 years old") # (2)
|
||||
print(f"Time taken: {time.perf_counter() - start}")
|
||||
#> Time taken: 1.00000761449337e-06
|
||||
#> Time taken: 1.7080456018447876e-06
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
@@ -159,8 +159,8 @@ async def print_iterable_results():
|
||||
)
|
||||
async for m in model:
|
||||
print(m)
|
||||
#> name='John Doe' age=30
|
||||
#> name='Jane Doe' age=28
|
||||
#> name='John Doe' age=32
|
||||
#> name='Jane Smith' age=28
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
@@ -88,7 +88,7 @@ print(user2.model_dump_json(indent=2))
|
||||
"""
|
||||
{
|
||||
"result": null,
|
||||
"error": true,
|
||||
"error": false,
|
||||
"message": "Unknown user"
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -46,7 +46,7 @@ for fc in function_calls:
|
||||
print(fc)
|
||||
#> location='Toronto' units='metric'
|
||||
#> location='Dallas' units='imperial'
|
||||
#> query='Super Bowl winner'
|
||||
#> query='super bowl winner'
|
||||
```
|
||||
|
||||
1. Set the mode to `PARALLEL_TOOLS` to enable parallel function calling.
|
||||
|
||||
@@ -216,6 +216,30 @@ async def print_partial_results():
|
||||
"age": 12
|
||||
}
|
||||
"""
|
||||
"""
|
||||
{
|
||||
"name": "",
|
||||
"age": null
|
||||
}
|
||||
"""
|
||||
"""
|
||||
{
|
||||
"name": "Jason",
|
||||
"age": null
|
||||
}
|
||||
"""
|
||||
"""
|
||||
{
|
||||
"name": "Jason Liu",
|
||||
"age": null
|
||||
}
|
||||
"""
|
||||
"""
|
||||
{
|
||||
"name": "Jason Liu",
|
||||
"age": 12
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
import asyncio
|
||||
|
||||
@@ -25,7 +25,7 @@ user: UserExtract = client.chat.completions.create(
|
||||
print(user._raw_response)
|
||||
"""
|
||||
ChatCompletion(
|
||||
id='chatcmpl-8p1AMuoml2bkdXK4284rbPmNRNNwF',
|
||||
id='chatcmpl-8pOAsSOIHAmngMBBki3BLN3p552L0',
|
||||
choices=[
|
||||
Choice(
|
||||
finish_reason='stop',
|
||||
@@ -42,7 +42,7 @@ ChatCompletion(
|
||||
),
|
||||
)
|
||||
],
|
||||
created=1707169902,
|
||||
created=1707258346,
|
||||
model='gpt-3.5-turbo-0613',
|
||||
object='chat.completion',
|
||||
system_fingerprint=None,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
The easiest way to get usage for non streaming requests is to access the raw response.
|
||||
|
||||
```python
|
||||
import instructor
|
||||
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
client = instructor.patch(OpenAI())
|
||||
|
||||
|
||||
class UserExtract(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
|
||||
user: UserExtract = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserExtract,
|
||||
messages=[
|
||||
{"role": "user", "content": "Extract jason is 25 years old"},
|
||||
],
|
||||
)
|
||||
|
||||
print(user._raw_response.usage)
|
||||
#> CompletionUsage(completion_tokens=16, prompt_tokens=73, total_tokens=89)
|
||||
```
|
||||
+2
-2
@@ -120,7 +120,7 @@ print(response.model_dump_json(indent=2))
|
||||
print(user._raw_response.model_dump_json(indent=2))
|
||||
"""
|
||||
{
|
||||
"id": "chatcmpl-8p19pXZ5BIqGtaPhVLo184UuQNH3v",
|
||||
"id": "chatcmpl-8pOAKwq8OXZVvOCMw4dv713oKplLF",
|
||||
"choices": [
|
||||
{
|
||||
"finish_reason": "stop",
|
||||
@@ -137,7 +137,7 @@ print(response.model_dump_json(indent=2))
|
||||
}
|
||||
}
|
||||
],
|
||||
"created": 1707169869,
|
||||
"created": 1707258312,
|
||||
"model": "gpt-3.5-turbo-0613",
|
||||
"object": "chat.completion",
|
||||
"system_fingerprint": null,
|
||||
|
||||
@@ -132,6 +132,7 @@ nav:
|
||||
- Philosophy: 'concepts/philosophy.md'
|
||||
- Models: 'concepts/models.md'
|
||||
- Fields: 'concepts/fields.md'
|
||||
- Usage Tokens: 'concepts/usage.md'
|
||||
- Missing: "concepts/maybe.md"
|
||||
- Patching: 'concepts/patching.md'
|
||||
- Retrying: 'concepts/retrying.md'
|
||||
|
||||
Reference in New Issue
Block a user