Skip to main content

Summarizer

In this example we will use Guardrails to summarize text in terms of length, quality and output read time.

!!! note To download this example as a Jupyter notebook, click here.

In this example, we will use Guardrails in the summarization of a text document. We will check whether the summarized document has a high semantic similarity with the original document. We will ensure the ouptput meets a certain range of length and read time.

Setup

In order to check semantic similarity we will need the numpy package via the command below. We will also install the validators we indtend to use.

guardrails hub install hub://guardrails/reading_time --quiet --install-local-models
guardrails hub install hub://guardrails/similar_to_document --quiet --install-local-models
guardrails hub install hub://guardrails/valid_length --quiet --install-local-models
    
[notice] A new release of pip is available: 23.2.1 -> 24.1.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
Installing hub://guardrails/reading_time...
✅Successfully installed guardrails/reading_time!


Installing hub://guardrails/similar_to_document...
✅Successfully installed guardrails/similar_to_document!


Installing hub://guardrails/valid_length...
✅Successfully installed guardrails/valid_length!


Step 1: Load data and create Pydantic Model

Load our text with the code below

with open("data/twain.txt", "r") as file:
document = file.read()
file.seek(0)
content = "".join(line.strip() for line in file.readlines())

Next we can define our return output with a pydantic model

from pydantic import BaseModel, Field

from guardrails.hub import SimilarToDocument, ValidLength, ReadingTime

prompt = """
Summarize the following text faithfully:

${document}

${gr.complete_xml_suffix}
"""

THREE_MINUTES = 180 / 60

class TextSummary(BaseModel):
summary: str = Field(
description="Faithful summary of the text",
validators=[
ReadingTime(reading_time=THREE_MINUTES, on_fail="exception"),
ValidLength(min=100, max=1000, on_fail="exception"),
SimilarToDocument(document=f"'{content}'", threshold=0.60, on_fail="filter")
],
)

Step 2 Create Guard from pydantic

The guard we create will:

  1. Enforce reading time
  2. Enforce length
  3. Enforce similarity
import guardrails as gd
from guardrails.errors import ValidationError

guard = gd.Guard().from_pydantic(TextSummary)

