列表解析器
当您想返回一个逗号分隔的项目列表时,可以使用此输出解析器。
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_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
template="列举五个{subject}。\n{format_instructions}",
input_variables=["subject"],
partial_variables={"format_instructions": format_instructions}
)
model = OpenAI(temperature=0)
_input = prompt.format(subject="冰淇淋口味")
output = model(_input)
output_parser.parse(output)
['香草',
'巧克力',
'草莓',
'薄荷巧克力碎片',
'曲奇和奶油']