Skip to content

Commit

Permalink
🔀 move parser base model
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Feb 12, 2024
1 parent 195a77d commit b366b74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
38 changes: 35 additions & 3 deletions src/funcchain/parser/custom.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
import json
import re
from typing import Type, TypeVar

from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import BaseOutputParser
from pydantic import ValidationError
from langchain_core.output_parsers import BaseLLMOutputParser, BaseOutputParser
from pydantic import BaseModel, ValidationError
from typing_extensions import Self

from ..syntax.output_types import CodeBlock as CodeBlock
from ..syntax.output_types import ParserBaseModel


class ParserBaseModel(BaseModel):
@classmethod
def output_parser(cls) -> BaseLLMOutputParser[Self]:
from ..parser.custom import CustomPydanticOutputParser

return CustomPydanticOutputParser(pydantic_object=cls)

@classmethod
def parse(cls, text: str) -> Self:
"""Override for custom parsing."""
match = re.search(r"\{.*\}", text.strip(), re.MULTILINE | re.IGNORECASE | re.DOTALL)
json_str = ""
if match:
json_str = match.group()
json_object = json.loads(json_str, strict=False)
return cls.model_validate(json_object)

@staticmethod
def format_instructions() -> str:
return (
"Please respond with a json result matching the following schema:"
"\n\n```schema\n{schema}\n```\n"
"Do not repeat the schema. Only respond with the result."
)

@staticmethod
def custom_grammar() -> str | None:
return None


P = TypeVar("P", bound=ParserBaseModel)

Expand Down
33 changes: 1 addition & 32 deletions src/funcchain/syntax/output_types.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import json
import re
from typing import Optional

from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import BaseLLMOutputParser
from pydantic import BaseModel, Field
from typing_extensions import Self


class ParserBaseModel(BaseModel):
@classmethod
def output_parser(cls) -> BaseLLMOutputParser[Self]:
from ..parser.custom import CustomPydanticOutputParser

return CustomPydanticOutputParser(pydantic_object=cls)

@classmethod
def parse(cls, text: str) -> Self:
"""Override for custom parsing."""
match = re.search(r"\{.*\}", text.strip(), re.MULTILINE | re.IGNORECASE | re.DOTALL)
json_str = ""
if match:
json_str = match.group()
json_object = json.loads(json_str, strict=False)
return cls.model_validate(json_object)

@staticmethod
def format_instructions() -> str:
return (
"Please respond with a json result matching the following schema:"
"\n\n```schema\n{schema}\n```\n"
"Do not repeat the schema. Only respond with the result."
)

@staticmethod
def custom_grammar() -> str | None:
return None
from ..parser.custom import ParserBaseModel as ParserBaseModel


class CodeBlock(ParserBaseModel):
Expand Down

0 comments on commit b366b74

Please sign in to comment.