07. EnumOutputParser
EnumOutputParser
Let's find out how to use the Enum output parser!
from langchain.output_parsers.enum import EnumOutputParserenumUsing moduleColorsDefine the class.ColorsClassEnumInheritance,RED,GREEN,BLUEIt has three color values.
from enum import Enum
class Colors(Enum):
RED = "RED"
GREEN = "GREEN"
BLUE = "BLUE"# EnumOutputParser create immediately
parser = EnumOutputParser(enum=Colors)People's information at the prompt (
{person}) And parsing instructions ({instructions}).parser.get_format_instructions()Call the function to get the parsing instructions.prompt,
ChatOpenAIThe model, parser is connected to form a processing chain.
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
# Create a prompt template.
prompt = PromptTemplate.from_template(
"""What color is the following object?
Object: {object}
Instructions: {instructions}"""
# Get the instruction format from the parser and apply it partially.
).partial(instructions=parser.get_format_instructions())
# Prompt and ChatOpenAI, Connect the parser.
chain = prompt | ChatOpenAI() | parserchain.invokeUse the function to request information about the "sky".
Check the results.
Last updated