Exercise#1 Enumerations Parser

Objective

In this exercise, you will use the EnumOutputParser to implement the behavior explained in the following prompt. This prompt may or may not work as-is with the LLM of your choice. As a result, you will need to adjust it as part of the exercise.

Note : This prompt was optimized in LangChain/Exercise#7 :)

template_1 = """
what is the color of {object}.

Format instructions: {format_instructions}
""" 
  • Select the LLM model to work with. Here are a few suggestions:
    • OpenAI GPT
    • Cohere Command
    • AI21 Jurasic
  • Create a notebook
  • Setup your environment & LLM
  • Copy and paste the following code in your notebook to start coding
from langchain.output_parsers import EnumOutputParser
from enum import Enum

# Create a class with a set of options
class Colors(Enum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"
    WHITE = "white"
    UNKNOWN = "others"

# Create the output parser
output_parser_enum = EnumOutputParser(enum=Colors)
  1. create a LLMChain to parse the received response automatically
  2. create a LLLChain to receive raw response and then use .parse(..) to parse the response

Solution

To run on your own machine

The solution to the exercise is available in the notebook:

ex-1-enum-output-parser.png

Google colab

  • Make sure to follow instructions for setting up packages
Open In Colab