참고소스 수정본

This commit is contained in:
LASTA_DEV01\lasta
2026-05-12 19:40:31 +09:00
parent 0f34a451fc
commit 2e9204243d
8708 changed files with 3259488 additions and 869 deletions

View File

@@ -0,0 +1,46 @@
import json
import os
import pytest
from guardrails.applications.text2sql import Text2Sql
CURRENT_DIR_PARENT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SCHEMA_PATH = os.path.join(CURRENT_DIR_PARENT, "test_assets/text2sql/schema.sql")
EXAMPLES_PATH = os.path.join(CURRENT_DIR_PARENT, "test_assets/text2sql/examples.json")
DB_PATH = os.path.join(
CURRENT_DIR_PARENT, "test_assets/text2sql/department_management.sqlite"
)
@pytest.mark.parametrize(
"conn_str, schema_path, examples",
[
("sqlite://", SCHEMA_PATH, EXAMPLES_PATH),
(f"sqlite:///{DB_PATH}", None, None),
],
)
def test_text2sql_with_examples(conn_str: str, schema_path: str, examples: str, mocker):
"""Test that Text2Sql can be initialized with examples."""
# Mock the call to the OpenAI API.
mocker.patch(
"guardrails.embedding.OpenAIEmbedding._get_embedding",
new=lambda *args, **kwargs: [[0.1] * 1536],
)
if examples is not None:
with open(examples, "r") as f:
examples = json.load(f)
# This should not raise an exception.
Text2Sql(conn_str, schema_file=schema_path, examples=examples)
def test_text2sql_with_coro():
async def mock_llm(*args, **kwargs):
return {"choices": [{"text": "SELECT * FROM employees;"}]}
s = Text2Sql("sqlite://", llm_api=mock_llm)
with pytest.raises(ValueError):
s("")

View File

@@ -0,0 +1,145 @@
from typing import Any, Optional
import io
import sys
import pytest
from pydantic import PrivateAttr
from guardrails.guard import Guard
from guardrails.integrations.langchain.guard_runnable import GuardRunnable
from guardrails.errors import ValidationError
from guardrails.classes import ValidationOutcome
from tests.integration_tests.test_assets.validators import ReadingTime, RegexMatch
@pytest.fixture
def guard_runnable():
return GuardRunnable(
Guard().use(
RegexMatch("Ice cream", match_type="search", on_fail="refrain"),
ReadingTime(0.05, on_fail="noop"),
on="output",
)
)
@pytest.mark.parametrize(
"output,throws",
[
("Ice cream is frozen.", False),
("Ice cream is a frozen dairy product that is consumed in many places.", True),
("This response isn't relevant.", True),
],
)
def test_guard_as_runnable(guard_runnable: GuardRunnable, output: str, throws: bool):
from langchain_core.language_models import LanguageModelInput
from langchain_core.messages import AIMessage, BaseMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import Runnable, RunnableConfig
class MockModel(Runnable):
def invoke(
self,
input: LanguageModelInput,
config: Optional[RunnableConfig] = None,
**kwargs: Any,
) -> BaseMessage:
return AIMessage(content=output)
prompt = ChatPromptTemplate.from_template("ELIF: {topic}")
model = MockModel()
output_parser = StrOutputParser()
chain = prompt | model | guard_runnable | output_parser
topic = "ice cream"
if throws:
with pytest.raises(ValidationError) as exc_info:
chain.invoke({"topic": topic})
assert str(exc_info.value) == (
"The response from the LLM failed validation!"
"See `guard.history` for more details."
)
assert guard_runnable.guard.history.last.status == "fail"
assert guard_runnable.guard.history.last.status == "fail"
else:
result = chain.invoke({"topic": topic})
assert result == output
def test_guard_runnable_with_callback_config(guard_runnable):
from langchain_core.callbacks import CallbackManager
from langchain_core.tracers import ConsoleCallbackHandler
from langchain_core.runnables import RunnableConfig
console_handler = ConsoleCallbackHandler()
callback_manager = CallbackManager([console_handler])
config_with_callbacks = RunnableConfig(callbacks=callback_manager)
captured_output = io.StringIO()
sys.stdout = captured_output
guard_runnable.invoke("Ice cream is sweet", config=config_with_callbacks)
sys.stdout = sys.__stdout__
assert "Ice cream is sweet" in captured_output.getvalue()
@pytest.mark.parametrize(
"succeed_on_attempt, max_retries, expected_attempts, expected_result",
[
(1, 2, 1, "Succeeded on attempt 1"),
(2, 2, 1, "Failed attempt 1"),
(2, None, 1, "Failed attempt 1"),
(2, 0, 1, "Failed attempt 1"),
],
)
def test_guard_runnable_max_retries(
succeed_on_attempt, max_retries, expected_attempts, expected_result
):
from langchain_core.runnables import RunnableConfig
class CountingGuard(Guard):
_attempt_count: int = PrivateAttr(default=0)
_succeed_on_attempt: int = PrivateAttr()
def __init__(self, succeed_on_attempt: int, **kwargs):
super().__init__(**kwargs)
self._succeed_on_attempt = succeed_on_attempt
def validate(self, value):
self._attempt_count += 1
if self._attempt_count >= self._succeed_on_attempt:
return ValidationOutcome(
call_id="0", # type: ignore
raw_llm_output=value,
validated_output=f"Succeeded on attempt {self._attempt_count}",
validation_passed=True,
)
raise ValidationError(f"Failed attempt {self._attempt_count}")
@property
def attempt_count(self):
return self._attempt_count
guard = CountingGuard(succeed_on_attempt)
runnable = GuardRunnable(guard)
config = (
RunnableConfig(max_retries=max_retries) if max_retries is not None else None
)
if "Failed" in expected_result:
with pytest.raises(ValidationError) as exc_info:
runnable.invoke("test input", config=config)
assert expected_result in str(exc_info.value)
else:
result = runnable.invoke("test input", config=config)
assert result == expected_result
assert guard.attempt_count == expected_attempts

View File

@@ -0,0 +1,95 @@
from typing import Any, Optional
import io
import sys
import pytest
from guardrails.errors import ValidationError
from tests.integration_tests.test_assets.validators import ReadingTime, RegexMatch
@pytest.mark.parametrize(
"output,throws,expected_error",
[
("Ice cream is frozen.", False, None),
(
"Ice cream is a frozen dairy product that is consumed in many places.",
True,
"String should be readable within 0.05 minutes.",
),
("This response isn't relevant.", True, "Result must match Ice cream"),
],
)
def test_validator_runnable(output: str, throws: bool, expected_error: Optional[str]):
from langchain_core.language_models import LanguageModelInput
from langchain_core.messages import AIMessage, BaseMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import Runnable, RunnableConfig
class MockModel(Runnable):
def invoke(
self,
input: LanguageModelInput,
config: Optional[RunnableConfig] = None,
**kwargs: Any,
) -> BaseMessage:
return AIMessage(content=output)
prompt = ChatPromptTemplate.from_template("ELIF: {topic}")
model = MockModel()
regex_match = RegexMatch(
"Ice cream", match_type="search", on_fail="refrain"
).to_runnable()
reading_time = ReadingTime(0.05, on_fail="refrain").to_runnable()
output_parser = StrOutputParser()
chain = prompt | model | regex_match | reading_time | output_parser
topic = "ice cream"
if throws:
with pytest.raises(ValidationError) as exc_info:
chain.invoke({"topic": topic})
assert str(exc_info.value) == (
f"The response from the LLM failed validation! {expected_error}"
)
else:
result = chain.invoke({"topic": topic})
assert result == output
def test_validator_runnable_with_callback_config():
from langchain_core.callbacks import CallbackManager
from langchain_core.tracers import ConsoleCallbackHandler
from langchain_core.runnables import RunnableConfig
console_handler = ConsoleCallbackHandler()
callback_manager = CallbackManager([console_handler])
config_with_callbacks = RunnableConfig(callbacks=callback_manager)
regex_match = RegexMatch(
"Ice cream", match_type="search", on_fail="exception"
).to_runnable()
captured_output = io.StringIO()
sys.stdout = captured_output
result = regex_match.invoke("Ice cream is delicious.", config=config_with_callbacks)
assert result == "Ice cream is delicious."
with pytest.raises(ValidationError) as exc_info:
regex_match.invoke("Chocolate is delicious.", config=config_with_callbacks)
assert "The response from the LLM failed validation!" in str(exc_info.value)
assert "Result must match Ice cream" in str(exc_info.value)
sys.stdout = sys.__stdout__
console_output = captured_output.getvalue()
assert "Ice cream is delicious." in console_output
assert "Chocolate is delicious." in console_output

View File

@@ -0,0 +1,67 @@
import pytest
from guardrails import Guard
from typing import List, Optional
from tests.integration_tests.test_assets.validators import RegexMatch
pytest.importorskip("llama_index")
from llama_index.core.chat_engine.types import ( # noqa
BaseChatEngine, # noqa
AgentChatResponse, # noqa
StreamingAgentChatResponse, # noqa
) # noqa
from llama_index.core.base.llms.types import ChatMessage # noqa
from guardrails.integrations.llama_index import GuardrailsChatEngine # noqa
class MockChatEngine(BaseChatEngine):
def chat(
self, message: str, chat_history: Optional[List[ChatMessage]] = None
) -> AgentChatResponse:
return AgentChatResponse(response="Mock response")
async def achat(
self, message: str, chat_history: Optional[List[ChatMessage]] = None
) -> AgentChatResponse:
return AgentChatResponse(response="Mock async chat response")
def stream_chat(
self, message: str, chat_history: Optional[List[ChatMessage]] = None
):
return StreamingAgentChatResponse(response="Mock stream chat response")
async def astream_chat(
self, message: str, chat_history: Optional[List[ChatMessage]] = None
):
return StreamingAgentChatResponse(response="Mock async stream chat response")
@property
def chat_history(self) -> List[ChatMessage]:
return []
def reset(self):
pass
pytest.importorskip("llama_index")
@pytest.fixture
def guard():
return Guard().use(RegexMatch("Mock response", match_type="search"))
class TestGuardrailsChatEngine:
def test_guardrails_engine_init(self, guard):
engine = MockChatEngine()
guardrails_engine = GuardrailsChatEngine(engine, guard)
assert isinstance(guardrails_engine, GuardrailsChatEngine)
assert guardrails_engine.guard == guard
def test_guardrails_engine_chat(self, guard):
engine = MockChatEngine()
guardrails_engine = GuardrailsChatEngine(engine, guard)
result = guardrails_engine.chat("Mock response")
assert isinstance(result, AgentChatResponse)
assert result.response == "Mock response"

View File

@@ -0,0 +1,63 @@
import pytest
from guardrails import Guard
from guardrails.errors import ValidationError
from typing import Optional
from tests.integration_tests.test_assets.validators import RegexMatch
pytest.importorskip("llama_index")
from llama_index.core.query_engine import BaseQueryEngine # noqa
from llama_index.core.schema import QueryBundle # noqa
from llama_index.core.base.response.schema import Response # noqa
from llama_index.core.prompts.mixin import PromptMixinType # noqa
from llama_index.core.callbacks import CallbackManager # noqa
class MockQueryEngine(BaseQueryEngine):
def __init__(self, callback_manager: Optional[CallbackManager] = None):
super().__init__(callback_manager)
def _query(self, query_bundle: QueryBundle) -> Response:
return Response(response="Mock response")
async def _aquery(self, query_bundle: QueryBundle) -> Response:
return Response(response="Mock async query response")
def _get_prompt_modules(self) -> PromptMixinType:
return {}
@pytest.fixture
def guard():
return Guard().use(RegexMatch("Mock response", match_type="search"))
class TestGuardrailsQueryEngine:
def test_guardrails_engine_init(self, guard):
from guardrails.integrations.llama_index import GuardrailsQueryEngine
engine = MockQueryEngine()
guardrails_engine = GuardrailsQueryEngine(engine, guard)
assert isinstance(guardrails_engine, GuardrailsQueryEngine)
assert guardrails_engine.guard == guard
def test_guardrails_engine_query(self, guard):
from guardrails.integrations.llama_index import GuardrailsQueryEngine
engine = MockQueryEngine()
guardrails_engine = GuardrailsQueryEngine(engine, guard)
result = guardrails_engine._query(QueryBundle(query_str="Mock response"))
assert isinstance(result, Response)
assert result.response == "Mock response"
def test_guardrails_engine_query_validation_failure(self, guard):
from guardrails.integrations.llama_index import GuardrailsQueryEngine
engine = MockQueryEngine()
guardrails_engine = GuardrailsQueryEngine(engine, guard)
engine._query = lambda _: Response(response="Invalid response")
with pytest.raises(ValidationError, match="Validation failed"):
guardrails_engine._query(QueryBundle(query_str="Invalid query"))

View File

@@ -0,0 +1,6 @@
MOCK_EMBEDDINGS = {
"broadcom": [0.91, 0.81, 0.21],
"paypal": [0.89, 0.79, 0.22],
"cisco": [0.9, 0.8, 0.2], # similar example
"taj mahal": [0.03, 0.1, 0.11], # dissimilar example
}

View File

@@ -0,0 +1,182 @@
from guardrails.llm_providers import (
ArbitraryCallable,
AsyncArbitraryCallable,
AsyncLiteLLMCallable,
LiteLLMCallable,
)
from guardrails.classes.llm.llm_response import LLMResponse
from .test_assets import entity_extraction, lists_object, pydantic, python_rail, string
class MockLiteLLMCallableOther(LiteLLMCallable):
# NOTE: this class normally overrides `llm_providers.LiteLLMCallable`,
# which compiles instructions and prompt into a single prompt;
# here the instructions are passed into kwargs and ignored
def _invoke_llm(self, messages, *args, **kwargs):
"""Mock the OpenAI API call to Completion.create."""
_rail_to_compiled_prompt = { # noqa
entity_extraction.RAIL_SPEC_WITH_REASK: entity_extraction.COMPILED_PROMPT,
}
mock_llm_responses = {
entity_extraction.COMPILED_PROMPT: entity_extraction.LLM_OUTPUT,
entity_extraction.COMPILED_PROMPT_REASK: entity_extraction.LLM_OUTPUT_REASK,
entity_extraction.COMPILED_PROMPT_FULL_REASK: entity_extraction.LLM_OUTPUT_FULL_REASK, # noqa: E501
entity_extraction.COMPILED_PROMPT_SKELETON_REASK_1: entity_extraction.LLM_OUTPUT_SKELETON_REASK_1, # noqa: E501
entity_extraction.COMPILED_PROMPT_SKELETON_REASK_2: entity_extraction.LLM_OUTPUT_SKELETON_REASK_2, # noqa: E501
pydantic.COMPILED_PROMPT: pydantic.LLM_OUTPUT,
pydantic.COMPILED_PROMPT_REASK_1: pydantic.LLM_OUTPUT_REASK_1,
pydantic.COMPILED_PROMPT_FULL_REASK_1: pydantic.LLM_OUTPUT_FULL_REASK_1,
pydantic.COMPILED_PROMPT_REASK_2: pydantic.LLM_OUTPUT_REASK_2,
pydantic.COMPILED_PROMPT_FULL_REASK_2: pydantic.LLM_OUTPUT_FULL_REASK_2,
pydantic.COMPILED_PROMPT_ENUM: pydantic.LLM_OUTPUT_ENUM,
pydantic.COMPILED_PROMPT_ENUM_2: pydantic.LLM_OUTPUT_ENUM_2,
string.COMPILED_PROMPT: string.LLM_OUTPUT,
string.COMPILED_PROMPT_REASK: string.LLM_OUTPUT_REASK,
string.COMPILED_LIST_PROMPT: string.LIST_LLM_OUTPUT,
python_rail.VALIDATOR_PARALLELISM_PROMPT_1: python_rail.VALIDATOR_PARALLELISM_RESPONSE_1, # noqa: E501
python_rail.VALIDATOR_PARALLELISM_PROMPT_2: python_rail.VALIDATOR_PARALLELISM_RESPONSE_2, # noqa: E501
python_rail.VALIDATOR_PARALLELISM_PROMPT_3: python_rail.VALIDATOR_PARALLELISM_RESPONSE_3, # noqa: E501
lists_object.LIST_PROMPT: lists_object.LIST_OUTPUT,
}
try:
output = mock_llm_responses[messages[0]["content"]]
return LLMResponse(
output=output,
prompt_token_count=123,
response_token_count=1234,
)
except KeyError:
print("Unrecognized messages!")
print(messages)
raise ValueError("Compiled messages not found")
class MockAsyncLiteLLMCallable(AsyncLiteLLMCallable):
async def invoke_llm(self, prompt, *args, **kwargs):
sync_mock = MockLiteLLMCallable()
return sync_mock._invoke_llm(prompt, *args, **kwargs)
class MockLiteLLMCallable(LiteLLMCallable):
def _invoke_llm(
self,
prompt=None,
instructions=None,
messages=None,
base_model=None,
*args,
**kwargs,
):
"""Mock the OpenAI API call to ChatCompletion.create."""
_rail_to_prompt = {
entity_extraction.RAIL_SPEC_WITH_FIX_CHAT_MODEL: (
entity_extraction.COMPILED_PROMPT_WITHOUT_INSTRUCTIONS,
entity_extraction.COMPILED_INSTRUCTIONS,
)
}
mock_llm_responses = {
(
entity_extraction.COMPILED_PROMPT_WITHOUT_INSTRUCTIONS,
entity_extraction.COMPILED_INSTRUCTIONS,
): entity_extraction.LLM_OUTPUT,
(
entity_extraction.COMPILED_PROMPT_REASK_WITHOUT_INSTRUCTIONS,
entity_extraction.COMPILED_INSTRUCTIONS_REASK,
): entity_extraction.LLM_OUTPUT_REASK,
(
python_rail.COMPILED_PROMPT_1_WITHOUT_INSTRUCTIONS,
python_rail.COMPILED_INSTRUCTIONS,
): python_rail.LLM_OUTPUT_1_FAIL_GUARDRAILS_VALIDATION,
(
python_rail.COMPILED_PROMPT_1_PYDANTIC_2_WITHOUT_INSTRUCTIONS,
python_rail.COMPILED_INSTRUCTIONS,
): python_rail.LLM_OUTPUT_1_FAIL_GUARDRAILS_VALIDATION,
(
python_rail.COMPILED_PROMPT_2_WITHOUT_INSTRUCTIONS,
python_rail.COMPILED_INSTRUCTIONS,
): python_rail.LLM_OUTPUT_2_SUCCEED_GUARDRAILS_BUT_FAIL_PYDANTIC_VALIDATION,
(
string.MSG_COMPILED_PROMPT_REASK,
string.MSG_COMPILED_INSTRUCTIONS_REASK,
): string.MSG_LLM_OUTPUT_CORRECT,
(
pydantic.MSG_COMPILED_PROMPT_REASK,
pydantic.MSG_COMPILED_INSTRUCTIONS_REASK,
): pydantic.MSG_HISTORY_LLM_OUTPUT_CORRECT,
(
pydantic.COMPILED_PROMPT_CHAT,
pydantic.COMPILED_INSTRUCTIONS_CHAT,
): pydantic.LLM_OUTPUT,
(
pydantic.COMPILED_PROMPT_FULL_REASK_1,
pydantic.COMPILED_INSTRUCTIONS_CHAT,
): pydantic.LLM_OUTPUT_FULL_REASK_1,
(
pydantic.COMPILED_PROMPT_FULL_REASK_2,
pydantic.COMPILED_INSTRUCTIONS_CHAT,
): pydantic.LLM_OUTPUT_FULL_REASK_2,
(
string.PARSE_COMPILED_PROMPT_REASK,
string.MSG_COMPILED_INSTRUCTIONS_REASK,
): string.MSG_LLM_OUTPUT_CORRECT,
}
try:
out_text = None
if messages:
if len(messages) == 2:
key = (messages[0]["content"], messages[1]["content"])
elif len(messages) == 1:
key = (messages[0]["content"], None)
if hasattr(mock_llm_responses[key], "read"):
out_text = mock_llm_responses[key]
else:
raise ValueError("specify either prompt and instructions or messages")
return LLMResponse(
output=out_text,
prompt_token_count=123,
response_token_count=1234,
)
except KeyError:
print("Unrecognized prompt!")
print("\n prompt: \n", prompt)
print("\n instructions: \n", instructions)
print("\n messages: \n", messages)
print("\n base_model: \n", base_model)
raise ValueError("Compiled prompt not found in mock llm response")
class MockArbitraryCallable(ArbitraryCallable):
# NOTE: this class normally overrides `llm_providers.ArbitraryCallable`,
# which compiles instructions and prompt into a single prompt;
# here the instructions are passed into kwargs and ignored
def _invoke_llm(self, prompt, *args, **kwargs):
"""Mock an arbitrary callable."""
mock_llm_responses = {
pydantic.PARSING_COMPILED_PROMPT: pydantic.PARSING_UNPARSEABLE_LLM_OUTPUT,
pydantic.PARSING_COMPILED_REASK: pydantic.PARSING_EXPECTED_LLM_OUTPUT,
}
try:
return LLMResponse(
output=mock_llm_responses[prompt],
prompt_token_count=123,
response_token_count=1234,
)
except KeyError:
print(prompt)
raise ValueError("Compiled prompt not found")
class MockAsyncArbitraryCallable(AsyncArbitraryCallable):
async def invoke_llm(self, prompt, *args, **kwargs):
sync_mock = MockArbitraryCallable(kwargs.get("llm_api"))
return sync_mock._invoke_llm(prompt, *args, **kwargs)

View File

@@ -0,0 +1,57 @@
from typing import List
PII_ENTITIES_MAP = {
"pii": [
"EMAIL_ADDRESS",
"PHONE_NUMBER",
"DOMAIN_NAME",
"IP_ADDRESS",
"DATE_TIME",
"LOCATION",
"PERSON",
"URL",
],
"spi": [
"CREDIT_CARD",
"CRYPTO",
"IBAN_CODE",
"NRP",
"MEDICAL_LICENSE",
"US_BANK_NUMBER",
"US_DRIVER_LICENSE",
"US_ITIN",
"US_PASSPORT",
"US_SSN",
],
}
class MockAnalyzerEngine:
"""Mocks the AnalyzerEngine class from presidio-analyzer."""
def __init__(self) -> None:
pass
class MockAnonymizerEngine:
"""Mocks the AnonymizerEngine class from presidio-anonymizer."""
def __init__(self) -> None:
pass
def mock_anonymize(self, text: str, entities: List[str]) -> str:
output = None
if text == "My email address is demo@lol.com, and my phone number is 1234567890":
if entities == [
"EMAIL_ADDRESS",
"PHONE_NUMBER",
] or entities == PII_ENTITIES_MAP.get("pii"):
output = "My email address is <EMAIL_ADDRESS>, and my phone number is <PHONE_NUMBER>" # noqa
elif entities == ["EMAIL_ADDRESS"]:
output = (
"My email address is <EMAIL_ADDRESS>, and my phone number is 1234567890"
)
elif text == "My email address is xyz and my phone number is unavailable.":
output = text
return output

View File

@@ -0,0 +1,51 @@
from typing import Any, Dict, List, Tuple
SECRETS_CODE_SNIPPET = """
import os
import openai
SECRET_TOKEN = "DUMMY_SECRET_TOKEN_abcdefgh"
ADMIN_CREDENTIALS = {"username": "admin", "password": "dummy_admin_password"}
"""
EXPECTED_SECRETS_CODE_SNIPPET = """
import os
import openai
SECRET_TOKEN = "********"
ADMIN_CREDENTIALS = {"username": "admin", "password": "********"}
"""
NO_SECRETS_CODE_SNIPPET = """
import os
import openai
ADMIN_INFO = {"username": "admin", "country": "United States"}
countries = ["United States", "Canada", "Mexico"]
for country in countries:
print(country)
if country == "United States":
print("Found admin_info for United States")
"""
def mock_get_unique_secrets(self, value: str) -> Tuple[Dict[str, Any], List[str]]:
lines = value.split("\n")[:-1]
lines = [line + "\n" for line in lines]
if value == SECRETS_CODE_SNIPPET:
unique_secrets = {
"DUMMY_SECRET_TOKEN_abcdefgh": [5],
"dummy_admin_password": [7],
}
else:
unique_secrets = {}
return unique_secrets, lines
class MockDetectSecrets:
"""Mock class for the detect_secrets package."""
def __init__(self) -> None:
pass

View File

@@ -0,0 +1,72 @@
import json
import re
import pytest
from guardrails.schema.generator import generate_example, gen_string
from guardrails.schema.validator import validate_payload, SchemaValidationError
with open(
"tests/integration_tests/test_assets/json_schemas/choice_case.json", "r"
) as choice_case_json_file:
choice_case_json_schema = json.loads(choice_case_json_file.read())
with open(
"tests/integration_tests/test_assets/json_schemas/choice_case_openapi.json", "r"
) as choice_case_openapi_file:
choice_case_openapi_schema = json.loads(choice_case_openapi_file.read())
with open(
"tests/integration_tests/test_assets/json_schemas/credit_card_agreement.json", "r"
) as credit_card_agreement_file:
credit_card_agreement_schema = json.loads(credit_card_agreement_file.read())
@pytest.mark.parametrize(
"schema",
[
(choice_case_json_schema),
(choice_case_openapi_schema),
(credit_card_agreement_schema),
({"type": "string", "format": "email"}),
({"type": "string", "format": "not a real format"}),
],
)
def test_generate_example(schema):
sample = generate_example(schema)
try:
validate_payload(sample, schema)
except SchemaValidationError as sve:
print(sve)
print(json.dumps(sve.fields, indent=2))
pytest.fail(reason="Schema validation failed!")
except Exception as e:
print(e)
pytest.fail(reason="validate_payload raised an unexpected exception!")
@pytest.mark.parametrize(
"schema,property_name,pattern",
[
(
{"type": "string", "format": "email"},
None,
r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b",
),
({"type": "string", "format": "not a real format"}, None, ".*"),
(
{"type": "string"},
"email",
r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b",
),
],
)
def test_gen_string(schema, property_name, pattern):
sample = gen_string(schema, property_name=property_name)
try:
validate_payload(sample, schema)
except Exception as e:
pytest.fail(reason="validate_payload raises an exception!", msg=str(e))
assert re.fullmatch(pattern, sample)

View File

@@ -0,0 +1,48 @@
import json
from guardrails_ai.types import Validator as ValidatorReference
from guardrails.classes.schema.processed_schema import ProcessedSchema
from guardrails.schema.primitive_schema import primitive_to_schema
from guardrails.classes.output_type import OutputTypes
from guardrails.validator_base import OnFailAction
from tests.integration_tests.test_assets.validators import ValidChoices, ValidLength
class TestPrimitiveSchema:
# Did this one first because it's what I was most concerned about
def test_choice_case_happy_path(self):
with open(
"tests/integration_tests/test_assets/json_schemas/string.json", "r"
) as choice_case_json_file:
expected_schema = json.loads(choice_case_json_file.read())
choice_validator = ValidChoices(choices=["north", "south", "east", "west"])
length_validator = ValidLength(4, 5, "filter")
processed_schema: ProcessedSchema = primitive_to_schema(
validators=[choice_validator, length_validator],
description="Some string...",
)
assert processed_schema.json_schema == expected_schema
assert processed_schema.output_type == OutputTypes.STRING
assert processed_schema.output_type == "str"
assert processed_schema.validators == [
ValidatorReference(
id="valid-choices",
on="$",
on_fail=OnFailAction.EXCEPTION,
kwargs={"choices": ["north", "south", "east", "west"]},
),
ValidatorReference(
id="length",
on="$",
on_fail=OnFailAction.FILTER,
kwargs={"min": 4, "max": 5},
),
]
assert len(processed_schema.validator_map) == 1
assert processed_schema.validator_map.get("$") == [
choice_validator,
length_validator,
]

View File

@@ -0,0 +1,62 @@
import json
from guardrails_ai.types import Validator as ValidatorReference
from guardrails.classes.schema.processed_schema import ProcessedSchema
from guardrails.schema.pydantic_schema import pydantic_model_to_schema
from guardrails.classes.output_type import OutputTypes
from guardrails.validator_base import OnFailAction
from tests.integration_tests.test_assets.pydantic_models.fight_or_flight import (
FightOrFlight,
)
from tests.integration_tests.test_assets.validators.valid_choices import ValidChoices
class TestPydanticSchema:
# Did this one first because it's what I was most concerned about
def test_choice_case_happy_path(self):
with open(
"tests/integration_tests/test_assets/json_schemas/choice_case_openapi.json",
"r",
) as choice_case_json_file:
expected_schema = json.loads(choice_case_json_file.read())
processed_schema: ProcessedSchema = pydantic_model_to_schema(FightOrFlight)
assert processed_schema.json_schema == expected_schema
assert processed_schema.output_type == OutputTypes.DICT
assert processed_schema.output_type == "dict"
assert processed_schema.validators == [
ValidatorReference(
id="valid-choices",
on="$.action.weapon",
on_fail=OnFailAction.REASK,
kwargs={"choices": ["crossbow", "machine gun"]},
),
ValidatorReference(
id="valid-choices",
on="$.action.flight_direction",
on_fail=OnFailAction.EXCEPTION,
kwargs={"choices": ["north", "south", "east", "west"]},
),
ValidatorReference(
id="valid-choices",
on="$.action.distance",
on_fail=OnFailAction.EXCEPTION,
kwargs={"choices": [1, 2, 3, 4]},
),
]
assert len(processed_schema.validator_map) == 3
assert processed_schema.validator_map.get("$.action.distance") == [
ValidChoices(choices=[1, 2, 3, 4], on_fail=OnFailAction.EXCEPTION)
]
assert processed_schema.validator_map.get("$.action.flight_direction") == [
ValidChoices(
choices=["north", "south", "east", "west"],
on_fail=OnFailAction.EXCEPTION,
)
]
assert processed_schema.validator_map.get("$.action.weapon") == [
ValidChoices(
choices=["crossbow", "machine gun"], on_fail=OnFailAction.REASK
)
]

View File

@@ -0,0 +1,179 @@
import json
import pytest
from xml.etree.ElementTree import canonicalize
from guardrails_ai.types import Validator as ValidatorReference
from guardrails.classes.schema.processed_schema import ProcessedSchema
from guardrails.schema.rail_schema import (
rail_file_to_schema,
json_schema_to_rail_output,
)
from guardrails.classes.output_type import OutputTypes
from guardrails.validator_base import OnFailAction
from tests.integration_tests.test_assets.validators import (
ValidChoices,
LowerCase,
OneLine,
TwoWords,
)
### JSON Schemas ###
with open(
"tests/integration_tests/test_assets/json_schemas/choice_case.json", "r"
) as choice_case_json_file:
choice_case_json_schema = json.loads(choice_case_json_file.read())
with open(
"tests/integration_tests/test_assets/json_schemas/choice_case_openapi.json", "r"
) as choice_case_openapi_file:
choice_case_openapi_schema = json.loads(choice_case_openapi_file.read())
with open(
"tests/integration_tests/test_assets/json_schemas/credit_card_agreement.json", "r"
) as credit_card_agreement_file:
credit_card_agreement_schema = json.loads(credit_card_agreement_file.read())
with open(
"tests/integration_tests/test_assets/json_schemas/string.json", "r"
) as string_file:
string_schema = json.loads(string_file.read())
class TestRailToJsonSchema:
# Did this one first because it's what I was most concerned about
def test_choice_case_happy_path(self):
from tests.integration_tests.test_assets.validators.valid_choices import (
ValidChoices,
)
processed_schema: ProcessedSchema = rail_file_to_schema(
"tests/integration_tests/test_assets/rail_specs/choice_case.rail"
)
assert processed_schema.json_schema == choice_case_json_schema
assert processed_schema.output_type == OutputTypes.DICT
assert processed_schema.output_type == "dict"
assert processed_schema.validators == [
ValidatorReference(
id="valid-choices",
on="$.action.weapon",
on_fail=OnFailAction.REASK,
kwargs={"choices": ["crossbow", "machine gun"]},
),
ValidatorReference(
id="valid-choices",
on="$.action.flight_direction",
on_fail=OnFailAction.EXCEPTION,
kwargs={"choices": ["north", "south", "east", "west"]},
),
ValidatorReference(
id="valid-choices",
on="$.action.distance",
on_fail=OnFailAction.EXCEPTION,
kwargs={"choices": [1, 2, 3, 4]},
),
]
assert len(processed_schema.validator_map) == 3
assert processed_schema.validator_map.get("$.action.distance") == [
ValidChoices(choices=[1, 2, 3, 4], on_fail=OnFailAction.EXCEPTION)
]
assert processed_schema.validator_map.get("$.action.flight_direction") == [
ValidChoices(
choices=["north", "south", "east", "west"],
on_fail=OnFailAction.EXCEPTION,
)
]
assert processed_schema.validator_map.get("$.action.weapon") == [
ValidChoices(
choices=["crossbow", "machine gun"], on_fail=OnFailAction.REASK
)
]
### ReConstructed RAIL Specs for Prompting ###
case_choice_rail = """
<output>
<choice name="action" discriminator="chosen_action" required="true">
<case name="fight">
<string name="weapon" format="valid-choices: ['crossbow', 'machine gun']" required="true" />
</case>
<case name="flight">
<string name="flight_direction" format="valid-choices: ['north', 'south', 'east', 'west']" required="false" />
<integer name="distance" format="valid-choices: [1, 2, 3, 4]" required="true" />
</case>
</choice>
</output>
""".strip() # noqa
# flight_direction.required is true here because Pydantic compiles optional properties
# as a Union of the actual type and null but still marks it as required...
case_choice_openapi_rail = """
<output>
<choice name="action" discriminator="chosen_action" required="true">
<case name="fight">
<string name="weapon" format="valid-choices: ['crossbow', 'machine gun']" required="true" />
</case>
<case name="flight">
<string name="flight_direction" format="valid-choices: ['north', 'south', 'east', 'west']" required="true" />
<integer name="distance" format="valid-choices: [1, 2, 3, 4]" required="true" />
</case>
</choice>
</output>
""".strip() # noqa
credit_card_agreement_rail = """
<output>
<list name="fees" description="What fees and charges are associated with my account?" required="true">
<object required="true" >
<integer name="index" format="1-indexed" required="true" />
<string name="name" format="lower-case; two-words" required="true" />
<string name="explanation" format="one-line" required="true" />
<float name="value" format="percentage" required="true" />
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" required="true" />
</output>
""".strip() # noqa
string_schema_rail = """
<output type="string" description="Some string..." format="lower-case; two-words" />
""".strip() # noqa
### Validator Maps ###
case_choice_validator_map = {
"$.action.weapon": [ValidChoices(["crossbow", "machine gun"], OnFailAction.REASK)],
"$.action.flight_direction": [
ValidChoices(["north", "south", "east", "west"], OnFailAction.EXCEPTION)
],
"$.action.distance": [ValidChoices([1, 2, 3, 4], OnFailAction.EXCEPTION)],
}
credit_card_agreement_validator_map = {
"$.fees.name": [LowerCase(), TwoWords()],
"$.fees.explanation": [OneLine()],
}
@pytest.mark.parametrize(
"json_schema,validator_map,rail_output",
[
(choice_case_json_schema, case_choice_validator_map, case_choice_rail),
(
choice_case_openapi_schema,
case_choice_validator_map,
case_choice_openapi_rail,
),
(
credit_card_agreement_schema,
credit_card_agreement_validator_map,
credit_card_agreement_rail,
),
],
)
def test_json_schema_to_rail_output(json_schema, validator_map, rail_output):
actual_rail_output = json_schema_to_rail_output(json_schema, validator_map)
actual_rail_xml = canonicalize(actual_rail_output)
expected_rail_xml = canonicalize(rail_output)
assert actual_rail_xml == expected_rail_xml

View File

@@ -0,0 +1,113 @@
import json
import pytest
from guardrails.schema.validator import SchemaValidationError, validate_payload
with open(
"tests/integration_tests/test_assets/json_schemas/choice_case.json", "r"
) as choice_case_json_file:
schema = json.loads(choice_case_json_file.read())
class TestValidatePayload:
def test_happy_path(self):
payload = {"action": {"chosen_action": "fight", "weapon": "crossbow"}}
validate_payload(payload, schema)
def test_extra_properties_allowed(self):
payload = {
"action": {"chosen_action": "fight", "weapon": "crossbow"},
"reason": "Peregrin Took is a brave hobbit",
}
validate_payload(payload, schema)
def test_failure_invalid_discriminator_value(self):
payload = {"action": {"chosen_action": "dance", "type": "jig"}}
with pytest.raises(Exception) as excinfo:
validate_payload(payload, schema)
assert isinstance(excinfo.value, SchemaValidationError) is True
schema_error: SchemaValidationError = excinfo.value
assert (
str(schema_error)
== "The provided payload is not compliant with the provided schema!"
)
assert schema_error.fields == {
"$.action.chosen_action": ["'dance' is not one of ['fight', 'flight']"]
}
def test_failure_invalid_type(self):
payload = {
"action": {
"chosen_action": "flight",
"flight_direction": "north",
"distance": "2",
}
}
with pytest.raises(Exception) as excinfo:
validate_payload(payload, schema)
assert isinstance(excinfo.value, SchemaValidationError) is True
schema_error: SchemaValidationError = excinfo.value
assert (
str(schema_error)
== "The provided payload is not compliant with the provided schema!"
)
# Type coercion is not automatic!
assert schema_error.fields == {
"$.action.distance": ["'2' is not of type 'integer'"]
}
# NOTE: Technically the same as an invalid type
def test_failure_invalid_structure(self):
payload = {
"action": [
{
"chosen_action": "flight",
"flight_direction": "north",
"distance": "2",
}
]
}
with pytest.raises(Exception) as excinfo:
validate_payload(payload, schema)
assert isinstance(excinfo.value, SchemaValidationError) is True
schema_error: SchemaValidationError = excinfo.value
assert (
str(schema_error)
== "The provided payload is not compliant with the provided schema!"
)
assert schema_error.fields == {
"$.action": [
"[{'chosen_action': 'flight', 'flight_direction': 'north', 'distance': '2'}] is not of type 'object'" # noqa
]
}
def test_failure_missing_required_properties(self):
payload = {"action": {"chosen_action": "flight"}}
with pytest.raises(Exception) as excinfo:
validate_payload(payload, schema)
assert isinstance(excinfo.value, SchemaValidationError) is True
schema_error: SchemaValidationError = excinfo.value
assert (
str(schema_error)
== "The provided payload is not compliant with the provided schema!"
)
assert schema_error.fields == {
"$.action": ["'distance' is a required property"]
}
def test_subschema_validation(self):
# Missing required properites, but that's allowed with validate_subschema
payload = {"action": {"chosen_action": "flight"}}
validate_payload(payload, schema, validate_subschema=True)

View File

@@ -0,0 +1,14 @@
def mock_llm(
messages,
*args,
**kwargs,
) -> str:
return ""
async def mock_async_llm(
messages,
*args,
**kwargs,
) -> str:
return ""

View File

@@ -0,0 +1,118 @@
# ruff: noqa: E501
import os
from .optional_prompts import (
OPTIONAL_INSTRUCTIONS_CHAT_MODEL,
OPTIONAL_MSG_HISTORY,
OPTIONAL_PROMPT_CHAT_MODEL,
OPTIONAL_PROMPT_COMPLETION_MODEL,
)
from .pydantic_models import (
INSTRUCTIONS_CHAT_MODEL,
PROMPT,
PROMPT_CHAT_MODEL,
ContractDetailsFilter,
ContractDetailsFix,
ContractDetailsNoop,
ContractDetailsReask,
ContractDetailsRefrain,
)
from .validated_output_filter import VALIDATED_OUTPUT_FILTER
from .validated_output_fix import VALIDATED_OUTPUT_FIX
from .validated_output_noop import VALIDATED_OUTPUT_NOOP
from .validated_output_reask_1 import VALIDATED_OUTPUT_REASK_1
from .validated_output_reask_2 import VALIDATED_OUTPUT_REASK_2
from .validated_output_refrain import VALIDATED_OUTPUT_REFRAIN
from .validated_output_skeleton_reask_1 import VALIDATED_OUTPUT_SKELETON_REASK_1
from .validated_output_skeleton_reask_2 import VALIDATED_OUTPUT_SKELETON_REASK_2
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)))
reader = (
lambda filename: open(os.path.join(DATA_DIR, filename)).read().replace("\r", "")
)
# Compiled prompts
COMPILED_PROMPT = reader("compiled_prompt.txt")
NON_OPENAI_COMPILED_PROMPT = reader("non_openai_compiled_prompt.txt")
COMPILED_PROMPT_WITHOUT_INSTRUCTIONS = reader(
"compiled_prompt_without_instructions.txt"
)
COMPILED_PROMPT_REASK = reader("compiled_prompt_reask.txt")
NON_OPENAI_COMPILED_PROMPT_REASK = reader("non_openai_compiled_prompt_reask.txt")
COMPILED_PROMPT_REASK_WITHOUT_INSTRUCTIONS = reader(
"compiled_prompt_reask_without_instructions.txt"
)
COMPILED_PROMPT_FULL_REASK = reader("compiled_prompt_full_reask.txt")
COMPILED_INSTRUCTIONS = reader("compiled_instructions.txt")
COMPILED_INSTRUCTIONS_REASK = reader("compiled_instructions_reask.txt")
COMPILED_PROMPT_SKELETON_REASK_1 = reader("compiled_prompt_skeleton_reask_1.txt")
COMPILED_PROMPT_SKELETON_REASK_2 = reader("compiled_prompt_skeleton_reask_2.txt")
COMPILED_MSG_HISTORY = [
{"role": "system", "content": COMPILED_INSTRUCTIONS},
{"role": "user", "content": COMPILED_PROMPT_WITHOUT_INSTRUCTIONS},
]
# LLM outputs
LLM_OUTPUT = reader("llm_output.txt")
LLM_OUTPUT_REASK = reader("llm_output_reask.txt")
LLM_OUTPUT_FULL_REASK = reader("llm_output_full_reask.txt")
LLM_OUTPUT_SKELETON_REASK_1 = reader("llm_output_skeleton_reask_1.txt")
LLM_OUTPUT_SKELETON_REASK_2 = reader("llm_output_skeleton_reask_2.txt")
# Rail specs
RAIL_SPEC_WITH_FILTER = reader("filter.rail")
RAIL_SPEC_WITH_FIX = reader("fix.rail")
RAIL_SPEC_WITH_NOOP = reader("noop.rail")
RAIL_SPEC_WITH_REASK = reader("reask.rail")
RAIL_SPEC_WITH_SKELETON_REASK = reader("skeleton_reask.rail")
RAIL_SPEC_WITH_REFRAIN = reader("refrain.rail")
RAIL_SPEC_WITH_FIX_CHAT_MODEL = reader("fix_chat_model.rail")
# Rail specs without prompts and instructions
RAIL_SPEC_WITH_REASK_NO_PROMPT = reader("reask_without_prompt.rail")
# Pydantic models
PYDANTIC_RAIL_WITH_FILTER = ContractDetailsFilter
PYDANTIC_RAIL_WITH_FIX = ContractDetailsFix
PYDANTIC_RAIL_WITH_NOOP = ContractDetailsNoop
PYDANTIC_RAIL_WITH_REASK = ContractDetailsReask
PYDANTIC_RAIL_WITH_REFRAIN = ContractDetailsRefrain
PYDANTIC_PROMPT = PROMPT
PYDANTIC_PROMPT_CHAT_MODEL = PROMPT_CHAT_MODEL
PYDANTIC_INSTRUCTIONS_CHAT_MODEL = INSTRUCTIONS_CHAT_MODEL
__all__ = [
"COMPILED_PROMPT",
"NON_OPENAI_COMPILED_PROMPT",
"COMPILED_PROMPT_WITHOUT_INSTRUCTIONS",
"COMPILED_PROMPT_REASK",
"NON_OPENAI_COMPILED_PROMPT_REASK",
"COMPILED_PROMPT_REASK_WITHOUT_INSTRUCTIONS",
"COMPILED_INSTRUCTIONS",
"COMPILED_INSTRUCTIONS_REASK",
"COMPILED_MSG_HISTORY",
"COMPILED_MSG_HISTORY_PROMPT",
"LLM_OUTPUT",
"LLM_OUTPUT_REASK",
"PYDANTIC_INSTRUCTIONS",
"PYDANTIC_PROMPT",
"RAIL_SPEC_WITH_FILTER",
"RAIL_SPEC_WITH_FIX",
"RAIL_SPEC_WITH_FIX_CHAT_MODEL",
"RAIL_SPEC_WITH_NOOP",
"RAIL_SPEC_WITH_REASK",
"RAIL_SPEC_WITH_REFRAIN",
"VALIDATED_OUTPUT_FILTER",
"VALIDATED_OUTPUT_FIX",
"VALIDATED_OUTPUT_NOOP",
"VALIDATED_OUTPUT_REASK_1",
"VALIDATED_OUTPUT_REASK_2",
"VALIDATED_OUTPUT_REFRAIN",
"VALIDATED_OUTPUT_SKELETON_REASK_1",
"VALIDATED_OUTPUT_SKELETON_REASK_2",
"OPTIONAL_PROMPT_COMPLETION_MODEL",
"OPTIONAL_PROMPT_CHAT_MODEL",
"OPTIONAL_INSTRUCTIONS_CHAT_MODEL",
"OPTIONAL_MSG_HISTORY",
]

View File

@@ -0,0 +1,11 @@
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

View File

@@ -0,0 +1,9 @@
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

View File

@@ -0,0 +1,131 @@
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
2/25/23, 7:59 PM about:blank
about:blank 1/4
PRICING INFORMATION
INTEREST RATES AND INTEREST CHARGES
Purchase Annual
Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
My Chase Loan
SM APR 19.49%. This APR will vary with the market based on the Prime Rate.
a
Promotional offers with fixed APRs and varying durations may be available from
time to time on some accounts.
Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.
b
Penalty APR and When
It Applies
Up to 29.99%. This APR will vary with the market based on the Prime Rate.
c
We may apply the Penalty APR to your account if you:
fail to make a Minimum Payment by the date and time that it is due; or
make a payment to us that is returned unpaid.
How Long Will the Penalty APR Apply?: If we apply the Penalty APR for
either of these reasons, the Penalty APR could potentially remain in effect
indefinitely.
How to Avoid Paying
Interest on Purchases
Your due date will be a minimum of 21 days after the close of each billing cycle.
We will not charge you interest on new purchases if you pay your entire balance
or Interest Saving Balance by the due date each month. We will begin charging
interest on balance transfers and cash advances on the transaction date.
Minimum Interest
Charge
None
Credit Card Tips from
the Consumer Financial
Protection Bureau
To learn more about factors to consider when applying for or using a credit card,
visit the website of the Consumer Financial Protection Bureau at
http://www.consumerfinance.gov/learnmore.
FEES
Annual Membership
Fee
None
My Chase Plan
SM Fee
(fixed finance charge)
Monthly fee of 0% of the amount of each eligible purchase transaction or
amount selected to create a My Chase Plan while in the 0% Intro Purchase
APR period.
After that, monthly fee of 1.72% of the amount of each eligible purchase
transaction or amount selected to create a My Chase Plan. The My Chase Plan
Fee will be determined at the time each My Chase Plan is created and will
remain the same until the My Chase Plan is paid in full.
d
Transaction Fees
Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater,
on transfers made within 60 days of account opening. After that: Either $5 or 5%
of the amount of each transfer, whichever is greater.
Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.
2/25/23, 7:59 PM about:blank
about:blank 2/4
Foreign Transactions 3% of the amount of each transaction in U.S. dollars.
Penalty Fees
Late Payment Up to $40.
Over-the-Credit-Limit None
Return Payment Up to $40.
Return Check None
Note: This account may not be eligible for balance transfers.
Loss of Intro APR: We will end your introductory APR if any required Minimum Payment is 60 days late, and
apply the Penalty APR.
How We Will Calculate Your Balance: We use the daily balance method (including new transactions).
Prime Rate: Variable APRs are based on the 7.75% Prime Rate as of 2/7/2023.
aWe add 11.74% to the Prime Rate to determine the Purchase/My Chase Loan/Balance Transfer APR.
Maximum APR 29.99%.
bWe add 21.74% to the Prime Rate to determine the Cash Advance APR. Maximum APR 29.99%.
cWe add up to 26.99% to the Prime Rate to determine the Penalty APR. Maximum APR 29.99%.
dMy Chase Plan Fee: The My Chase Plan Fee is calculated at the time each plan is created and is based on
the amount of each purchase transaction or amount selected to create the plan, the number of billing periods
you choose to pay the balance in full, and other factors. The monthly and aggregate dollar amount of your My
Chase Plan Fee will be disclosed during the activation of each My Chase Plan.
MILITARY LENDING ACT NOTICE: Federal law provides important protections to members of the Armed
Forces and their dependents relating to extensions of consumer credit. In general, the cost of consumer credit
to a member of the Armed Forces and his or her dependent may not exceed an annual percentage rate of 36
percent. This rate must include, as applicable to the credit transaction or account: the costs associated with
credit insurance premiums; fees for ancillary products sold in connection with the credit transaction; any
application fee charged (other than certain application fees for specified credit transactions or accounts); and
any participation fee charged (other than certain participation fees for a credit card account). To receive this
information and a description of your payment obligation verbally, please call 1-800-235-9978.
TERMS & CONDITIONS
Authorization: When you respond to this credit card offer from JPMorgan Chase Bank, N.A., Member FDIC, a
subsidiary of JPMorgan Chase & Co. ("Chase", "we", or "us"), you agree to the following:
1. You authorize us to obtain credit bureau reports, employment, and income information about you that we
will use when considering your application for credit. We may obtain and use information about your
accounts with us and others such as Checking, Deposit, Investment, and Utility accounts from credit
bureaus and other entities. You also authorize us to obtain credit bureau reports and any other
information about you in connection with: 1) extensions of credit on your account; 2) the administration,
review or collection of your account; and 3) offering you enhanced or additional products and services. If
you ask, we will tell you the name and address of the credit bureau from which we obtained a report
about you.
2. If an account is opened, you will receive a Cardmember Agreement with your card(s). You agree to the
terms of this agreement by: using the account or any card, authorizing their use, or making any payment
on the account.
3. By providing your mobile ph
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.

View File

@@ -0,0 +1,112 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0
},
{
"index": 2,
"name": {
"incorrect_value": "my chase plan",
"error_messages": [
"must be exactly two words"
]
},
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5.0
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5.0
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3.0
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0
},
{
"index": 7,
"name": {
"incorrect_value": "over-the-credit-limit",
"error_messages": [
"must be exactly two words"
]
},
"explanation": "Over-the-Credit-Limit None",
"value": 0
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0
}
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate."
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate."
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate."
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate."
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate."
},
"maximum_apr": 29.99
}
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,41 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"fees": [
{
"name": {
"incorrect_value": "my chase plan",
"error_messages": [
"must be exactly two words"
]
}
},
{
"name": {
"incorrect_value": "over-the-credit-limit",
"error_messages": [
"must be exactly two words"
]
}
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,41 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"fees": [
{
"name": {
"incorrect_value": "my chase plan",
"error_messages": [
"must be exactly two words"
]
}
},
{
"name": {
"incorrect_value": "over-the-credit-limit",
"error_messages": [
"must be exactly two words"
]
}
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,131 @@
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter
'None'.
2/25/23, 7:59 PM about:blank
about:blank 1/4
PRICING INFORMATION
INTEREST RATES AND INTEREST CHARGES
Purchase Annual
Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
My Chase Loan
SM APR 19.49%. This APR will vary with the market based on the Prime Rate.
a
Promotional offers with fixed APRs and varying durations may be available from
time to time on some accounts.
Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.
b
Penalty APR and When
It Applies
Up to 29.99%. This APR will vary with the market based on the Prime Rate.
c
We may apply the Penalty APR to your account if you:
fail to make a Minimum Payment by the date and time that it is due; or
make a payment to us that is returned unpaid.
How Long Will the Penalty APR Apply?: If we apply the Penalty APR for
either of these reasons, the Penalty APR could potentially remain in effect
indefinitely.
How to Avoid Paying
Interest on Purchases
Your due date will be a minimum of 21 days after the close of each billing cycle.
We will not charge you interest on new purchases if you pay your entire balance
or Interest Saving Balance by the due date each month. We will begin charging
interest on balance transfers and cash advances on the transaction date.
Minimum Interest
Charge
None
Credit Card Tips from
the Consumer Financial
Protection Bureau
To learn more about factors to consider when applying for or using a credit card,
visit the website of the Consumer Financial Protection Bureau at
http://www.consumerfinance.gov/learnmore.
FEES
Annual Membership
Fee
None
My Chase Plan
SM Fee
(fixed finance charge)
Monthly fee of 0% of the amount of each eligible purchase transaction or
amount selected to create a My Chase Plan while in the 0% Intro Purchase
APR period.
After that, monthly fee of 1.72% of the amount of each eligible purchase
transaction or amount selected to create a My Chase Plan. The My Chase Plan
Fee will be determined at the time each My Chase Plan is created and will
remain the same until the My Chase Plan is paid in full.
d
Transaction Fees
Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater,
on transfers made within 60 days of account opening. After that: Either $5 or 5%
of the amount of each transfer, whichever is greater.
Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.
2/25/23, 7:59 PM about:blank
about:blank 2/4
Foreign Transactions 3% of the amount of each transaction in U.S. dollars.
Penalty Fees
Late Payment Up to $40.
Over-the-Credit-Limit None
Return Payment Up to $40.
Return Check None
Note: This account may not be eligible for balance transfers.
Loss of Intro APR: We will end your introductory APR if any required Minimum Payment is 60 days late, and
apply the Penalty APR.
How We Will Calculate Your Balance: We use the daily balance method (including new transactions).
Prime Rate: Variable APRs are based on the 7.75% Prime Rate as of 2/7/2023.
aWe add 11.74% to the Prime Rate to determine the Purchase/My Chase Loan/Balance Transfer APR.
Maximum APR 29.99%.
bWe add 21.74% to the Prime Rate to determine the Cash Advance APR. Maximum APR 29.99%.
cWe add up to 26.99% to the Prime Rate to determine the Penalty APR. Maximum APR 29.99%.
dMy Chase Plan Fee: The My Chase Plan Fee is calculated at the time each plan is created and is based on
the amount of each purchase transaction or amount selected to create the plan, the number of billing periods
you choose to pay the balance in full, and other factors. The monthly and aggregate dollar amount of your My
Chase Plan Fee will be disclosed during the activation of each My Chase Plan.
MILITARY LENDING ACT NOTICE: Federal law provides important protections to members of the Armed
Forces and their dependents relating to extensions of consumer credit. In general, the cost of consumer credit
to a member of the Armed Forces and his or her dependent may not exceed an annual percentage rate of 36
percent. This rate must include, as applicable to the credit transaction or account: the costs associated with
credit insurance premiums; fees for ancillary products sold in connection with the credit transaction; any
application fee charged (other than certain application fees for specified credit transactions or accounts); and
any participation fee charged (other than certain participation fees for a credit card account). To receive this
information and a description of your payment obligation verbally, please call 1-800-235-9978.
TERMS & CONDITIONS
Authorization: When you respond to this credit card offer from JPMorgan Chase Bank, N.A., Member FDIC, a
subsidiary of JPMorgan Chase & Co. ("Chase", "we", or "us"), you agree to the following:
1. You authorize us to obtain credit bureau reports, employment, and income information about you that we
will use when considering your application for credit. We may obtain and use information about your
accounts with us and others such as Checking, Deposit, Investment, and Utility accounts from credit
bureaus and other entities. You also authorize us to obtain credit bureau reports and any other
information about you in connection with: 1) extensions of credit on your account; 2) the administration,
review or collection of your account; and 3) offering you enhanced or additional products and services. If
you ask, we will tell you the name and address of the credit bureau from which we obtained a report
about you.
2. If an account is opened, you will receive a Cardmember Agreement with your card(s). You agree to the
terms of this agreement by: using the account or any card, authorizing their use, or making any payment
on the account.
3. By providing your mobile ph
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.

View File

@@ -0,0 +1,99 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"incorrect_value": {
"fees": [
{
"name": "annual membership fee",
"value": 0.0
},
{
"name": "my chase plan fee",
"value": 1.72
},
{
"name": "balance transfers",
"value": 5.0
},
{
"name": "cash advances",
"value": 5.0
},
{
"name": "foreign transactions",
"value": 3.0
},
{
"name": "late payment",
"value": 0.0
},
{
"name": "over-the-credit-limit",
"value": 0.0
},
{
"name": "return payment",
"value": 0.0
},
{
"name": "return check",
"value": 0.0
}
],
"interest_rates": {
"purchase": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"balance_transfer": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"cash_advance": {
"annual_percentage_rate": 29.49,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"penalty": {
"annual_percentage_rate": 0.0,
"variation_explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
"when_applies": "We may apply the Penalty APR to your account if you: fail to make a Minimum Payment by the date and time that it is due; or make a payment to us that is returned unpaid.",
"how_long_apr_applies": "If we apply the Penalty APR for either of these reasons, the Penalty APR could potentially remain in effect indefinitely."
}
}
},
"error_messages": [
"JSON does not match schema:\n{\n \"$.fees[0]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[1]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[2]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[3]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[4]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[5]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[6]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[7]\": [\n \"'explanation' is a required property\"\n ],\n \"$.fees[8]\": [\n \"'explanation' is a required property\"\n ]\n}"
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here's an example of the structure:
{
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0
}
],
"interest_rates": {}
}

View File

@@ -0,0 +1,130 @@
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter `null`.
2/25/23, 7:59 PM about:blank
about:blank 1/4
PRICING INFORMATION
INTEREST RATES AND INTEREST CHARGES
Purchase Annual
Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
My Chase Loan
SM APR 19.49%. This APR will vary with the market based on the Prime Rate.
a
Promotional offers with fixed APRs and varying durations may be available from
time to time on some accounts.
Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.
b
Penalty APR and When
It Applies
Up to 29.99%. This APR will vary with the market based on the Prime Rate.
c
We may apply the Penalty APR to your account if you:
fail to make a Minimum Payment by the date and time that it is due; or
make a payment to us that is returned unpaid.
How Long Will the Penalty APR Apply?: If we apply the Penalty APR for
either of these reasons, the Penalty APR could potentially remain in effect
indefinitely.
How to Avoid Paying
Interest on Purchases
Your due date will be a minimum of 21 days after the close of each billing cycle.
We will not charge you interest on new purchases if you pay your entire balance
or Interest Saving Balance by the due date each month. We will begin charging
interest on balance transfers and cash advances on the transaction date.
Minimum Interest
Charge
None
Credit Card Tips from
the Consumer Financial
Protection Bureau
To learn more about factors to consider when applying for or using a credit card,
visit the website of the Consumer Financial Protection Bureau at
http://www.consumerfinance.gov/learnmore.
FEES
Annual Membership
Fee
None
My Chase Plan
SM Fee
(fixed finance charge)
Monthly fee of 0% of the amount of each eligible purchase transaction or
amount selected to create a My Chase Plan while in the 0% Intro Purchase
APR period.
After that, monthly fee of 1.72% of the amount of each eligible purchase
transaction or amount selected to create a My Chase Plan. The My Chase Plan
Fee will be determined at the time each My Chase Plan is created and will
remain the same until the My Chase Plan is paid in full.
d
Transaction Fees
Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater,
on transfers made within 60 days of account opening. After that: Either $5 or 5%
of the amount of each transfer, whichever is greater.
Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.
2/25/23, 7:59 PM about:blank
about:blank 2/4
Foreign Transactions 3% of the amount of each transaction in U.S. dollars.
Penalty Fees
Late Payment Up to $40.
Over-the-Credit-Limit None
Return Payment Up to $40.
Return Check None
Note: This account may not be eligible for balance transfers.
Loss of Intro APR: We will end your introductory APR if any required Minimum Payment is 60 days late, and
apply the Penalty APR.
How We Will Calculate Your Balance: We use the daily balance method (including new transactions).
Prime Rate: Variable APRs are based on the 7.75% Prime Rate as of 2/7/2023.
aWe add 11.74% to the Prime Rate to determine the Purchase/My Chase Loan/Balance Transfer APR.
Maximum APR 29.99%.
bWe add 21.74% to the Prime Rate to determine the Cash Advance APR. Maximum APR 29.99%.
cWe add up to 26.99% to the Prime Rate to determine the Penalty APR. Maximum APR 29.99%.
dMy Chase Plan Fee: The My Chase Plan Fee is calculated at the time each plan is created and is based on
the amount of each purchase transaction or amount selected to create the plan, the number of billing periods
you choose to pay the balance in full, and other factors. The monthly and aggregate dollar amount of your My
Chase Plan Fee will be disclosed during the activation of each My Chase Plan.
MILITARY LENDING ACT NOTICE: Federal law provides important protections to members of the Armed
Forces and their dependents relating to extensions of consumer credit. In general, the cost of consumer credit
to a member of the Armed Forces and his or her dependent may not exceed an annual percentage rate of 36
percent. This rate must include, as applicable to the credit transaction or account: the costs associated with
credit insurance premiums; fees for ancillary products sold in connection with the credit transaction; any
application fee charged (other than certain application fees for specified credit transactions or accounts); and
any participation fee charged (other than certain participation fees for a credit card account). To receive this
information and a description of your payment obligation verbally, please call 1-800-235-9978.
TERMS & CONDITIONS
Authorization: When you respond to this credit card offer from JPMorgan Chase Bank, N.A., Member FDIC, a
subsidiary of JPMorgan Chase & Co. ("Chase", "we", or "us"), you agree to the following:
1. You authorize us to obtain credit bureau reports, employment, and income information about you that we
will use when considering your application for credit. We may obtain and use information about your
accounts with us and others such as Checking, Deposit, Investment, and Utility accounts from credit
bureaus and other entities. You also authorize us to obtain credit bureau reports and any other
information about you in connection with: 1) extensions of credit on your account; 2) the administration,
review or collection of your account; and 3) offering you enhanced or additional products and services. If
you ask, we will tell you the name and address of the credit bureau from which we obtained a report
about you.
2. If an account is opened, you will receive a Cardmember Agreement with your card(s). You agree to the
terms of this agreement by: using the account or any card, authorizing their use, or making any payment
on the account.
3. By providing your mobile ph
Extract information from this document and return a JSON that follows the correct schema.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>

View File

@@ -0,0 +1,30 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="filter"
on-fail-two-words="filter"/>
<string name="explanation" validators="one-line" on-fail-one-line="filter" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}</message>
</messages>
</rail>

View File

@@ -0,0 +1,29 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="fix" on-fail-two-words="fix"/>
<string name="explanation" validators="one-line" on-fail-one-line="fix" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}</message>
</messages>
</rail>

View File

@@ -0,0 +1,35 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="fix" on-fail-two-words="fix"/>
<string name="explanation" validators="one-line" on-fail-one-line="fix" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="system">
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
${gr.xml_suffix_prompt_examples}
</message>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter `null`.
${document}
Extract information from this document and return a JSON that follows the correct schema.
${gr.xml_prefix_prompt}
${xml_output_schema}
</message>
</messages>
</rail>

View File

@@ -0,0 +1,81 @@
{
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0
},
{
"index": 2,
"name": "my chase plan",
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0
},
{
"index": 7,
"name": "over-the-credit-limit",
"explanation": "Over-the-Credit-Limit None",
"value": 0
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0
}
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate."
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate."
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate."
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate."
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate."
},
"maximum_apr": 29.99
}
}

View File

@@ -0,0 +1,81 @@
{
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0
},
{
"index": 2,
"name": "my chase",
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0
},
{
"index": 7,
"name": "over-the-credit-limit",
"explanation": "Over-the-Credit-Limit None",
"value": 0
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0
}
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate."
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate."
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate."
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate."
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate."
},
"maximum_apr": 29.99
}
}

View File

@@ -0,0 +1,10 @@
{
"fees": [
{
"name": "my chase"
},
{
"name": "over-the-credit-limit"
}
]
}

View File

@@ -0,0 +1,60 @@
{
"fees": [
{
"name": "annual membership fee",
"value": 0.0
},
{
"name": "my chase plan fee",
"value": 1.72
},
{
"name": "balance transfers",
"value": 5.0
},
{
"name": "cash advances",
"value": 5.0
},
{
"name": "foreign transactions",
"value": 3.0
},
{
"name": "late payment",
"value": 0.0
},
{
"name": "over-the-credit-limit",
"value": 0.0
},
{
"name": "return payment",
"value": 0.0
},
{
"name": "return check",
"value": 0.0
}
],
"interest_rates": {
"purchase": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"balance_transfer": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"cash_advance": {
"annual_percentage_rate": 29.49,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"penalty": {
"annual_percentage_rate": 0.0,
"variation_explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
"when_applies": "We may apply the Penalty APR to your account if you: fail to make a Minimum Payment by the date and time that it is due; or make a payment to us that is returned unpaid.",
"how_long_apr_applies": "If we apply the Penalty APR for either of these reasons, the Penalty APR could potentially remain in effect indefinitely."
}
}
}

View File

@@ -0,0 +1,69 @@
{
"fees": [
{
"name": "annual_membership_fee",
"explanation": "",
"value": 0.0
},
{
"name": "my_chase_plan_fee",
"explanation": "",
"value": 1.72
},
{
"name": "balance_transfers",
"explanation": "",
"value": 5.0
},
{
"name": "cash_advances",
"explanation": "",
"value": 5.0
},
{
"name": "foreign_transactions",
"explanation": "",
"value": 3.0
},
{
"name": "late_payment",
"explanation": "",
"value": 0.0
},
{
"name": "over-the-credit-limit",
"explanation": "",
"value": 0.0
},
{
"name": "return_payment",
"explanation": "",
"value": 0.0
},
{
"name": "return_check",
"explanation": "",
"value": 0.0
}
],
"interest_rates": {
"purchase": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"balance_transfer": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"cash_advance": {
"annual_percentage_rate": 29.49,
"variation_explanation": "This APR will vary with the market based on the Prime Rate."
},
"penalty": {
"annual_percentage_rate": 29.99,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
"when_applies": "We may apply the Penalty APR to your account if you: fail to make a Minimum Payment by the date and time that it is due; or make a payment to us that is returned unpaid.",
"how_long_apr_applies": "If we apply the Penalty APR for either of these reasons, the Penalty APR could potentially remain in effect indefinitely."
}
}
}

View File

@@ -0,0 +1,131 @@
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
2/25/23, 7:59 PM about:blank
about:blank 1/4
PRICING INFORMATION
INTEREST RATES AND INTEREST CHARGES
Purchase Annual
Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
My Chase Loan
SM APR 19.49%. This APR will vary with the market based on the Prime Rate.
a
Promotional offers with fixed APRs and varying durations may be available from
time to time on some accounts.
Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open.
After that, 19.49%. This APR will vary with the market based on the Prime
Rate.
a
Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.
b
Penalty APR and When
It Applies
Up to 29.99%. This APR will vary with the market based on the Prime Rate.
c
We may apply the Penalty APR to your account if you:
fail to make a Minimum Payment by the date and time that it is due; or
make a payment to us that is returned unpaid.
How Long Will the Penalty APR Apply?: If we apply the Penalty APR for
either of these reasons, the Penalty APR could potentially remain in effect
indefinitely.
How to Avoid Paying
Interest on Purchases
Your due date will be a minimum of 21 days after the close of each billing cycle.
We will not charge you interest on new purchases if you pay your entire balance
or Interest Saving Balance by the due date each month. We will begin charging
interest on balance transfers and cash advances on the transaction date.
Minimum Interest
Charge
None
Credit Card Tips from
the Consumer Financial
Protection Bureau
To learn more about factors to consider when applying for or using a credit card,
visit the website of the Consumer Financial Protection Bureau at
http://www.consumerfinance.gov/learnmore.
FEES
Annual Membership
Fee
None
My Chase Plan
SM Fee
(fixed finance charge)
Monthly fee of 0% of the amount of each eligible purchase transaction or
amount selected to create a My Chase Plan while in the 0% Intro Purchase
APR period.
After that, monthly fee of 1.72% of the amount of each eligible purchase
transaction or amount selected to create a My Chase Plan. The My Chase Plan
Fee will be determined at the time each My Chase Plan is created and will
remain the same until the My Chase Plan is paid in full.
d
Transaction Fees
Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater,
on transfers made within 60 days of account opening. After that: Either $5 or 5%
of the amount of each transfer, whichever is greater.
Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.
2/25/23, 7:59 PM about:blank
about:blank 2/4
Foreign Transactions 3% of the amount of each transaction in U.S. dollars.
Penalty Fees
Late Payment Up to $40.
Over-the-Credit-Limit None
Return Payment Up to $40.
Return Check None
Note: This account may not be eligible for balance transfers.
Loss of Intro APR: We will end your introductory APR if any required Minimum Payment is 60 days late, and
apply the Penalty APR.
How We Will Calculate Your Balance: We use the daily balance method (including new transactions).
Prime Rate: Variable APRs are based on the 7.75% Prime Rate as of 2/7/2023.
aWe add 11.74% to the Prime Rate to determine the Purchase/My Chase Loan/Balance Transfer APR.
Maximum APR 29.99%.
bWe add 21.74% to the Prime Rate to determine the Cash Advance APR. Maximum APR 29.99%.
cWe add up to 26.99% to the Prime Rate to determine the Penalty APR. Maximum APR 29.99%.
dMy Chase Plan Fee: The My Chase Plan Fee is calculated at the time each plan is created and is based on
the amount of each purchase transaction or amount selected to create the plan, the number of billing periods
you choose to pay the balance in full, and other factors. The monthly and aggregate dollar amount of your My
Chase Plan Fee will be disclosed during the activation of each My Chase Plan.
MILITARY LENDING ACT NOTICE: Federal law provides important protections to members of the Armed
Forces and their dependents relating to extensions of consumer credit. In general, the cost of consumer credit
to a member of the Armed Forces and his or her dependent may not exceed an annual percentage rate of 36
percent. This rate must include, as applicable to the credit transaction or account: the costs associated with
credit insurance premiums; fees for ancillary products sold in connection with the credit transaction; any
application fee charged (other than certain application fees for specified credit transactions or accounts); and
any participation fee charged (other than certain participation fees for a credit card account). To receive this
information and a description of your payment obligation verbally, please call 1-800-235-9978.
TERMS & CONDITIONS
Authorization: When you respond to this credit card offer from JPMorgan Chase Bank, N.A., Member FDIC, a
subsidiary of JPMorgan Chase & Co. ("Chase", "we", or "us"), you agree to the following:
1. You authorize us to obtain credit bureau reports, employment, and income information about you that we
will use when considering your application for credit. We may obtain and use information about your
accounts with us and others such as Checking, Deposit, Investment, and Utility accounts from credit
bureaus and other entities. You also authorize us to obtain credit bureau reports and any other
information about you in connection with: 1) extensions of credit on your account; 2) the administration,
review or collection of your account; and 3) offering you enhanced or additional products and services. If
you ask, we will tell you the name and address of the credit bureau from which we obtained a report
about you.
2. If an account is opened, you will receive a Cardmember Agreement with your card(s). You agree to the
terms of this agreement by: using the account or any card, authorizing their use, or making any payment
on the account.
3. By providing your mobile ph
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.

View File

@@ -0,0 +1,41 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"fees": [
{
"name": {
"incorrect_value": "my chase plan",
"error_messages": [
"must be exactly two words"
]
}
},
{
"name": {
"incorrect_value": "over-the-credit-limit",
"error_messages": [
"must be exactly two words"
]
}
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<list description="What fees and charges are associated with my account?" name="fees" required="true">
<object required="true">
<integer format="1-indexed" name="index" required="true"></integer>
<string format="lower-case; two-words" name="name" required="true"></string>
<string format="one-line" name="explanation" required="true"></string>
<float format="percentage" name="value" required="true"></float>
</object>
</list>
<object description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" name="interest_rates" required="true"></object>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,29 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="noop" on-fail-two-words="noop"/>
<string name="explanation" validators="one-line" on-fail-one-line="noop" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}</message>
</messages>
</rail>

View File

@@ -0,0 +1,42 @@
# ruff: noqa: E501
OPTIONAL_PROMPT_COMPLETION_MODEL = """
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}"""
OPTIONAL_PROMPT_CHAT_MODEL = """
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter `null`.
${document}
Extract information from this document and return a JSON that follows the correct schema.
${gr.xml_prefix_prompt}
${xml_output_schema}
"""
OPTIONAL_INSTRUCTIONS_CHAT_MODEL = """
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
${gr.xml_suffix_prompt_examples}
"""
OPTIONAL_MSG_HISTORY = [
{
"role": "system",
"content": "\nYou are a helpful assistant only capable of communicating with valid JSON, and no other text.\n\n${gr.xml_suffix_prompt_examples}\n",
},
{
"role": "user",
"content": "\nGiven the following document, answer the following questions. If the answer doesn't exist in the document, enter `null`.\n\n${document}\n\nExtract information from this document and return a JSON that follows the correct schema.\n\n${gr.xml_prefix_prompt}\n\n${xml_output_schema}\n",
},
]

View File

@@ -0,0 +1,148 @@
from typing import Dict, List
from pydantic import BaseModel, Field
from guardrails.validator_base import OnFailAction
from tests.integration_tests.test_assets.validators import LowerCase, OneLine, TwoWords
class FeeDetailsFilter(BaseModel):
index: int = Field(format="1-indexed")
name: str = Field(
validators=[
LowerCase(on_fail=OnFailAction.FILTER),
TwoWords(on_fail=OnFailAction.FILTER),
]
)
explanation: str = Field(validators=OneLine(on_fail=OnFailAction.FILTER))
value: float = Field(format="percentage")
class ContractDetailsFilter(BaseModel):
fees: List[FeeDetailsFilter] = Field(
description="What fees and charges are associated with my account?"
)
interest_rates: Dict = Field(
description="What are the interest rates offered by the bank on savings "
"and checking accounts, loans, and credit products?"
)
class FeeDetailsFix(BaseModel):
index: int = Field(format="1-indexed")
name: str = Field(
validators=[
LowerCase(on_fail=OnFailAction.FIX),
TwoWords(on_fail=OnFailAction.FIX),
]
)
explanation: str = Field(validators=OneLine(on_fail=OnFailAction.FIX))
value: float = Field(format="percentage")
class ContractDetailsFix(BaseModel):
fees: List[FeeDetailsFix] = Field(
description="What fees and charges are associated with my account?"
)
interest_rates: Dict = Field(
description="What are the interest rates offered by the bank on savings "
"and checking accounts, loans, and credit products?"
)
class FeeDetailsNoop(BaseModel):
index: int = Field(format="1-indexed")
name: str = Field(
validators=[
LowerCase(on_fail=OnFailAction.NOOP),
TwoWords(on_fail=OnFailAction.NOOP),
]
)
explanation: str = Field(validators=OneLine(on_fail=OnFailAction.NOOP))
value: float = Field(format="percentage")
class ContractDetailsNoop(BaseModel):
fees: List[FeeDetailsNoop] = Field(
description="What fees and charges are associated with my account?"
)
interest_rates: Dict = Field(
description="What are the interest rates offered by the bank on savings "
"and checking accounts, loans, and credit products?"
)
class FeeDetailsReask(BaseModel):
index: int = Field(format="1-indexed")
name: str = Field(
validators=[
LowerCase(on_fail=OnFailAction.NOOP),
TwoWords(on_fail=OnFailAction.REASK),
]
)
explanation: str = Field(validators=OneLine(on_fail=OnFailAction.NOOP))
value: float = Field(format="percentage")
class ContractDetailsReask(BaseModel):
fees: List[FeeDetailsReask] = Field(
description="What fees and charges are associated with my account?"
)
interest_rates: Dict = Field(
description="What are the interest rates offered by the bank on savings "
"and checking accounts, loans, and credit products?"
)
class FeeDetailsRefrain(BaseModel):
index: int = Field(format="1-indexed")
name: str = Field(
validators=[
LowerCase(on_fail=OnFailAction.REFRAIN),
TwoWords(on_fail=OnFailAction.REFRAIN),
]
)
explanation: str = Field(validators=OneLine(on_fail=OnFailAction.REFRAIN))
value: float = Field(format="percentage")
class ContractDetailsRefrain(BaseModel):
fees: List[FeeDetailsRefrain] = Field(
description="What fees and charges are associated with my account?"
)
interest_rates: Dict = Field(
description="What are the interest rates offered by the bank on savings "
"and checking accounts, loans, and credit products?"
)
PROMPT = """
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}""" # noqa: E501
INSTRUCTIONS_CHAT_MODEL = """
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
${gr.xml_suffix_prompt_examples}
""" # noqa: E501
PROMPT_CHAT_MODEL = """
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter `null`.
${document}
Extract information from this document and return a JSON that follows the correct schema.
${gr.xml_prefix_prompt}
${xml_output_schema}
""" # noqa: E501

View File

@@ -0,0 +1,31 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="noop"
on-fail-two-words="reask"/>
<string name="explanation" validators="one-line" on-fail-one-line="noop" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}</message>
</messages>
</rail>

View File

@@ -0,0 +1,17 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="noop"
on-fail-two-words="reask"/>
<string name="explanation" validators="one-line" on-fail-one-line="noop" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
</rail>

View File

@@ -0,0 +1,29 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<integer name="index" format="1-indexed" />
<string name="name" validators="lower-case; two-words" on-fail-lower-case="refrain" on-fail-two-words="refrain"/>
<string name="explanation" validators="one-line" on-fail-one-line="refrain" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter 'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}</message>
</messages>
</rail>

View File

@@ -0,0 +1,28 @@
<rail version="0.1">
<output>
<list name="fees" description="What fees and charges are associated with my account?">
<object>
<string name="name" validators="lower-case; two-words" on-fail-lower-case="noop" on-fail-two-words="reask"/>
<string name="explanation" validators="one-line" on-fail-one-line="noop" />
<float name="value" format="percentage"/>
</object>
</list>
<object name="interest_rates" description="What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?" />
</output>
<messages>
<message role="user">
Given the following document, answer the following questions. If the answer doesn't exist in the document, enter
'None'.
${document}
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.xml_suffix_prompt_v2_wo_none}</message>
</messages>
</rail>

View File

@@ -0,0 +1,76 @@
# ruff: noqa: E501
VALIDATED_OUTPUT_FILTER = {
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0,
},
{
"index": 2,
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72,
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5,
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5,
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3,
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0,
},
{"index": 7, "explanation": "Over-the-Credit-Limit None", "value": 0},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0,
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0,
},
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
},
"maximum_apr": 29.99,
},
}

View File

@@ -0,0 +1,82 @@
# ruff: noqa: E501
VALIDATED_OUTPUT_FIX = {
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0,
},
{
"index": 2,
"name": "my chase",
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72,
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5,
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5,
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3,
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0,
},
{
"index": 7,
"name": "over the",
"explanation": "Over-the-Credit-Limit None",
"value": 0,
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0,
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0,
},
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
},
"maximum_apr": 29.99,
},
}

View File

@@ -0,0 +1,82 @@
# ruff: noqa: E501
VALIDATED_OUTPUT_NOOP = {
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0,
},
{
"index": 2,
"name": "my chase plan",
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72,
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5,
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5,
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3,
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0,
},
{
"index": 7,
"name": "over-the-credit-limit",
"explanation": "Over-the-Credit-Limit None",
"value": 0,
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0,
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0,
},
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
},
"maximum_apr": 29.99,
},
}

View File

@@ -0,0 +1,103 @@
# ruff: noqa: E501
from guardrails.actions.reask import FieldReAsk
from guardrails_ai.types import FailResult
VALIDATED_OUTPUT_REASK_1 = {
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0,
},
{
"index": 2,
"name": FieldReAsk(
incorrect_value="my chase plan",
fail_results=[
FailResult(
error_message="must be exactly two words",
fix_value="my chase",
)
],
path=["fees", 1, "name"],
),
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72,
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5,
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5,
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3,
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0,
},
{
"index": 7,
"name": FieldReAsk(
incorrect_value="over-the-credit-limit",
fail_results=[
FailResult(
error_message="must be exactly two words",
fix_value="over the",
)
],
path=["fees", 6, "name"],
),
"explanation": "Over-the-Credit-Limit None",
"value": 0,
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0,
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0,
},
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
},
"maximum_apr": 29.99,
},
}

View File

@@ -0,0 +1,82 @@
# ruff: noqa: E501
VALIDATED_OUTPUT_REASK_2 = {
"fees": [
{
"index": 1,
"name": "annual membership",
"explanation": "Annual Membership Fee",
"value": 0,
},
{
"index": 2,
"name": "my chase",
"explanation": "My Chase Plan Fee (fixed finance charge)",
"value": 1.72,
},
{
"index": 3,
"name": "balance transfers",
"explanation": "Balance Transfers Intro fee of either $5 or 3% of the amount of each transfer, whichever is greater, on transfers made within 60 days of account opening. After that: Either $5 or 5% of the amount of each transfer.",
"value": 5,
},
{
"index": 4,
"name": "cash advances",
"explanation": "Cash Advances Either $10 or 5% of the amount of each transaction, whichever is greater.",
"value": 5,
},
{
"index": 5,
"name": "foreign transactions",
"explanation": "Foreign Transactions 3% of the amount of each transaction in U.S. dollars.",
"value": 3,
},
{
"index": 6,
"name": "late payment",
"explanation": "Late Payment Up to $40.",
"value": 0,
},
{
"index": 7,
"name": "over the",
"explanation": "Over-the-Credit-Limit None",
"value": 0,
},
{
"index": 8,
"name": "return payment",
"explanation": "Return Payment Up to $40.",
"value": 0,
},
{
"index": 9,
"name": "return check",
"explanation": "Return Check None",
"value": 0,
},
],
"interest_rates": {
"purchase": {
"apr": 0,
"explanation": "Purchase Annual Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"my_chase_loan": {
"apr": 19.49,
"explanation": "My Chase Loan SM APR 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"apr": 0,
"explanation": "Balance Transfer APR 0% Intro APR for the first 18 months that your Account is open. After that, 19.49%. This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"apr": 29.49,
"explanation": "Cash Advance APR 29.49%. This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"apr": 29.99,
"explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
},
"maximum_apr": 29.99,
},
}

View File

@@ -0,0 +1 @@
VALIDATED_OUTPUT_REFRAIN = None

View File

@@ -0,0 +1,47 @@
# ruff: noqa: E501
from guardrails.actions.reask import SkeletonReAsk
from guardrails_ai.types import FailResult
VALIDATED_OUTPUT_SKELETON_REASK_1 = SkeletonReAsk(
incorrect_value={
"fees": [
{"name": "annual membership fee", "value": 0.0},
{"name": "my chase plan fee", "value": 1.72},
{"name": "balance transfers", "value": 5.0},
{"name": "cash advances", "value": 5.0},
{"name": "foreign transactions", "value": 3.0},
{"name": "late payment", "value": 0.0},
{"name": "over-the-credit-limit", "value": 0.0},
{"name": "return payment", "value": 0.0},
{"name": "return check", "value": 0.0},
],
"interest_rates": {
"purchase": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"annual_percentage_rate": 29.49,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"annual_percentage_rate": 0.0,
"variation_explanation": "Up to 29.99%. This APR will vary with the market based on the Prime Rate.",
"when_applies": "We may apply the Penalty APR to your account if you: fail to make a Minimum Payment by the date and time that it is due; or make a payment to us that is returned unpaid.",
"how_long_apr_applies": "If we apply the Penalty APR for either of these reasons, the Penalty APR could potentially remain in effect indefinitely.",
},
},
},
fail_results=[
FailResult(
outcome="fail",
error_message='JSON does not match schema:\n{\n "$.fees[0]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[1]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[2]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[3]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[4]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[5]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[6]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[7]": [\n "\'explanation\' is a required property"\n ],\n "$.fees[8]": [\n "\'explanation\' is a required property"\n ]\n}',
fix_value=None,
metadata=None,
)
],
)

View File

@@ -0,0 +1,34 @@
# ruff: noqa: E501
VALIDATED_OUTPUT_SKELETON_REASK_2 = {
"fees": [
{"name": "annual membership", "explanation": "", "value": 0.0},
{"name": "my chase", "explanation": "", "value": 1.72},
{"name": "balance transfers", "explanation": "", "value": 5.0},
{"name": "cash advances", "explanation": "", "value": 5.0},
{"name": "foreign transactions", "explanation": "", "value": 3.0},
{"name": "late payment", "explanation": "", "value": 0.0},
{"name": "over the", "explanation": "", "value": 0.0},
{"name": "return payment", "explanation": "", "value": 0.0},
{"name": "return check", "explanation": "", "value": 0.0},
],
"interest_rates": {
"purchase": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
},
"balance_transfer": {
"annual_percentage_rate": 0.0,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
},
"cash_advance": {
"annual_percentage_rate": 29.49,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
},
"penalty": {
"annual_percentage_rate": 29.99,
"variation_explanation": "This APR will vary with the market based on the Prime Rate.",
"when_applies": "We may apply the Penalty APR to your account if you: fail to make a Minimum Payment by the date and time that it is due; or make a payment to us that is returned unpaid.",
"how_long_apr_applies": "If we apply the Penalty APR for either of these reasons, the Penalty APR could potentially remain in effect indefinitely.",
},
},
}

View File

@@ -0,0 +1,63 @@
import pytest
@pytest.fixture(name="rail_spec")
def fixture_rail_spec():
return """
<rail version="0.1">
<output>
<string name="dummy_string" description="Any dummy string" />
<integer name="dummy_integer" description="Any dummy integer" />
<float name="dummy_float" description="Any dummy float" />
<bool name="dummy_boolean" description="Any dummy boolean" />
<date name="dummy_date" description="Any dummy date" />
<time name="dummy_time" description="Any dummy time" />
<list name="dummy_list" description="Any dummy list" />
<object name="dummy_object" description="Any dummy object" />
</output>
<prompt>
Generate a JSON of dummy data, where the data types are specified by the user.
${gr.complete_json_suffix}
</prompt>
</rail>
"""
@pytest.fixture(name="llm_output")
def fixture_llm_output():
return """
{
"dummy_string": "Some string",
"dummy_integer": 42,
"dummy_float": 3.14,
"dummy_boolean": true,
"dummy_date": "2020-01-01",
"dummy_time": "12:00:00",
"dummy_list": ["item1", "item2", "item3"],
"dummy_object": {
"key1": "value1",
"key2": "value2"
}
}
"""
@pytest.fixture(name="validated_output")
def fixture_validated_output():
return {
"dummy_string": "Some string",
"dummy_integer": 42,
"dummy_float": 3.14,
"dummy_boolean": True,
"dummy_date": "2020-01-01",
"dummy_time": "12:00:00",
"dummy_list": ["item1", "item2", "item3"],
"dummy_object": {"key1": "value1", "key2": "value2"},
}

View File

@@ -0,0 +1,62 @@
{
"type": "object",
"properties": {
"action": {
"type": "object",
"properties": {
"chosen_action": {
"type": "string",
"enum": [
"fight",
"flight"
]
}
},
"allOf": [
{
"if": {
"properties": {
"chosen_action": { "const": "fight" }
}
},
"then": {
"properties": {
"weapon": {
"type": "string"
}
},
"required": [
"weapon"
]
}
},
{
"if": {
"properties": {
"chosen_action": { "const": "flight" }
}
},
"then": {
"properties": {
"flight_direction": {
"type": "string"
},
"distance": {
"type": "integer"
}
},
"required": [
"distance"
]
}
}
],
"required": [
"chosen_action"
]
}
},
"required": [
"action"
]
}

View File

@@ -0,0 +1,94 @@
{
"$defs": {
"Fight": {
"properties": {
"chosen_action": {
"const": "fight",
"title": "Chosen Action",
"type": "string"
},
"weapon": {
"title": "Weapon",
"type": "string",
"validators": [
{
"rail_alias": "valid-choices"
}
]
}
},
"required": [
"chosen_action",
"weapon"
],
"title": "Fight",
"type": "object"
},
"Flight": {
"properties": {
"chosen_action": {
"const": "flight",
"title": "Chosen Action",
"type": "string"
},
"flight_direction": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Flight Direction",
"validators": [
{
"rail_alias": "valid-choices"
}
]
},
"distance": {
"title": "Distance",
"type": "integer",
"validators": [
{
"rail_alias": "valid-choices"
}
]
}
},
"required": [
"chosen_action",
"flight_direction",
"distance"
],
"title": "Flight",
"type": "object"
}
},
"properties": {
"action": {
"discriminator": {
"mapping": {
"fight": "#/$defs/Fight",
"flight": "#/$defs/Flight"
},
"propertyName": "chosen_action"
},
"oneOf": [
{
"$ref": "#/$defs/Fight"
},
{
"$ref": "#/$defs/Flight"
}
],
"title": "Action"
}
},
"required": [
"action"
],
"title": "FightOrFlight",
"type": "object"
}

View File

@@ -0,0 +1,32 @@
{
"$defs": {
"Fee": {
"properties": {
"index": {"title": "Index", "type": "integer", "format": "1-indexed"},
"name": {"title": "Name", "type": "string"},
"explanation": {"title": "Explanation", "type": "string"},
"value": {"title": "Value", "type": "number", "format": "percentage"}
},
"required": ["index", "name", "explanation", "value"],
"title": "Fee",
"type": "object"
}
},
"properties": {
"fees": {
"description": "What fees and charges are associated with my account?",
"items": {"$ref": "#/$defs/Fee"},
"title": "Fees",
"type": "array"
},
"interest_rates": {
"additionalProperties": {"type": "string"},
"description": "What are the interest rates offered by the bank on savings and checking accounts, loans, and credit products?",
"title": "Interest Rates",
"type": "object"
}
},
"required": ["fees", "interest_rates"],
"title": "CreditCardAgreement",
"type": "object"
}

View File

@@ -0,0 +1,4 @@
{
"type": "string",
"description": "Some string..."
}

View File

@@ -0,0 +1,36 @@
from typing import List
from pydantic import BaseModel
LIST_PROMPT = """Create a list of items that may be found in a grocery store."""
LIST_OUTPUT = """[{"name": "apple", "price": 1.0}, {"name": "banana", "price": 0.5}, {"name": "orange", "price": 1.5}]""" # noqa: E501
class Item(BaseModel):
name: str
price: float
PYDANTIC_RAIL_WITH_LIST = List[Item]
message = (
'<message role="user">'
"Create a list of items that may be found in a grocery store."
"</message>"
)
RAIL_SPEC_WITH_LIST = f"""
<rail version="0.1">
<output type="list">
<object>
<string name="name" />
<float name="price" />
</object>
</output>
<messages>
{message}
</messages>
</rail>
"""

