Spaces:
Running
Running
File size: 3,237 Bytes
cfd3735 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
{
"cells": [
{
"cell_type": "markdown",
"id": "872bb8b5",
"metadata": {},
"source": [
"# Transformation Chain\n",
"\n",
"This notebook showcases using a generic transformation chain.\n",
"\n",
"As an example, we will create a dummy transformation that takes in a super long text, filters the text to only the first 3 paragraphs, and then passes that into an LLMChain to summarize those."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bbbb4330",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import TransformChain, LLMChain, SimpleSequentialChain\n",
"from langchain.llms import OpenAI\n",
"from langchain.prompts import PromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8ae5937c",
"metadata": {},
"outputs": [],
"source": [
"with open(\"../../state_of_the_union.txt\") as f:\n",
" state_of_the_union = f.read()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "98739592",
"metadata": {},
"outputs": [],
"source": [
"def transform_func(inputs: dict) -> dict:\n",
" text = inputs[\"text\"]\n",
" shortened_text = \"\\n\\n\".join(text.split(\"\\n\\n\")[:3])\n",
" return {\"output_text\": shortened_text}\n",
"\n",
"transform_chain = TransformChain(input_variables=[\"text\"], output_variables=[\"output_text\"], transform=transform_func)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e9397934",
"metadata": {},
"outputs": [],
"source": [
"template = \"\"\"Summarize this text:\n",
"\n",
"{output_text}\n",
"\n",
"Summary:\"\"\"\n",
"prompt = PromptTemplate(input_variables=[\"output_text\"], template=template)\n",
"llm_chain = LLMChain(llm=OpenAI(), prompt=prompt)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "06f51f17",
"metadata": {},
"outputs": [],
"source": [
"sequential_chain = SimpleSequentialChain(chains=[transform_chain, llm_chain])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f7caa1ee",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' The speaker addresses the nation, noting that while last year they were kept apart due to COVID-19, this year they are together again. They are reminded that regardless of their political affiliations, they are all Americans.'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sequential_chain.run(state_of_the_union)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3ca6409",
"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"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|