mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Guardrails output parser: Pass LLM api for reasking (#6089)
<!-- Thank you for contributing to LangChain! Your PR will appear in our release under the title you set. Please make sure it highlights your valuable contribution. Replace this with a description of the change, the issue it fixes (if applicable), and relevant context. List any dependencies required for this change. After you're done, someone will review your PR. They may suggest improvements. If no one reviews your PR within a few days, feel free to @-mention the same people again, as notifications can get lost. Finally, we'd love to show appreciation for your contribution - if you'd like us to shout you out on Twitter, please also include your handle! --> <!-- Remove if not applicable --> Fixes https://github.com/ShreyaR/guardrails/issues/155 Enables guardrails reasking by specifying an LLM api in the output parser.
This commit is contained in:
parent
ec850e607f
commit
ebfffaa38f
@ -1,31 +1,28 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Callable, Dict, Optional
|
||||||
|
|
||||||
from langchain.schema import BaseOutputParser
|
from langchain.schema import BaseOutputParser
|
||||||
|
|
||||||
|
|
||||||
class GuardrailsOutputParser(BaseOutputParser):
|
class GuardrailsOutputParser(BaseOutputParser):
|
||||||
guard: Any
|
guard: Any
|
||||||
|
api: Optional[Callable]
|
||||||
|
args: Any
|
||||||
|
kwargs: Any
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _type(self) -> str:
|
def _type(self) -> str:
|
||||||
return "guardrails"
|
return "guardrails"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_rail(cls, rail_file: str, num_reasks: int = 1) -> GuardrailsOutputParser:
|
def from_rail(
|
||||||
try:
|
cls,
|
||||||
from guardrails import Guard
|
rail_file: str,
|
||||||
except ImportError:
|
num_reasks: int = 1,
|
||||||
raise ValueError(
|
api: Optional[Callable] = None,
|
||||||
"guardrails-ai package not installed. "
|
*args: Any,
|
||||||
"Install it by running `pip install guardrails-ai`."
|
**kwargs: Any,
|
||||||
)
|
|
||||||
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:
|
) -> GuardrailsOutputParser:
|
||||||
try:
|
try:
|
||||||
from guardrails import Guard
|
from guardrails import Guard
|
||||||
@ -34,10 +31,38 @@ class GuardrailsOutputParser(BaseOutputParser):
|
|||||||
"guardrails-ai package not installed. "
|
"guardrails-ai package not installed. "
|
||||||
"Install it by running `pip install guardrails-ai`."
|
"Install it by running `pip install guardrails-ai`."
|
||||||
)
|
)
|
||||||
return cls(guard=Guard.from_rail_string(rail_str, num_reasks=num_reasks))
|
return cls(
|
||||||
|
guard=Guard.from_rail(rail_file, num_reasks=num_reasks),
|
||||||
|
api=api,
|
||||||
|
args=args,
|
||||||
|
kwargs=kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_rail_string(
|
||||||
|
cls,
|
||||||
|
rail_str: str,
|
||||||
|
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_rail_string(rail_str, num_reasks=num_reasks),
|
||||||
|
api=api,
|
||||||
|
args=args,
|
||||||
|
kwargs=kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
def get_format_instructions(self) -> str:
|
def get_format_instructions(self) -> str:
|
||||||
return self.guard.raw_prompt.format_instructions
|
return self.guard.raw_prompt.format_instructions
|
||||||
|
|
||||||
def parse(self, text: str) -> Dict:
|
def parse(self, text: str) -> Dict:
|
||||||
return self.guard.parse(text)
|
return self.guard.parse(text, llm_api=self.api, *self.args, **self.kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user