Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JoshuaPurtell/19cbb0422543cf03997682c5e3276a2e to your computer and use it in GitHub Desktop.
Save JoshuaPurtell/19cbb0422543cf03997682c5e3276a2e to your computer and use it in GitHub Desktop.
from pydantic import BaseModel
from typing import List
import anthropic
import instructor
import asyncio
# Patching the Anthropics client with the instructor for enhanced capabilities
anthropic_client = instructor.patch(
create=anthropic.AsyncAnthropic().messages.create,
mode=instructor.Mode.ANTHROPIC_TOOLS
)
class Properties(BaseModel):
name: str
value: str
class User(BaseModel):
name: str
age: int
properties: List[Properties]
user_response = asyncio.run(anthropic_client(
model="claude-3-haiku-20240307",
max_tokens=1024,
max_retries=0,
messages=[
{
"role": "user",
"content": "Create a user for a model with a name, age, and properties.",
}
],
response_model=User,
))
# First Run
"""
John Doe
"""
# Second Run
"""
Traceback (most recent call last):
File "/Users/jp/Documents/GitHub/arnold/arnold/vendors/openai/test.py", line 22, in <module>
user_response = asyncio.run(anthropic_client(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/3.11.4/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/3.11.4/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/3.11.4/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/instructor/patch.py", line 104, in new_create_async
response = await retry_async(
^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/instructor/retry.py", line 128, in retry_async
async for attempt in max_retries:
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/tenacity/_asyncio.py", line 71, in __anext__
do = self.iter(retry_state=self._retry_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/tenacity/__init__.py", line 325, in iter
raise retry_exc.reraise()
^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/tenacity/__init__.py", line 158, in reraise
raise self.last_attempt.result()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/3.11.4/lib/python3.11/concurrent/futures/_base.py", line 449, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/3.11.4/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/instructor/retry.py", line 135, in retry_async
return await process_response_async(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/instructor/process_response.py", line 79, in process_response_async
model = response_model.from_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/instructor/function_calls.py", line 97, in from_response
return xml_to_model(cls, extract_xml(completion.content[0].text)) # type:ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/instructor/anthropic_utils.py", line 120, in xml_to_model
parsed_xml = xmltodict.parse(xml_string)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jp/.pyenv/versions/arnold/lib/python3.11/site-packages/xmltodict.py", line 378, in parse
parser.Parse(xml_input, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 8, column 1
"""
print(user_response.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment