langchain/docs/modules/prompts/examples
jerwelborn 55efbb8a7e
pydantic/json parsing (#1722)
```
class Joke(BaseModel):
    setup: str = Field(description="question to set up a joke")
    punchline: str = Field(description="answer to resolve the joke")

joke_query = "Tell me a joke."

# Or, an example with compound type fields.
#class FloatArray(BaseModel):
#    values: List[float] = Field(description="list of floats")
#
#float_array_query = "Write out a few terms of fiboacci."

model = OpenAI(model_name='text-davinci-003', temperature=0.0)
parser = PydanticOutputParser(pydantic_object=Joke)
prompt = PromptTemplate(
    template="Answer the user query.\n{format_instructions}\n{query}\n",
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()}
)

_input = prompt.format_prompt(query=joke_query)
print("Prompt:\n", _input.to_string())
output = model(_input.to_string())
print("Completion:\n", output)
parsed_output = parser.parse(output)
print("Parsed completion:\n", parsed_output)
```

```
Prompt:
 Answer the user query.
The output should be formatted as a JSON instance that conforms to the JSON schema below.  For example, the object {"foo":  ["bar", "baz"]} conforms to the schema {"foo": {"description": "a list of strings field", "type": "string"}}.

Here is the output schema:
---
{"setup": {"description": "question to set up a joke", "type": "string"}, "punchline": {"description": "answer to resolve the joke", "type": "string"}}
---

Tell me a joke.

Completion:
 {"setup": "Why don't scientists trust atoms?", "punchline": "Because they make up everything!"}

Parsed completion:
 setup="Why don't scientists trust atoms?" punchline='Because they make up everything!'
```

Ofc, works only with LMs of sufficient capacity. DaVinci is reliable but
not always.

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
2023-03-16 21:43:11 -07:00
..
custom_example_selector.md Minor grammatical fixes (#1325) 2023-03-01 21:18:09 -08:00
custom_prompt_template.ipynb fix docs custom prompt template (#917) 2023-02-06 20:29:48 -08:00
example_prompt.json Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
example_selectors.ipynb chroma docs (#1012) 2023-02-12 23:02:01 -08:00
examples.json Docs refactor (#480) 2023-01-02 08:24:09 -08:00
examples.yaml Harrison/few shot yaml (#682) 2023-01-21 16:08:03 -08:00
few_shot_examples.ipynb chroma docs (#1012) 2023-02-12 23:02:01 -08:00
few_shot_prompt_example_prompt.json Docs refactor (#480) 2023-01-02 08:24:09 -08:00
few_shot_prompt_examples_in.json Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
few_shot_prompt_yaml_examples.yaml Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
few_shot_prompt.json Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
few_shot_prompt.yaml Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
output_parsers.ipynb pydantic/json parsing (#1722) 2023-03-16 21:43:11 -07:00
partial.ipynb partial variables (#1308) 2023-02-28 08:40:35 -08:00
prompt_management.ipynb chroma docs (#1012) 2023-02-12 23:02:01 -08:00
prompt_serialization.ipynb Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
simple_prompt_with_template_file.json Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
simple_prompt.json Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
simple_prompt.yaml Update examples to prevent confusing missing _type warning (#1391) 2023-03-02 07:39:57 -08:00
simple_template.txt Docs refactor (#480) 2023-01-02 08:24:09 -08:00