Skip to content

Instantly share code, notes, and snippets.

@ninehills
Created April 21, 2023 12:18
Show Gist options
  • Save ninehills/b29c04be3aa925f151e4af27f05d73aa to your computer and use it in GitHub Desktop.
Save ninehills/b29c04be3aa925f151e4af27f05d73aa to your computer and use it in GitHub Desktop.
ChatGLM.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyP/Hrd8lwjezMm63Lusxid1",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ninehills/b29c04be3aa925f151e4af27f05d73aa/chatglm.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "C6GWoQKozTdw"
},
"outputs": [],
"source": [
"!pip install protobuf==3.20.0 transformers==4.27.1 icetk cpm_kernels\n",
"\n",
"from transformers import AutoTokenizer, AutoModel\n",
"tokenizer = AutoTokenizer.from_pretrained(\"THUDM/chatglm-6b-int4\", trust_remote_code=True)\n",
"model = AutoModel.from_pretrained(\"THUDM/chatglm-6b-int4\", trust_remote_code=True).half().cuda()\n",
"\n"
]
},
{
"cell_type": "code",
"source": [
"response, history = model.chat(tokenizer, \"你好\", history=[])\n",
"print(response)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EQCmmYOY0IMe",
"outputId": "d39d4b7e-d65b-4461-9fb8-f3bf4ca85cfb"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:transformers_modules.THUDM.chatglm-6b-int4.68873da92104a23cfc1553421aecf32dd862f703.modeling_chatglm:The dtype of attention mask (torch.int64) is not bool\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"response, history = model.chat(tokenizer, \"小明的爸爸有三个孩子,其中两个是小红和小黑,第三个孩子叫什么?\", history=history)\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Qsa2ZLUm0Kpi",
"outputId": "25d2eeb3-57ea-4e04-d4a3-fbcaa0a650f6"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"小明的爸爸有三个孩子,其中两个是小红和小黑,因此第三个孩子的名字应该是小明。\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"response, history = model.chat(tokenizer, \"写一个机甲大战怪兽的故事\", history=history)\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bce0ce84-a807-462d-ec25-beae468ad6e9",
"id": "u238Q_cK2Net"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"在一个遥远的未来,人类已经掌握了高科技,机甲技术的发展也达到了巅峰。人们开始将机甲用于战争,保护自己和维护世界的和平。但是,一些怪兽也发现了机甲的力量,开始对人类构成威胁。\n",
"\n",
"故事的主人公是一名叫做“机甲战士”的机甲驾驶员。他驾驶的机甲非常强大,可以与怪兽进行战斗。在一次巡逻中,机甲战士发现了一只怪兽,它正在摧毁一座人类的城市。机甲战士决定拯救这座城市,但是怪兽也不甘示弱。\n",
"\n",
"机甲战士和怪兽展开了激烈的战斗。在战斗中,机甲战士不断学习和进化,变得越来越强大。最终,机甲战士和怪兽展开了一场决战。在这场战斗中,机甲战士发挥了自己最大的能力,与怪兽进行了一场惊心动魄的战斗。\n",
"\n",
"在战斗的最后,机甲战士战胜了怪兽,拯救了城市。人们为机甲战士和怪兽欢呼,同时也明白了机甲的力量不仅可以用于战争,也可以用于和平和拯救。从此,机甲战士和怪兽成为了人们的英雄,机甲大战怪兽的故事也成为了传说。\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment