2023-02-08 08:35:33 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "9936fa27",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"# CommaSeparatedListOutputParser\n",
|
2023-02-08 08:35:33 +00:00
|
|
|
"\n",
|
2023-03-27 02:49:46 +00:00
|
|
|
"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,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "872246d7",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"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,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "c3f9aee6",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"output_parser = CommaSeparatedListOutputParser()"
|
2023-02-08 08:35:33 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 3,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "e77871b7",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"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,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "a71cb5d3",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
2023-03-27 02:49:46 +00:00
|
|
|
"outputs": [],
|
2023-02-08 08:35:33 +00:00
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"model = OpenAI(temperature=0)"
|
2023-02-08 08:35:33 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 5,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "783d7d98",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"_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,
|
2023-03-27 02:49:46 +00:00
|
|
|
"id": "fcb81344",
|
2023-02-08 08:35:33 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"['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": [
|
2023-03-27 02:49:46 +00:00
|
|
|
"output_parser.parse(output)"
|
2023-02-08 08:35:33 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
2023-03-27 02:49:46 +00:00
|
|
|
"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",
|
2023-03-27 02:49:46 +00:00
|
|
|
"version": "3.9.1"
|
2023-02-08 08:35:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|