StructuredOutputParser Object
StructuredOutputParser Object
One of LangChain's output parsers is the StructuredOutputParser class and will be the
one we use in the application. There are two main components to LangChain output
parsers: formatting instructions and the parsing method.
The formatting instructions play a pivotal role, allowing us to construct the exact JSON
that will be returned as the response. Additionally, we can use prompts to define the
values of the data that is being returned dynamically using OpenAI. For instance, not
only can we pass back the response, we can also ask OpenAI to provide additional
information such as a source, the date the source was last updated, or even the
response in another language! It's worth mentioning that the additional information is not
static. You can think of it as asking follow-up questions based on the response and
passed back to the user as a more completed dataset.
The .parse() method takes in the normal response from OpenAI as an argument and
structures it based on the formatting instructions.
Now that we have a high-level, conceptual understanding of an output parser, let's
implement it in our application!
Implementing StructuredOutputParser
To start, we'll need to require the StructuredOutputParser class:
const { StructuredOutputParser } =
require("langchain/output_parsers");
Next, we’ll instantiate a new object from the StructuredOutputParser class with some
additional properties. We can define the exact structure and properties we want
returned to the user with the .fromNamesAndDescriptions() method. We're going to
keep things simple and provide a single object, and the object we return will have two
properties. Although we can make these properties anything we want, in this case we'll
pass in code and explanation. The values for each property are static prompts. This
means that we can direct OpenAI to fill in the data for us:
});
template: "You are a programming expert and will answer the user’s
coding questions as thoroughly as possible using JavaScript. If the question is
unrelated to coding, do not answer.\n{format_instructions}\n{question}",
inputVariables: ["question"],
partialVariables: { format_instructions: formatInstructions }
});
We also make a small change to our template. First we add a new property
where we instantiate our prompt object called partialVariables, which is an
object that contains the key format_instructions. The format_instructions key
holds our formatInstructions as its value. Lastly, we
add format_instructions as a variable within the template itself.
Finally, we modify promptFunc() to incorporate the parser:
try {
question: input
});
// Call the model with the formatted prompt
// In this case, simply return the error message instead of the parsed
results.
try {
return parsedResult;
} catch (e) {
return res;
}
}
catch (err) {
console.error(err);
throw(err);
};