{ "cells": [ { "cell_type": "markdown", "id": "c730c5b4", "metadata": {}, "source": [ "# SimpleSequentialChain\n", "In this series of chains, each individual chain has a single input and a single output, and the output of one step is used as input to the next." ] }, { "cell_type": "code", "execution_count": null, "id": "f65a100b", "metadata": {}, "outputs": [], "source": [ "!pip install openai\n", "!pip install langchain" ] }, { "cell_type": "code", "execution_count": null, "id": "454013f3", "metadata": {}, "outputs": [], "source": [ "%env OPENAI_API_TYPE=azure\n", "%env OPENAI_API_VERSION=2022-12-01\n", "%env OPENAI_API_BASE=\n", "%env OPENAI_API_KEY=" ] }, { "cell_type": "code", "execution_count": null, "id": "c8c0d462", "metadata": {}, "outputs": [], "source": [ "from langchain import PromptTemplate, LLMChain\n", "from langchain.llms import AzureOpenAI\n", "azllm=AzureOpenAI(deployment_name=\"test-text-davinci\", model_name=\"text-davinci-003\", temperature=0)" ] }, { "cell_type": "code", "execution_count": null, "id": "af343423", "metadata": {}, "outputs": [], "source": [ "# This is an LLMChain to write a synopsis given a title of a play.\n", "template = \"\"\"You are a playwright. Given the title of play, it is your job to write a synopsis for that title.\n", "\n", "Title: {title}\n", "Playwright: This is a synopsis for the above play:\"\"\"\n", "prompt_template = PromptTemplate(input_variables=[\"title\"], template=template)\n", "synopsis_chain = LLMChain(llm=azllm, prompt=prompt_template)" ] }, { "cell_type": "code", "execution_count": null, "id": "8dddc018", "metadata": {}, "outputs": [], "source": [ "# This is an LLMChain to write a review of a play given a synopsis.\n", "template = \"\"\"You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.\n", "\n", "Play Synopsis:\n", "{synopsis}\n", "Review from a New York Times play critic of the above play:\"\"\"\n", "prompt_template = PromptTemplate(input_variables=[\"synopsis\"], template=template)\n", "review_chain = LLMChain(llm=azllm, prompt=prompt_template)" ] }, { "cell_type": "code", "execution_count": null, "id": "06767f4a", "metadata": {}, "outputs": [], "source": [ "# This is the overall chain where we run these two chains in sequence.\n", "from langchain.chains import SimpleSequentialChain\n", "overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "c4e4a202", "metadata": {}, "outputs": [], "source": [ "review = overall_chain.run(\"Tragedy at sunset on the beach\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f1327102", "metadata": {}, "outputs": [], "source": [ "print(review)" ] }, { "cell_type": "code", "execution_count": null, "id": "b2119bbf", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }