Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openai_function_calling/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class FunctionDict(TypedDict):
name: str
description: str
parameters: ParametersDict
strict: NotRequired[bool]


class Function:
Expand All @@ -37,6 +38,7 @@ def __init__(
description: str,
parameters: list[Parameter] | None = None,
required_parameters: list[str] | None = None,
strict: bool | None = None,
) -> None:
"""Create a new function instance.

Expand All @@ -46,12 +48,14 @@ def __init__(
parameters: A list of parameters.
required_parameters: A list of parameter names that are required to run the\
function.
strict: If the function should enforce strict parameters.

"""
self.name: str = name
self.description: str = description
self.parameters: list[Parameter] = parameters or []
self.required_parameters: list[str] = required_parameters or []
self.strict: bool | None = strict

self.validate()

Expand Down Expand Up @@ -110,6 +114,9 @@ def to_json_schema(self) -> FunctionDict:
},
}

if self.strict is not None:
output_dict["strict"] = self.strict

if self.required_parameters is None or len(self.required_parameters) == 0:
return output_dict

Expand Down
32 changes: 32 additions & 0 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,35 @@ def test_merge_with_no_parameters_does_not_add_any() -> None:
get_current_weather_function.merge(get_tomorrows_weather_function)

assert len(get_current_weather_function.parameters) == 0


def test_function_with_strict_true_includes_strict_in_output() -> None:
func = Function(
name="example_function",
description="An example function",
strict=True,
)
func_dict: FunctionDict = func.to_json_schema()

assert func_dict.get("strict") is True


def test_function_with_strict_false_includes_strict_in_output() -> None:
func = Function(
name="example_function",
description="An example function",
strict=False,
)
func_dict: FunctionDict = func.to_json_schema()

assert func_dict.get("strict") is False


def test_function_without_strict_excludes_strict_in_output() -> None:
func = Function(
name="example_function",
description="An example function",
)
func_dict: FunctionDict = func.to_json_schema()

assert "strict" not in func_dict