-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e86ce2
commit bb180db
Showing
1 changed file
with
41 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import re | ||
from typing import Optional | ||
|
||
from langchain.pydantic_v1 import BaseModel, Field | ||
from langchain.schema import OutputParserException | ||
|
||
from .parser import ParserBaseModel | ||
|
||
# PARSER TYPES | ||
|
||
|
||
class CodeBlock(ParserBaseModel): | ||
code: str | ||
language: Optional[str] = None | ||
|
||
@classmethod | ||
def parse(cls, text: str) -> "CodeBlock": | ||
matches = re.finditer(r"```(?P<language>\w+)?\n?(?P<code>.*?)```", text, re.DOTALL) | ||
for match in matches: | ||
groupdict = match.groupdict() | ||
groupdict["language"] = groupdict.get("language", None) | ||
|
||
# custom markdown fix | ||
if groupdict["language"] == "markdown": | ||
t = text.split("```markdown")[1] | ||
return cls( | ||
language="markdown", | ||
code=t[: -(len(t.split("```")[-1]) + 3)], | ||
) | ||
|
||
return cls(**groupdict) | ||
|
||
return cls(code=text) # TODO: fix this hack | ||
raise OutputParserException("Invalid codeblock") | ||
|
||
|
||
class Error(BaseModel): | ||
"""If anything goes wrong and you can not do what is expected, use this error function as fallback.""" | ||
|
||
title: str = Field(..., description="CamelCase Name titeling the error") | ||
description: str = Field(..., description="Short description of the unexpected situation") |