Step 3: Call LLM via guard(

We use the tools api to ensure our data is returned in a structured form.

import os
# TODO: Replace OPENAI_API_KEY with your OpenAI API key, uncomment
# os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"

response = guard(
messages=[{"role":"user", "content": prompt}],
prompt_params={"document": document},
model="gpt-4o",
tools=guard.json_function_calling_tool(),
tool_choice="required",
)

print(f"Validated Output: {response.validated_output}")
    Similarity: 0.789, Type: <class 'float'>
Validated Output: {'summary': "Mark Twain discusses the art of storytelling, focusing on the humorous story, which he considers uniquely American. He contrasts it with the comic story, which is English, and the witty story, which is French. The humorous story relies on the manner of telling, can be lengthy, and often ends without a clear point. In contrast, comic and witty stories are brief and end with a punchline. Twain emphasizes that telling a humorous story requires high artistic skill, while anyone can tell comic or witty stories. He notes that humorous stories are told gravely, with the teller pretending not to know it's funny, whereas comic stories are told with obvious delight and emphasis on the punchline. Twain mentions that some storytellers, like Artemus Ward, use subtle techniques to deliver the punchline, while comic storytellers make it very obvious. He finds the latter approach less appealing."}

We can see the step-wise history of the Guard object below:

guard.history.last.tree
Logs
└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮
│ No prompt │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮
│ ┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
│ ┃ Role Content ┃ │
│ ┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
│ │ user │ │ │
│ │ │ Summarize the following text faithfully: │ │
│ │ │ │ │
│ │ │ The Humorous Story an American Development.— Its │ │
│ │ │ Difference from Comic and Witty Stories. │ │
│ │ │ │ │
│ │ │ DO not claim that I can tell a story as it ought to │ │
│ │ │ be told. I only claim to know how a story │ │
│ │ │ ought to be told, for I have been almost daily in the │ │
│ │ │ company of the most expert story-tellers for many │ │
│ │ │ years. │ │
│ │ │ │ │
│ │ │ There are several kinds of stories, but only one │ │
│ │ │ difficult kind —the humorous. I will talk mainly │ │
│ │ │ about that one. The humorous story is American, │ │
│ │ │ the comic story is English, the witty story is French. │ │
│ │ │ The humorous story depends for its effect upon the │ │
│ │ │ manner of the telling; the comic story and the witty │ │
│ │ │ story upon the matter. │ │
│ │ │ │ │
│ │ │ The humorous story may be spun out to great │ │
│ │ │ length, and may wander around as much as it │ │
│ │ │ pleases, and arrive nowhere in particular; but the │ │
│ │ │ comic and witty stories must be brief and end with │ │
│ │ │ a point. The humorous story bubbles gently along, │ │
│ │ │ the others burst. │ │
│ │ │ │ │
│ │ │ The humorous story is strictly a work of art — │ │
│ │ │ high and delicate art — and only an artist can tell it; │ │
│ │ │ but no art is necessary in telling the comic and the │ │
│ │ │ witty story; anybody can do it. The art of telling │ │
│ │ │ a humorous story — understand, I mean by word of │ │
│ │ │ mouth, not print — was created in America, and │ │
│ │ │ has remained at home. │ │
│ │ │ │ │
│ │ │ The humorous story is told gravely; the teller │ │
│ │ │ does his best to conceal the fact that he even dimly │ │
│ │ │ suspects that there is anything funny about it; but │ │
│ │ │ the teller of the comic story tells you beforehand │ │
│ │ │ that it is one of the funniest things he has ever │ │
│ │ │ heard, then tells it with eager delight, and is the │ │
│ │ │ first person to laugh when he gets through. And │ │
│ │ │ sometimes, if he has had good success, he is so glad │ │
│ │ │ and happy that he will repeat the ‘‘ nub’’ of it and │ │
│ │ │ slance around from face to face, collecting applause, │ │
│ │ │ and then repeat it again. It is a pathetic thing to │ │
│ │ │ see. │ │
│ │ │ │ │
│ │ │ Very often, of course, the rambling and disjointed │ │
│ │ │ humorous story finishes with a nub, point, snapper, │ │
│ │ │ or whatever you like to call it. Then the listener │ │
│ │ │ must be alert, for in many cases the teller will divert │ │
│ │ │ attention from that nub by dropping it in a carefully. │ │
│ │ │ casual and indifferent way, with the pretence that he │ │
│ │ │ does not know it is a nub. │ │
│ │ │ │ │
│ │ │ Artemus Ward used that trick a good deal; then │ │
│ │ │ when the belated audience presently caught the joke │ │
│ │ │ he would look up with innocent surprise, as if │ │
│ │ │ wondering what they had found to laugh at. Dan │ │
│ │ │ Setchell used it before him, Nye and Riley and │ │
│ │ │ others use it to-day. │ │
│ │ │ │ │
│ │ │ But the teller of the comic story does not slur │ │
│ │ │ the nub; he shouts it at you—every time. And │ │
│ │ │ when he prints it, in England, France, Germany, │ │
│ │ │ and Italy, he italicizes it, puts some whooping │ │
│ │ │ exclamation-points after it, and sometimes explains │ │
│ │ │ it in a parenthesis. All of which is very depressing, │ │
│ │ │ and makes one want to renounce joking and lead a │ │
│ │ │ better life. │ │
│ │ │ │ │
│ │ │ - Mark Twain │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ Given below is XML that describes the information to extract from this document and the tags │ │
│ │ │ to extract it into. │ │
│ │ │ │ │
│ │ │ <output> │ │
│ │ │ <string description="Faithful summary of the text" format="guardrails/reading_time: 3.0; │ │
│ │ │ guardrails/valid_length: 100 1000; guardrails/similar_to_document: 'The Humorous Story an │ │
│ │ │ American Development.— ItsDifference from Comic and Witty Stories.DO not claim that I can │ │
│ │ │ tell a story as it ought tobe told. I only claim to know how a storyought to be told, for I │ │
│ │ │ have been almost daily in thecompany of the most expert story-tellers for manyyears.There │ │
│ │ │ are several kinds of stories, but only onedifficult kind —the humorous. I will talk │ │
│ │ │ mainlyabout that one. The humorous story is American,the comic story is English, the witty │ │
│ │ │ story is French.The humorous story depends for its effect upon themanner of the telling; the │ │
│ │ │ comic story and the wittystory upon the matter.The humorous story may be spun out to │ │
│ │ │ greatlength, and may wander around as much as itpleases, and arrive nowhere in particular; │ │
│ │ │ but thecomic and witty stories must be brief and end witha point. The humorous story bubbles │ │
│ │ │ gently along,the others burst.The humorous story is strictly a work of art —high and │ │
│ │ │ delicate art — and only an artist can tell it;but no art is necessary in telling the comic │ │
│ │ │ and thewitty story; anybody can do it. The art of tellinga humorous story — understand, I │ │
│ │ │ mean by word ofmouth, not print — was created in America, andhas remained at home.The │ │
│ │ │ humorous story is told gravely; the tellerdoes his best to conceal the fact that he even │ │
│ │ │ dimlysuspects that there is anything funny about it; butthe teller of the comic story tells │ │
│ │ │ you beforehandthat it is one of the funniest things he has everheard, then tells it with │ │
│ │ │ eager delight, and is thefirst person to laugh when he gets through. Andsometimes, if he has │ │
│ │ │ had good success, he is so gladand happy that he will repeat the ‘‘ nub’’ of it andslance │ │
│ │ │ around from face to face, collecting applause,and then repeat it again. It is a pathetic │ │
│ │ │ thing tosee.Very often, of course, the rambling and disjointedhumorous story finishes with a │ │
│ │ │ nub, point, snapper,or whatever you like to call it. Then the listenermust be alert, for in │ │
│ │ │ many cases the teller will divertattention from that nub by dropping it in a │ │
│ │ │ carefully.casual and indifferent way, with the pretence that hedoes not know it is a │ │
│ │ │ nub.Artemus Ward used that trick a good deal; thenwhen the belated audience presently caught │ │
│ │ │ the jokehe would look up with innocent surprise, as ifwondering what they had found to laugh │ │
│ │ │ at. DanSetchell used it before him, Nye and Riley andothers use it to-day.But the teller of │ │
│ │ │ the comic story does not slurthe nub; he shouts it at you—every time. Andwhen he prints it, │ │
│ │ │ in England, France, Germany,and Italy, he italicizes it, puts some │ │
│ │ │ whoopingexclamation-points after it, and sometimes explainsit in a parenthesis. All of which │ │
│ │ │ is very depressing,and makes one want to renounce joking and lead abetter life.- Mark Twain' │ │
│ │ │ 0.6 all-MiniLM-L6-v2" name="summary" 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}}` │ │
│ │ │ │ │
│ │ │ │ │
│ └──────┴──────────────────────────────────────────────────────────────────────────────────────────────┘ │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮
│ {"summary":"Mark Twain discusses the art of storytelling, focusing on the humorous story, which he │
│ considers uniquely American. He contrasts it with the comic story, which is English, and the witty │
│ story, which is French. The humorous story relies on the manner of telling, can be lengthy, and often │
│ ends without a clear point. In contrast, comic and witty stories are brief and end with a punchline. │
│ Twain emphasizes that telling a humorous story requires high artistic skill, while anyone can tell │
│ comic or witty stories. He notes that humorous stories are told gravely, with the teller pretending not │
│ to know it's funny, whereas comic stories are told with obvious delight and emphasis on the punchline. │
│ Twain mentions that some storytellers, like Artemus Ward, use subtle techniques to deliver the │
│ punchline, while comic storytellers make it very obvious. He finds the latter approach less │
│ appealing."} │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮
│ { │
│ 'summary': "Mark Twain discusses the art of storytelling, focusing on the humorous story, which he │
│ considers uniquely American. He contrasts it with the comic story, which is English, and the witty │
│ story, which is French. The humorous story relies on the manner of telling, can be lengthy, and often │
│ ends without a clear point. In contrast, comic and witty stories are brief and end with a punchline. │
│ Twain emphasizes that telling a humorous story requires high artistic skill, while anyone can tell │
│ comic or witty stories. He notes that humorous stories are told gravely, with the teller pretending not │
│ to know it's funny, whereas comic stories are told with obvious delight and emphasis on the punchline. │
│ Twain mentions that some storytellers, like Artemus Ward, use subtle techniques to deliver the │
│ punchline, while comic storytellers make it very obvious. He finds the latter approach less appealing." │
│ } │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

The guard wrapper returns the raw_llm_respose (which is a simple string), and the validated and corrected output (which is a dictionary). We can see that the output is a dictionary with the correct schema and types.

Now lets try a model thats not as proficent at summarization and we can see the ouptput is filtered and validation has failed.

The final validated output is None due to the failed validation.

response = guard(
messages=[{"role":"user", "content": prompt}],
prompt_params={"document": document},
model="babbage-002",
max_tokens=512,
temperature=0,
)

print(f"Validated Output: {response.validated_output}")
    Validated Output: None

We can see the step wise history of the guard execution below:

guard.history.last.tree
Logs
├── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
│ │ ╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮
│ │ │ No prompt │
│ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
│ │ ╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮
│ │ │ ┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │
│ │ │ ┃ Role Content ┃ │
│ │ │ ┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │
│ │ │ │ user │ │ │
│ │ │ │ │ Summarize the following text faithfully: │ │
│ │ │ │ │ │ │
│ │ │ │ │ The Humorous Story an American Development.— Its │ │
│ │ │ │ │ Difference from Comic and Witty Stories. │ │
│ │ │ │ │ │ │
│ │ │ │ │ DO not claim that I can tell a story as it ought to │ │
│ │ │ │ │ be told. I only claim to know how a story │ │
│ │ │ │ │ ought to be told, for I have been almost daily in the │ │
│ │ │ │ │ company of the most expert story-tellers for many │ │
│ │ │ │ │ years. │ │
│ │ │ │ │ │ │
│ │ │ │ │ There are several kinds of stories, but only one │ │
│ │ │ │ │ difficult kind —the humorous. I will talk mainly │ │
│ │ │ │ │ about that one. The humorous story is American, │ │
│ │ │ │ │ the comic story is English, the witty story is French. │ │
│ │ │ │ │ The humorous story depends for its effect upon the │ │
│ │ │ │ │ manner of the telling; the comic story and the witty │ │
│ │ │ │ │ story upon the matter. │ │
│ │ │ │ │ │ │
│ │ │ │ │ The humorous story may be spun out to great │ │
│ │ │ │ │ length, and may wander around as much as it │ │
│ │ │ │ │ pleases, and arrive nowhere in particular; but the │ │
│ │ │ │ │ comic and witty stories must be brief and end with │ │
│ │ │ │ │ a point. The humorous story bubbles gently along, │ │
│ │ │ │ │ the others burst. │ │
│ │ │ │ │ │ │
│ │ │ │ │ The humorous story is strictly a work of art — │ │
│ │ │ │ │ high and delicate art — and only an artist can tell it; │ │
│ │ │ │ │ but no art is necessary in telling the comic and the │ │
│ │ │ │ │ witty story; anybody can do it. The art of telling │ │
│ │ │ │ │ a humorous story — understand, I mean by word of │ │
│ │ │ │ │ mouth, not print — was created in America, and │ │
│ │ │ │ │ has remained at home. │ │
│ │ │ │ │ │ │
│ │ │ │ │ The humorous story is told gravely; the teller │ │
│ │ │ │ │ does his best to conceal the fact that he even dimly │ │
│ │ │ │ │ suspects that there is anything funny about it; but │ │
│ │ │ │ │ the teller of the comic story tells you beforehand │ │
│ │ │ │ │ that it is one of the funniest things he has ever │ │
│ │ │ │ │ heard, then tells it with eager delight, and is the │ │
│ │ │ │ │ first person to laugh when he gets through. And │ │
│ │ │ │ │ sometimes, if he has had good success, he is so glad │ │
│ │ │ │ │ and happy that he will repeat the ‘‘ nub’’ of it and │ │
│ │ │ │ │ slance around from face to face, collecting applause, │ │
│ │ │ │ │ and then repeat it again. It is a pathetic thing to │ │
│ │ │ │ │ see. │ │
│ │ │ │ │ │ │
│ │ │ │ │ Very often, of course, the rambling and disjointed │ │
│ │ │ │ │ humorous story finishes with a nub, point, snapper, │ │
│ │ │ │ │ or whatever you like to call it. Then the listener │ │
│ │ │ │ │ must be alert, for in many cases the teller will divert │ │
│ │ │ │ │ attention from that nub by dropping it in a carefully. │ │
│ │ │ │ │ casual and indifferent way, with the pretence that he │ │
│ │ │ │ │ does not know it is a nub. │ │
│ │ │ │ │ │ │
│ │ │ │ │ Artemus Ward used that trick a good deal; then │ │
│ │ │ │ │ when the belated audience presently caught the joke │ │
│ │ │ │ │ he would look up with innocent surprise, as if │ │
│ │ │ │ │ wondering what they had found to laugh at. Dan │ │
│ │ │ │ │ Setchell used it before him, Nye and Riley and │ │
│ │ │ │ │ others use it to-day. │ │
│ │ │ │ │ │ │
│ │ │ │ │ But the teller of the comic story does not slur │ │
│ │ │ │ │ the nub; he shouts it at you—every time. And │ │
│ │ │ │ │ when he prints it, in England, France, Germany, │ │
│ │ │ │ │ and Italy, he italicizes it, puts some whooping │ │
│ │ │ │ │ exclamation-points after it, and sometimes explains │ │
│ │ │ │ │ it in a parenthesis. All of which is very depressing, │ │
│ │ │ │ │ and makes one want to renounce joking and lead a │ │
│ │ │ │ │ better life. │ │
│ │ │ │ │ │ │
│ │ │ │ │ - Mark Twain │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ │ │ Given below is XML that describes the information to extract from this document and the tags │ │
│ │ │ │ │ to extract it into. │ │
│ │ │ │ │ │ │
│ │ │ │ │ <output> │ │
│ │ │ │ │ <string description="Faithful summary of the text" format="guardrails/reading_time: 3.0; │ │
│ │ │ │ │ guardrails/valid_length: 100 1000; guardrails/similar_to_document: 'The Humorous Story an │ │
│ │ │ │ │ American Development.— ItsDifference from Comic and Witty Stories.DO not claim that I can │ │
│ │ │ │ │ tell a story as it ought tobe told. I only claim to know how a storyought to be told, for I │ │
│ │ │ │ │ have been almost daily in thecompany of the most expert story-tellers for manyyears.There │ │
│ │ │ │ │ are several kinds of stories, but only onedifficult kind —the humorous. I will talk │ │
│ │ │ │ │ mainlyabout that one. The humorous story is American,the comic story is English, the witty │ │
│ │ │ │ │ story is French.The humorous story depends for its effect upon themanner of the telling; the │ │
│ │ │ │ │ comic story and the wittystory upon the matter.The humorous story may be spun out to │ │
│ │ │ │ │ greatlength, and may wander around as much as itpleases, and arrive nowhere in particular; │ │
│ │ │ │ │ but thecomic and witty stories must be brief and end witha point. The humorous story bubbles │ │
│ │ │ │ │ gently along,the others burst.The humorous story is strictly a work of art —high and │ │
│ │ │ │ │ delicate art — and only an artist can tell it;but no art is necessary in telling the comic │ │
│ │ │ │ │ and thewitty story; anybody can do it. The art of tellinga humorous story — understand, I │ │
│ │ │ │ │ mean by word ofmouth, not print — was created in America, andhas remained at home.The │ │
│ │ │ │ │ humorous story is told gravely; the tellerdoes his best to conceal the fact that he even │ │
│ │ │ │ │ dimlysuspects that there is anything funny about it; butthe teller of the comic story tells │ │
│ │ │ │ │ you beforehandthat it is one of the funniest things he has everheard, then tells it with │ │
│ │ │ │ │ eager delight, and is thefirst person to laugh when he gets through. Andsometimes, if he has │ │
│ │ │ │ │ had good success, he is so gladand happy that he will repeat the ‘‘ nub’’ of it andslance │ │
│ │ │ │ │ around from face to face, collecting applause,and then repeat it again. It is a pathetic │ │
│ │ │ │ │ thing tosee.Very often, of course, the rambling and disjointedhumorous story finishes with a │ │
│ │ │ │ │ nub, point, snapper,or whatever you like to call it. Then the listenermust be alert, for in │ │
│ │ │ │ │ many cases the teller will divertattention from that nub by dropping it in a │ │
│ │ │ │ │ carefully.casual and indifferent way, with the pretence that hedoes not know it is a │ │
│ │ │ │ │ nub.Artemus Ward used that trick a good deal; thenwhen the belated audience presently caught │ │
│ │ │ │ │ the jokehe would look up with innocent surprise, as ifwondering what they had found to laugh │ │
│ │ │ │ │ at. DanSetchell used it before him, Nye and Riley andothers use it to-day.But the teller of │ │
│ │ │ │ │ the comic story does not slurthe nub; he shouts it at you—every time. Andwhen he prints it, │ │
│ │ │ │ │ in England, France, Germany,and Italy, he italicizes it, puts some │ │
│ │ │ │ │ whoopingexclamation-points after it, and sometimes explainsit in a parenthesis. All of which │ │
│ │ │ │ │ is very depressing,and makes one want to renounce joking and lead abetter life.- Mark Twain' │ │
│ │ │ │ │ 0.6 all-MiniLM-L6-v2" name="summary" 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}}` │ │
│ │ │ │ │ │ │
│ │ │ │ │ │ │
│ │ │ └──────┴──────────────────────────────────────────────────────────────────────────────────────────────┘ │
│ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
│ │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮
│ │ │ The following XML is invalid: │
│ │ │ - `<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}}` │
│ │ │ │
│ │ │ The following JSON is invalid: │
│ │ │ - `<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}}` │
│ │ │ │
│ │ │ The following XML is valid: │
│ │ │ - `<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}}` │
│ │ │ │
│ │ │ The following JSON is valid: │
│ │ │ - `<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}}` │
│ │ │ │
│ │ │ The following XML is valid: │
│ │ │ - `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}` │
│ │ │ - `<list │
│ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
│ │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮
│ │ │ None │
│ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
└── ╭────────────────────────────────────────────────── Step 1 ───────────────────────────────────────────────────╮
╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮
│ │
│ I was given the following response, which was not parseable as JSON. │
│ │
│ "The following XML is invalid: - `<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}}` The following JSON is invalid: - `<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}}` The following XML is valid: - `<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}}` The following JSON is valid: - `<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}}` The following XML │
│ is valid: - `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}` - │
│ `<list" │
│ │
│ 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="Faithful summary of the text" format="guardrails/reading_time: 3.0; │
│ guardrails/valid_length: 100 1000; guardrails/similar_to_document: 'The Humorous Story an American │
│ Development.— ItsDifference from Comic and Witty Stories.DO not claim that I can tell a story as it │
│ ought tobe told. I only claim to know how a storyought to be told, for I have been almost daily in │
│ thecompany of the most expert story-tellers for manyyears.There are several kinds of stories, but only │
│ onedifficult kind —the humorous. I will talk mainlyabout that one. The humorous story is American,the │
│ comic story is English, the witty story is French.The humorous story depends for its effect upon │
│ themanner of the telling; the comic story and the wittystory upon the matter.The humorous story may be │
│ spun out to greatlength, and may wander around as much as itpleases, and arrive nowhere in particular; │
│ but thecomic and witty stories must be brief and end witha point. The humorous story bubbles gently │
│ along,the others burst.The humorous story is strictly a work of art —high and delicate art — and only │
│ an artist can tell it;but no art is necessary in telling the comic and thewitty story; anybody can do │
│ it. The art of tellinga humorous story — understand, I mean by word ofmouth, not print — was created in │
│ America, andhas remained at home.The humorous story is told gravely; the tellerdoes his best to conceal │
│ the fact that he even dimlysuspects that there is anything funny about it; butthe teller of the comic │
│ story tells you beforehandthat it is one of the funniest things he has everheard, then tells it with │
│ eager delight, and is thefirst person to laugh when he gets through. Andsometimes, if he has had good │
│ success, he is so gladand happy that he will repeat the ‘‘ nub’’ of it andslance around from face to │
│ face, collecting applause,and then repeat it again. It is a pathetic thing tosee.Very often, of course, │
│ the rambling and disjointedhumorous story finishes with a nub, point, snapper,or whatever you like to │
│ call it. Then the listenermust be alert, for in many cases the teller will divertattention from that │
│ nub by dropping it in a carefully.casual and indifferent way, with the pretence that hedoes not know it │
│ is a nub.Artemus Ward used that trick a good deal; thenwhen the belated audience presently caught the │
│ jokehe would look up with innocent surprise, as ifwondering what they had found to laugh at. │
│ DanSetchell used it before him, Nye and Riley andothers use it to-day.But the teller of the comic story │
│ does not slurthe nub; he shouts it at you—every time. Andwhen he prints it, in England, France, │
│ Germany,and Italy, he italicizes it, puts some whoopingexclamation-points after it, and sometimes │
│ explainsit in a parenthesis. All of which is very depressing,and makes one want to renounce joking and │
│ lead abetter life.- Mark Twain' 0.6 all-MiniLM-L6-v2" name="summary" 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`. │
│ │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── Instructions ──────────────────────────────────────────────╮
│ │
│ 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}}` │
│ │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮
│ No message history. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮
│ 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}}` The following XML │
│ is invalid: - `<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}}` The following JSON is invalid: - `<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}}` The following XML is valid: - `<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}}` The following │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮
│ None │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