-
-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anthropic streaming support #682
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -79,7 +79,12 @@ def extract_json( | |||||
) -> Generator[str, None, None]: | ||||||
for chunk in completion: | ||||||
try: | ||||||
if chunk.choices: | ||||||
if mode == Mode.ANTHROPIC_JSON: | ||||||
if json_chunk := chunk.delta.text: | ||||||
yield json_chunk | ||||||
if mode == Mode.ANTHROPIC_TOOLS: | ||||||
yield chunk.model_extra.get("delta", "").get("partial_json", "") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
should it be {} since |
||||||
elif chunk.choices: | ||||||
if mode == Mode.FUNCTIONS: | ||||||
if json_chunk := chunk.choices[0].delta.function_call.arguments: | ||||||
yield json_chunk | ||||||
|
@@ -102,7 +107,12 @@ async def extract_json_async( | |||||
) -> AsyncGenerator[str, None]: | ||||||
async for chunk in completion: | ||||||
try: | ||||||
if chunk.choices: | ||||||
if mode == Mode.ANTHROPIC_JSON: | ||||||
if json_chunk := chunk.delta.text: | ||||||
yield json_chunk | ||||||
if mode == Mode.ANTHROPIC_TOOLS: | ||||||
yield chunk.model_extra.get("delta", "").get("partial_json", "") | ||||||
elif chunk.choices: | ||||||
if mode == Mode.FUNCTIONS: | ||||||
if json_chunk := chunk.choices[0].delta.function_call.arguments: | ||||||
yield json_chunk | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# conftest.py | ||
from anthropic import AsyncAnthropic, Anthropic | ||
import pytest | ||
import os | ||
|
||
try: | ||
import braintrust | ||
|
||
wrap_anthropic = braintrust.wrap_anthropic | ||
except ImportError: | ||
|
||
def wrap_anthropic(x): | ||
return x | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def client(): | ||
if os.environ.get("BRAINTRUST_API_KEY"): | ||
yield wrap_anthropic( | ||
Anthropic( | ||
api_key=os.environ["BRAINTRUST_API_KEY"], | ||
base_url="https://braintrustproxy.com/v1", | ||
) | ||
) | ||
else: | ||
yield Anthropic() | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def aclient(): | ||
if os.environ.get("BRAINTRUST_API_KEY"): | ||
yield wrap_anthropic( | ||
AsyncAnthropic( | ||
api_key=os.environ["BRAINTRUST_API_KEY"], | ||
base_url="https://braintrustproxy.com/v1", | ||
) | ||
) | ||
else: | ||
yield AsyncAnthropic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from itertools import product | ||
from collections.abc import Iterable | ||
from pydantic import BaseModel | ||
import pytest | ||
import instructor | ||
from instructor.dsl.partial import Partial | ||
|
||
from .util import models, modes | ||
|
||
|
||
class UserExtract(BaseModel): | ||
name: str | ||
age: int | ||
|
||
|
||
@pytest.mark.parametrize("model, mode, stream", product(models, modes, [True, False])) | ||
def test_iterable_model(model, mode, stream, client): | ||
client = instructor.from_anthropic(client, mode=mode) | ||
model = client.messages.create( | ||
model=model, | ||
response_model=Iterable[UserExtract], | ||
max_retries=2, | ||
stream=stream, | ||
max_tokens=1024, | ||
messages=[ | ||
{"role": "user", "content": "Make two up people"}, | ||
], | ||
) | ||
for m in model: | ||
assert isinstance(m, UserExtract) | ||
|
||
|
||
@pytest.mark.parametrize("model, mode, stream", product(models, modes, [True, False])) | ||
@pytest.mark.asyncio | ||
async def test_iterable_model_async(model, mode, stream, aclient): | ||
aclient = instructor.from_anthropic(aclient, mode=mode) | ||
model = await aclient.messages.create( | ||
model=model, | ||
response_model=Iterable[UserExtract], | ||
max_retries=2, | ||
stream=stream, | ||
max_tokens=1024, | ||
messages=[ | ||
{"role": "user", "content": "Make two up people"}, | ||
], | ||
) | ||
if stream: | ||
async for m in model: | ||
assert isinstance(m, UserExtract) | ||
else: | ||
for m in model: | ||
assert isinstance(m, UserExtract) | ||
|
||
|
||
@pytest.mark.parametrize("model,mode", product(models, modes)) | ||
def test_partial_model(model, mode, client): | ||
client = instructor.from_anthropic(client, mode=mode) | ||
model = client.messages.create( | ||
model=model, | ||
response_model=Partial[UserExtract], | ||
max_retries=2, | ||
max_tokens=1024, | ||
stream=True, | ||
messages=[ | ||
{"role": "user", "content": "Jason Liu is 12 years old"}, | ||
], | ||
) | ||
for m in model: | ||
assert isinstance(m, UserExtract) | ||
|
||
|
||
@pytest.mark.parametrize("model,mode", product(models, modes)) | ||
@pytest.mark.asyncio | ||
async def test_partial_model_async(model, mode, aclient): | ||
aclient = instructor.from_anthropic(aclient, mode=mode) | ||
model = await aclient.messages.create( | ||
model=model, | ||
response_model=Partial[UserExtract], | ||
max_retries=2, | ||
stream=True, | ||
max_tokens=1024, | ||
messages=[ | ||
{"role": "user", "content": "Jason Liu is 12 years old"}, | ||
], | ||
) | ||
async for m in model: | ||
assert isinstance(m, UserExtract) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import instructor | ||
|
||
models = ["claude-3-haiku-20240307"] | ||
modes = [ | ||
instructor.Mode.ANTHROPIC_TOOLS, | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR introduces significant functionality changes by supporting streaming for Anthropic. Please ensure that the documentation is updated to reflect these changes, and consider adding relevant entries to the mkdocs.yml if new documentation files are created.