Skip to content

Commit

Permalink
rail_parser: Add from_guard classmethod, deprecate others
Browse files Browse the repository at this point in the history
  • Loading branch information
irgolic committed Dec 13, 2023
1 parent 7be3eb6 commit d826774
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion libs/langchain/langchain/output_parsers/rail_parser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from __future__ import annotations

from typing import Any, Callable, Dict, Optional
from typing import Any, Callable, Dict, Optional, TYPE_CHECKING
import warnings

from langchain_core.output_parsers import BaseOutputParser


if TYPE_CHECKING:
from guardrails.guard import Guard


class GuardrailsOutputParser(BaseOutputParser):
"""Parse the output of an LLM call using Guardrails."""

Expand All @@ -21,6 +26,23 @@ class GuardrailsOutputParser(BaseOutputParser):
def _type(self) -> str:
return "guardrails"

@classmethod
def from_guard(cls, guard: "Guard") -> GuardrailsOutputParser:
"""Create a GuardrailsOutputParser from a Guard object.
See `guardrails.Guard` for more information.
Args:
guard: A guard object.
Returns:
GuardrailsOutputParser
"""
return cls(
guard=guard,
args=(),
kwargs={},
)

@classmethod
def from_rail(
cls,
Expand Down Expand Up @@ -49,6 +71,13 @@ def from_rail(
"guardrails-ai package not installed. "
"Install it by running `pip install guardrails-ai`."
)

warnings.warn(
"GuardrailsOutputParser.from_rail is deprecated. "
"Use GuardrailsOutputParser.from_guard instead.",
DeprecationWarning,
)

return cls(
guard=Guard.from_rail(rail_file, num_reasks=num_reasks),
api=api,
Expand All @@ -72,6 +101,13 @@ def from_rail_string(
"guardrails-ai package not installed. "
"Install it by running `pip install guardrails-ai`."
)

warnings.warn(
"GuardrailsOutputParser.from_rail_string is deprecated. "
"Use GuardrailsOutputParser.from_guard instead.",
DeprecationWarning,
)

return cls(
guard=Guard.from_rail_string(rail_str, num_reasks=num_reasks),
api=api,
Expand All @@ -95,6 +131,13 @@ def from_pydantic(
"guardrails-ai package not installed. "
"Install it by running `pip install guardrails-ai`."
)

warnings.warn(
"GuardrailsOutputParser.from_pydantic is deprecated. "
"Use GuardrailsOutputParser.from_guard instead.",
DeprecationWarning,
)

return cls(
guard=Guard.from_pydantic(output_class, "", num_reasks=num_reasks),
api=api,
Expand Down

0 comments on commit d826774

Please sign in to comment.