forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Фикс функций и распределение кода по папкам (#233)
* - Перенес парсеры в папку output_parsers - Перенес преобразователи функций в utils/function_calling.py - Изменил парсеры, добавив поддержку pydantic v2 методов - Пофиксил преобразование тулов, сделав кастомный генератор JSON схемы `pydantic_generator.py`, также добавил проверку на тулы с Union[X,Y] -> теперь кидается exception при встрече такого типа, так как гигачат такое не поддерживает (убрал anyOf с Optional полями, сделал их добавления в required) - Добавил кастомные классы GigaBaseTool и метот giga_tool, позволяющий добавлять return_schema и few_shot_examples - Добавил тесты на преобразования стандартных тулов (нужно будет сделать тесты на giga_tool) - Пофиксил метод with_structured_output. Возможно нужно будет убрать json_mode * Пофиксил линтер * Пофиксил линтер
- Loading branch information
Showing
10 changed files
with
894 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
113 changes: 113 additions & 0 deletions
113
libs/langchain_gigachat/langchain_gigachat/output_parsers/gigachat_functions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import copy | ||
from types import GenericAlias | ||
from typing import Any, Dict, List, Type, Union | ||
|
||
from langchain_core.exceptions import OutputParserException | ||
from langchain_core.output_parsers import BaseGenerationOutputParser | ||
from langchain_core.outputs import ChatGeneration, Generation | ||
from pydantic import BaseModel, model_validator | ||
|
||
|
||
class OutputFunctionsParser(BaseGenerationOutputParser[Any]): | ||
"""Parse an output that is one of sets of values.""" | ||
|
||
args_only: bool = True | ||
"""Whether to only return the arguments to the function call.""" | ||
|
||
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any: | ||
generation = result[0] | ||
if not isinstance(generation, ChatGeneration): | ||
raise OutputParserException( | ||
"This output parser can only be used with a chat generation." | ||
) | ||
message = generation.message | ||
try: | ||
func_call = copy.deepcopy(message.additional_kwargs["function_call"]) | ||
except KeyError as exc: | ||
raise OutputParserException( | ||
f"Could not parse function call: {exc}" | ||
) from exc | ||
|
||
if self.args_only: | ||
return func_call["arguments"] | ||
return func_call | ||
|
||
|
||
class PydanticOutputFunctionsParser(OutputFunctionsParser): | ||
"""Parse an output as a pydantic object.""" | ||
|
||
pydantic_schema: Union[Type[BaseModel], Dict[str, Type[BaseModel]]] | ||
"""The pydantic schema to parse the output with. | ||
If multiple schemas are provided, then the function name will be used to | ||
determine which schema to use. | ||
""" | ||
|
||
@model_validator(mode="before") | ||
@classmethod | ||
def validate_schema(cls, values: dict) -> Any: | ||
"""Validate the pydantic schema. | ||
Args: | ||
values: The values to validate. | ||
Returns: | ||
The validated values. | ||
Raises: | ||
ValueError: If the schema is not a pydantic schema. | ||
""" | ||
schema = values["pydantic_schema"] | ||
if "args_only" not in values: | ||
values["args_only"] = ( | ||
isinstance(schema, type) | ||
and not isinstance(schema, GenericAlias) | ||
and issubclass(schema, BaseModel) | ||
) | ||
elif values["args_only"] and isinstance(schema, dict): | ||
msg = ( | ||
"If multiple pydantic schemas are provided then args_only should be" | ||
" False." | ||
) | ||
raise ValueError(msg) | ||
return values | ||
|
||
def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any: | ||
"""Parse the result of an LLM call to a JSON object. | ||
Args: | ||
result: The result of the LLM call. | ||
partial: Whether to parse partial JSON objects. Default is False. | ||
Returns: | ||
The parsed JSON object. | ||
""" | ||
_result = super().parse_result(result) | ||
if self.args_only: | ||
if hasattr(self.pydantic_schema, "model_validate"): | ||
pydantic_args = self.pydantic_schema.model_validate(_result) | ||
else: | ||
pydantic_args = self.pydantic_schema.parse_obj(_result) # type: ignore | ||
else: | ||
fn_name = _result["name"] | ||
_args = _result["arguments"] | ||
if isinstance(self.pydantic_schema, dict): | ||
pydantic_schema = self.pydantic_schema[fn_name] | ||
else: | ||
pydantic_schema = self.pydantic_schema | ||
if hasattr(pydantic_schema, "model_validate"): | ||
pydantic_args = pydantic_schema.model_validate(_args) # type: ignore | ||
else: | ||
pydantic_args = pydantic_schema.parse_obj(_args) # type: ignore | ||
return pydantic_args | ||
|
||
|
||
class PydanticAttrOutputFunctionsParser(PydanticOutputFunctionsParser): | ||
"""Parse an output as an attribute of a pydantic object.""" | ||
|
||
attr_name: str | ||
"""The name of the attribute to return.""" | ||
|
||
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any: | ||
result = super().parse_result(result) | ||
return getattr(result, self.attr_name) |
Oops, something went wrong.