Skip to content

Commit

Permalink
feat: add pydantic schema support for few_shot_examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Rai220 committed Nov 12, 2024
1 parent 032dcd5 commit 4cddb51
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions libs/gigachat/langchain_gigachat/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ def convert_pydantic_to_gigachat_function(
"Incorrect function or tool description. Description is required."
)

if few_shot_examples is None and hasattr(model, 'few_shot_examples'):
few_shot_examples_attr = getattr(model, 'few_shot_examples')
if few_shot_examples is None and hasattr(model, "few_shot_examples"):
few_shot_examples_attr = getattr(model, "few_shot_examples")
if inspect.isfunction(few_shot_examples_attr):
few_shot_examples = few_shot_examples_attr()

Expand Down
43 changes: 42 additions & 1 deletion libs/gigachat/tests/unit_tests/test_gigachat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
_convert_dict_to_message,
_convert_message_to_dict,
)
from langchain_gigachat.tools.giga_tool import giga_tool
from langchain_gigachat.tools.giga_tool import FewShotExamples, giga_tool
from tests.unit_tests.stubs import FakeAsyncCallbackHandler, FakeCallbackHandler


Expand Down Expand Up @@ -334,3 +334,44 @@ def test_gigachat_bind_gigatool() -> None:
"required": ["status", "message"],
"type": "object",
}


class SomeResult(BaseModel):
"""My desc"""

@staticmethod
def few_shot_examples() -> FewShotExamples:
return [
{
"request": "request example",
"params": {"is_valid": 1, "description": "correct message"},
}
]

value: int = Field(description="some value")
description: str = Field(description="some descriptin")


def test_structured_output() -> None:
llm = GigaChat().with_structured_output(SomeResult)
assert llm.steps[0].kwargs["function_call"] == {"name": "SomeResult"} # type: ignore[attr-defined]
assert llm.steps[0].kwargs["tools"][0]["function"] == { # type: ignore[attr-defined]
"name": "SomeResult",
"description": "My desc",
"parameters": {
"description": "My desc",
"properties": {
"value": {"description": "some value", "type": "integer"},
"description": {"description": "some descriptin", "type": "string"},
},
"required": ["value", "description"],
"type": "object",
},
"return_parameters": None,
"few_shot_examples": [
{
"request": "request example",
"params": {"is_valid": 1, "description": "correct message"},
}
],
}

0 comments on commit 4cddb51

Please sign in to comment.