├── README.md ├── requirements.txt ├── test.py ├── wechatServer.py └── wechatServerTest.py /README.md: -------------------------------------------------------------------------------- 1 | # 序言 2 | 人工智能的发展越来越快,为了让更多的技术人员同屏共振,我们必须不停地更新个人的技术栈和技术边界,充分利用个人业务时间学习和总结;同时,避开重复和复杂的调研过程,少掉进坑里,尽快爬出来走到下一步;再看Python语言层面,各种安装环境,硬件资源的要求,对于学习来说是个挑战的事情;欢迎各位技术人员,按照步骤进行操作,有问题可以留言或微信沟通!~ 3 | 4 | 技术选型:ChatGLM-6B 是一个开源的、支持中英双语的对话语言模型,基于 General Language Model (GLM) 架构,具有 62 亿参数。结合模型量化技术,用户可以在消费级的显卡上进行本地部署(INT4 量化级别下最低只需 6GB 显存)。ChatGLM-6B 使用了和 ChatGPT 相似的技术,针对中文问答和对话进行了优化。 5 | 6 | # 【技术篇】个人微信公众号对接chatGLM-6B 7 | [微信文章点击](https://mp.weixin.qq.com/s?__biz=MzIxNDAxNjk0NA==&mid=2651413599&idx=1&sn=df98873bc40a554e185638b8414434fa&chksm=8c500a80bb278396f546583f75cc67edcf1fb5b944fa941d314b3198790a52cc27bbfc7a5f77&mpshare=1&scene=23&srcid=0402ejfj5n3FdQjs1VYVPliC&sharer_sharetime=1680434778616&sharer_shareid=37b45325f2c7e679e85f0cae67be583a#rd) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | protobuf>=3.19.5,<3.20.1 2 | transformers>=4.26.1 3 | icetk 4 | cpm_kernels 5 | gradio 6 | cryptography 7 | wechatpy 8 | Flask 9 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from transformers import AutoTokenizer, AutoModel 2 | tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) 3 | model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float() 4 | response, history = model.chat(tokenizer, "你好", history=[]) 5 | print(response) -------------------------------------------------------------------------------- /wechatServer.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from flask import Flask 3 | 4 | from flask import request 5 | import sys 6 | from wechatpy.utils import check_signature 7 | from wechatpy.exceptions import InvalidSignatureException,InvalidAppIdException 8 | from wechatpy import parse_message 9 | from wechatpy.replies import create_reply 10 | from wechatpy.replies import TextReply 11 | from wechatpy import WeChatClient 12 | 13 | from transformers import AutoTokenizer, AutoModel 14 | 15 | from threading import Thread 16 | 17 | client = WeChatClient('wxe1ce7221aa9b8225', '7eb79a6a232ebeab03fa840b74f1513f') 18 | app = Flask(__name__) 19 | app.debug = True 20 | handler = logging.StreamHandler() 21 | app.logger.addHandler(handler) 22 | wechatToken = "peterli2015" 23 | 24 | tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True) 25 | model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True).float() 26 | 27 | 28 | def asyncTask(userId, content): 29 | print("ask a quesion with userId:{}, content:{}".format(userId, content)) 30 | response, history = model.chat(tokenizer, content, history=[]) 31 | print("chat-GLB replay:{}".format(response)) 32 | client.message.send_text(userId, response) 33 | 34 | 35 | @app.route('/wechat', methods=['GET', 'POST']) 36 | def wechat(): 37 | timestamp = request.args.get("timestamp") 38 | nonce = request.args.get("nonce") 39 | if request.method == 'GET': 40 | # token, signature, timestamp, nonce 41 | echostr = request.args.get("echostr") 42 | signature = request.args.get("signature") 43 | if echostr: 44 | print("request timestamp:{},nonce:{}, echostr:{}, signature:{}".format(timestamp, 45 | nonce, echostr, signature)) 46 | try: 47 | check_signature(wechatToken, signature, timestamp, nonce) 48 | return echostr 49 | except InvalidSignatureException: 50 | print("invalid message from request") 51 | else: 52 | xml = request.data 53 | if xml: 54 | try: 55 | msg = parse_message(xml) 56 | print("message from wechat msg:{}".format(msg)) 57 | 58 | t1 = Thread(target=asyncTask, args=(msg.source, msg.content)) 59 | t1.start() 60 | 61 | return "success" 62 | except (InvalidAppIdException, InvalidSignatureException): 63 | print("cannot decrypt message!") 64 | else: 65 | print("no xml body, invalid request!") 66 | return "" 67 | if __name__ == '__main__': 68 | print('starting wechat of chatGLM') 69 | print('completed to load chatGLM') 70 | app.run(host='127.0.0.1', port=6006, debug=True) 71 | -------------------------------------------------------------------------------- /wechatServerTest.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from flask import Flask 3 | 4 | from flask import request 5 | import sys 6 | from wechatpy.utils import check_signature 7 | from wechatpy.exceptions import InvalidSignatureException, InvalidAppIdException 8 | from wechatpy import parse_message 9 | from wechatpy.replies import create_reply 10 | from wechatpy.replies import TextReply 11 | from wechatpy import WeChatClient 12 | 13 | from threading import Thread 14 | 15 | client = WeChatClient('wxe1ce7221aa9b8225', '7eb79a6a232ebeab03fa840b74f1513f') 16 | app = Flask(__name__) 17 | app.debug = True 18 | handler = logging.StreamHandler() 19 | app.logger.addHandler(handler) 20 | 21 | wechatToken = "peterli2015" 22 | 23 | def asyncTask(userId, content): 24 | response = "hello world" 25 | print("ask a quesion with userId:{}, content:{}".format(userId, content)) 26 | print("chat-GLB replay:{}".format(response)) 27 | client.message.send_text(userId, response) 28 | 29 | 30 | @app.route('/wechat', methods=['GET', 'POST']) 31 | def wechat(): 32 | timestamp = request.args.get("timestamp") 33 | nonce = request.args.get("nonce") 34 | if request.method == 'GET': 35 | # token, signature, timestamp, nonce 36 | echostr = request.args.get("echostr") 37 | signature = request.args.get("signature") 38 | if echostr: 39 | print("request timestamp:{},nonce:{}, echostr:{}, signature:{}".format(timestamp, 40 | nonce, echostr, signature)) 41 | try: 42 | check_signature(wechatToken, signature, timestamp, nonce) 43 | return echostr 44 | except InvalidSignatureException: 45 | print("invalid message from request") 46 | else: 47 | xml = request.data 48 | if xml: 49 | try: 50 | msg = parse_message(xml) 51 | print("message from wechat msg:{}".format(msg)) 52 | 53 | t1 = Thread(target=asyncTask, args=(msg.source, msg.content)) 54 | t1.start() 55 | 56 | return "success" 57 | except (InvalidAppIdException, InvalidSignatureException): 58 | print("cannot decrypt message!") 59 | else: 60 | print("no xml body, invalid request!") 61 | return "" 62 | if __name__ == '__main__': 63 | print('starting wechat of chatGLM') 64 | print('completed to load chatGLM') 65 | app.run(host='127.0.0.1', port=6006, debug=True) 66 | 67 | --------------------------------------------------------------------------------