LangChain

How to Return a List of Comma-Separated Items Using List Parser in LangChain?

LangChain is the framework to create chatbots that can interact with humans by fetching data from vector stores. List Parser is a Python module that is used to get the answers in the form of a list with separated commas. Large Language Models or LLMs can also use parsers to find answers or replies in the form of lists according to a particular prompt.

This guide will demonstrate the process of returning a list of comma-separated items using a list parser in LangChain.

How to Return a List of Comma-Separated Items Using List Parser in LangChain?

To return a list of common-separated items using a list parser in LangChain, simply follow this easy guide:

Setup Frameworks/Modules

Start the process by installing the LangChain framework using the following code:

pip install langchain

Now, install the OpenAI module to use its resources like libraries and methods:

pip install openai

A screenshot of a computer Description automatically generated

After that, simply provide the OpenAI API key using the “os” and “getpass” libraries to access the operating system and insert the API key using getpass() function:

import os

import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

Create List Parser

Once the API key is inserted, simply import libraries like CommaSeparatedListOutputParser, PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, OpenAI, and ChatOpenAI to create a list parser. After that, simply use the “output_parser” variable to initialize the CommaSeparatedListOutputParser() function:

from langchain.output_parsers import CommaSeparatedListOutputParser

from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate

from langchain.llms import OpenAI

from langchain.chat_models import ChatOpenAI

output_parser = CommaSeparatedListOutputParser()

Format Template

Configure the format of the list parser using a format for the prompt that is used to get the list. The PromptTemplate() function contains the number of objects to be returned for the query and variables configurations:

format_instructions = output_parser.get_format_instructions()

prompt = PromptTemplate(

template="Give three {subject}.\n{format_instructions}",

  input_variables=["subject"],

  partial_variables={"format_instructions": format_instructions}

)

Test Parser

Use the OpenAI() function to initialize the model variable and use it to get answers from the bot:

model = OpenAI(temperature=0)

Provide the query in the prompt.format() function with the subject as its parameter containing the prompt and use the model() method to get the output:

_input = prompt.format(subject="colors")

output = model(_input)

Now, simply use the parser() function with the output variable to print the comma-separated list:

output_parser.parse(output)

The following screenshot displays the returned list of colors which are three as configured in the prompt template:

That is all about returning a list of comma-separated items using a list parser in LangChain.

Conclusion

To return a list of comma-separated items using a list parser in LangChain, simply install LangChain and OpenAI modules to get started with the process. After that, connect to OpenAI using its API key and access its libraries like OpenAI and ChatOpenAI. Configure the prompt template with the variable and subjects to be fetched using the prompt and then provide the prompt to get the comma-separated list of items. This guide has explained the process of returning a list of comma-separated items using a list parser in LangChain.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.