Skip to content

Commit

Permalink
feat: support anthropic tools (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Apr 5, 2024
1 parent a8f5981 commit 2076079
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 257 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ import openai
import instructor
from pydantic import BaseModel


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


client = instructor.from_openai(openai.OpenAI())

user = client.chat.completions.create(
Expand Down Expand Up @@ -238,6 +240,17 @@ user_stream = client.chat.completions.create_partial(

for user in user_stream:
print(user)
#> name=None age=None
#> name=None age=None
#> name=None age=None
#> name=None age=None
#> name=None age=25
#> name=None age=25
#> name=None age=25
#> name=None age=25
#> name=None age=25
#> name=None age=25
#> name='John Doe' age=25
# name=None age=None
# name='' age=None
# name='John' age=None
Expand Down Expand Up @@ -277,6 +290,8 @@ users = client.chat.completions.create_iterable(

for user in users:
print(user)
#> name='John' age=30
#> name='Jane' age=25
# User(name='John Doe', age=30)
# User(name='Jane Smith', age=25)
```
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.417595750070177
#> Time taken: 0.5761224159505218

start = time.perf_counter()
model = extract("Extract jason is 25 years old") # (2)
print(f"Time taken: {time.perf_counter() - start}")
#> Time taken: 1.374981366097927e-06
#> Time taken: 4.37488779425621e-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.
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/parallel.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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.
Expand Down
14 changes: 10 additions & 4 deletions docs/concepts/partial.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ print(extraction.model_dump_json(indent=2))
"twitter": "@CodeMaster2023"
}
],
"date": "2024-03-15",
"date": "March 15th, 2024",
"location": "Grand Tech Arena located at 4521 Innovation Drive",
"budget": 50000,
"deadline": "2024-02-20"
"deadline": "February 20th"
}
"""
```
Expand Down Expand Up @@ -161,8 +161,14 @@ async def print_partial_results():
async for m in user:
print(m)
#> name=None age=None
#> name=None age=12
#> name='' age=12
#> name=None age=None
#> name=None age=None
#> name=None age=None
#> name=None age=None
#> name='Jason' age=None
#> name='Jason' age=None
#> name='Jason' age=None
#> name='Jason' age=12
#> name='Jason' age=12


Expand Down
6 changes: 3 additions & 3 deletions docs/concepts/raw_response.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ print(user)
print(completion)
"""
ChatCompletion(
id='chatcmpl-98za6bFyfGSmZ90n0QfroijxvF37Q',
id='chatcmpl-9AUijAMiPb1gQ3qs2bdJepRTVlWip',
choices=[
Choice(
finish_reason='stop',
Expand All @@ -43,7 +43,7 @@ ChatCompletion(
function_call=None,
tool_calls=[
ChatCompletionMessageToolCall(
id='call_9TylRriehvbU1scs1H3iDSgK',
id='call_yAwmIEIXBdMaKvU8aXnsjdem',
function=Function(
arguments='{"name":"Jason","age":25}', name='UserExtract'
),
Expand All @@ -53,7 +53,7 @@ ChatCompletion(
),
)
],
created=1711930370,
created=1712288397,
model='gpt-3.5-turbo-0125',
object='chat.completion',
system_fingerprint='fp_b28b39ffa8',
Expand Down
6 changes: 3 additions & 3 deletions docs/concepts/reask_validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ except ValidationError as e:
1 validation error for UserDetail
name
Value error, Name must contain a space. [type=value_error, input_value='Jason', input_type=str]
For further information visit https://errors.pydantic.dev/2.6/v/value_error
For further information visit https://errors.pydantic.dev/2.7/v/value_error
"""
```

Expand Down Expand Up @@ -89,8 +89,8 @@ except ValidationError as e:
"""
1 validation error for QuestionAnswer
answer
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
Assertion failed, The statement promotes objectionable behavior by encouraging evil actions like 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.7/v/assertion_error
"""
```

Expand Down
7 changes: 6 additions & 1 deletion docs/concepts/retrying.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ except Exception as e:
1 validation error for UserDetail
name
Value error, Name must be ALL CAPS [type=value_error, input_value='jason', input_type=str]
For further information visit https://errors.pydantic.dev/2.6/v/value_error
For further information visit https://errors.pydantic.dev/2.7/v/value_error
"""
```

Expand Down Expand Up @@ -207,6 +207,10 @@ resp = client.messages.create(
max_retries=tenacity.Retrying(
stop=tenacity.stop_after_attempt(3),
before=lambda _: print("before:", _),
"""
before:
<RetryCallState 4746154832: attempt #1; slept for 0.0; last result: none yet>
"""
after=lambda _: print("after:", _),
),
messages=[
Expand All @@ -222,6 +226,7 @@ assert isinstance(resp, User)
assert resp.name == "JOHN" # due to validation
assert resp.age == 18
print(resp)
#> name='JOHN' age=18

"""
before: <RetryCallState 4421908816: attempt #1; slept for 0.0; last result: none yet>
Expand Down
1 change: 1 addition & 0 deletions docs/concepts/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ print(model.model_json_schema())
'type': 'boolean',
}
},
'required': ['content'],
'title': 'Response',
'type': 'object',
}
Expand Down
12 changes: 6 additions & 6 deletions docs/hub/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ print(user_response.model_dump_json(indent=2))
"age": 35,
"properties": [
{
"name": "Address",
"value": "123 Main St, Anytown USA"
"name": "Hair Color",
"value": "Brown"
},
{
"name": "Phone",
"value": "555-1234"
"name": "Height",
"value": "6'2\""
},
{
"name": "Email",
"value": "john.doe@example.com"
"name": "Occupation",
"value": "Software Engineer"
}
]
}
Expand Down
24 changes: 12 additions & 12 deletions docs/hub/pandas_df.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ if __name__ == "__main__":
assert isinstance(df, pd.DataFrame)
print(df)
"""
Party Years Served
Party Years Served
President
Joe Biden Democratic 2021 - Present
Donald Trump Republican 2017 - 2021
Barack Obama Democratic 2009 - 2017
George W. Bush Republican 2001 - 2009
Bill Clinton Democratic 1993 - 2001
Joe Biden Democrat 2021-present
Donald Trump Republican 2017-2021
Barack Obama Democrat 2009-2017
George W. Bush Republican 2001-2009
Bill Clinton Democrat 1993-2001
"""