View File

@@ -0,0 +1,80 @@
# ruff: noqa: E501
import os
from .msg_validated_output_reask import MSG_VALIDATED_OUTPUT_REASK
from .parsing_reask import PersonalDetails
from .parsing_reask import compiled_prompt as PARSING_COMPILED_PROMPT
from .parsing_reask import compiled_reask as PARSING_COMPILED_REASK
from .parsing_reask import document as PARSING_DOCUMENT
from .parsing_reask import expected_llm_output as PARSING_EXPECTED_LLM_OUTPUT
from .parsing_reask import expected_output as PARSING_EXPECTED_OUTPUT
from .parsing_reask import prompt as PARSING_INITIAL_PROMPT
from .parsing_reask import unparseable_llm_response as PARSING_UNPARSEABLE_LLM_OUTPUT
from .validated_response_reask import VALIDATED_OUTPUT_1 as VALIDATED_OUTPUT_REASK_1
from .validated_response_reask import VALIDATED_OUTPUT_2 as VALIDATED_OUTPUT_REASK_2
from .validated_response_reask import VALIDATED_OUTPUT_3 as VALIDATED_OUTPUT_REASK_3
from .validated_response_reask import ListOfPeople
from .validated_response_reask import prompt as VALIDATED_RESPONSE_REASK_PROMPT
from .with_msg_history import Movie as WITH_MSG_HISTORY
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)))
reader = (
lambda filename: open(os.path.join(DATA_DIR, filename)).read().replace("\r", "")
)
COMPILED_PROMPT = reader("compiled_prompt.txt")
COMPILED_PROMPT_CHAT = reader("compiled_prompt_chat.txt")
COMPILED_INSTRUCTIONS_CHAT = reader("compiled_instructions_chat.txt")
COMPILED_PROMPT_REASK_1 = reader("compiled_prompt_reask_1.txt")
COMPILED_PROMPT_FULL_REASK_1 = reader("compiled_prompt_full_reask_1.txt")
COMPILED_INSTRUCTIONS_REASK_1 = reader("compiled_instructions_reask_1.txt")
COMPILED_PROMPT_REASK_2 = reader("compiled_prompt_reask_2.txt")
COMPILED_PROMPT_FULL_REASK_2 = reader("compiled_prompt_full_reask_2.txt")
COMPILED_INSTRUCTIONS_REASK_2 = reader("compiled_instructions_reask_2.txt")
COMPILED_PROMPT_ENUM = reader("compiled_prompt_enum.txt")
COMPILED_PROMPT_ENUM_2 = reader("compiled_prompt_enum_2.txt")
LLM_OUTPUT = reader("llm_output.txt")
LLM_OUTPUT_REASK_1 = reader("llm_output_reask_1.txt")
LLM_OUTPUT_FULL_REASK_1 = reader("llm_output_full_reask_1.txt")
LLM_OUTPUT_REASK_2 = reader("llm_output_reask_2.txt")
LLM_OUTPUT_FULL_REASK_2 = reader("llm_output_full_reask_2.txt")
LLM_OUTPUT_ENUM = reader("llm_output_enum.txt")
LLM_OUTPUT_ENUM_2 = reader("llm_output_enum_2.txt")
RAIL_SPEC_WITH_REASK = reader("reask.rail")
MSG_HISTORY_LLM_OUTPUT_INCORRECT = reader("msg_history_llm_output_incorrect.txt")
MSG_HISTORY_LLM_OUTPUT_CORRECT = reader("msg_history_llm_output_correct.txt")
MSG_COMPILED_PROMPT_REASK = reader("msg_compiled_prompt_reask.txt")
MSG_COMPILED_INSTRUCTIONS_REASK = reader("msg_compiled_instructions_reask.txt")
__all__ = [
"COMPILED_PROMPT",
"COMPILED_PROMPT_REASK_1",
"COMPILED_PROMPT_REASK_2",
"LLM_OUTPUT",
"LLM_OUTPUT_REASK_1",
"LLM_OUTPUT_REASK_2",
"RAIL_SPEC_WITH_REASK",
"VALIDATED_OUTPUT_REASK_1",
"VALIDATED_OUTPUT_REASK_2",
"VALIDATED_OUTPUT_REASK_3",
"WITH_MSG_HISTORY",
"MSG_HISTORY_LLM_OUTPUT_INCORRECT",
"MSG_HISTORY_LLM_OUTPUT_CORRECT",
"MSG_COMPILED_PROMPT_REASK",
"MSG_COMPILED_INSTRUCTIONS_REASK",
"MSG_VALIDATED_OUTPUT_REASK",
"MSG_HISTORY_LLM_OUTPUT",
"VALIDATED_RESPONSE_REASK_PROMPT",
"ListOfPeople",
"PersonalDetails",
"PARSING_INITIAL_PROMPT",
"PARSING_DOCUMENT",
"PARSING_EXPECTED_LLM_OUTPUT",
"PARSING_UNPARSEABLE_LLM_OUTPUT",
"PARSING_COMPILED_PROMPT",
"PARSING_COMPILED_REASK",
"PARSING_EXPECTED_OUTPUT",
]

View File

@@ -0,0 +1,9 @@
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

View File

@@ -0,0 +1,35 @@
Generate data for possible users in accordance with the specification below.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

View File

@@ -0,0 +1,35 @@
Generate data for possible users in accordance with the specification below.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

View File

@@ -0,0 +1 @@
What is the status of this task?

View File

@@ -0,0 +1 @@
What is the status of this task REALLY?

View File

@@ -0,0 +1,43 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": {
"incorrect_value": "90210",
"error_messages": [
"Zip code must not be Beverly Hills."
]
}
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,44 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": {
"incorrect_value": "None",
"error_messages": [
"Zip code must be numeric.",
"Zip code must be in California, and start with 9."
]
}
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,31 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"people": [
{
"zip_code": {
"incorrect_value": "90210",
"error_messages": [
"Zip code must not be Beverly Hills."
]
}
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,32 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"people": [
{
"zip_code": {
"incorrect_value": "None",
"error_messages": [
"Zip code must be numeric.",
"Zip code must be in California, and start with 9."
]
}
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output description="A list of people.Args: people (List[Person]): A list of people.">
<list name="people" required="true">
<object description="Information about a person.Args: name (str): The name of the person. age (int): The age of the person. zip_code (str): The zip code of the person." required="true">
<string name="name" required="true"></string>
<integer format="age_must_be_between_0_and_150" name="age" required="true"></integer>
<string format="zip_code_must_be_numeric; zip_code_in_california" name="zip_code" required="true"></string>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,19 @@
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": "90210"
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}

View File

@@ -0,0 +1 @@
{"status": "not started"}

View File

@@ -0,0 +1 @@
{"status": "i dont know?"}

View File

@@ -0,0 +1,19 @@
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": "None"
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}

View File

@@ -0,0 +1,19 @@
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": "None"
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}

View File

@@ -0,0 +1,19 @@
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": "None"
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}

View File

@@ -0,0 +1,19 @@
{
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": "None"
},
{
"name": "Jane Doe",
"age": 32,
"zip_code": "94103"
},
{
"name": "James Smith",
"age": 40,
"zip_code": "92101"
}
]
}

View File

@@ -0,0 +1,10 @@
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the JSON Schema provided, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here are examples of simple (JSON Schema, JSON) pairs that show the expected behavior:
- `{"type":"object","properties":{"foo":{"type":"string","format":"two-words lower-case"}}}` => `{'foo': 'example one'}`
- `{"type":"object","properties":{"bar":{"type":"array","items":{"type":"string","format":"upper-case"}}}}` => `{"bar": ['STRING ONE', 'STRING TWO']}`
- `{"type":"object","properties":{"baz":{"type":"object","properties":{"foo":{"type":"string","format":"capitalize two-words"},"index":{"type":"integer","format":"1-indexed"}}}}}` => `{'baz': {'foo': 'Some String', 'index': 1}}`
- `{"type":"object","properties":{"bar":{"type":"array","items":{"type":"string","format":"upper-case"}},"baz":{"type":"object","properties":{"foo":{"type":"string","format":"two-words lower-case"}}}}}` => `{'bar': ['STRING ONE', 'STRING TWO'], 'baz': {'foo': 'example one'}}`

View File

@@ -0,0 +1,32 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"name": "Inception",
"director": "Christopher Nolan"
}
Help me correct the incorrect values based on the given error messages.
Error Messages:
"JSON does not match schema:\n{\n \"$\": [\n \"'release_year' is a required property\"\n ]\n}"
Given below is a JSON Schema that describes the output structure you should return.
{"properties": {"name": {"type": "string", "title": "Name", "description": "The name of the movie."}, "director": {"type": "string", "title": "Director", "description": "The name of the director."}, "release_year": {"type": "integer", "title": "Release Year", "description": "The year the movie was released."}}, "type": "object", "required": ["name", "director", "release_year"], "title": "Movie"}
ONLY return a valid JSON object (no other text is necessary), where the key of the field in the JSON is the key of the entries within the schema's `properties`, and the value is of the type specified by the `type` property under that key.
The JSON MUST conform to the structure described by the JSON Schema provided BUT SHOULD NOT BE A JSON Schema ITSELF.
Be sure to include any types and format requests e.g. requests for lists, objects and specific types.
Be correct and concise.
If you are unsure anywhere, enter `null`.
Here's an example of the structure:
{
"name": "Star Wars",
"director": "George Lucas",
"release_year": 1977
}

View File

@@ -0,0 +1,5 @@
{
"name": "Inception",
"director": "Christopher Nolan",
"release_year": 2010
}

View File

@@ -0,0 +1,5 @@
{
"name": "Inception",
"director": "Christopher Nolan",
"extra": "key"
}

View File

@@ -0,0 +1,19 @@
from guardrails.actions.reask import SkeletonReAsk
from guardrails_ai.types import FailResult
MSG_VALIDATED_OUTPUT_REASK = SkeletonReAsk(
incorrect_value={"name": "Inception", "director": "Christopher Nolan"},
fail_results=[
FailResult(
outcome="fail",
metadata=None,
error_message="""JSON does not match schema:
{
"$": [
"'release_year' is a required property"
]
}""",
fix_value=None,
)
],
)

View File

@@ -0,0 +1,117 @@
from pydantic import BaseModel, Field
prompt = """\n\nHuman:
Given the following resume, answer the following questions. If the answer doesn't exist in the resume, enter `null`.
${document}
Extract information from this resume and return a JSON that follows the correct schema.
${gr.complete_xml_suffix}
\n\nAssistant:
""" # noqa
document = (
"""Joe Smith 1234 5678 / joe@example.com PRIVATE & CONFIDENTIAL
1 Joe Smith
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum."""
""
) # noqa
compiled_prompt = """
Human:
Given the following resume, answer the following questions. If the answer doesn't exist in the resume, enter `null`.
Joe Smith 1234 5678 / joe@example.com PRIVATE & CONFIDENTIAL
1 Joe Smith
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
Extract information from this resume and return a JSON that follows the correct schema.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<string description="What is the candidate name?" name="name" required="true"></string>
<string description="What is the candidate contact number?" name="contact_number" required="true"></string>
<string description="What is the candidate email address?" name="contact_email" required="true"></string>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`
Assistant:
""" # noqa
compiled_reask = """
I was given the following response, which was not parseable as JSON.
"Here is the JSON containing the requested information extracted from the resume:\\n\\n```\\n{\\n \\"name\\": \\"Joe Smith\\",\\n \\"contact_number\\": \\"1234 5678\\",\\n \\"contact_email\\": \\"joe@example.com\\"\\n```"
Help me correct this by making it valid JSON.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<string description="What is the candidate name?" name="name" required="true"></string>
<string description="What is the candidate contact number?" name="contact_number" required="true"></string>
<string description="What is the candidate email address?" name="contact_email" required="true"></string>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
""" # noqa
class PersonalDetails(BaseModel):
name: str = Field(..., description="What is the candidate name?")
contact_number: str = Field(
..., description="What is the candidate contact number?"
)
contact_email: str = Field(..., description="What is the candidate email address?")
expected_llm_output = """Here is the JSON containing the requested information extracted from the resume:
```json
{
"name": "Joe Smith",
"contact_number": "1234 5678",
"contact_email": "joe@example.com"
}
```""" # noqa
unparseable_llm_response = """Here is the JSON containing the requested information extracted from the resume:
```
{
"name": "Joe Smith",
"contact_number": "1234 5678",
"contact_email": "joe@example.com"
```""" # noqa
expected_output = {
"name": "Joe Smith",
"contact_number": "1234 5678",
"contact_email": "joe@example.com",
}

View File

@@ -0,0 +1,20 @@
<rail version="0.1">
<output>
<list name="people" description="A list of 3 people.">
<pydantic description="Information about a person." model="Person" on-fail-pydantic="reask" />
</list>
</output>
<prompt>
Generate data for possible users in accordance with the specification below.
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.complete_json_suffix_v2}</prompt>
</rail>

View File

@@ -0,0 +1,161 @@
# ruff: noqa: E501
from typing import Any, Dict, List
from pydantic import BaseModel, Field
from guardrails import Validator, register_validator
from guardrails.actions.reask import FieldReAsk
from guardrails.types import OnFailAction
from guardrails_ai.types import (
FailResult,
PassResult,
ValidationResult,
)
prompt = """Generate data for possible users in accordance with the specification below.
${gr.xml_prefix_prompt}
${xml_output_schema}
${gr.complete_xml_suffix_v2}"""
@register_validator(name="zip_code_must_be_numeric", data_type="string")
class ZipCodeMustBeNumeric(Validator):
def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
if not value.isnumeric():
return FailResult(error_message="Zip code must be numeric.")
return PassResult()
@register_validator(name="age_must_be_between_0_and_150", data_type="integer")
class AgeMustBeBetween0And150(Validator):
def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
if not 0 <= value <= 150:
return FailResult(error_message="Age must be between 0 and 150.")
return PassResult()
@register_validator(name="zip_code_in_california", data_type="string")
class ZipCodeInCalifornia(Validator):
def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
if not value.startswith("9"):
return FailResult(
error_message="Zip code must be in California, and start with 9."
)
if value == "90210":
return FailResult(error_message="Zip code must not be Beverly Hills.")
return PassResult()
class Person(BaseModel):
"""Information about a person.
Args:
name (str): The name of the person.
age (int): The age of the person.
zip_code (str): The zip code of the person.
"""
name: str
age: int = Field(
...,
json_schema_extra={
"validators": [AgeMustBeBetween0And150(on_fail=OnFailAction.REASK)]
},
)
zip_code: str = Field(
...,
json_schema_extra={
"validators": [
ZipCodeMustBeNumeric(on_fail=OnFailAction.REASK),
ZipCodeInCalifornia(on_fail=OnFailAction.REASK),
],
},
)
class ListOfPeople(BaseModel):
"""A list of people.
Args:
people (List[Person]): A list of people.
"""
people: List[Person]
VALIDATED_OUTPUT_1 = {
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": FieldReAsk(
incorrect_value="90210",
fail_results=[
FailResult(
error_message="Zip code must not be Beverly Hills.",
fix_value=None,
)
],
path=["people", 0, "zip_code"],
),
},
{"name": "Jane Doe", "age": 32, "zip_code": "94103"},
{"name": "James Smith", "age": 40, "zip_code": "92101"},
]
}
VALIDATED_OUTPUT_2 = {
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": FieldReAsk(
incorrect_value="None",
fail_results=[
FailResult(
error_message="Zip code must be numeric.",
fix_value=None,
),
FailResult(
error_message="Zip code must be in California, and start with 9.",
fix_value=None,
),
],
path=["people", 0, "zip_code"],
),
},
{"name": "Jane Doe", "age": 32, "zip_code": "94103"},
{"name": "James Smith", "age": 40, "zip_code": "92101"},
]
}
VALIDATED_OUTPUT_3 = {
"people": [
{
"name": "John Doe",
"age": 28,
"zip_code": FieldReAsk(
incorrect_value="None",
fail_results=[
FailResult(
error_message="Zip code must be numeric.",
fix_value=None,
),
FailResult(
error_message="Zip code must be in California, and start with 9.",
fix_value=None,
),
],
path=["people", 0, "zip_code"],
),
},
{"name": "Jane Doe", "age": 32, "zip_code": "94103"},
{"name": "James Smith", "age": 40, "zip_code": "92101"},
]
}

