mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-07 13:40:46 +00:00
Harrison/gr int (#1700)
Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
This commit is contained in:
parent
aad4bff098
commit
b84d190fd0
@ -3,6 +3,7 @@ from langchain.output_parsers.list import (
|
||||
CommaSeparatedListOutputParser,
|
||||
ListOutputParser,
|
||||
)
|
||||
from langchain.output_parsers.rail_parser import GuardrailsOutputParser
|
||||
from langchain.output_parsers.regex import RegexParser
|
||||
from langchain.output_parsers.regex_dict import RegexDictParser
|
||||
from langchain.output_parsers.structured import ResponseSchema, StructuredOutputParser
|
||||
@ -15,4 +16,5 @@ __all__ = [
|
||||
"BaseOutputParser",
|
||||
"StructuredOutputParser",
|
||||
"ResponseSchema",
|
||||
"GuardrailsOutputParser",
|
||||
]
|
||||
|
43
langchain/output_parsers/rail_parser.py
Normal file
43
langchain/output_parsers/rail_parser.py
Normal file
@ -0,0 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict
|
||||
|
||||
from langchain.output_parsers.base import BaseOutputParser
|
||||
|
||||
|
||||
class GuardrailsOutputParser(BaseOutputParser):
|
||||
guard: Any
|
||||
|
||||
@property
|
||||
def _type(self) -> str:
|
||||
return "guardrails"
|
||||
|
||||
@classmethod
|
||||
def from_rail(cls, rail_file: str, num_reasks: int = 1) -> GuardrailsOutputParser:
|
||||
try:
|
||||
from guardrails import Guard
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"guardrails-ai package not installed. "
|
||||
"Install it by running `pip install guardrails-ai`."
|
||||
)
|
||||
return cls(guard=Guard.from_rail(rail_file, num_reasks=num_reasks))
|
||||
|
||||
@classmethod
|
||||
def from_rail_string(
|
||||
cls, rail_str: str, num_reasks: int = 1
|
||||
) -> GuardrailsOutputParser:
|
||||
try:
|
||||
from guardrails import Guard
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"guardrails-ai package not installed. "
|
||||
"Install it by running `pip install guardrails-ai`."
|
||||
)
|
||||
return cls(guard=Guard.from_rail_string(rail_str, num_reasks=num_reasks))
|
||||
|
||||
def get_format_instructions(self) -> str:
|
||||
return self.guard.raw_prompt.format_instructions
|
||||
|
||||
def parse(self, text: str) -> Dict:
|
||||
return self.guard.parse(text)
|
Loading…
Reference in New Issue
Block a user