table = extract_table(
Expand All @@ -127,13 +127,13 @@ if __name__ == "__main__":
#> Last 5 Presidents of the United States
print(table.data)
"""
Party Years Served
Party Years Served
President
Joe Biden Democratic 2021 - Present
Donald Trump Republican 2017 - 2021
Barack Obama Democratic 2009 - 2017
George W. Bush Republican 2001 - 2009
Bill Clinton Democratic 1993 - 2001
Joe Biden Democratic 2021 - Present
Donald Trump Republican 2017 - 2021
Barack Obama Democratic 2009 - 2017
George W. Bush Republican 2001 - 2009
Bill Clinton Democratic 1993 - 2001
"""
```

Expand Down
14 changes: 14 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ import openai
import instructor
from pydantic import BaseModel


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


client = instructor.from_openai(openai.OpenAI())

user = client.chat.completions.create(
Expand Down Expand Up @@ -258,6 +260,16 @@ user_stream = client.chat.completions.create_partial(

for user in user_stream:
print(user)
#> name=None age=None
#> name=None age=None
#> name=None age=None
#> name=None age=None
#> name=None age=30
#> name=None age=30
#> name=None age=30
#> name=None age=30
#> name=None age=30
#> name='John' age=30
# name=None age=None
# name='' age=None
# name='John' age=None
Expand Down Expand Up @@ -297,6 +309,8 @@ users = client.chat.completions.create_iterable(

for user in users:
print(user)
#> name='Alice' age=30
#> name='Bob' age=25
# User(name='John Doe', age=30)
# User(name='Jane Smith', age=25)
```
Expand Down
2 changes: 0 additions & 2 deletions examples/partial_streaming/run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Part of this code is adapted from the following examples from OpenAI Cookbook:
# https://cookbook.openai.com/examples/how_to_stream_completions
# https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
import time
import tiktoken
import instructor
from openai import OpenAI
from pydantic import BaseModel
Expand Down

0 comments on commit 2076079

Please sign in to comment.