Skip to content

Commit

Permalink
Merge branch 'main' into feature/sambanovacloud_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-92 authored Dec 6, 2024
2 parents b4eb16a + d15209a commit 294b486
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 324 deletions.
2 changes: 0 additions & 2 deletions docs/examples/bulk_classification.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,4 @@ async def get_tags(text: List[str], tags: List[Tag]) -> List[Tag]:
tag_results = asyncio.run(get_tags(text, tags))
for tag in tag_results:
print(tag)
#> id=0 name='personal'
#> id=1 name='phone'
```
15 changes: 15 additions & 0 deletions docs/integrations/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pip install "instructor[anthropic]"

Once we've done so, getting started is as simple as using our `from_anthropic` method to patch the client up.

### Basic Usage

```python
from pydantic import BaseModel
from typing import List
Expand Down Expand Up @@ -71,6 +73,19 @@ print(user_response.model_dump_json(indent=2))
"""
```

### Beta API Features

Anthropic provides beta features through their beta API endpoint. To use these features, enable the beta parameter when creating the client:

```python
client = instructor.from_anthropic(
anthropic.Anthropic(),
beta=True # Enable beta API features
)
```

This allows you to access beta features like PDF support and other experimental capabilities. For more information about available beta features, refer to [Anthropic's documentation](https://docs.anthropic.com/claude/docs/beta-features).

## Streaming Support

Instructor has two main ways that you can use to stream responses out
Expand Down
21 changes: 21 additions & 0 deletions instructor/client_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def from_anthropic(
),
mode: instructor.Mode = instructor.Mode.ANTHROPIC_TOOLS,
enable_prompt_caching: bool = False,
beta: bool = False,
**kwargs: Any,
) -> instructor.Instructor: ...

Expand All @@ -26,6 +27,7 @@ def from_anthropic(
),
mode: instructor.Mode = instructor.Mode.ANTHROPIC_TOOLS,
enable_prompt_caching: bool = False,
beta: bool = False,
**kwargs: Any,
) -> instructor.AsyncInstructor: ...

Expand All @@ -41,8 +43,25 @@ def from_anthropic(
),
mode: instructor.Mode = instructor.Mode.ANTHROPIC_TOOLS,
enable_prompt_caching: bool = False,
beta: bool = False,
**kwargs: Any,
) -> instructor.Instructor | instructor.AsyncInstructor:
"""Create an Instructor instance from an Anthropic client.
Args:
client: An instance of Anthropic client (sync or async)
mode: The mode to use for the client (ANTHROPIC_JSON or ANTHROPIC_TOOLS)
enable_prompt_caching: Whether to enable prompt caching (requires Anthropic or AsyncAnthropic client)
beta: Whether to use beta API features (uses client.beta.messages.create)
**kwargs: Additional keyword arguments to pass to the Instructor constructor
Returns:
An Instructor instance (sync or async depending on the client type)
Raises:
TypeError: If enable_prompt_caching is True and client is not Anthropic or AsyncAnthropic
AssertionError: If mode is not ANTHROPIC_JSON or ANTHROPIC_TOOLS
"""
assert (
mode
in {
Expand Down Expand Up @@ -70,6 +89,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

Expand Down
620 changes: 311 additions & 309 deletions poetry.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rich = "^13.7.0"
aiohttp = "^3.9.1"
tenacity = ">=9.0.0,<10.0.0"
pydantic-core = "^2.18.0"
jiter = ">=0.6.1,<0.7"
jiter = ">=0.6.1,<0.9"
jinja2 = "^3.1.4"
requests = "^2.32.3"

Expand All @@ -30,9 +30,9 @@ pandas = { version = "^2.2.0", optional = true }
tabulate = { version = "^0.9.0", optional = true }
pydantic_extra_types = { version = "^2.6.0", optional = true }
litellm = { version = "^1.35.31", optional = true }
anthropic = { version = ">=0.36.2,<0.38.0", optional = true }
anthropic = { version = ">=0.36.2,<0.41.0", optional = true }
xmltodict = { version = ">=0.13,<0.15", optional = true }
groq = { version = ">=0.4.2,<0.12.0", optional = true }
groq = { version = ">=0.4.2,<0.14.0", optional = true }
cohere = { version = "^5.1.8", optional = true }
mistralai = { version = "^1.0.3", optional = true }
google-generativeai = { version = "^0.8.2", optional = true }
Expand Down Expand Up @@ -81,16 +81,16 @@ black = "^24.10.0"
[tool.poetry.group.docs.dependencies]
mkdocs = "^1.4.3"
mkdocs-material = { extras = ["imaging"], version = "^9.5.9" }
mkdocstrings = "^0.26.1"
mkdocstrings = ">=0.26.1,<0.28.0"
mkdocstrings-python = "^1.11.1"
pytest-examples = "^0.0.13"
pytest-examples = ">=0.0.13,<0.0.16"
mkdocs-jupyter = ">=0.24.6,<0.26.0"
mkdocs-rss-plugin = "^1.12.0"
mkdocs-minify-plugin = "^0.8.0"
mkdocs-redirects = "^1.2.1"

[tool.poetry.group.anthropic.dependencies]
anthropic = ">=0.36.2,<0.38.0"
anthropic = ">=0.36.2,<0.41.0"

[tool.poetry.group.test-docs.dependencies]
fastapi = ">=0.109.2,<0.116.0"
Expand All @@ -100,17 +100,17 @@ pandas = "^2.2.0"
tabulate = "^0.9.0"
pydantic_extra_types = "^2.6.0"
litellm = "^1.35.31"
anthropic = ">=0.36.2,<0.38.0"
anthropic = ">=0.36.2,<0.41.0"
xmltodict = ">=0.13,<0.15"
groq = ">=0.4.2,<0.12.0"
groq = ">=0.4.2,<0.14.0"
phonenumbers = "^8.13.33"
cohere = "^5.1.8"
mistralai = "^1.0.3"
cerebras_cloud_sdk = "^1.5.0"
fireworks-ai = "^0.15.4"
graphviz = "^0.20.3"
sqlmodel = "^0.0.22"
trafilatura = "^1.12.2"
trafilatura = ">=1.12.2,<3.0.0"
pydub = "^0.25.1"
datasets = "^3.0.1"
writer-sdk = "^1.2.0"
Expand Down
4 changes: 2 additions & 2 deletions requirements-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pydantic
docstring-parser
rich
aiohttp
ruff==0.7.1
ruff==0.8.1
pre-commit==4.0.1
pyright==1.1.386
pyright==1.1.390
typer
cohere
datasets
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pydantic
docstring-parser
rich
aiohttp
ruff==0.7.1
ruff==0.8.1
pre-commit==4.0.1
pyright==1.1.386
pyright==1.1.390
typer
cohere

0 comments on commit 294b486

Please sign in to comment.