forked from Archives/langchain
move output parsing (#1605)
parent
cb04ba0136
commit
c9b5a30b37
@ -0,0 +1,13 @@
|
|||||||
|
from langchain.output_parsers.base import BaseOutputParser
|
||||||
|
from langchain.output_parsers.list import (
|
||||||
|
CommaSeparatedListOutputParser,
|
||||||
|
ListOutputParser,
|
||||||
|
)
|
||||||
|
from langchain.output_parsers.regex import RegexParser
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"RegexParser",
|
||||||
|
"ListOutputParser",
|
||||||
|
"CommaSeparatedListOutputParser",
|
||||||
|
"BaseOutputParser",
|
||||||
|
]
|
@ -0,0 +1,25 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class BaseOutputParser(BaseModel, ABC):
|
||||||
|
"""Class to parse the output of an LLM call."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def parse(self, text: str) -> Any:
|
||||||
|
"""Parse the output of an LLM call."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _type(self) -> str:
|
||||||
|
"""Return the type key."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def dict(self, **kwargs: Any) -> Dict:
|
||||||
|
"""Return dictionary representation of output parser."""
|
||||||
|
output_parser_dict = super().dict()
|
||||||
|
output_parser_dict["_type"] = self._type
|
||||||
|
return output_parser_dict
|
@ -0,0 +1,22 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from abc import abstractmethod
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from langchain.output_parsers.base import BaseOutputParser
|
||||||
|
|
||||||
|
|
||||||
|
class ListOutputParser(BaseOutputParser):
|
||||||
|
"""Class to parse the output of an LLM call to a list."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def parse(self, text: str) -> List[str]:
|
||||||
|
"""Parse the output of an LLM call."""
|
||||||
|
|
||||||
|
|
||||||
|
class CommaSeparatedListOutputParser(ListOutputParser):
|
||||||
|
"""Parse out comma separated lists."""
|
||||||
|
|
||||||
|
def parse(self, text: str) -> List[str]:
|
||||||
|
"""Parse the output of an LLM call."""
|
||||||
|
return text.strip().split(", ")
|
@ -0,0 +1,15 @@
|
|||||||
|
from langchain.output_parsers.regex import RegexParser
|
||||||
|
|
||||||
|
|
||||||
|
def load_output_parser(config: dict) -> dict:
|
||||||
|
"""Load output parser."""
|
||||||
|
if "output_parsers" in config:
|
||||||
|
if config["output_parsers"] is not None:
|
||||||
|
_config = config["output_parsers"]
|
||||||
|
output_parser_type = _config["_type"]
|
||||||
|
if output_parser_type == "regex_parser":
|
||||||
|
output_parser = RegexParser(**_config)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unsupported output parser {output_parser_type}")
|
||||||
|
config["output_parsers"] = output_parser
|
||||||
|
return config
|
@ -0,0 +1,35 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from langchain.output_parsers.base import BaseOutputParser
|
||||||
|
|
||||||
|
|
||||||
|
class RegexParser(BaseOutputParser, BaseModel):
|
||||||
|
"""Class to parse the output into a dictionary."""
|
||||||
|
|
||||||
|
regex: str
|
||||||
|
output_keys: List[str]
|
||||||
|
default_output_key: Optional[str] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _type(self) -> str:
|
||||||
|
"""Return the type key."""
|
||||||
|
return "regex_parser"
|
||||||
|
|
||||||
|
def parse(self, text: str) -> Dict[str, str]:
|
||||||
|
"""Parse the output of an LLM call."""
|
||||||
|
match = re.search(self.regex, text)
|
||||||
|
if match:
|
||||||
|
return {key: match.group(i + 1) for i, key in enumerate(self.output_keys)}
|
||||||
|
else:
|
||||||
|
if self.default_output_key is None:
|
||||||
|
raise ValueError(f"Could not parse output: {text}")
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
key: text if key == self.default_output_key else ""
|
||||||
|
for key in self.output_keys
|
||||||
|
}
|
Loading…
Reference in New Issue