Skip to content

Commit

Permalink
Use new parallel_tool_calls arg with OpenAI API (#267)
Browse files Browse the repository at this point in the history
* Add paralle_tool_calls to openai and mistral ChatModels

* Fix type hints. Tidy logic. Check for Async

* Fix: do not provide parallel_tool_calls arg when no tools
  • Loading branch information
jackmpcollins authored Jul 15, 2024
1 parent d41e77a commit c6d86a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/magentic/chat_model/mistral_chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def _get_tool_choice( # type: ignore[override]
"""
return openai.NOT_GIVEN if allow_string_output else _MistralToolChoice.ANY.value

def _get_parallel_tool_calls(
self, *, tools_specified: bool, output_types: Iterable[type]
) -> bool | openai.NotGiven:
return openai.NOT_GIVEN


R = TypeVar("R")

Expand Down
19 changes: 19 additions & 0 deletions src/magentic/chat_model/openai_chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,19 @@ def _get_tool_choice(
return tool_schemas[0].as_tool_choice()
return "required"

def _get_parallel_tool_calls(
self, *, tools_specified: bool, output_types: Iterable[type]
) -> bool | openai.NotGiven:
if not tools_specified: # Enforced by OpenAI API
return openai.NOT_GIVEN
if self.api_type == "azure":
return openai.NOT_GIVEN
if is_any_origin_subclass(output_types, ParallelFunctionCall):
return openai.NOT_GIVEN
if is_any_origin_subclass(output_types, AsyncParallelFunctionCall):
return openai.NOT_GIVEN
return False

@overload
def complete(
self,
Expand Down Expand Up @@ -496,6 +509,9 @@ def complete(
tool_choice=self._get_tool_choice(
tool_schemas=tool_schemas, allow_string_output=allow_string_output
),
parallel_tool_calls=self._get_parallel_tool_calls(
tools_specified=bool(tool_schemas), output_types=output_types
),
)
usage_ref, response = _create_usage_ref(response)

Expand Down Expand Up @@ -607,6 +623,9 @@ async def acomplete(
tool_choice=self._get_tool_choice(
tool_schemas=tool_schemas, allow_string_output=allow_string_output
),
parallel_tool_calls=self._get_parallel_tool_calls(
tools_specified=bool(tool_schemas), output_types=output_types
),
)
usage_ref, response = _create_usage_ref_async(response)

Expand Down

0 comments on commit c6d86a9

Please sign in to comment.