From 047a5bb609a8c22a85951cd4a4af3c5024c812f9 Mon Sep 17 00:00:00 2001 From: Alex M Date: Mon, 25 Nov 2024 11:51:19 +0000 Subject: [PATCH 1/2] add beta support for anthropic --- instructor/client_anthropic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/instructor/client_anthropic.py b/instructor/client_anthropic.py index f53ddeba7..376ffde90 100644 --- a/instructor/client_anthropic.py +++ b/instructor/client_anthropic.py @@ -41,6 +41,7 @@ def from_anthropic( ), mode: instructor.Mode = instructor.Mode.ANTHROPIC_TOOLS, enable_prompt_caching: bool = False, + beta:bool = False, **kwargs: Any, ) -> instructor.Instructor | instructor.AsyncInstructor: assert ( @@ -70,6 +71,8 @@ def from_anthropic( raise TypeError( "Client must be an instance of {anthropic.Anthropic, anthropic.AsyncAnthropic} to enable prompt caching" ) + elif beta: + create = client.beta.messages.create else: create = client.messages.create From 1554ca75cb55176d88d96477a5cccac684cc25b7 Mon Sep 17 00:00:00 2001 From: Alex M Date: Tue, 3 Dec 2024 09:33:50 +0000 Subject: [PATCH 2/2] add documentation --- docs/integrations/anthropic.md | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/integrations/anthropic.md b/docs/integrations/anthropic.md index 5e97308b5..7cf6d6913 100644 --- a/docs/integrations/anthropic.md +++ b/docs/integrations/anthropic.md @@ -241,3 +241,45 @@ response = client.chat.completions.create( autodetect_images=True ) ``` + +## Beta support +The Anthropic beta API has [multiple features](https://docs.anthropic.com/en/docs/build-with-claude/pdf-support) other than those mentioned above. You can access the beta api by setting the beta flag to `True` when creating your client. The example below shows extraction from a pdf. + +```python +import instructor +import requests +import base64 +from pydantic import BaseModel +from anthropic import Anthropic + +class Character(BaseModel): + name: str + description: str + +client = instructor.from_anthropic(anthropic.AsyncAnthropic(), beta=True) + +pdf_data=base64.b64encode(requests.get("https://freekidsbooks.org/wp-content/uploads/2019/12/FKB-Kids-Stories-Peter-Rabbit.pdf").content).decode() + +extracted_content = await self.instructor_client.messages.create( + model="claude-3-5-sonnet-20241022", + betas=["pdfs-2024-09-25"], + max_tokens=1024, + messages=[ + {"role": "system", "content": "Extract a character from the pdf and describe them from any pictures ine the document"}, + { + "role": "user", + "content": [ + { + "type": "document", + "source": { + "type": "base64", + "media_type": "application/pdf", + "data": pdf_data, + }, + }, + ], + }, + ], + response_model=Character, + ) +```