-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(messages): add support for image inputs (#359)
- Loading branch information
1 parent
35b0347
commit 579f013
Showing
26 changed files
with
756 additions
and
362 deletions.
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
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,30 @@ | ||
from pathlib import Path | ||
|
||
from anthropic import Anthropic | ||
|
||
client = Anthropic() | ||
|
||
response = client.messages.create( | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "text", | ||
"text": "Hello!", | ||
}, | ||
{ | ||
"type": "image", | ||
"source": { | ||
"type": "base64", | ||
"media_type": "image/png", | ||
"data": Path(__file__).parent.joinpath("logo.png"), | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
model="claude-3-opus-20240229", | ||
) | ||
print(response.model_dump_json(indent=2)) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,20 @@ | ||
#!/usr/bin/env -S poetry run python | ||
|
||
import asyncio | ||
|
||
import anthropic | ||
from anthropic import AsyncAnthropic | ||
|
||
|
||
async def main() -> None: | ||
client = AsyncAnthropic() | ||
|
||
res = await client.completions.create( | ||
model="claude-2.1", | ||
prompt=f"{anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? {anthropic.AI_PROMPT}", | ||
max_tokens_to_sample=1000, | ||
) | ||
print(res.completion) | ||
|
||
|
||
asyncio.run(main()) |
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,18 @@ | ||
#!/usr/bin/env -S poetry run python | ||
|
||
import anthropic | ||
from anthropic import Anthropic | ||
|
||
|
||
def main() -> None: | ||
client = Anthropic() | ||
|
||
res = client.completions.create( | ||
model="claude-2.1", | ||
prompt=f"{anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? {anthropic.AI_PROMPT}", | ||
max_tokens_to_sample=1000, | ||
) | ||
print(res.completion) | ||
|
||
|
||
main() |
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,57 @@ | ||
#!/usr/bin/env -S poetry run python | ||
|
||
import asyncio | ||
|
||
from anthropic import AI_PROMPT, HUMAN_PROMPT, Anthropic, APIStatusError, AsyncAnthropic | ||
|
||
client = Anthropic() | ||
async_client = AsyncAnthropic() | ||
|
||
question = """ | ||
Hey Claude! How can I recursively list all files in a directory in Python? | ||
""" | ||
|
||
|
||
def sync_stream() -> None: | ||
stream = client.completions.create( | ||
prompt=f"{HUMAN_PROMPT} {question}{AI_PROMPT}", | ||
model="claude-2.1", | ||
stream=True, | ||
max_tokens_to_sample=300, | ||
) | ||
|
||
for completion in stream: | ||
print(completion.completion, end="", flush=True) | ||
|
||
print() | ||
|
||
|
||
async def async_stream() -> None: | ||
stream = await async_client.completions.create( | ||
prompt=f"{HUMAN_PROMPT} {question}{AI_PROMPT}", | ||
model="claude-2.1", | ||
stream=True, | ||
max_tokens_to_sample=300, | ||
) | ||
|
||
async for completion in stream: | ||
print(completion.completion, end="", flush=True) | ||
|
||
print() | ||
|
||
|
||
def stream_error() -> None: | ||
try: | ||
client.completions.create( | ||
prompt=f"{HUMAN_PROMPT} {question}{AI_PROMPT}", | ||
model="claude-unknown-model", | ||
stream=True, | ||
max_tokens_to_sample=300, | ||
) | ||
except APIStatusError as err: | ||
print(f"Caught API status error with response body: {err.response.text}") | ||
|
||
|
||
sync_stream() | ||
asyncio.run(async_stream()) | ||
stream_error() |
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
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
Oops, something went wrong.