From 89c141d36eea29813b7af7aeaa8e939c221124b3 Mon Sep 17 00:00:00 2001 From: Rafael Irgolic Date: Tue, 13 Jun 2023 10:42:53 +0100 Subject: [PATCH 1/3] output_parsers.rail_parser: Pass LLM api for reasking --- langchain/output_parsers/rail_parser.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/langchain/output_parsers/rail_parser.py b/langchain/output_parsers/rail_parser.py index 0dab50d9a894c..17e0fc7484aee 100644 --- a/langchain/output_parsers/rail_parser.py +++ b/langchain/output_parsers/rail_parser.py @@ -1,19 +1,25 @@ from __future__ import annotations -from typing import Any, Dict +from typing import Any, Dict, Callable, Optional from langchain.schema import BaseOutputParser class GuardrailsOutputParser(BaseOutputParser): guard: Any + api: Optional[Callable] @property def _type(self) -> str: return "guardrails" @classmethod - def from_rail(cls, rail_file: str, num_reasks: int = 1) -> GuardrailsOutputParser: + def from_rail( + cls, + rail_file: str, + num_reasks: int = 1, + api: Optional[Callable] = None, + ) -> GuardrailsOutputParser: try: from guardrails import Guard except ImportError: @@ -21,11 +27,15 @@ def from_rail(cls, rail_file: str, num_reasks: int = 1) -> GuardrailsOutputParse "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)) + return cls(guard=Guard.from_rail(rail_file, num_reasks=num_reasks), + api=api) @classmethod def from_rail_string( - cls, rail_str: str, num_reasks: int = 1 + cls, + rail_str: str, + num_reasks: int = 1, + api: Optional[Callable] = None, ) -> GuardrailsOutputParser: try: from guardrails import Guard @@ -34,10 +44,11 @@ def from_rail_string( "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)) + return cls(guard=Guard.from_rail_string(rail_str, num_reasks=num_reasks), + api=api) def get_format_instructions(self) -> str: return self.guard.raw_prompt.format_instructions def parse(self, text: str) -> Dict: - return self.guard.parse(text) + return self.guard.parse(text, llm_api=self.api) From 93c819a4c66032f59870f4d3814c8c840e01511b Mon Sep 17 00:00:00 2001 From: Rafael Irgolic Date: Tue, 13 Jun 2023 10:49:34 +0100 Subject: [PATCH 2/3] rail_parser: Allow passing args and kwargs (model) --- langchain/output_parsers/rail_parser.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/langchain/output_parsers/rail_parser.py b/langchain/output_parsers/rail_parser.py index 17e0fc7484aee..9de23ec6ed820 100644 --- a/langchain/output_parsers/rail_parser.py +++ b/langchain/output_parsers/rail_parser.py @@ -8,6 +8,8 @@ class GuardrailsOutputParser(BaseOutputParser): guard: Any api: Optional[Callable] + args: Any + kwargs: Any @property def _type(self) -> str: @@ -19,6 +21,8 @@ def from_rail( rail_file: str, num_reasks: int = 1, api: Optional[Callable] = None, + *args, + **kwargs, ) -> GuardrailsOutputParser: try: from guardrails import Guard @@ -28,7 +32,9 @@ def from_rail( "Install it by running `pip install guardrails-ai`." ) return cls(guard=Guard.from_rail(rail_file, num_reasks=num_reasks), - api=api) + api=api, + args=args, + kwargs=kwargs) @classmethod def from_rail_string( @@ -36,6 +42,8 @@ def from_rail_string( rail_str: str, num_reasks: int = 1, api: Optional[Callable] = None, + *args, + **kwargs, ) -> GuardrailsOutputParser: try: from guardrails import Guard @@ -45,10 +53,12 @@ def from_rail_string( "Install it by running `pip install guardrails-ai`." ) return cls(guard=Guard.from_rail_string(rail_str, num_reasks=num_reasks), - api=api) + api=api, + args=args, + kwargs=kwargs) def get_format_instructions(self) -> str: return self.guard.raw_prompt.format_instructions def parse(self, text: str) -> Dict: - return self.guard.parse(text, llm_api=self.api) + return self.guard.parse(text, llm_api=self.api, *self.args, **self.kwargs) From 2b042bd2175546a54f57034d3e7a4da6684ec8bf Mon Sep 17 00:00:00 2001 From: Rafael Irgolic Date: Tue, 13 Jun 2023 11:14:00 +0100 Subject: [PATCH 3/3] lint --- langchain/output_parsers/rail_parser.py | 30 ++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/langchain/output_parsers/rail_parser.py b/langchain/output_parsers/rail_parser.py index 9de23ec6ed820..cd02b337330cd 100644 --- a/langchain/output_parsers/rail_parser.py +++ b/langchain/output_parsers/rail_parser.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Dict, Callable, Optional +from typing import Any, Callable, Dict, Optional from langchain.schema import BaseOutputParser @@ -21,8 +21,8 @@ def from_rail( rail_file: str, num_reasks: int = 1, api: Optional[Callable] = None, - *args, - **kwargs, + *args: Any, + **kwargs: Any, ) -> GuardrailsOutputParser: try: from guardrails import Guard @@ -31,10 +31,12 @@ def from_rail( "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), - api=api, - args=args, - kwargs=kwargs) + return cls( + guard=Guard.from_rail(rail_file, num_reasks=num_reasks), + api=api, + args=args, + kwargs=kwargs, + ) @classmethod def from_rail_string( @@ -42,8 +44,8 @@ def from_rail_string( rail_str: str, num_reasks: int = 1, api: Optional[Callable] = None, - *args, - **kwargs, + *args: Any, + **kwargs: Any, ) -> GuardrailsOutputParser: try: from guardrails import Guard @@ -52,10 +54,12 @@ def from_rail_string( "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) + 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: return self.guard.raw_prompt.format_instructions