langchain/docs/modules/prompts/output_parsers/examples/comma_separated.ipynb

128 lines
2.7 KiB
Plaintext
Raw Normal View History

2023-02-08 08:35:33 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "9936fa27",
2023-02-08 08:35:33 +00:00
"metadata": {},
"source": [
"# CommaSeparatedListOutputParser\n",
2023-02-08 08:35:33 +00:00
"\n",
"Here's another parser strictly less powerful than Pydantic/JSON parsing."
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "872246d7",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [],
"source": [
"from langchain.output_parsers import CommaSeparatedListOutputParser\n",
"from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate\n",
"from langchain.llms import OpenAI\n",
"from langchain.chat_models import ChatOpenAI"
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c3f9aee6",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [],
"source": [
"output_parser = CommaSeparatedListOutputParser()"
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e77871b7",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [],
"source": [
"format_instructions = output_parser.get_format_instructions()\n",
"prompt = PromptTemplate(\n",
" template=\"List five {subject}.\\n{format_instructions}\",\n",
" input_variables=[\"subject\"],\n",
" partial_variables={\"format_instructions\": format_instructions}\n",
")"
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a71cb5d3",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [],
2023-02-08 08:35:33 +00:00
"source": [
"model = OpenAI(temperature=0)"
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "783d7d98",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [],
"source": [
"_input = prompt.format(subject=\"ice cream flavors\")\n",
"output = model(_input)"
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fcb81344",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Vanilla',\n",
" 'Chocolate',\n",
" 'Strawberry',\n",
" 'Mint Chocolate Chip',\n",
" 'Cookies and Cream']"
2023-02-08 08:35:33 +00:00
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output_parser.parse(output)"
2023-02-08 08:35:33 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca5a23c5",
2023-02-08 08:35:33 +00:00
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
2023-02-08 08:35:33 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}