From ad139827878c3db110ef32bca0646eaa1881a537 Mon Sep 17 00:00:00 2001 From: joyenjoye
\n"," OPENAI_API_KEY=\"your_api_key\" \n","
\n"," \n"," 替换\"your_api_key\"为你自己的 API Key"]},{"cell_type":"code","execution_count":1,"id":"cc33ceb1-535f-454d-988c-347a8b14fd72","metadata":{},"outputs":[],"source":["# 下载需要的包python-dotenv和openai\n","# 如果你需要查看安装过程日志,可删除 -q \n","!pip install -q python-dotenv\n","!pip install -q openai"]},{"cell_type":"code","execution_count":2,"id":"e3c97235-f101-47f2-92db-1c37f4bf9845","metadata":{"tags":[]},"outputs":[],"source":["import os\n","import openai\n","from dotenv import load_dotenv, find_dotenv\n","\n","# 读取本地/项目的环境变量。\n","\n","# find_dotenv()寻找并定位.env文件的路径\n","# load_dotenv()读取该.env文件,并将其中的环境变量加载到当前的运行环境中 \n","# 如果你设置的是全局的环境变量,这行代码则没有任何作用。\n","_ = load_dotenv(find_dotenv())\n","\n","# 获取环境变量 OPENAI_API_KEY\n","openai.api_key = os.environ['OPENAI_API_KEY'] "]},{"cell_type":"code","execution_count":52,"id":"af8c3c96","metadata":{},"outputs":[{"data":{"text/plain":["'\\n\\n人工智能是一项极具前景的技术,它的发展正在改变人类的生活方式,带来了无数的便利,也被认为是未来发展的重要标志。人工智能的发展让许多复杂的任务变得更加容易,更高效的完成,节省了大量的时间和精力,为人类发展带来了极大的帮助。'"]},"execution_count":52,"metadata":{},"output_type":"execute_result"}],"source":["from langchain.llms import OpenAI\n","\n","llm = OpenAI(model_name=\"text-davinci-003\",max_tokens=1024)\n","llm(\"怎么评价人工智能\")"]},{"cell_type":"markdown","id":"8cb7a7ec","metadata":{"height":30},"source":["## 一、导入embedding模型和向量存储组件\n","使用Dock Array内存搜索向量存储,作为一个内存向量存储,不需要连接外部数据库"]},{"cell_type":"code","execution_count":3,"id":"974acf8e-8f88-42de-88f8-40a82cb58e8b","metadata":{"height":98},"outputs":[],"source":["from langchain.chains import RetrievalQA #检索QA链,在文档上进行检索\n","from langchain.chat_models import ChatOpenAI #openai模型\n","from langchain.document_loaders import CSVLoader #文档加载器,采用csv格式存储\n","from langchain.vectorstores import DocArrayInMemorySearch #向量存储\n","from IPython.display import display, Markdown #在jupyter显示信息的工具"]},{"cell_type":"code","execution_count":4,"id":"7249846e","metadata":{"height":75},"outputs":[],"source":["#读取文件\n","file = 'OutdoorClothingCatalog_1000.csv'\n","loader = CSVLoader(file_path=file)"]},{"cell_type":"code","execution_count":24,"id":"7724f00e","metadata":{"height":30},"outputs":[{"data":{"text/html":["| \n"," | 0 | \n","1 | \n","2 | \n","
|---|---|---|---|
| 0 | \n","NaN | \n","name | \n","description | \n","
| 1 | \n","0.0 | \n","Women's Campside Oxfords | \n","This ultracomfortable lace-to-toe Oxford boast... | \n","
| 2 | \n","1.0 | \n","Recycled Waterhog Dog Mat, Chevron Weave | \n","Protect your floors from spills and splashing ... | \n","
| 3 | \n","2.0 | \n","Infant and Toddler Girls' Coastal Chill Swimsu... | \n","She'll love the bright colors, ruffles and exc... | \n","
| 4 | \n","3.0 | \n","Refresh Swimwear, V-Neck Tankini Contrasts | \n","Whether you're going for a swim or heading out... | \n","
| ... | \n","... | \n","... | \n","... | \n","
| 996 | \n","995.0 | \n","Men's Classic Denim, Standard Fit | \n","Crafted from premium denim that will last wash... | \n","
| 997 | \n","996.0 | \n","CozyPrint Sweater Fleece Pullover | \n","The ultimate sweater fleece - made from superi... | \n","
| 998 | \n","997.0 | \n","Women's NRS Endurance Spray Paddling Pants | \n","These comfortable and affordable splash paddli... | \n","
| 999 | \n","998.0 | \n","Women's Stop Flies Hoodie | \n","This great-looking hoodie uses No Fly Zone Tec... | \n","
| 1000 | \n","999.0 | \n","Modern Utility Bag | \n","This US-made crossbody bag is built with the s... | \n","
1001 rows × 3 columns
\n","\n", + " OPENAI_API_KEY=\"your_api_key\" \n", + "
\n", + " \n", + " 替换\"your_api_key\"为你自己的 API Key" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cc33ceb1-535f-454d-988c-347a8b14fd72", + "metadata": {}, + "outputs": [], + "source": [ + "# 下载需要的包python-dotenv和openai\n", + "# 如果你需要查看安装过程日志,可删除 -q \n", + "!pip install -q python-dotenv\n", + "!pip install -q openai" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e3c97235-f101-47f2-92db-1c37f4bf9845", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import os\n", + "import openai\n", + "from dotenv import load_dotenv, find_dotenv\n", + "\n", + "# 读取本地/项目的环境变量。\n", + "\n", + "# find_dotenv()寻找并定位.env文件的路径\n", + "# load_dotenv()读取该.env文件,并将其中的环境变量加载到当前的运行环境中 \n", + "# 如果你设置的是全局的环境变量,这行代码则没有任何作用。\n", + "_ = load_dotenv(find_dotenv())\n", + "\n", + "# 获取环境变量 OPENAI_API_KEY\n", + "openai.api_key = os.environ['OPENAI_API_KEY'] " + ] + }, + { + "cell_type": "markdown", + "id": "8cb7a7ec", + "metadata": { + "height": 30 + }, + "source": [ + "## 一、直接" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "6fbf1fe4-411a-4d22-b362-ac8400fa31b9", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --upgrade -q langchain\n", + "!pip install -q docarray" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", + "metadata": { + "height": 98 + }, + "outputs": [], + "source": [ + "from langchain.chains import RetrievalQA #检索QA链,在文档上进行检索\n", + "from langchain.chat_models import ChatOpenAI #openai模型\n", + "from langchain.document_loaders import CSVLoader #文档加载器,采用csv格式存储\n", + "from langchain.vectorstores import DocArrayInMemorySearch #向量存储\n", + "from IPython.display import display, Markdown #在jupyter显示信息的工具" + ] + }, + { + "cell_type": "markdown", + "id": "159206b6-46a8-47ee-99a8-13bf7d554da4", + "metadata": {}, + "source": [ + "### 1.1 导入数据" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "7249846e", + "metadata": { + "height": 75 + }, + "outputs": [], + "source": [ + "file = 'data/OutdoorClothingCatalog_1000.csv'\n", + "loader = CSVLoader(file_path=file)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "7724f00e", + "metadata": { + "height": 30 + }, + "outputs": [ + { + "data": { + "text/html": [ + "| \n", + " | name | \n", + "description | \n", + "
|---|---|---|
| 0 | \n", + "Women's Campside Oxfords | \n", + "This ultracomfortable lace-to-toe Oxford boast... | \n", + "
| 1 | \n", + "Recycled Waterhog Dog Mat, Chevron Weave | \n", + "Protect your floors from spills and splashing ... | \n", + "
| 2 | \n", + "Infant and Toddler Girls' Coastal Chill Swimsu... | \n", + "She'll love the bright colors, ruffles and exc... | \n", + "
| 3 | \n", + "Refresh Swimwear, V-Neck Tankini Contrasts | \n", + "Whether you're going for a swim or heading out... | \n", + "
| 4 | \n", + "EcoFlex 3L Storm Pants | \n", + "Our new TEK O2 technology makes our four-seaso... | \n", + "