Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis committed Dec 16, 2024
1 parent 13db743 commit 7fa23af
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions libs/core/tests/unit_tests/utils/test_function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
from langchain_core.tools import BaseTool, StructuredTool, Tool, tool
from langchain_core.utils.function_calling import (
_convert_typed_dict_to_openai_function,
_parse_google_docstring,
convert_to_openai_function,
tool_example_to_messages,
_parse_google_docstring
)


@pytest.fixture()
def pydantic() -> type[BaseModel]:
class dummy_function(BaseModel): # noqa: N801
Expand Down Expand Up @@ -996,6 +997,7 @@ def empty_tool() -> str:
"strict": True,
}


@pytest.fixture()
def google_docstring_example() -> str:
return """This function demonstrates a Google-style docstring.
Expand All @@ -1009,21 +1011,30 @@ def google_docstring_example() -> str:
bool: Indicates if the operation was successful.
"""


@pytest.fixture()
def invalid_google_docstring() -> str:
return """This function has a malformed docstring.
Arguments:
param1 (int) The first parameter without a colon.
"""


def test_parse_google_docstring_simple(google_docstring_example):
description, args = _parse_google_docstring(google_docstring_example, ["param1", "param2"])
description, args = _parse_google_docstring(
google_docstring_example, ["param1", "param2"]
)
assert description == "This function demonstrates a Google-style docstring."
assert args == {
"param1": "(int): The first parameter. It has additional explanation in multiple lines.",
"param1": (
"(int): The first parameter. It has additional explanation "
"in multiple lines."
),
"param2": "(str): The second parameter.",
}


def test_parse_google_docstring_missing_args_section():
docstring = """This function lacks an arguments section.
Expand All @@ -1034,9 +1045,13 @@ def test_parse_google_docstring_missing_args_section():
assert description == "This function lacks an arguments section."
assert args == {}


def test_parse_google_docstring_invalid_format(invalid_google_docstring):
with pytest.raises(ValueError, match="Found invalid Google-Style docstring."):
_parse_google_docstring(invalid_google_docstring, ["param1"], error_on_invalid_docstring=True)
_parse_google_docstring(
invalid_google_docstring, ["param1"], error_on_invalid_docstring=True
)


def test_parse_google_docstring_extra_parameters():
docstring = """This function has only one argument.
Expand All @@ -1050,6 +1065,7 @@ def test_parse_google_docstring_extra_parameters():
"param1": "(int): The first parameter.",
}


def test_parse_google_docstring_multiline_and_empty_lines():
docstring = """This function has multiline argument descriptions.
Expand All @@ -1069,6 +1085,7 @@ def test_parse_google_docstring_multiline_and_empty_lines():
"param2": "(str): The second parameter.",
}


def test_parse_google_docstring_no_description():
docstring = """
Args:
Expand All @@ -1082,6 +1099,7 @@ def test_parse_google_docstring_no_description():
"param2": "(str): The second parameter.",
}


def test_parse_google_docstring_multiple_paragraphs():
docstring = """This function does something.
Expand All @@ -1094,10 +1112,12 @@ def test_parse_google_docstring_multiple_paragraphs():
Returns:
bool: Indicates if the operation was successful."""

description, args = _parse_google_docstring(docstring, ['param1', 'param2'], error_on_invalid_docstring=True)

assert description == 'This function does something. It has multiple paragraphs that should be handled correctly.'
assert args == {
'param1': 'The first parameter.',
'param2': 'The second parameter.'
}
description, args = _parse_google_docstring(
docstring, ["param1", "param2"], error_on_invalid_docstring=True
)

assert description == (
"This function does something. "
"It has multiple paragraphs that should be handled correctly."
)
assert args == {"param1": "The first parameter.", "param2": "The second parameter."}

0 comments on commit 7fa23af

Please sign in to comment.