diff --git a/docs/examples/chains/llm_bash.ipynb b/docs/examples/chains/llm_bash.ipynb index b9d27fc9744..6e9a44f8dfb 100644 --- a/docs/examples/chains/llm_bash.ipynb +++ b/docs/examples/chains/llm_bash.ipynb @@ -24,16 +24,17 @@ "\n", "```bash\n", "echo \"Hello World\"\n", - "```\u001b[0m\n", - "Answer: \u001b[33;1m\u001b[1;3m{'success': True, 'outputs': ['Hello World\\n']}\u001b[0m\n", + "```\u001b[0m['```bash', 'echo \"Hello World\"', '```']\n", + "\n", + "Answer: \u001b[33;1m\u001b[1;3mHello World\n", + "\u001b[0m\n", "\u001b[1m> Finished LLMBashChain chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ - "{'commands': ['echo \"Hello World\"'],\n", - " 'output': {'success': True, 'outputs': ['Hello World\\n']}}" + "'Hello World\\n'" ] }, "execution_count": 2, @@ -42,7 +43,7 @@ } ], "source": [ - "from langchain.chains.llm_bash.base import LLMBashChain\n", + "from langchain.chains import LLMBashChain\n", "from langchain.llms import OpenAI\n", "\n", "llm = OpenAI(temperature=0)\n", @@ -78,9 +79,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.6" + "version": "3.10.8" } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} diff --git a/docs/examples/chains/llm_requests.ipynb b/docs/examples/chains/llm_requests.ipynb index 4d2ece402e4..e4efdcbebce 100644 --- a/docs/examples/chains/llm_requests.ipynb +++ b/docs/examples/chains/llm_requests.ipynb @@ -115,7 +115,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.10.8" } }, "nbformat": 4, diff --git a/docs/examples/chains/map_reduce.ipynb b/docs/examples/chains/map_reduce.ipynb index 212bb5eb85d..78cd31bd150 100644 --- a/docs/examples/chains/map_reduce.ipynb +++ b/docs/examples/chains/map_reduce.ipynb @@ -85,7 +85,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.7" + "version": "3.10.8" } }, "nbformat": 4, diff --git a/docs/examples/chains/transformation.ipynb b/docs/examples/chains/transformation.ipynb new file mode 100644 index 00000000000..5358e187f7f --- /dev/null +++ b/docs/examples/chains/transformation.ipynb @@ -0,0 +1,130 @@ +{ + "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": 5, + "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": 7, + "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": 2, + "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": 4, + "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": 8, + "id": "f7caa1ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "' This speech addresses the American people and acknowledges the difficulties of last year due to COVID-19. It emphasizes the importance of coming together regardless of political affiliation and encourages a sense of unity as Americans.'" + ] + }, + "execution_count": 8, + "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.10.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/langchain/chains/__init__.py b/langchain/chains/__init__.py index 9d1ccdea0b4..97dd72f430c 100644 --- a/langchain/chains/__init__.py +++ b/langchain/chains/__init__.py @@ -10,6 +10,7 @@ from langchain.chains.qa_with_sources.base import QAWithSourcesChain from langchain.chains.qa_with_sources.vector_db import VectorDBQAWithSourcesChain from langchain.chains.sequential import SequentialChain, SimpleSequentialChain from langchain.chains.sql_database.base import SQLDatabaseChain +from langchain.chains.transform import TransformChain from langchain.chains.vector_db_qa.base import VectorDBQA __all__ = [ @@ -27,4 +28,5 @@ __all__ = [ "VectorDBQAWithSourcesChain", "APIChain", "LLMRequestsChain", + "TransformChain", ]