docstrings output_parsers (#7859)

Added/updated the docstrings from `output_parsers`
 @baskaryan
This commit is contained in:
Leonid Ganeline 2023-07-18 07:51:44 -07:00 committed by GitHub
parent c460c29a64
commit 527210972e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 117 additions and 19 deletions

View File

@ -2,14 +2,18 @@ from langchain.schema import BaseOutputParser
class BooleanOutputParser(BaseOutputParser[bool]): class BooleanOutputParser(BaseOutputParser[bool]):
"""Parse the output of an LLM call to a boolean."""
true_val: str = "YES" true_val: str = "YES"
"""The string value that should be parsed as True."""
false_val: str = "NO" false_val: str = "NO"
"""The string value that should be parsed as False."""
def parse(self, text: str) -> bool: def parse(self, text: str) -> bool:
"""Parse the output of an LLM call to a boolean. """Parse the output of an LLM call to a boolean.
Args: Args:
text: output of language model text: output of a language model
Returns: Returns:
boolean boolean
@ -25,5 +29,5 @@ class BooleanOutputParser(BaseOutputParser[bool]):
@property @property
def _type(self) -> str: def _type(self) -> str:
"""Snake-case string identifier for output parser type.""" """Snake-case string identifier for an output parser type."""
return "boolean_output_parser" return "boolean_output_parser"

View File

@ -8,7 +8,7 @@ from langchain.schema import BaseOutputParser
class CombiningOutputParser(BaseOutputParser): class CombiningOutputParser(BaseOutputParser):
"""Class to combine multiple output parsers into one.""" """Combine multiple output parsers into one."""
@property @property
def lc_serializable(self) -> bool: def lc_serializable(self) -> bool:

View File

@ -12,9 +12,9 @@ def _generate_random_datetime_strings(
start_date: datetime = datetime(1, 1, 1), start_date: datetime = datetime(1, 1, 1),
end_date: datetime = datetime.now() + timedelta(days=3650), end_date: datetime = datetime.now() + timedelta(days=3650),
) -> List[str]: ) -> List[str]:
""" """Generates n random datetime strings conforming to the
Generates n random datetime strings conforming to the
given pattern within the specified date range. given pattern within the specified date range.
Pattern should be a string containing the desired format codes. Pattern should be a string containing the desired format codes.
start_date and end_date should be datetime objects representing start_date and end_date should be datetime objects representing
the start and end of the date range. the start and end of the date range.
@ -30,7 +30,10 @@ def _generate_random_datetime_strings(
class DatetimeOutputParser(BaseOutputParser[datetime]): class DatetimeOutputParser(BaseOutputParser[datetime]):
"""Parse the output of an LLM call to a datetime."""
format: str = "%Y-%m-%dT%H:%M:%S.%fZ" format: str = "%Y-%m-%dT%H:%M:%S.%fZ"
"""The string value that used as the datetime format."""
def get_format_instructions(self) -> str: def get_format_instructions(self) -> str:
examples = comma_list(_generate_random_datetime_strings(self.format)) examples = comma_list(_generate_random_datetime_strings(self.format))

View File

@ -7,7 +7,10 @@ from langchain.schema import BaseOutputParser, OutputParserException
class EnumOutputParser(BaseOutputParser): class EnumOutputParser(BaseOutputParser):
"""Parse an output that is one of a set of values."""
enum: Type[Enum] enum: Type[Enum]
"""The enum to parse. Its values must be strings."""
@root_validator() @root_validator()
def raise_deprecation(cls, values: Dict) -> Dict: def raise_deprecation(cls, values: Dict) -> Dict:

View File

@ -27,6 +27,16 @@ class OutputFixingParser(BaseOutputParser[T]):
parser: BaseOutputParser[T], parser: BaseOutputParser[T],
prompt: BasePromptTemplate = NAIVE_FIX_PROMPT, prompt: BasePromptTemplate = NAIVE_FIX_PROMPT,
) -> OutputFixingParser[T]: ) -> OutputFixingParser[T]:
"""Create an OutputFixingParser from a language model and a parser.
Args:
llm: llm to use for fixing
parser: parser to use for parsing
prompt: prompt to use for fixing
Returns:
OutputFixingParser
"""
chain = LLMChain(llm=llm, prompt=prompt) chain = LLMChain(llm=llm, prompt=prompt)
return cls(parser=parser, retry_chain=chain) return cls(parser=parser, retry_chain=chain)

View File

