{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "49ec4bd8", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://pypi.org/simple, https://pip.repos.neuron.amazonaws.com\n", "Collecting huggingface_hub\n", " Downloading huggingface_hub-0.13.4-py3-none-any.whl (200 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m200.1/200.1 kB\u001b[0m \u001b[31m11.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: requests in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from huggingface_hub) (2.28.1)\n", "Requirement already satisfied: filelock in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from huggingface_hub) (3.9.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from huggingface_hub) (4.64.1)\n", "Requirement already satisfied: packaging>=20.9 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from huggingface_hub) (21.3)\n", "Requirement already satisfied: pyyaml>=5.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from huggingface_hub) (5.4.1)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from huggingface_hub) (4.4.0)\n", "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from packaging>=20.9->huggingface_hub) (3.0.9)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests->huggingface_hub) (1.26.8)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests->huggingface_hub) (2022.12.7)\n", "Requirement already satisfied: idna<4,>=2.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests->huggingface_hub) (3.4)\n", "Requirement already satisfied: charset-normalizer<3,>=2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests->huggingface_hub) (2.1.1)\n", "Installing collected packages: huggingface_hub\n", "Successfully installed huggingface_hub-0.13.4\n" ] } ], "source": [ "!pip install langchain --quiet\n", "!pip install huggingface_hub" ] }, { "cell_type": "code", "execution_count": 8, "id": "d32f24b8", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Looking in indexes: https://pypi.org/simple, https://pip.repos.neuron.amazonaws.com\n", "Requirement already satisfied: openai in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (0.27.4)\n", "Requirement already satisfied: requests>=2.20 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai) (2.28.1)\n", "Requirement already satisfied: aiohttp in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai) (3.8.3)\n", "Requirement already satisfied: tqdm in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from openai) (4.64.1)\n", "Requirement already satisfied: idna<4,>=2.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests>=2.20->openai) (3.4)\n", "Requirement already satisfied: charset-normalizer<3,>=2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests>=2.20->openai) (2.1.1)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests>=2.20->openai) (2022.12.7)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from requests>=2.20->openai) (1.26.8)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aiohttp->openai) (6.0.4)\n", "Requirement already satisfied: attrs>=17.3.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aiohttp->openai) (22.2.0)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aiohttp->openai) (1.8.2)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aiohttp->openai) (1.3.3)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aiohttp->openai) (4.0.2)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from aiohttp->openai) (1.3.1)\n" ] } ], "source": [ "!pip install openai" ] }, { "cell_type": "markdown", "id": "08560f8b", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "bc4c618e", "metadata": {}, "outputs": [], "source": [ "import os\n", "#os.environ[\"OPENAI_API_KEY\"]= \"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\"\n", "#import os\n", "os.environ[\"HUGGINGFACEHUB_API_TOKEN\"]= \"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\"" ] }, { "cell_type": "markdown", "id": "733e776b", "metadata": {}, "source": [ "# 1 LLM说明" ] }, { "cell_type": "code", "execution_count": 10, "id": "aca39ebd", "metadata": {}, "outputs": [], "source": [ "#from langchain.llms import OpenAI\n", "from langchain import PromptTemplate, HuggingFaceHub, LLMChain\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "4d2de07b", "metadata": {}, "outputs": [], "source": [ "#llm = OpenAI(temperature = 0.9)\n", "llm=HuggingFaceHub(repo_id=\"bigscience/bloomz\")" ] }, { "cell_type": "code", "execution_count": 21, "id": "52c90bdc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ",然后 click on the name of the service you want to use.\n" ] } ], "source": [ "text = \"列举AWS的服务名称\"\n", "print(llm(text))" ] }, { "cell_type": "markdown", "id": "6e7b89ac", "metadata": {}, "source": [ "chatGPT输出\n", "\n", "1、Amazon EC2(Elastic Compute Cloud)\n", "2、Amazon S3(Simple Storage Service)\n", "3、Amazon RDS(Relational Database Service)\n", "4、Amazon VPC(Virtual Private Cloud)\n", "5、Amazon CloudFront(Content Delivery Network)\n" ] }, { "cell_type": "markdown", "id": "efe14a57", "metadata": {}, "source": [ "# 2 Prompt说明" ] }, { "cell_type": "code", "execution_count": 22, "id": "a8312234", "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import PromptTemplate\n", "prompt = PromptTemplate(\n", " input_variables=[\"food\"],\n", " template=\"推荐一个以{food}闻名的旅游城市?\",\n", ")" ] }, { "cell_type": "code", "execution_count": 25, "id": "4038e113", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "推荐一个以意大利面闻名的旅游城市?\n" ] } ], "source": [ "print(prompt.format(food=\"意大利面\"))" ] }, { "cell_type": "code", "execution_count": 26, "id": "219e2da4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 巴黎\n" ] } ], "source": [ "print(llm(prompt.format(food=\"dessert\")))" ] }, { "cell_type": "markdown", "id": "13f38472", "metadata": {}, "source": [ " # 3 Chain说明" ] }, { "cell_type": "code", "execution_count": 27, "id": "9b43c123", "metadata": {}, "outputs": [], "source": [ "chain = LLMChain(llm=llm, prompt=prompt)" ] }, { "cell_type": "code", "execution_count": 28, "id": "1a9fd565", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 泰国\n" ] } ], "source": [ "print(chain.run(\"榴莲\"))" ] }, { "cell_type": "markdown", "id": "a84fe9aa", "metadata": {}, "source": [ "# 4 agent" ] }, { "cell_type": "code", "execution_count": null, "id": "a457d6a6", "metadata": {}, "outputs": [], "source": [ "pip install google-search-results\n" ] }, { "cell_type": "code", "execution_count": null, "id": "33cfcb45", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import load_tools\n", "from langchain.agents import initialize_agent\n", "\n", "# Load the model\n", "#llm=HuggingFaceHub(repo_id=\"bigscience/bloomz\")\n", "llm = OpenAI(temperature=0)\n", "\n", "# Load in some tools to use\n", "\n", "os.environ[\"SERPAPI_API_KEY\"] = \"e919f6e0e7667fce51d670860d33089dae3393d9090c660c933b5dca9b0200e9\"\n", "\n", "tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n", "\n", "# Finally, let's initialize an agent with:\n", "# 1. The tools\n", "# 2. The language model\n", "# 3. The type of agent we want to use.\n", "\n", "agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "498fa3e5", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n", "from langchain import OpenAI, SerpAPIWrapper, LLMChain\n", "from langchain import PromptTemplate, HuggingFaceHub, LLMChain" ] }, { "cell_type": "code", "execution_count": null, "id": "97989bb3", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n", "from langchain import OpenAI, SerpAPIWrapper, LLMChain\n", "\n", "search = SerpAPIWrapper()\n", "tools = [\n", " Tool(\n", " name = \"Search\",\n", " func=search.run,\n", " description=\"useful for when you need to answer questions about current events\"\n", " )\n", "]\n", "\n", "prefix = \"\"\"Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:\"\"\"\n", "suffix = \"\"\"Begin! Remember to speak as a pirate when giving your final answer. Use lots of \"Args\"\n", "\n", "Question: {input}\n", "{agent_scratchpad}\"\"\"\n", "\n", "prompt = ZeroShotAgent.create_prompt(\n", " tools, \n", " prefix=prefix, \n", " suffix=suffix, \n", " input_variables=[\"input\", \"agent_scratchpad\"]\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e06c53ca", "metadata": {}, "outputs": [], "source": [ "#llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n", "\n", "llm_chain = LLMChain(llm=HuggingFaceHub(repo_id=\"bigscience/bloomz\"), prompt=prompt)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "77363f05", "metadata": {}, "outputs": [], "source": [ "tool_names = [tool.name for tool in tools]\n", "agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8ba9972e", "metadata": {}, "outputs": [], "source": [ "agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e6e50071", "metadata": {}, "outputs": [], "source": [ "agent_executor.run(\"How many people live in canada as of 2023?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "edeec7fa", "metadata": {}, "outputs": [], "source": [ "#agent.run(\"谁是中国的国家主席\")\n", "agent.run(\"Who is the current leader of Japan? What is the largest prime number that is smaller than their age?\")" ] }, { "cell_type": "markdown", "id": "2aff6b23", "metadata": {}, "source": [ "# 5 Memory" ] }, { "cell_type": "code", "execution_count": null, "id": "1aa6575a", "metadata": {}, "outputs": [], "source": [ "from langchain import OpenAI, ConversationChain\n", "\n", "#llm = OpenAI(temperature=0)\n", "conversation = ConversationChain(llm=llm, verbose=True)\n", "\n", "conversation.predict(input=\"你好!\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "529b3dbf", "metadata": {}, "outputs": [], "source": [ "conversation.predict(input=\"很高兴认识你\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4116d7db", "metadata": {}, "outputs": [], "source": [ "conversation.predict(input=\"你给我说的第一句话是什么?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "094eac07", "metadata": {}, "outputs": [], "source": [ "conversation.predict(input=\"我对你说的第一句话的另一个说法是什么?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2f7c824f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_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 }