mistralai / client-python

Python client library for Mistral AI platform
Apache License 2.0
477 stars 103 forks source link

tool_choice parameter does not match OpenAI API #78

Closed jackmpcollins closed 3 months ago

jackmpcollins commented 7 months ago

The the Mistral docs Azure AI page links to a notebook that shows that Mistral on Azure is compatible with the openai python package https://github.com/Azure/azureml-examples/blob/main/sdk/python/foundation-models/mistral/openaisdk.ipynb Do you also intend for the Mistral-hosted API to be compatible with the openai python package?

If so, the tool_choice parameter in the API and this package needs to be updated to support choosing a specific tool. According to the openai docs the allowed options for tool_choice are "none", "auto", or {"type": "function", "function": {"name": "my_function"}}.

tool_choice string or object

Optional Controls which (if any) function is called by the model. none means the model will not call a function and instead generates a message. auto means the model can pick between generating a message or calling a function. Specifying a particular function via {"type": "function", "function": {"name": "my_function"}} forces the model to call that function.

none is the default when no functions are present. auto is the default if functions are present.

https://platform.openai.com/docs/api-reference/chat/create#chat-create-tool_choice

It appears that the Mistral API does not support the {"type": "function", "function": {"name": "my_function"}} format, returning error

UnprocessableEntityError: Error code: 422 - {'object': 'error', 'message': {'detail': [{'type': 'string_type', 'loc': ['body', 'tool_choice'], 'msg': 'Input should be a valid string', 'input': {'type': 'function', 'function': {'name': 'return_list_of_str'}}, 'url': 'https://errors.pydantic.dev/2.6/v/string_type'}]}, 'type': 'invalid_request_error', 'param': None, 'code': None}

This package shows just the string options (with additional "any")

https://github.com/mistralai/client-python/blob/80c7951bad83338641d5e89684f841ce1cac938f/src/mistralai/models/chat_completion.py#L34-L39

While the openai python package includes the option for a specific tool

https://github.com/openai/openai-python/blob/5cfb125acce0e8304d12bdd39b405071021db658/src/openai/types/chat/chat_completion_tool_choice_option_param.py#L12

Lack of support for tool_choice in the Mistral API makes it incompatible with the openai python package, which means tools like https://github.com/jackmpcollins/magentic have to add custom support for it.

mnicstruwig commented 7 months ago

Gently bumping this, since there hasn't been a response from the maintainers, and this is a blocker for us in adopting / evaluating Mistral.

sophiamyang commented 3 months ago

Thanks for the suggestions. Support for {"type": "function", "function": {"name": "my_function"}} is coming soon. Stay tuned!