{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b46a7a6a-3182-421d-a208-f45eafd1c4ad",
   "metadata": {
    "tags": []
   },
   "source": [
    "# ChatGPT与ChatGLM对比\n",
    "\n",
    "&emsp;&emsp;国产大模型有很多，比如文心一言、通义千问、星火、MOSS和ChatGLM等等，但现在明确可以部署在本地并且开放api的只有MOOS和ChatGLM。MOOS由于需要的GPU显存过大（不量化的情况下需要80GB，多轮对话还是会爆显存），但ChatGLM可以在笔记本电脑部署（int4版本只需要6GB显存即可）。所以本文采用ChatGLM与ChatGPT做对比，看看国产模型的优点和缺点。\n",
    "\n",
    "&emsp;&emsp;会在选取本教程的各个方面进行对比，最后会总结ChatGPT与ChatGLM各自的优缺点。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9be808ea-5284-4399-b832-5205c2745d13",
   "metadata": {},
   "source": [
    "## ChatGLM环境配置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fbd3834-1cf9-47d5-a94f-351d3645e0f6",
   "metadata": {},
   "source": [
    "&emsp;&emsp;ChatGLM环境配置可以参考DataWhale的这篇文章：[ChatGLM-6B 本地部署指南！](https://mp.weixin.qq.com/s/545Z4DTB78q_sLqBq6dC1A)\n",
    "\n",
    "&emsp;&emsp;部署好了之后，运行项目中的`api.py`文件即可。下面是使用ChatGLM的api封装的类似chatgpt一样的`get_completion`函数，只需要传进去prompt参数即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "84a6ab28-e60a-4a67-bbde-4062cc844894",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fe69c47-ccc4-47db-a0f5-21e273b35fcb",
   "metadata": {},
   "source": [
    "如果你没有openai的key的话，部署好chatglm-6B之后，使用此函数也可以学完整个课程，加油~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "37dfda9c-768c-4c1b-921b-d5e2e03f8663",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_completion(prompt):\n",
    "    headers = {'Content-Type': 'application/json'}\n",
    "    data = {\"prompt\": prompt, \"history\": []}\n",
    "    response = requests.post(url='http://127.0.0.1:8000', headers=headers, data=json.dumps(data))\n",
    "    return response.json()['response']\n",
    "\n",
    "get_completion(\"你好\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "597ca502-d339-4667-9f7c-4da30ac1f410",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_completion_glm(prompt):\n",
    "    headers = {'Content-Type': 'application/json'}\n",
    "    data = {\"prompt\": prompt, \"history\": []}\n",
    "    response = requests.post(url='http://127.0.0.1:8000', headers=headers, data=json.dumps(data))\n",
    "    return response.json()['response']\n",
    "\n",
    "get_completion_glm(\"你好\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4c608de-2293-48df-bb0e-491686e427af",
   "metadata": {},
   "source": [
    "## ChatGPT环境配置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b262731f-870d-4810-8b63-67da58a6a7b8",
   "metadata": {},
   "source": [
    "&emsp;&emsp;本教程使用 OpenAI 所开放的 ChatGPT API，因此你需要首先拥有一个 ChatGPT 的 API_KEY（也可以直接访问官方网址在线测试），然后需要安装 openai 的第三方库\n",
    "\n",
    "&emsp;&emsp;首先需要安装所需第三方库：\n",
    "\n",
    "```\n",
    "    openai：\n",
    "\n",
    "    pip install openai\n",
    "    dotenv:\n",
    "\n",
    "    pip install -U python-dotenv\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6dbf0cd-8826-4e36-a8fc-357203cd0a34",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将自己的 API-KEY 导入系统环境变量\n",
    "!export OPENAI_API_KEY='api-key'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcff373b-c85d-4f64-b8eb-0042d646de96",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "# 导入第三方库\n",
    "\n",
    "_ = load_dotenv(find_dotenv())\n",
    "# 读取系统中的环境变量\n",
    "\n",
    "openai.api_key  = os.getenv('OPENAI_API_KEY')\n",
    "# 设置 API_KEY"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75927919-bdcd-4534-8b4c-e77acca05eae",
   "metadata": {},
   "source": [
    "封装一个函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2fc891b0-cebd-4235-81d6-6b1fb4cc65b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我可以帮助你的吗？'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_completion_gpt(prompt, model=\"gpt-3.5-turbo\", temperature=0): \n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        temperature=temperature, # 值越低则输出文本随机性越低\n",
    "    )\n",
    "    return response.choices[0].message[\"content\"]\n",
    "\n",
    "get_completion_gpt('你好')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c807a1f5-bdf2-46ab-a77f-59985374e647",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 文本理解"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1925a9e-54d9-4f75-a625-a1698b95e268",
   "metadata": {},
   "source": [
    "有步骤的文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d6b1bc6e-cd39-44dd-b2a5-0f20ce53fbc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有步骤的文本\n",
    "text_1 = f\"\"\"\n",
    "泡一杯茶很容易。首先，需要把水烧开。\\\n",
    "在等待期间，拿一个杯子并把茶包放进去。\\\n",
    "一旦水足够热，就把它倒在茶包上。\\\n",
    "等待一会儿，让茶叶浸泡。几分钟后，取出茶包。\\\n",
    "如果你愿意，可以加一些糖或牛奶调味。\\\n",
    "就这样，你可以享受一杯美味的茶了。\n",
    "\"\"\"\n",
    "prompt = f\"\"\"\n",
    "您将获得由三个引号括起来的文本。\\\n",
    "如果它包含一系列的指令，则需要按照以下格式重新编写这些指令：\n",
    "\n",
    "第一步 - ...\n",
    "第二步 - …\n",
    "…\n",
    "第N步 - …\n",
    "\n",
    "如果文本中不包含一系列的指令，则直接写“未提供步骤”。\"\n",
    "\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad49ba0c-38ae-4137-bfdc-207037ea7d9c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一步: 把水烧开\n",
      "第二步: 把茶包放入杯子中\n",
      "第三步: 等待水足够热\n",
      "第四步: 倒在茶包上\n",
      "第五步: 等待茶叶浸泡\n",
      "第六步: 取出茶包\n",
      "第七步: 如果喜欢,可以加一些糖或牛奶\n",
      "第八步: 搅拌均匀\n",
      "第九步: 享用美味的茶\n",
      "\n",
      "未提供步骤\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7fd825a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一步 - 把水烧开。\n",
      "第二步 - 拿一个杯子并把茶包放进去。\n",
      "第三步 - 把热水倒在茶包上。\n",
      "第四步 - 等待几分钟，让茶叶浸泡。\n",
      "第五步 - 取出茶包。\n",
      "第六步 - 如果你愿意，可以加一些糖或牛奶调味。\n",
      "第七步 - 就这样，你可以享受一杯美味的茶了。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b43edb5-37b5-4d43-9e16-d2a9b558ef73",
   "metadata": {},
   "source": [
    "**注**：这里可以看出，提供的文本是有步骤的文本。chatglm给出了步骤，但在最后说了`未提供步骤`。但chatgpt给出步骤，而且步骤要比chatglm的完整，而且回答正确。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37727f9a",
   "metadata": {},
   "source": [
    "无步骤文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "75a4b5ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 无步骤的文本\n",
    "text_2 = f\"\"\"\n",
    "今天阳光明媚，鸟儿在歌唱。\\\n",
    "这是一个去公园散步的美好日子。\\\n",
    "鲜花盛开，树枝在微风中轻轻摇曳。\\\n",
    "人们外出享受着这美好的天气，有些人在野餐，有些人在玩游戏或者在草地上放松。\\\n",
    "这是一个完美的日子，可以在户外度过并欣赏大自然的美景。\n",
    "\"\"\"\n",
    "prompt = f\"\"\"\n",
    "您将获得由三个引号括起来的文本。\\\n",
    "如果它包含一系列的指令，则需要按照以下格式重新编写这些指令：\n",
    "\n",
    "第一步 - ...\n",
    "第二步 - …\n",
    "…\n",
    "第N步 - …\n",
    "\n",
    "如果文本中不包含一系列的指令，则直接写“未提供步骤”。\"\n",
    "\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3f0d5ef3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一步: 去公园散步\n",
      "第二步: 欣赏鲜花和树枝\n",
      "第三步: 感受微风\n",
      "第四步: 放松身心\n",
      "第五步: 野餐或玩游戏\n",
      "第六步: 感受大自然的美丽\n",
      "\n",
      "未提供步骤。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7a58b936",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未提供步骤。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca4a5d02-0284-48fb-a22e-19b9d343ef65",
   "metadata": {},
   "source": [
    "**注：** 提供的是一个无步骤文本，但chatglm回答了一个步骤，在最后说了无步骤，这跟上面的有步骤文本回答几乎一样。chatgpt则是直接给出`未提供步骤`的回答。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "198f0fb0",
   "metadata": {},
   "source": [
    "提供少量示例的文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7371e549",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = f\"\"\"\n",
    "你的任务是以一致的风格回答问题。\n",
    "\n",
    "<孩子>: 教我耐心。\n",
    "\n",
    "<祖父母>: 挖出最深峡谷的河流源于一处不起眼的泉眼；最宏伟的交响乐从单一的音符开始；最复杂的挂毯以一根孤独的线开始编织。\n",
    "\n",
    "<孩子>: 教我韧性。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7b02cefd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韧性是一种强大的品质,可以帮助人们在挑战中保持冷静和坚韧。以下是一些培养韧性的方法:\n",
      "\n",
      "1. 接受挑战并做好准备:韧性需要面对挑战并克服困难。要培养韧性,需要先准备好接受挑战,并采取必要的措施来解决问题。\n",
      "\n",
      "2. 学会放松:当我们感到紧张和焦虑时,可能会失去韧性。学会放松自己,可以帮助缓解压力和焦虑,从而提高韧性。\n",
      "\n",
      "3. 培养自我意识:韧性需要了解自己的弱点和优点,并积极改善自己。学会关注自己的情绪和行为,并努力改善自己的韧性。\n",
      "\n",
      "4. 学习沟通技巧:沟通是影响韧性的重要因素。学习有效的沟通技巧,可以帮助更好地表达自己的意见和想法,同时也可以帮助更好地理解他人的观点。\n",
      "\n",
      "5. 坚持锻炼和健康饮食:锻炼和健康饮食可以帮助保持身体健康和精神健康,从而提高韧性。\n",
      "\n",
      "希望这些方法可以帮助培养韧性。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8fc62b66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<祖父母>: 韧性就像是一棵树，它需要经历风吹雨打、日晒风干，才能成长得更加坚强。所以，当你遇到挫折和困难时，不要轻易放弃，要坚持下去，相信自己的能力和潜力，最终你会成为一棵茁壮成长的大树。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "524d968f-41da-4f68-beef-a50800944254",
   "metadata": {},
   "source": [
    "**注：** 让你模仿，没让你超越啊！可以看出chatglm的回答与提供的少量示例文本几乎毫无关系，而chatgpt则是按照提供的示例模型续写。chatgpt薄纱chatglm。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bdbe63f",
   "metadata": {},
   "source": [
    "关注点侧重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73948cf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "prod_review_zh = \"\"\"\n",
    "这个熊猫公仔是我给女儿的生日礼物，她很喜欢，去哪都带着。\n",
    "公仔很软，超级可爱，面部表情也很和善。但是相比于价钱来说，\n",
    "它有点小，我感觉在别的地方用同样的价钱能买到更大的。\n",
    "快递比预期提前了一天到货，所以在送给女儿之前，我自己玩了会。\n",
    "\"\"\"\n",
    "# 侧重运输\n",
    "prompt = f\"\"\"\n",
    "你的任务是从电子商务网站上生成一个产品评论的简短摘要。\n",
    "\n",
    "请对三个反引号之间的评论文本进行概括，最多30个词汇，并且聚焦在产品运输上。\n",
    "\n",
    "评论: ```{prod_review_zh}```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f7a151b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "评论: 熊猫公仔送给女儿生日礼物,她很喜欢。公仔软、可爱,面部表情和善。但小,感觉在其他地方用同样的价钱能买到更大。快递比预期提前了一天到货,自己玩了会。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7d9715b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "快递提前一天到货，熊猫公仔很软可爱，但有点小，价钱不太划算。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abe00c2a-f8e6-4531-8077-33b50de7dba7",
   "metadata": {},
   "source": [
    "**注：**  让它侧重运输，chatglm甚至把运输的内容放在了回答的最后，chatgpt倒是把运输的部分放到了最前，表示侧重。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b64ec6e",
   "metadata": {},
   "source": [
    "关键信息提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8244e043",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = f\"\"\"\n",
    "你的任务是从电子商务网站上的产品评论中提取相关信息。\n",
    "\n",
    "请从以下三个反引号之间的评论文本中提取产品运输相关的信息，最多30个词汇。\n",
    "\n",
    "评论: ```{prod_review_zh}```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2ee29821",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "运输时间:快递比预期提前了一天到货,熊猫公仔小巧,快递过程安全顺利。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "17fa8390",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "快递比预期提前了一天到货。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cc52af4-bf0e-4592-9292-ed238233a195",
   "metadata": {},
   "source": [
    "**注：** 不错，不错，chatglm和chatgpt都把运输信息提取出来了，chatglm甚至还多说了点。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07bb807-0a6e-43e6-b8a8-a597c42a6753",
   "metadata": {},
   "source": [
    "### 总结\n",
    "\n",
    "- 文本理解方面，chatglm与chatgpt的差距有点大。首先是步骤文本，无论提供的文本是否有步骤，chatglm都给出了步骤。而chatgpt则是有步骤给步骤，没步骤就不给。\n",
    "\n",
    "- 示例文本续写方面，chatglm本着模仿就要超越的原则，直接舍弃提供的少量示例，放飞自我。chatgpt则是按照提供的少量示例给出了答案。\n",
    "\n",
    "- 关键信息提取，chatglm表现差强人意，不太行。chatgpt倒是符合我的要求。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8852532a-d1fb-44eb-87d5-8f95aa3e1606",
   "metadata": {},
   "source": [
    "## 结构化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0106c8e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = f\"\"\"\n",
    "在一个迷人的村庄里，兄妹杰克和吉尔出发去一个山顶井里打水。\\\n",
    "他们一边唱着欢乐的歌，一边往上爬，\\\n",
    "然而不幸降临——杰克绊了一块石头，从山上滚了下来，吉尔紧随其后。\\\n",
    "虽然略有些摔伤，但他们还是回到了温馨的家中。\\\n",
    "尽管出了这样的意外，他们的冒险精神依然没有减弱，继续充满愉悦地探索。\n",
    "\"\"\"\n",
    "# example \n",
    "prompt = f\"\"\"\n",
    "1-用一句话概括下面用<>括起来的文本。\n",
    "2-将摘要翻译成英语。\n",
    "3-在英语摘要中列出每个名称。\n",
    "4-输出一个 JSON 对象，其中包含以下键：English_summary，num_names。\n",
    "\n",
    "请使用以下格式：\n",
    "文本：<要总结的文本>\n",
    "摘要：<摘要>\n",
    "翻译：<摘要的翻译>\n",
    "名称：<英语摘要中的名称列表>\n",
    "输出 JSON：<带有 English_summary 和 num_names 的 JSON>\n",
    "\n",
    "Text: <{text}>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3c5c885e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "摘要:\n",
      "\n",
      "在一个迷人的村庄里,兄妹杰克和吉尔出发去一个山顶井里打水。他们一边唱着欢乐的歌,一边往上爬,然而不幸降临——杰克绊了一块石头,从山上滚了下来,吉尔紧随其后。虽然略有些摔伤,但他们还是回到了温馨的家中。尽管出了这样的意外,他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n",
      "\n",
      "翻译:\n",
      "\n",
      "In a charming village,兄妹杰克 and吉尔 went to a山顶 well to fetch water. They were singing joyfully as they reached the top, but something went wrong -杰克 stumbled and fell from the mountain, while吉尔 followed closely. Although they were slightly injured, they returned home feeling brave and愉悦. Although they encountered such an unexpected situation, their spirit of adventure did not dim, and they continued to explore with joy.\n",
      "\n",
      "名称:\n",
      "\n",
      "The names in the English summary are: Jack,吉尔, and their mother.\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0090af5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "摘要：兄妹杰克和吉尔在迷人的村庄里冒险，不幸摔伤后回到家中，但仍然充满冒险精神。\n",
      "翻译：In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well. While climbing and singing, Jack trips on a stone and tumbles down the mountain, with Jill following closely behind. Despite some bruises, they make it back home safely. Their adventurous spirit remains undiminished as they continue to explore with joy.\n",
      "名称：Jack，Jill\n",
      "输出 JSON：{\"English_summary\": \"In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well. While climbing and singing, Jack trips on a stone and tumbles down the mountain, with Jill following closely behind. Despite some bruises, they make it back home safely. Their adventurous spirit remains undiminished as they continue to explore with joy.\", \"num_names\": 2}\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3b6f8c4-e649-4dd5-9b1c-46d724f92f7b",
   "metadata": {},
   "source": [
    "**注：** 可以看出，chatglm完全忽略了prompt中的`输出json对象`， 而且这个输出的摘要像是重新说了一遍，翻译也有些中文没有完全翻译。chatgpt的回答是符合要求的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edd7c59f",
   "metadata": {},
   "source": [
    "从客户的评论中提取结构化信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e6372802",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 中文\n",
    "lamp_review_zh = \"\"\"\n",
    "我需要一盏漂亮的卧室灯，这款灯具有额外的储物功能，价格也不算太高。\\\n",
    "我很快就收到了它。在运输过程中，我们的灯绳断了，但是公司很乐意寄送了一个新的。\\\n",
    "几天后就收到了。这款灯很容易组装。我发现少了一个零件，于是联系了他们的客服，他们很快就给我寄来了缺失的零件！\\\n",
    "在我看来，Lumina 是一家非常关心顾客和产品的优秀公司！\n",
    "\"\"\"\n",
    "# 中文\n",
    "prompt = f\"\"\"\n",
    "从评论文本中识别以下项目：\n",
    "- 评论者购买的物品\n",
    "- 制造该物品的公司\n",
    "\n",
    "评论文本用三个反引号分隔。将你的响应格式化为以 “物品” 和 “品牌” 为键的 JSON 对象。\n",
    "如果信息不存在，请使用 “未知” 作为值。\n",
    "让你的回应尽可能简短。\n",
    "  \n",
    "评论文本: ```{lamp_review_zh}```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "fd30e290",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "物品:卧室灯\n",
      "品牌:Lumina\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ef715b71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"物品\": \"卧室灯\",\n",
      "  \"品牌\": \"Lumina\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9bd113e-2ffb-4828-a03a-a7d1c78b82d8",
   "metadata": {},
   "source": [
    "**注：** chatglm提取信息成功！口头表扬一次，但是并没有按照json对象输出，口头批评一次。chatgpt做的很好，表扬一次。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10edd035",
   "metadata": {},
   "source": [
    "一次提取多条信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ec787579",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 中文\n",
    "prompt = f\"\"\"\n",
    "从评论文本中识别以下项目：\n",
    "- 情绪（正面或负面）\n",
    "- 审稿人是否表达了愤怒？（是或否）\n",
    "- 评论者购买的物品\n",
    "- 制造该物品的公司\n",
    "\n",
    "评论用三个反引号分隔。将您的响应格式化为 JSON 对象，以 “Sentiment”、“Anger”、“Item” 和 “Brand” 作为键。\n",
    "如果信息不存在，请使用 “未知” 作为值。\n",
    "让你的回应尽可能简短。\n",
    "将 Anger 值格式化为布尔值。\n",
    "\n",
    "评论文本: ```{lamp_review_zh}```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c008c483",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sentiment: 正面\n",
      "Anger: 否\n",
      "Item: 卧室灯\n",
      "Brand:mina\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b5f91d12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"Sentiment\": \"正面\",\n",
      "  \"Anger\": false,\n",
      "  \"Item\": \"卧室灯\",\n",
      "  \"Brand\": \"Lumina\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aff470ae-7110-4e97-8e8b-45835af17df4",
   "metadata": {},
   "source": [
    "**注：** chatglm提取信息确实是提取的没问题，但是吧，还是没有转化为json对象输出。并且`Anger`没有给出布尔值，扣分项。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "163f5442-9b64-4e0a-a370-b34f51067c3a",
   "metadata": {},
   "source": [
    "### 总结\n",
    "\n",
    "提取信息+结构化输出，chatglm基本只能做到提取信息，并没有实现输出json对象。能力有待加强，不知道chatglm-130B的版本如何？希望能更好些，加油~"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0085689-c1f1-4cfa-ae1c-714731c02a3a",
   "metadata": {},
   "source": [
    "## 翻译"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff6b817b",
   "metadata": {},
   "source": [
    "多语种翻译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d1db0ebd",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = f\"\"\"\n",
    "请将以下文本分别翻译成中文、英文、法语和西班牙语: \n",
    "```I want to order a basketball.```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "df001eb3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```我想订购一把篮球。```\n",
      "\n",
      "中文:我想订购一把篮球。\n",
      "英文:I want to order a basketball.\n",
      "法语:Je veux orderir un basketball.\n",
      "西班牙语:Vuelgo a intentar orderar un basketball.\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "24b29ac8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中文：我想订购一个篮球。\n",
      "英文：I want to order a basketball.\n",
      "法语：Je veux commander un ballon de basket.\n",
      "西班牙语：Quiero pedir una pelota de baloncesto.\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6422cb54-6153-4bf5-bdbe-c87d0780cfb6",
   "metadata": {},
   "source": [
    "**注：** 本人知识浅薄，法语和西班牙语翻译是用有道翻译检验的。chatglm和chatgpt的翻译都正确。大胜利！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aeb18fc",
   "metadata": {},
   "source": [
    "翻译+正式语气"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "aac408fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = f\"\"\"\n",
    "请将以下文本翻译成中文，分别展示成正式与非正式两种语气: \n",
    "```Would you like to order a pillow?```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0a760260",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "``` Would you like to order a pillow?```\n",
      "\n",
      "正式语气:\n",
      "\n",
      "我想订购一张枕头。\n",
      "\n",
      "非正式语气:\n",
      "\n",
      "嘿,你想预订一张枕头吗?\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "729355da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正式语气：请问您需要订购枕头吗？\n",
      "非正式语气：你要不要订一个枕头？\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc886170-3b7d-484a-b79c-e7cad453109d",
   "metadata": {},
   "source": [
    "**注：** 两种语气，chatglm和chatgpt都回答的不错，都加分。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a07fd232-34fa-4c04-80db-ac6698740f20",
   "metadata": {},
   "source": [
    "### 总结\n",
    "\n",
    "在翻译这块，chatglm做的和chatgpt相差无几，甚至可以说有些超越。换个角度想想，本地部署一个chatglm-int4专门用来翻译也不错啊，起码本地部署的api不收费！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d5a0225",
   "metadata": {},
   "source": [
    "## 逻辑推理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5cae8d70",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = f\"\"\"\n",
    "请判断学生的解决方案是否正确，请通过如下步骤解决这个问题：\n",
    "\n",
    "步骤：\n",
    "\n",
    "    首先，自己解决问题。\n",
    "    然后将你的解决方案与学生的解决方案进行比较，并评估学生的解决方案是否正确。在自己完成问题之前，请勿决定学生的解决方案是否正确。\n",
    "\n",
    "使用以下格式：\n",
    "\n",
    "    问题：问题文本\n",
    "    学生的解决方案：学生的解决方案文本\n",
    "    实际解决方案和步骤：实际解决方案和步骤文本\n",
    "    学生的解决方案和实际解决方案是否相同：是或否\n",
    "    学生的成绩：正确或不正确\n",
    "\n",
    "问题：\n",
    "\n",
    "    我正在建造一个太阳能发电站，需要帮助计算财务。 \n",
    "    - 土地费用为每平方英尺100美元\n",
    "    - 我可以以每平方英尺250美元的价格购买太阳能电池板\n",
    "    - 我已经谈判好了维护合同，每年需要支付固定的10万美元，并额外支付每平方英尺10美元\n",
    "    作为平方英尺数的函数，首年运营的总费用是多少。\n",
    "\n",
    "学生的解决方案：\n",
    "\n",
    "    设x为发电站的大小，单位为平方英尺。\n",
    "    费用：\n",
    "    1. 土地费用：100x\n",
    "    2. 太阳能电池板费用：250x\n",
    "    3. 维护费用：100,000+100x\n",
    "    总费用：100x+250x+100,000+100x=450x+100,000\n",
    "\n",
    "实际解决方案和步骤：\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4b5d7427",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际解决方案和步骤与学生的解决方案相同。\n",
      "\n",
      "学生的解决方案可以简化为:\n",
      "\n",
      "总费用 = 土地费用 + 太阳能电池板费用 + 维护费用\n",
      "\n",
      "根据题目中的数据,土地费用为每平方英尺100美元,太阳能电池板费用为每平方英尺250美元,维护费用为每年10万美元和每平方英尺10美元,因此:\n",
      "\n",
      "总费用 = 100x + 250x + 100,000 + 10x\n",
      "\n",
      "将x的表达式代入总费用公式中,得到:\n",
      "\n",
      "总费用 = 100x + 250x + 100,000 + 10x = 450x + 100,000\n",
      "\n",
      "因此,学生的解决方案和实际解决方案相同。\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_glm(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "71c23747",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正确的解决方案和步骤：\n",
      "    1. 计算土地费用：100美元/平方英尺 * x平方英尺 = 100x美元\n",
      "    2. 计算太阳能电池板费用：250美元/平方英尺 * x平方英尺 = 250x美元\n",
      "    3. 计算维护费用：10万美元 + 10美元/平方英尺 * x平方英尺 = 10万美元 + 10x美元\n",
      "    4. 计算总费用：100x美元 + 250x美元 + 10万美元 + 10x美元 = 360x + 10万美元\n",
      "\n",
      "学生的解决方案和实际解决方案是否相同：否\n",
      "\n",
      "学生的成绩：不正确\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_gpt(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a313cd9-647e-4639-aa06-e28dd2df7827",
   "metadata": {},
   "source": [
    "**注：** 实际上学生的解决方案是不正确的，维护费用每平方英尺是10美元，在学生的解答中错误的将其写成了100美元，chatglm发现这个错误，但它没有指出学生解答中的错误。相反chatgpt发现了错误，并给出了正确解法。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