View File

@@ -0,0 +1,8 @@
from pydantic import BaseModel, Field
class Movie(BaseModel):
# """Details about a movie."""
name: str = Field(..., description="The name of the movie.")
director: str = Field(..., description="The name of the director.")
release_year: int = Field(..., description="The year the movie was released.")

View File

@@ -0,0 +1,33 @@
from tests.integration_tests.test_assets.validators.valid_choices import ValidChoices
from pydantic import BaseModel, Field
from typing import Literal, Optional, Union
prompt = """
You are a human in an enchanted forest.
You come across opponents of different types,
and you should fight smaller opponents and run away from bigger ones.
You run into a ${opp_type}. What do you do?
${gr.complete_json_suffix_v2}"""
class Fight(BaseModel):
chosen_action: Literal["fight"]
weapon: str = Field(
validators=[ValidChoices(["crossbow", "machine gun"], on_fail="reask")]
)
class Flight(BaseModel):
chosen_action: Literal["flight"]
flight_direction: Optional[str] = Field(
validators=[
ValidChoices(["north", "south", "east", "west"], on_fail="exception")
]
)
distance: int = Field(validators=[ValidChoices([1, 2, 3, 4], on_fail="exception")])
class FightOrFlight(BaseModel):
action: Union[Fight, Flight] = Field(discriminator="chosen_action")

View File

@@ -0,0 +1,46 @@
# ruff: noqa: E501
import os
from .validator_parallelism_reask_1 import VALIDATOR_PARALLELISM_REASK_1 # noqa: F401
from .validator_parallelism_reask_2 import VALIDATOR_PARALLELISM_REASK_2 # noqa: F401
DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)))
reader = (
lambda filename: open(os.path.join(DATA_DIR, filename)).read().replace("\r", "")
)
COMPILED_PROMPT_1_WITHOUT_INSTRUCTIONS = reader("compiled_prompt_1.txt")
COMPILED_PROMPT_1_PYDANTIC_2_WITHOUT_INSTRUCTIONS = reader(
"compiled_prompt_1_pydantic_2.txt"
)
COMPILED_PROMPT_2_WITHOUT_INSTRUCTIONS = reader("compiled_prompt_2.txt")
COMPILED_INSTRUCTIONS = reader("compiled_instructions.txt")
LLM_OUTPUT_1_FAIL_GUARDRAILS_VALIDATION = reader(
"llm_output_1_fail_guardrails_validation.txt"
)
LLM_OUTPUT_2_SUCCEED_GUARDRAILS_BUT_FAIL_PYDANTIC_VALIDATION = reader(
"llm_output_2_succeed_gd_but_fail_pydantic_validation.txt"
)
LLM_OUTPUT_3_SUCCEED_GUARDRAILS_AND_PYDANTIC = reader(
"llm_output_3_succeed_gd_and_pydantic.txt"
)
RAIL_SPEC_WITH_VALIDATOR_PARALLELISM = reader("validator_parallelism.rail")
VALIDATOR_PARALLELISM_PROMPT_1 = reader("validator_parallelism_prompt_1.txt")
VALIDATOR_PARALLELISM_RESPONSE_1 = reader("validator_parallelism_1.txt")
VALIDATOR_PARALLELISM_PROMPT_2 = reader("validator_parallelism_prompt_2.txt")
VALIDATOR_PARALLELISM_RESPONSE_2 = reader("validator_parallelism_2.txt")
VALIDATOR_PARALLELISM_PROMPT_3 = reader("validator_parallelism_prompt_3.txt")
VALIDATOR_PARALLELISM_RESPONSE_3 = reader("validator_parallelism_3.txt")
__all__ = [
"COMPILED_PROMPT_1_WITHOUT_INSTRUCTIONS",
"COMPILED_PROMPT_2_WITHOUT_INSTRUCTIONS",
"COMPILED_INSTRUCTIONS",
"LLM_OUTPUT_1_FAIL_GUARDRAILS_VALIDATION",
"LLM_OUTPUT_2_SUCCEED_GUARDRAILS_BUT_FAIL_PYDANTIC_VALIDATION",
"LLM_OUTPUT_3_SUCCEED_GUARDRAILS_AND_PYDANTIC",
]

View File

@@ -0,0 +1,9 @@
You are a helpful assistant only capable of communicating with valid JSON, and no other text.
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.
Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

View File

@@ -0,0 +1,34 @@
Provide detailed information about the top 5 grossing movies from Christopher Nolan including release date, duration, budget, whether it's a sequel, website, and contact email.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<string name="name" format="is-valid-director"/>
<list name="movies">
<object>
<integer name="rank"/>
<string name="title"/>
<object name="details">
<date name="release_date"/>
<time name="duration"/>
<float name="budget"/>
<bool name="is_sequel" required="false"/>
<string name="website" format="length: min=9 max=100"/>
<string name="contact_email"/>
<choice name="revenue" discriminator="revenue_type">
<case name="box_office">
<float name="gross" format="validate_gross"/>
<float name="opening_weekend"/>
</case>
<case name="streaming">
<integer name="subscriptions"/>
<float name="subscription_fee"/>
</case>
</choice>
</object>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,33 @@
Provide detailed information about the top 5 grossing movies from Christopher Nolan including release date, duration, budget, whether it's a sequel, website, and contact email.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<string format="is-valid-director" name="name" required="true"></string>
<list name="movies" required="true">
<object required="true">
<integer name="rank" required="true"></integer>
<string name="title" required="true"></string>
<object name="details" required="true">
<date name="release_date" required="true"></date>
<time name="duration" required="true"></time>
<float name="budget" required="true"></float>
<bool name="is_sequel" required="false"></bool>
<string format="length: 9 100" name="website" required="true"></string>
<string name="contact_email" required="true"></string>
<choice discriminator="revenue_type" name="revenue" required="true">
<case name="box_office">
<float name="gross" required="true"></float>
<float name="opening_weekend" required="true"></float>
</case>
<case name="streaming">
<integer name="subscriptions" required="true"></integer>
<float name="subscription_fee" required="true"></float>
</case>
</choice>
</object>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,51 @@
I was given the following JSON response, which had problems due to incorrect values.
{
"movies": [
{
"details": {
"website": {
"incorrect_value": "a.b.c",
"error_messages": [
"Value has length less than 9. Please return a longer output, that is shorter than 100 characters."
]
}
}
}
]
}
Help me correct the incorrect values based on the given error messages.
Given below is XML that describes the information to extract from this document and the tags to extract it into.
<output>
<string format="is-valid-director" name="name" required="true"></string>
<list name="movies" required="true">
<object required="true">
<integer name="rank" required="true"></integer>
<string name="title" required="true"></string>
<object name="details" required="true">
<date name="release_date" required="true"></date>
<time name="duration" required="true"></time>
<float name="budget" required="true"></float>
<bool name="is_sequel" required="false"></bool>
<string format="length: 9 100" name="website" required="true"></string>
<string name="contact_email" required="true"></string>
<choice discriminator="revenue_type" name="revenue" required="true">
<case name="box_office">
<float name="gross" required="true"></float>
<float name="opening_weekend" required="true"></float>
</case>
<case name="streaming">
<integer name="subscriptions" required="true"></integer>
<float name="subscription_fee" required="true"></float>
</case>
</choice>
</object>
</object>
</list>
</output>
ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `null`.

View File

@@ -0,0 +1,90 @@
{
"name": "Christopher Nolan",
"movies": [
{
"rank": 1,
"title": "Inception",
"details": {
"release_date": "2010-07-16",
"duration": "02:28:00",
"budget": 160000000.0,
"is_sequel": false,
"website": "a.b.c",
"contact_email": "info@inceptionmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 829895144.0,
"opening_weekend": 62785337.0
}
}
},
{
"rank": 2,
"title": "The Dark Knight",
"details": {
"release_date": "2008-07-18",
"duration": "02:32:00",
"budget": 185000000.0,
"is_sequel": true,
"website": "https://www.thedarkknightmovie.com",
"contact_email": "info@thedarkknightmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 1004558444.0,
"opening_weekend": 158411483.0
}
}
},
{
"rank": 3,
"title": "The Dark Knight Rises",
"details": {
"release_date": "2012-07-20",
"duration": "02:44:00",
"budget": 250000000.0,
"is_sequel": true,
"website": "https://www.thedarkknightrises.com",
"contact_email": "info@thedarkknightrises.com",
"revenue": {
"revenue_type": "streaming",
"subscriptions": 15000000,
"subscription_fee": 9.99
}
}
},
{
"rank": 4,
"title": "Interstellar",
"details": {
"release_date": "2014-11-07",
"duration": "02:49:00",
"budget": 165000000.0,
"is_sequel": false,
"website": "https://www.interstellarmovie.com",
"contact_email": "info@interstellarmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 115000000.0,
"opening_weekend": 47510360.0
}
}
},
{
"rank": 5,
"title": "Dunkirk",
"details": {
"release_date": "2017-07-21",
"duration": "01:46:00",
"budget": 100000000.0,
"is_sequel": false,
"website": "https://www.dunkirkmovie.com",
"contact_email": "info@dunkirkmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 526940665.0,
"opening_weekend": 50513488.0
}
}
}
]
}

View File

@@ -0,0 +1,90 @@
{
"name": "Christopher Nolan",
"movies": [
{
"rank": 1,
"title": "Inception",
"details": {
"release_date": "2010-07-16",
"duration": "02:28:00",
"budget": 160000000.0,
"is_sequel": false,
"website": "https://www.inceptionmovie.com",
"contact_email": "info@inceptionmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 829895144.0,
"opening_weekend": 62785337.0
}
}
},
{
"rank": 2,
"title": "The Dark Knight",
"details": {
"release_date": "2008-07-18",
"duration": "02:32:00",
"budget": 185000000.0,
"is_sequel": true,
"website": "https://www.thedarkknightmovie.com",
"contact_email": "info@thedarkknightmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 1004558444.0,
"opening_weekend": 158411483.0
}
}
},
{
"rank": 3,
"title": "The Dark Knight Rises",
"details": {
"release_date": "2012-07-20",
"duration": "02:44:00",
"budget": 250000000.0,
"is_sequel": true,
"website": "https://www.thedarkknightrises.com",
"contact_email": "info@thedarkknightrises.com",
"revenue": {
"revenue_type": "streaming",
"subscriptions": 15000000,
"subscription_fee": 9.99
}
}
},
{
"rank": 4,
"title": "Interstellar",
"details": {
"release_date": "2014-11-07",
"duration": "02:49:00",
"budget": 165000000.0,
"is_sequel": false,
"website": "https://www.interstellarmovie.com",
"contact_email": "info@interstellarmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 115000000.0,
"opening_weekend": 47510360.0
}
}
},
{
"rank": 5,
"title": "Dunkirk",
"details": {
"release_date": "2017-07-21",
"duration": "01:46:00",
"budget": 100000000.0,
"is_sequel": false,
"website": "https://www.dunkirkmovie.com",
"contact_email": "info@dunkirkmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 526940665.0,
"opening_weekend": 50513488.0
}
}
}
]
}

View File

@@ -0,0 +1,90 @@
{
"name": "Christopher Nolan",
"movies": [
{
"rank": 1,
"title": "Inception",
"details": {
"release_date": "2010-07-16",
"duration": "02:28:00",
"budget": 160000000.0,
"is_sequel": false,
"website": "https://www.inceptionmovie.com",
"contact_email": "info@inceptionmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 829895144.0,
"opening_weekend": 62785337.0
}
}
},
{
"rank": 2,
"title": "The Dark Knight",
"details": {
"release_date": "2008-07-18",
"duration": "02:32:00",
"budget": 185000000.0,
"is_sequel": true,
"website": "https://www.thedarkknightmovie.com",
"contact_email": "info@thedarkknightmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 1004558444.0,
"opening_weekend": 158411483.0
}
}
},
{
"rank": 3,
"title": "The Dark Knight Rises",
"details": {
"release_date": "2012-07-20",
"duration": "02:44:00",
"budget": 250000000.0,
"is_sequel": true,
"website": "https://www.thedarkknightrises.com",
"contact_email": "info@thedarkknightrises.com",
"revenue": {
"revenue_type": "streaming",
"subscriptions": 15000000,
"subscription_fee": 9.99
}
}
},
{
"rank": 4,
"title": "Interstellar",
"details": {
"release_date": "2014-11-07",
"duration": "02:49:00",
"budget": 165000000.0,
"is_sequel": false,
"website": "https://www.interstellarmovie.com",
"contact_email": "info@interstellarmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 677471339.0,
"opening_weekend": 47510360.0
}
}
},
{
"rank": 5,
"title": "Dunkirk",
"details": {
"release_date": "2017-07-21",
"duration": "01:46:00",
"budget": 100000000.0,
"is_sequel": false,
"website": "https://www.dunkirkmovie.com",
"contact_email": "info@dunkirkmovie.com",
"revenue": {
"revenue_type": "box_office",
"gross": 526940665.0,
"opening_weekend": 50513488.0
}
}
}
]
}

View File

@@ -0,0 +1,22 @@
<rail version="0.1">
<output
type="string"
name="greeting"
validators="two-words; lower-case; one-line; valid-url; valid-choices: {['a']}; length: 1 10"
on-fail-two-words="reask"
on-fail-lower-case="reask"
on-fail-one-line="fix"
on-fail-valid-url="noop"
on-fail-valid-choices="noop"
on-fail-length="reask"
/>
<messages>
<message role="user">
Say hullo to my little friend
${gr.complete_string_suffix}
</message>
</messages>
</rail>

View File

@@ -0,0 +1,16 @@
Say hullo to my little friend
Your generated response should satisfy the following properties:
- two-words
- lower-case
- one-line
- valid-url
- valid-choices: choices=['a']
- length: min=1 max=10
Don't talk; just go.

Some files were not shown because too many files have changed in this diff Show More