@ -63,6 +63,8 @@ def parse_and_check_json_markdown(text: str, expected_keys: List[str]) -> dict:
class SimpleJsonOutputParser(BaseOutputParser[Any]): class SimpleJsonOutputParser(BaseOutputParser[Any]):
"""Parse the output of an LLM call to a JSON object."""
def parse(self, text: str) -> Any: def parse(self, text: str) -> Any:
text = text.strip() text = text.strip()
try: try:

View File

@ -7,7 +7,7 @@ from langchain.schema import BaseOutputParser
class ListOutputParser(BaseOutputParser): class ListOutputParser(BaseOutputParser):
"""Class to parse the output of an LLM call to a list.""" """Parse the output of an LLM call to a list."""
@property @property
def _type(self) -> str: def _type(self) -> str:
@ -19,7 +19,7 @@ class ListOutputParser(BaseOutputParser):
class CommaSeparatedListOutputParser(ListOutputParser): class CommaSeparatedListOutputParser(ListOutputParser):
"""Parse out comma separated lists.""" """Parse the output of an LLM call to a comma-separated list."""
@property @property
def lc_serializable(self) -> bool: def lc_serializable(self) -> bool:

View File

@ -2,7 +2,14 @@ from langchain.output_parsers.regex import RegexParser
def load_output_parser(config: dict) -> dict: def load_output_parser(config: dict) -> dict:
"""Load output parser.""" """Load an output parser.
Args:
config: config dict
Returns:
config dict with output parser loaded
"""
if "output_parsers" in config: if "output_parsers" in config:
if config["output_parsers"] is not None: if config["output_parsers"] is not None:
_config = config["output_parsers"] _config = config["output_parsers"]

View File

@ -12,7 +12,10 @@ from langchain.schema import (
class OutputFunctionsParser(BaseLLMOutputParser[Any]): class OutputFunctionsParser(BaseLLMOutputParser[Any]):
"""Parse an output that is one of sets of values."""
args_only: bool = True args_only: bool = True
"""Whether to only return the arguments to the function call."""
def parse_result(self, result: List[Generation]) -> Any: def parse_result(self, result: List[Generation]) -> Any:
generation = result[0] generation = result[0]
@ -32,6 +35,8 @@ class OutputFunctionsParser(BaseLLMOutputParser[Any]):
class JsonOutputFunctionsParser(OutputFunctionsParser): class JsonOutputFunctionsParser(OutputFunctionsParser):
"""Parse an output as the Json object."""
def parse_result(self, result: List[Generation]) -> Any: def parse_result(self, result: List[Generation]) -> Any:
func = super().parse_result(result) func = super().parse_result(result)
if self.args_only: if self.args_only:
@ -41,7 +46,10 @@ class JsonOutputFunctionsParser(OutputFunctionsParser):
class JsonKeyOutputFunctionsParser(JsonOutputFunctionsParser): class JsonKeyOutputFunctionsParser(JsonOutputFunctionsParser):
"""Parse an output as the element of the Json object."""
key_name: str key_name: str
"""The name of the key to return."""
def parse_result(self, result: List[Generation]) -> Any: def parse_result(self, result: List[Generation]) -> Any:
res = super().parse_result(result) res = super().parse_result(result)
@ -49,7 +57,10 @@ class JsonKeyOutputFunctionsParser(JsonOutputFunctionsParser):
class PydanticOutputFunctionsParser(OutputFunctionsParser): class PydanticOutputFunctionsParser(OutputFunctionsParser):
"""Parse an output as a pydantic object."""
pydantic_schema: Union[Type[BaseModel], Dict[str, Type[BaseModel]]] pydantic_schema: Union[Type[BaseModel], Dict[str, Type[BaseModel]]]
"""The pydantic schema to parse the output with."""
@root_validator(pre=True) @root_validator(pre=True)
def validate_schema(cls, values: Dict) -> Dict: def validate_schema(cls, values: Dict) -> Dict:
@ -77,7 +88,10 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser):
class PydanticAttrOutputFunctionsParser(PydanticOutputFunctionsParser): class PydanticAttrOutputFunctionsParser(PydanticOutputFunctionsParser):
"""Parse an output as an attribute of a pydantic object."""
attr_name: str attr_name: str
"""The name of the attribute to return."""
def parse_result(self, result: List[Generation]) -> Any: def parse_result(self, result: List[Generation]) -> Any:
result = super().parse_result(result) result = super().parse_result(result)

View File

