chat: flatten namespace tools to ns__function for Chat Completions
This commit is contained in:
@@ -834,6 +834,26 @@ class Tool(BaseModel):
|
||||
return self
|
||||
|
||||
|
||||
class ChatCompletionNamespaceTool(BaseModel):
|
||||
"""A Responses-style namespace grouping sent to the Chat Completions API.
|
||||
|
||||
Chat Completions has no namespace field in its tool-call shape, so the
|
||||
members are flattened to ``<namespace>__<function>`` before rendering; the
|
||||
model calls that flat name and the response reports it back unchanged.
|
||||
"""
|
||||
|
||||
type: Literal["namespace"]
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
tools: List[Tool]
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_members(self) -> "ChatCompletionNamespaceTool":
|
||||
if not self.tools:
|
||||
raise ValueError("namespace tools must list at least one function tool")
|
||||
return self
|
||||
|
||||
|
||||
# Tool is defined after the message params that reference it, so the forward
|
||||
# reference has to be resolved explicitly.
|
||||
ChatCompletionMessageGenericParam.model_rebuild()
|
||||
@@ -921,7 +941,9 @@ class ChatCompletionRequest(PDRoutingFields):
|
||||
temperature: Optional[float] = None
|
||||
top_p: Optional[float] = None
|
||||
user: Optional[str] = None
|
||||
tools: Optional[List[Tool]] = Field(default=None, examples=[None])
|
||||
tools: Optional[List[Union[Tool, ChatCompletionNamespaceTool]]] = Field(
|
||||
default=None, examples=[None]
|
||||
)
|
||||
# Set by /v1/responses when the request has namespace tools: the chat template
|
||||
# renders these, while ``tools`` (flattened to ``<namespace>.<function>``) still
|
||||
# drives tool-call parsing.
|
||||
@@ -2123,6 +2145,39 @@ class ResponsesRequest(PDRoutingFields):
|
||||
return choice
|
||||
return "auto"
|
||||
|
||||
@model_validator(mode="after")
|
||||
def flatten_namespace_tools(self) -> "ChatCompletionRequest":
|
||||
"""Render namespace groups as flat ``ns__function`` chat tools."""
|
||||
if not self.tools:
|
||||
return self
|
||||
flattened: List[Tool] = []
|
||||
for tool in self.tools:
|
||||
if isinstance(tool, ChatCompletionNamespaceTool):
|
||||
for member in tool.tools:
|
||||
name = f"{tool.name}__{member.function.name}"
|
||||
description = member.function.description
|
||||
if tool.description:
|
||||
description = (
|
||||
tool.description + chr(10) + description
|
||||
if description
|
||||
else tool.description
|
||||
)
|
||||
flattened.append(
|
||||
Tool(
|
||||
type="function",
|
||||
function=Function(
|
||||
name=name,
|
||||
description=description,
|
||||
parameters=member.function.parameters,
|
||||
strict=member.function.strict,
|
||||
),
|
||||
)
|
||||
)
|
||||
else:
|
||||
flattened.append(tool)
|
||||
self.tools = flattened
|
||||
return self
|
||||
|
||||
def to_sampling_params(
|
||||
self,
|
||||
default_max_tokens: Optional[int] = None,
|
||||
|
||||
Reference in New Issue
Block a user