chore: automatic commit 2025-04-30 12:48
This commit is contained in:
66
venv/lib/python3.11/site-packages/openai/lib/_tools.py
Normal file
66
venv/lib/python3.11/site-packages/openai/lib/_tools.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, cast
|
||||
|
||||
import pydantic
|
||||
|
||||
from ._pydantic import to_strict_json_schema
|
||||
from ..types.chat import ChatCompletionToolParam
|
||||
from ..types.shared_params import FunctionDefinition
|
||||
from ..types.responses.function_tool_param import FunctionToolParam as ResponsesFunctionToolParam
|
||||
|
||||
|
||||
class PydanticFunctionTool(Dict[str, Any]):
|
||||
"""Dictionary wrapper so we can pass the given base model
|
||||
throughout the entire request stack without having to special
|
||||
case it.
|
||||
"""
|
||||
|
||||
model: type[pydantic.BaseModel]
|
||||
|
||||
def __init__(self, defn: FunctionDefinition, model: type[pydantic.BaseModel]) -> None:
|
||||
super().__init__(defn)
|
||||
self.model = model
|
||||
|
||||
def cast(self) -> FunctionDefinition:
|
||||
return cast(FunctionDefinition, self)
|
||||
|
||||
|
||||
class ResponsesPydanticFunctionTool(Dict[str, Any]):
|
||||
model: type[pydantic.BaseModel]
|
||||
|
||||
def __init__(self, tool: ResponsesFunctionToolParam, model: type[pydantic.BaseModel]) -> None:
|
||||
super().__init__(tool)
|
||||
self.model = model
|
||||
|
||||
def cast(self) -> ResponsesFunctionToolParam:
|
||||
return cast(ResponsesFunctionToolParam, self)
|
||||
|
||||
|
||||
def pydantic_function_tool(
|
||||
model: type[pydantic.BaseModel],
|
||||
*,
|
||||
name: str | None = None, # inferred from class name by default
|
||||
description: str | None = None, # inferred from class docstring by default
|
||||
) -> ChatCompletionToolParam:
|
||||
if description is None:
|
||||
# note: we intentionally don't use `.getdoc()` to avoid
|
||||
# including pydantic's docstrings
|
||||
description = model.__doc__
|
||||
|
||||
function = PydanticFunctionTool(
|
||||
{
|
||||
"name": name or model.__name__,
|
||||
"strict": True,
|
||||
"parameters": to_strict_json_schema(model),
|
||||
},
|
||||
model,
|
||||
).cast()
|
||||
|
||||
if description is not None:
|
||||
function["description"] = description
|
||||
|
||||
return {
|
||||
"type": "function",
|
||||
"function": function,
|
||||
}
|
||||
Reference in New Issue
Block a user