@ -11,7 +11,10 @@ T = TypeVar("T", bound=BaseModel)
class PydanticOutputParser(BaseOutputParser[T]): class PydanticOutputParser(BaseOutputParser[T]):
"""Parse an output using a pydantic model."""
pydantic_object: Type[T] pydantic_object: Type[T]
"""The pydantic model to parse."""
def parse(self, text: str) -> T: def parse(self, text: str) -> T:
try: try:

View File

@ -6,10 +6,16 @@ from langchain.schema import BaseOutputParser
class GuardrailsOutputParser(BaseOutputParser): class GuardrailsOutputParser(BaseOutputParser):
"""Parse the output of an LLM call using Guardrails."""
guard: Any guard: Any
"""The Guardrails object."""
api: Optional[Callable] api: Optional[Callable]
"""The API to use for the Guardrails object."""
args: Any args: Any
"""The arguments to pass to the API."""
kwargs: Any kwargs: Any
"""The keyword arguments to pass to the API."""
@property @property
def _type(self) -> str: def _type(self) -> str:
@ -24,10 +30,22 @@ class GuardrailsOutputParser(BaseOutputParser):
*args: Any, *args: Any,
**kwargs: Any, **kwargs: Any,
) -> GuardrailsOutputParser: ) -> GuardrailsOutputParser:
"""Create a GuardrailsOutputParser from a rail file.
Args:
rail_file: a rail file.
num_reasks: number of times to re-ask the question.
api: the API to use for the Guardrails object.
*args: The arguments to pass to the API
**kwargs: The keyword arguments to pass to the API.
Returns:
GuardrailsOutputParser
"""
try: try:
from guardrails import Guard from guardrails import Guard
except ImportError: except ImportError:
raise ValueError( raise ImportError(
"guardrails-ai package not installed. " "guardrails-ai package not installed. "
"Install it by running `pip install guardrails-ai`." "Install it by running `pip install guardrails-ai`."
) )
@ -50,7 +68,7 @@ class GuardrailsOutputParser(BaseOutputParser):
try: try:
from guardrails import Guard from guardrails import Guard
except ImportError: except ImportError:
raise ValueError( raise ImportError(
"guardrails-ai package not installed. " "guardrails-ai package not installed. "
"Install it by running `pip install guardrails-ai`." "Install it by running `pip install guardrails-ai`."
) )
@ -73,7 +91,7 @@ class GuardrailsOutputParser(BaseOutputParser):
try: try:
from guardrails import Guard from guardrails import Guard
except ImportError: except ImportError:
raise ValueError( raise ImportError(
"guardrails-ai package not installed. " "guardrails-ai package not installed. "
"Install it by running `pip install guardrails-ai`." "Install it by running `pip install guardrails-ai`."
) )

View File

@ -7,15 +7,18 @@ from langchain.schema import BaseOutputParser
class RegexParser(BaseOutputParser): class RegexParser(BaseOutputParser):
"""Class to parse the output into a dictionary.""" """Parse the output of an LLM call using a regex."""
@property @property
def lc_serializable(self) -> bool: def lc_serializable(self) -> bool:
return True return True
regex: str regex: str
"""The regex to use to parse the output."""
output_keys: List[str] output_keys: List[str]
"""The keys to use for the output."""
default_output_key: Optional[str] = None default_output_key: Optional[str] = None
"""The default key to use for the output."""
@property @property
def _type(self) -> str: def _type(self) -> str:

View File

@ -7,11 +7,14 @@ from langchain.schema import BaseOutputParser
class RegexDictParser(BaseOutputParser): class RegexDictParser(BaseOutputParser):
"""Class to parse the output into a dictionary.""" """Parse the output of an LLM call into a Dictionary using a regex."""
regex_pattern: str = r"{}:\s?([^.'\n']*)\.?" # : :meta private: regex_pattern: str = r"{}:\s?([^.'\n']*)\.?" # : :meta private:
"""The regex pattern to use to parse the output."""
output_key_to_format: Dict[str, str] output_key_to_format: Dict[str, str]
"""The keys to use for the output."""
no_update_value: Optional[str] = None no_update_value: Optional[str] = None
"""The default key to use for the output."""
@property @property
def _type(self) -> str: def _type(self) -> str:

View File

