mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-19 03:01:29 +00:00
rail_parser: Allow creation from pydantic (#6832)
<!-- Thank you for contributing to LangChain! Replace this comment with: - Description: a description of the change, - Issue: the issue # it fixes (if applicable), - Dependencies: any dependencies required for this change, - Tag maintainer: for a quicker response, tag the relevant maintainer (see below), - Twitter handle: we announce bigger features on Twitter. If your PR gets announced and you'd like a mention, we'll gladly shout you out! If you're adding a new integration, please include: 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. Maintainer responsibilities: - General / Misc / if you don't know who to tag: @dev2049 - DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev - Models / Prompts: @hwchase17, @dev2049 - Memory: @hwchase17 - Agents / Tools / Toolkits: @vowelparrot - Tracing / Callbacks: @agola11 - Async: @agola11 If no one reviews your PR within a few days, feel free to @-mention the same people again. See contribution guidelines for more information on how to write/run tests, lint, etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md --> Adds a way to create the guardrails output parser from a pydantic model.
This commit is contained in:
parent
a980095efc
commit
ef72a7cf26
@ -61,6 +61,29 @@ class GuardrailsOutputParser(BaseOutputParser):
|
||||
kwargs=kwargs,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_pydantic(
|
||||
cls,
|
||||
output_class: Any,
|
||||
num_reasks: int = 1,
|
||||
api: Optional[Callable] = None,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
) -> 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_pydantic(output_class, "", num_reasks=num_reasks),
|
||||
api=api,
|
||||
args=args,
|
||||
kwargs=kwargs,
|
||||
)
|
||||
|
||||
def get_format_instructions(self) -> str:
|
||||
return self.guard.raw_prompt.format_instructions
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user