{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "52824b89-532a-4e54-87e9-1410813cd39e", "metadata": {}, "source": [ "# Chains in LangChain(LangChain中的链)\n", "\n", "## Outline\n", "\n", "* LLMChain(大语言模型链)\n", "* Sequential Chains(顺序链)\n", " * SimpleSequentialChain\n", " * SequentialChain\n", "* Router Chain(路由链)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "54810ef7", "metadata": {}, "source": [ "### 为什么我们需要Chains ?\n", "链允许我们将多个组件组合在一起,以创建一个单一的、连贯的应用程序。链(Chains)通常将一个LLM(大语言模型)与提示结合在一起,使用这个构建块,您还可以将一堆这些构建块组合在一起,对您的文本或其他数据进行一系列操作。例如,我们可以创建一个链,该链接受用户输入,使用提示模板对其进行格式化,然后将格式化的响应传递给LLM。我们可以通过将多个链组合在一起,或者通过将链与其他组件组合在一起来构建更复杂的链。" ] }, { "cell_type": "code", "execution_count": 44, "id": "541eb2f1", "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 45, "id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef", "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "import openai\n", "from dotenv import load_dotenv, find_dotenv\n", "\n", "_ = load_dotenv(find_dotenv()) # 读取本地 .env 文件\n", "\n", "# 获取环境变量 OPENAI_API_KEY\n", "openai.api_key = os.environ['OPENAI_API_KEY'] #\"填入你的专属的API key\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "b84e441b", "metadata": {}, "outputs": [], "source": [ "#!pip install pandas" ] }, { "attachments": {}, "cell_type": "markdown", "id": "663fc885", "metadata": {}, "source": [ "这些链的一部分的强大之处在于你可以一次运行它们在许多输入上,因此,我们将加载一个pandas数据框架" ] }, { "cell_type": "code", "execution_count": 46, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.read_csv('Data.csv')" ] }, { "cell_type": "code", "execution_count": 47, "id": "b7a09c35", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Product | \n", "Review | \n", "
|---|---|---|
| 0 | \n", "Queen Size Sheet Set | \n", "I ordered a king size set. My only criticism w... | \n", "
| 1 | \n", "Waterproof Phone Pouch | \n", "I loved the waterproof sac, although the openi... | \n", "
| 2 | \n", "Luxury Air Mattress | \n", "This mattress had a small hole in the top of i... | \n", "
| 3 | \n", "Pillows Insert | \n", "This is the best throw pillow fillers on Amazo... | \n", "
| 4 | \n", "Milk Frother Handheld\\n | \n", "I loved this product. But they only seem to l... | \n", "