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:
Now, install the OpenAI module to use its resources like libraries and methods:
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 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.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:
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:
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:
output = model(_input)
Now, simply use the parser() function with the output variable to print the comma-separated list:
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.