From 0ecf04596a69069015b9fa64b90a2337b06655cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Wed, 18 Dec 2024 15:05:25 +0100 Subject: [PATCH] fix: migrated to latest anthropic version v0.42 (prompt caching now stable) (#352) --- gptme/llm/llm_anthropic.py | 43 +++++++++++++++----------------------- poetry.lock | 10 ++++----- pyproject.toml | 2 +- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/gptme/llm/llm_anthropic.py b/gptme/llm/llm_anthropic.py index 3901c10c..aca2ea10 100644 --- a/gptme/llm/llm_anthropic.py +++ b/gptme/llm/llm_anthropic.py @@ -17,9 +17,7 @@ if TYPE_CHECKING: # noreorder import anthropic.types # fmt: skip - import anthropic.types.beta.prompt_caching # fmt: skip from anthropic import Anthropic # fmt: skip - from anthropic.types.beta.prompt_caching import PromptCachingBetaTextBlockParam logger = logging.getLogger(__name__) @@ -55,7 +53,7 @@ def chat(messages: list[Message], model: str, tools: list[ToolSpec] | None) -> s messages, tools ) - response = _anthropic.beta.prompt_caching.messages.create( + response = _anthropic.messages.create( model=model, messages=messages_dicts, system=system_messages, @@ -75,7 +73,6 @@ def stream( messages: list[Message], model: str, tools: list[ToolSpec] | None ) -> Generator[str, None, None]: import anthropic.types # fmt: skip - import anthropic.types.beta.prompt_caching # fmt: skip from anthropic import NOT_GIVEN # fmt: skip assert _anthropic, "LLM not initialized" @@ -83,7 +80,7 @@ def stream( messages, tools ) - with _anthropic.beta.prompt_caching.messages.stream( + with _anthropic.messages.stream( model=model, messages=messages_dicts, system=system_messages, @@ -121,7 +118,7 @@ def stream( pass case "message_start": chunk = cast( - anthropic.types.beta.prompt_caching.RawPromptCachingBetaMessageStartEvent, + anthropic.types.MessageStartEvent, chunk, ) logger.debug(chunk.message.usage) @@ -200,7 +197,7 @@ def _process_file(message_dict: dict) -> dict: def _transform_system_messages( messages: list[Message], -) -> tuple[list[Message], list["PromptCachingBetaTextBlockParam"]]: +) -> tuple[list[Message], list["anthropic.types.TextBlockParam"]]: """Transform system messages into Anthropic's expected format. This function: @@ -215,7 +212,7 @@ def _transform_system_messages( 2. Earlier messages in multi-turn conversations Returns: - tuple[list[Message], list[PromptCachingBetaTextBlockParam]]: Transformed messages and system messages + tuple[list[Message], list[TextBlockParam]]: Transformed messages and system messages """ assert messages[0].role == "system" system_prompt = messages[0].content @@ -244,7 +241,7 @@ def _transform_system_messages( else: messages_new.append(message) messages = messages_new - system_messages: list[PromptCachingBetaTextBlockParam] = [ + system_messages: list[anthropic.types.TextBlockParam] = [ { "type": "text", "text": system_prompt, @@ -273,7 +270,7 @@ def parameters2dict(parameters: list[Parameter]) -> dict[str, object]: def _spec2tool( spec: ToolSpec, -) -> "anthropic.types.beta.prompt_caching.PromptCachingBetaToolParam": +) -> "anthropic.types.ToolParam": name = spec.name if spec.block_types: name = spec.block_types[0] @@ -289,9 +286,9 @@ def _spec2tool( def _prepare_messages_for_api( messages: list[Message], tools: list[ToolSpec] | None ) -> tuple[ - list["anthropic.types.beta.prompt_caching.PromptCachingBetaMessageParam"], - list["anthropic.types.beta.prompt_caching.PromptCachingBetaTextBlockParam"], - list["anthropic.types.beta.prompt_caching.PromptCachingBetaToolParam"] | None, + list["anthropic.types.MessageParam"], + list["anthropic.types.TextBlockParam"], + list["anthropic.types.ToolParam"] | None, ]: """Prepare messages for the Anthropic API. @@ -312,13 +309,7 @@ def _prepare_messages_for_api( - Tool dictionaries (if tools provided) """ # noreorder - from anthropic.types.beta.prompt_caching import ( # fmt: skip - PromptCachingBetaImageBlockParam, - PromptCachingBetaMessageParam, - PromptCachingBetaTextBlockParam, - PromptCachingBetaToolResultBlockParam, - PromptCachingBetaToolUseBlockParam, - ) + import anthropic.types # fmt: skip # Transform system messages messages, system_messages = _transform_system_messages(messages) @@ -327,13 +318,13 @@ def _prepare_messages_for_api( messages_dicts = _handle_files(msgs2dicts(messages)) # Apply cache control to optimize performance - messages_dicts_new: list[PromptCachingBetaMessageParam] = [] + messages_dicts_new: list[anthropic.types.MessageParam] = [] for msg in messages_dicts: content_parts: list[ - PromptCachingBetaTextBlockParam - | PromptCachingBetaImageBlockParam - | PromptCachingBetaToolUseBlockParam - | PromptCachingBetaToolResultBlockParam + anthropic.types.TextBlockParam + | anthropic.types.ImageBlockParam + | anthropic.types.ToolUseBlockParam + | anthropic.types.ToolResultBlockParam ] = [] raw_content = ( msg["content"] @@ -359,7 +350,7 @@ def _prepare_messages_for_api( # https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#continuing-a-multi-turn-conversation for msgp in [msg for msg in messages_dicts_new if msg["role"] == "user"][-2:]: assert isinstance(msgp["content"], list) - msgp["content"][-1]["cache_control"] = {"type": "ephemeral"} + msgp["content"][-1]["cache_control"] = {"type": "ephemeral"} # type: ignore # Prepare tools tools_dict = [_spec2tool(tool) for tool in tools] if tools else None diff --git a/poetry.lock b/poetry.lock index 3a449e08..bcca3465 100644 --- a/poetry.lock +++ b/poetry.lock @@ -42,13 +42,13 @@ files = [ [[package]] name = "anthropic" -version = "0.40.0" +version = "0.42.0" description = "The official Python library for the anthropic API" optional = false python-versions = ">=3.8" files = [ - {file = "anthropic-0.40.0-py3-none-any.whl", hash = "sha256:442028ae8790ff9e3b6f8912043918755af1230d193904ae2ef78cc22995280c"}, - {file = "anthropic-0.40.0.tar.gz", hash = "sha256:3efeca6d9e97813f93ed34322c6c7ea2279bf0824cd0aa71b59ce222665e2b87"}, + {file = "anthropic-0.42.0-py3-none-any.whl", hash = "sha256:46775f65b723c078a2ac9e9de44a46db5c6a4fabeacfd165e5ea78e6817f4eff"}, + {file = "anthropic-0.42.0.tar.gz", hash = "sha256:bf8b0ed8c8cb2c2118038f29c58099d2f99f7847296cafdaa853910bfff4edf4"}, ] [package.dependencies] @@ -58,7 +58,7 @@ httpx = ">=0.23.0,<1" jiter = ">=0.4.0,<1" pydantic = ">=1.9.0,<3" sniffio = "*" -typing-extensions = ">=4.7,<5" +typing-extensions = ">=4.10,<5" [package.extras] bedrock = ["boto3 (>=1.28.57)", "botocore (>=1.31.57)"] @@ -3384,4 +3384,4 @@ youtube = ["youtube_transcript_api"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "e6c684f2ed44722a137e5ea2bda0a8bbd89593cb99954ec16b19ca905a540a38" +content-hash = "08f5521530bdc833366e98513d0dbb83d25ed355811e5d2411fe8d097936775d" diff --git a/pyproject.toml b/pyproject.toml index 1ebc220b..bc10883f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,7 +59,7 @@ json-repair = "^0.30.2" # providers openai = "^1.0" -anthropic = "^0.40" +anthropic = "^0.42" # tools ipython = "^8.17.2"