The langchain docs include this example for configuring and invoking a PydanticOutputParser
# Define your desired data structure.
class Joke(BaseModel):
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")
# You can add custom validation logic easily with Pydantic.
@validator('setup')
def question_ends_with_question_mark(cls, field):
if field[-1] != '?':
raise ValueError("Badly formed question!")
return field
# And a query intented to prompt a language model to populate the data structure.
joke_query = "Tell me a joke."
# Set up a parser + inject instructions into the prompt template.
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()}
)
However, the code for actually making the API call is a bit weird:
model_name = 'text-davinci-003'
temperature = 0.0
my_llm = OpenAI(model_name=model_name, temperature=temperature)
_input = prompt.format_prompt(query=joke_query)
output = my_llm(_input.to_string())
parser.parse(output)
This returns exactly what we want: Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')
However, it seems odd to not to use Chains
for this.
I can get kind of close, as follows:
chain = LLMChain(llm=my_llm, prompt=prompt)
chain.run(query=joke_query)
But this returns raw, unparsed text: '\n{"setup": "Why did the chicken cross the road?", "punchline": "To get to the other side!"}'
Is there a preferred method to get the Chain
class to make full use of a Parser
, and return the parsed object? I could subclass and extend LLMChain
, but I'd be surprised if this functionality doesn't already exist.