fix(chat): move namespace-tool flattening validator into ChatCompletionRequest
This commit is contained in:
@@ -1184,6 +1184,39 @@ class ChatCompletionRequest(PDRoutingFields):
|
||||
|
||||
return values
|
||||
|
||||
@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,
|
||||
stop: List[str],
|
||||
@@ -2145,39 +2178,6 @@ 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