jackmpcollins / magentic

Seamlessly integrate LLMs as Python functions
https://magentic.dev/
MIT License
1.98k stars 96 forks source link

Tool use not working for `AnthropicChatModel` #219

Closed mnicstruwig closed 4 months ago

mnicstruwig commented 4 months ago

Hi @jackmpcollins,

It looks like tool use is currently broken for the AnthropicChatModel:

from magentic import prompt, ParallelFunctionCall
from magentic.chat_model.anthropic_chat_model import AnthropicChatModel

def get_weather(city: str) -> str:
    return f"The weather in {city} is 20°C."

@prompt(
    "What is the weather in Cape Town?",
    functions=[get_weather],
    model=AnthropicChatModel(
        model="claude-3-opus",
        temperature=0.2,
    )
)
def _llm() -> str | ParallelFunctionCall: ...

_llm()

Produces the following error:

File ~/miniforge3/envs/copilot-opus/lib/python3.11/site-packages/anthropic/_base_client.py:921, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
    912 def request(
    913     self,
    914     cast_to: Type[ResponseT],
   (...)
    919     stream_cls: type[_StreamT] | None = None,
    920 ) -> ResponseT | _StreamT:
--> 921     return self._request(
    922         cast_to=cast_to,
    923         options=options,
    924         stream=stream,
    925         stream_cls=stream_cls,
    926         remaining_retries=remaining_retries,
    927     )

File ~/miniforge3/envs/copilot-opus/lib/python3.11/site-packages/anthropic/_base_client.py:1019, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
   1016         err.response.read()
   1018     log.debug("Re-raising status error")
-> 1019     raise self._make_status_error_from_response(err.response) from None
   1021 return self._process_response(
   1022     cast_to=cast_to,
   1023     options=options,
   (...)
   1026     stream_cls=stream_cls,
   1027 )

BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': "'claude-3-opus' does not support tool use via API fields."}}

I'm wondering if this breakage doesn't have to do with the public beta launch of tool use for Claude 3 models on 15 May 2024?

That being said, I see magentic uses the same anthropic library call used in the example above. So I'm not quite sure what the issue could be.

I'm on the latest version of anthropic, 0.26.1 and magentic at 0.24.0.

mnicstruwig commented 4 months ago

Closing. There was a typo in the model specification (I didn't use the fully-qualified model name: claude-3-opus-20240229). Bizarrely, the Anthropic API throws the tool use error first rather than a model error!