mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
55efbb8a7e
``` 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> |
||
---|---|---|
.. | ||
custom_example_selector.md | ||
custom_prompt_template.ipynb | ||
example_prompt.json | ||
example_selectors.ipynb | ||
examples.json | ||
examples.yaml | ||
few_shot_examples.ipynb | ||
few_shot_prompt_example_prompt.json | ||
few_shot_prompt_examples_in.json | ||
few_shot_prompt_yaml_examples.yaml | ||
few_shot_prompt.json | ||
few_shot_prompt.yaml | ||
output_parsers.ipynb | ||
partial.ipynb | ||
prompt_management.ipynb | ||
prompt_serialization.ipynb | ||
simple_prompt_with_template_file.json | ||
simple_prompt.json | ||
simple_prompt.yaml | ||
simple_template.txt |