Skip to content

Commit

Permalink
feat: support for customizing “tool_choice” in OpenAI
Browse files Browse the repository at this point in the history
The ModelSettings class uses a TypedDict as its underlying type, providing
flexibility to add options beyond the predefined attributes.

Before generating chat completions, the code now checks if tool_choice is set
in ModelSettings and applies it accordingly.

Fixes: #224
Signed-off-by: Sébastien Han <seb@redhat.com>
  • Loading branch information
leseb committed Jan 27, 2025
1 parent 368b2e1 commit 1016c92
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pydantic_ai_slim/pydantic_ai/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ async def _completions_create(
self, messages: list[ModelMessage], stream: bool, model_settings: OpenAIModelSettings
) -> chat.ChatCompletion | AsyncStream[ChatCompletionChunk]:
# standalone function to make it easier to override
if not self.tools:
if tool_choice := model_settings.get('tool_choice'):
pass
elif not self.tools:
tool_choice: Literal['none', 'required', 'auto'] | None = None
elif not self.allow_text_result:
tool_choice = 'required'
Expand Down

0 comments on commit 1016c92

Please sign in to comment.