Skip to content

Commit

Permalink
Support few-shots from class description (#10)
Browse files Browse the repository at this point in the history
* docs: use monthly downloads counter
* few_shot_examples можно задавать через pydantic схему
* feat: add pydantic schema support for few_shot_examples

---------

Co-authored-by: Dmitry Labazkin <[email protected]>
Co-authored-by: NIK-TIGER-BILL <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2024
1 parent 41facb2 commit ffb087c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libs/gigachat/langchain_gigachat/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ 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 inspect.isfunction(few_shot_examples_attr):
few_shot_examples = few_shot_examples_attr()

return GigaFunctionDescription(
name=name or title,
description=description,
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 ffb087c

Please sign in to comment.