Langchain Applications — Part 5— Example Selectors & Output Parsers
************IN PROGRESS************
This article is part of a series explaining Langchain applications with simple Python code examples using OpenAI. Part 3 discussed Embedding Model Applications
Langchain Concepts — Part 4 introduced Prompts & Prompt Templates. In this article, we will take a deeper look into ChatModels applications.
Prompts form a central part of LangChain’s functionality. They consist of PromptValue, Prompt Templates, Example Selectors, and Output Parsers. The PromptValue represents an input to a model, while Prompt Templates are responsible for constructing this input. Example Selectors select examples to include in prompts, and Output Parsers are responsible for formatting the output and parsing it into the desired structure.
Example Selectors: Dynamic Selection of Examples
ExampleSelectors
are objects tasked with choosing examples to include in the prompts. These examples can be statically defined, but the LangChain library accentuates the power of dynamic selection, increasing the adaptability and versatility of prompts. ExampleSelectors
accept user input and return a list of examples to be used, making the prompt creation process more dynamic and context-sensitive.
Output Parsers: Structuring Language Model Responses
OutputParsers
are classes designed to structure responses from language models. There are two mandatory methods a OutputParser
must implement: get_format_instructions() -> str
and parse(str) -> Any
. The get_format_instructions()
method returns a string with instructions on how the model output should be formatted. The parse()
method takes a string (assumed to be the response from a language model) and parses it into a structured format. An optional method, parse_with_prompt(str) -> Any
, uses both the response from a language model and the prompt that generated the response to parse it into a structured format. The prompt is provided in case the OutputParser
needs to retry or fix the output and requires prompt information to do so.