From 24884600dcbe97609b7fe39465e1a852964c6645 Mon Sep 17 00:00:00 2001 From: Kim Tran Date: Thu, 23 May 2024 00:57:16 -0400 Subject: [PATCH] Add anthropic async examples --- .../anthropic_async_messages_stream.py | 35 ++++++++++++ ...anthropic_async_messages_stream_handler.py | 38 +++++++++++++ .../logging/anthropic_async_tools_stream.py | 47 ++++++++++++++++ .../anthropic_async_tools_stream_handler.py | 55 +++++++++++++++++++ .../logging/anthropic_messages_not_given.py | 17 ++++++ .../logging/magentic_async_chat_prompt.py | 23 ++++++++ log10/anthropic.py | 5 -- 7 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 examples/logging/anthropic_async_messages_stream.py create mode 100644 examples/logging/anthropic_async_messages_stream_handler.py create mode 100644 examples/logging/anthropic_async_tools_stream.py create mode 100644 examples/logging/anthropic_async_tools_stream_handler.py create mode 100644 examples/logging/anthropic_messages_not_given.py create mode 100644 examples/logging/magentic_async_chat_prompt.py diff --git a/examples/logging/anthropic_async_messages_stream.py b/examples/logging/anthropic_async_messages_stream.py new file mode 100644 index 00000000..77fdc6c1 --- /dev/null +++ b/examples/logging/anthropic_async_messages_stream.py @@ -0,0 +1,35 @@ +import asyncio + +import anthropic + +from log10.load import log10 + + +log10(anthropic) + +client = anthropic.AsyncAnthropic() + + +async def main() -> None: + async with client.messages.stream( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Say hello there!", + } + ], + model="claude-3-haiku-20240307", + ) as stream: + async for text in stream.text_stream: + print(text, end="", flush=True) + print() + + # you can still get the accumulated final message outside of + # the context manager, as long as the entire stream was consumed + # inside of the context manager + accumulated = await stream.get_final_message() + print("accumulated message: ", accumulated.to_json()) + + +asyncio.run(main()) diff --git a/examples/logging/anthropic_async_messages_stream_handler.py b/examples/logging/anthropic_async_messages_stream_handler.py new file mode 100644 index 00000000..382bbb6b --- /dev/null +++ b/examples/logging/anthropic_async_messages_stream_handler.py @@ -0,0 +1,38 @@ +import asyncio + +import anthropic +from anthropic import AsyncAnthropic, AsyncMessageStream +from anthropic.types import MessageStreamEvent +from typing_extensions import override + +from log10.load import log10 + + +log10(anthropic) + +client = AsyncAnthropic() + + +class MyStream(AsyncMessageStream): + @override + async def on_stream_event(self, event: MessageStreamEvent) -> None: + print("on_event fired with:", event) + + +async def main() -> None: + async with client.messages.stream( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Say hello there!", + } + ], + model="claude-3-haiku-20240307", + event_handler=MyStream, + ) as stream: + accumulated = await stream.get_final_message() + print("accumulated message: ", accumulated.to_json()) + + +asyncio.run(main()) diff --git a/examples/logging/anthropic_async_tools_stream.py b/examples/logging/anthropic_async_tools_stream.py new file mode 100644 index 00000000..f0eaff2d --- /dev/null +++ b/examples/logging/anthropic_async_tools_stream.py @@ -0,0 +1,47 @@ +import asyncio + +import anthropic +from anthropic import AsyncAnthropic + +from log10.load import log10 + + +log10(anthropic) + +client = AsyncAnthropic() + + +async def run_conversation(): + tools = [ + { + "name": "get_weather", + "description": "Get the weather in a given location", + "input_schema": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"], + "description": 'The unit of temperature, either "celsius" or "fahrenheit"', + }, + }, + "required": ["location"], + }, + } + ] + async with client.beta.tools.messages.stream( + model="claude-3-haiku-20240307", + tools=tools, + messages=[ + { + "role": "user", + "content": "What's the weather like in San Francisco?", + } + ], + max_tokens=1024, + ) as stream: + await stream.until_done() + + +asyncio.run(run_conversation()) diff --git a/examples/logging/anthropic_async_tools_stream_handler.py b/examples/logging/anthropic_async_tools_stream_handler.py new file mode 100644 index 00000000..cf8826eb --- /dev/null +++ b/examples/logging/anthropic_async_tools_stream_handler.py @@ -0,0 +1,55 @@ +import asyncio + +import anthropic +from anthropic import AsyncAnthropic +from anthropic.lib.streaming.beta import AsyncToolsBetaMessageStream +from typing_extensions import override + +from log10.load import log10 + + +log10(anthropic) + + +client = AsyncAnthropic() + + +class MyHandler(AsyncToolsBetaMessageStream): + @override + async def on_input_json(self, delta: str, snapshot: object) -> None: + print(f"delta: {repr(delta)}") + print(f"snapshot: {snapshot}") + print() + + +async def main() -> None: + async with client.beta.tools.messages.stream( + max_tokens=1024, + model="claude-3-haiku-20240307", + tools=[ + { + "name": "get_weather", + "description": "Get the weather at a specific location.", + "input_schema": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"], + "description": "Unit for the output", + }, + }, + "required": ["location"], + }, + } + ], + messages=[{"role": "user", "content": "What is the weather in SF?"}], + event_handler=MyHandler, + ) as stream: + await stream.until_done() + + print() + + +asyncio.run(main()) diff --git a/examples/logging/anthropic_messages_not_given.py b/examples/logging/anthropic_messages_not_given.py new file mode 100644 index 00000000..1be9d8ef --- /dev/null +++ b/examples/logging/anthropic_messages_not_given.py @@ -0,0 +1,17 @@ +from log10.load import Anthropic + + +client = Anthropic() + +completion = client.messages.create( + model="claude-instant-1.2", + messages=[ + { + "role": "user", + "content": "tell a short joke.", + }, + ], + max_tokens=1000, +) + +print(completion.content[0].text) diff --git a/examples/logging/magentic_async_chat_prompt.py b/examples/logging/magentic_async_chat_prompt.py new file mode 100644 index 00000000..06b3c107 --- /dev/null +++ b/examples/logging/magentic_async_chat_prompt.py @@ -0,0 +1,23 @@ +import asyncio + +import anthropic +from magentic import UserMessage, chatprompt +from magentic.chat_model.anthropic_chat_model import AnthropicChatModel + +from log10.load import log10 + + +log10(anthropic) + + +async def main(topic: str) -> str: + @chatprompt( + UserMessage(f"Tell me a joke about {topic}"), + model=AnthropicChatModel("claude-3-opus-20240229"), + ) + async def tell_joke(topic: str) -> str: ... + + print(await tell_joke(topic)) + + +asyncio.run(main("cats")) diff --git a/log10/anthropic.py b/log10/anthropic.py index 630a9fca..24ca63b6 100644 --- a/log10/anthropic.py +++ b/log10/anthropic.py @@ -135,11 +135,6 @@ def create_tokens_usage(prompt: str, completion: str): def prepare_response( response: anthropic.types.Completion | anthropic.types.Message, input_prompt: str = "" ) -> dict: - print(type(response)) - - if isinstance(response, dict): - response = anthropic.types.Message(**response) - if not hasattr(response, "stop_reason"): return None