@ -45,7 +45,9 @@ class RetryOutputParser(BaseOutputParser[T]):
""" """
parser: BaseOutputParser[T] parser: BaseOutputParser[T]
"""The parser to use to parse the output."""
retry_chain: LLMChain retry_chain: LLMChain
"""The LLMChain to use to retry the completion."""
@classmethod @classmethod
def from_llm( def from_llm(
@ -58,6 +60,15 @@ class RetryOutputParser(BaseOutputParser[T]):
return cls(parser=parser, retry_chain=chain) return cls(parser=parser, retry_chain=chain)
def parse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T: def parse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T:
"""Parse the output of an LLM call using a wrapped parser.
Args:
completion: The chain completion to parse.
prompt_value: The prompt to use to parse the completion.
Returns:
The parsed completion.
"""
try: try:
parsed_completion = self.parser.parse(completion) parsed_completion = self.parser.parse(completion)
except OutputParserException: except OutputParserException:
@ -101,6 +112,16 @@ class RetryWithErrorOutputParser(BaseOutputParser[T]):
parser: BaseOutputParser[T], parser: BaseOutputParser[T],
prompt: BasePromptTemplate = NAIVE_RETRY_WITH_ERROR_PROMPT, prompt: BasePromptTemplate = NAIVE_RETRY_WITH_ERROR_PROMPT,
) -> RetryWithErrorOutputParser[T]: ) -> RetryWithErrorOutputParser[T]:
"""Create a RetryWithErrorOutputParser from an LLM.
Args:
llm: The LLM to use to retry the completion.
parser: The parser to use to parse the output.
prompt: The prompt to use to retry the completion.
Returns:
A RetryWithErrorOutputParser.
"""
chain = LLMChain(llm=llm, prompt=prompt) chain = LLMChain(llm=llm, prompt=prompt)
return cls(parser=parser, retry_chain=chain) return cls(parser=parser, retry_chain=chain)

View File

@ -15,9 +15,14 @@ line_template = '\t"{name}": {type} // {description}'
class ResponseSchema(BaseModel): class ResponseSchema(BaseModel):
"""A schema for a response from a structured output parser."""
name: str name: str
"""The name of the schema."""
description: str description: str
"""The description of the schema."""
type: str = "string" type: str = "string"
"""The type of the response."""
def _get_sub_string(schema: ResponseSchema) -> str: def _get_sub_string(schema: ResponseSchema) -> str:
@ -27,7 +32,10 @@ def _get_sub_string(schema: ResponseSchema) -> str:
class StructuredOutputParser(BaseOutputParser): class StructuredOutputParser(BaseOutputParser):
"""Parse the output of an LLM call to a structured output."""
response_schemas: List[ResponseSchema] response_schemas: List[ResponseSchema]
"""The schemas for the response."""
@classmethod @classmethod
def from_response_schemas( def from_response_schemas(
@ -36,8 +44,7 @@ class StructuredOutputParser(BaseOutputParser):
return cls(response_schemas=response_schemas) return cls(response_schemas=response_schemas)
def get_format_instructions(self, only_json: bool = False) -> str: def get_format_instructions(self, only_json: bool = False) -> str:
""" """Get format instructions for the output parser.
Method to get the format instructions for the output parser.
example: example:
```python ```python
@ -63,7 +70,7 @@ class StructuredOutputParser(BaseOutputParser):
print(parser.get_format_instructions()) print(parser.get_format_instructions())
output: output:
# The output should be a markdown code snippet formatted in the following # The output should be a Markdown code snippet formatted in the following
# schema, including the leading and trailing "```json" and "```": # schema, including the leading and trailing "```json" and "```":
# #
# ```json # ```json
@ -73,7 +80,7 @@ class StructuredOutputParser(BaseOutputParser):
# } # }
Args: Args:
only_json (bool): If True, only the json in the markdown code snippet only_json (bool): If True, only the json in the Markdown code snippet
will be returned, without the introducing text. Defaults to False. will be returned, without the introducing text. Defaults to False.
""" """
schema_str = "\n".join( schema_str = "\n".join(

View File

@ -73,7 +73,7 @@ class BaseOutputParser(BaseLLMOutputParser, ABC, Generic[T]):
"""Parse a single string model output into some structure. """Parse a single string model output into some structure.
Args: Args:
text: String output of language model. text: String output of a language model.
Returns: Returns:
Structured output. Structured output.
@ -88,7 +88,7 @@ class BaseOutputParser(BaseLLMOutputParser, ABC, Generic[T]):
the prompt to do so. the prompt to do so.
Args: Args:
completion: String output of language model. completion: String output of a language model.
prompt: Input PromptValue. prompt: Input PromptValue.
Returns: Returns: