From 287e7a2592cc9e1c5bc4a23771cd9dd3c4bd79b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=AA?= <757078144@qq.com> Date: Sun, 12 Nov 2023 13:41:01 +0800 Subject: [PATCH 1/5] =?UTF-8?q?chatglm3=E6=8E=A5=E5=85=A5=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E5=AE=9E=E7=8E=B0=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E4=BB=A3=E7=A0=81=E6=89=A7=E8=A1=8C=E7=AD=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml.template | 7 ++ configuration.py | 1 + robot.py | 3 + tool/__init__.py | 0 tool/base.json | 88 +++++++++++++++++++ tool/code_kernel.py | 193 ++++++++++++++++++++++++++++++++++++++++++ tool/comfyUI_api.py | 177 ++++++++++++++++++++++++++++++++++++++ tool/tool_registry.py | 158 ++++++++++++++++++++++++++++++++++ 8 files changed, 627 insertions(+) create mode 100644 tool/__init__.py create mode 100644 tool/base.json create mode 100644 tool/code_kernel.py create mode 100644 tool/comfyUI_api.py create mode 100644 tool/tool_registry.py diff --git a/config.yaml.template b/config.yaml.template index e23de14..7de764e 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -54,6 +54,13 @@ report_reminder: # proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 # prompt: 你是智能聊天机器人,你叫wcferry # 根据需要对角色进行设定 +chatglm: + key: key + api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 + proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 + prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定 + file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 + tigerbot: key: key model: tigerbot-7b-sft diff --git a/configuration.py b/configuration.py index afc2b04..b0a4bcf 100644 --- a/configuration.py +++ b/configuration.py @@ -33,3 +33,4 @@ class Config(object): self.REPORT_REMINDERS = yconfig["report_reminder"]["receivers"] self.TIGERBOT = yconfig.get("tigerbot") self.XINGHUO_WEB = yconfig.get("xinghuo_web") + self.CHATGLM = yconfig.get("chatglm") diff --git a/robot.py b/robot.py index c635b19..53096ad 100644 --- a/robot.py +++ b/robot.py @@ -11,6 +11,7 @@ from wcferry import Wcf, WxMsg from configuration import Config from func_chatgpt import ChatGPT +from func_chatglm import ChatGLM from func_chengyu import cy from func_news import News from func_tigerbot import TigerBot @@ -36,6 +37,8 @@ class Robot(Job): self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt")) elif self.config.XINGHUO_WEB: self.chat = XinghuoWeb(self.config.XINGHUO_WEB) + elif self.config.CHATGLM: + self.chat = ChatGLM(wcf, self.config.CHATGLM) else: self.chat = None diff --git a/tool/__init__.py b/tool/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tool/base.json b/tool/base.json new file mode 100644 index 0000000..6bfcd3f --- /dev/null +++ b/tool/base.json @@ -0,0 +1,88 @@ +{ + "prompt": { + "3": { + "inputs": { + "seed": 1000573256060686, + "steps": 20, + "cfg": 8, + "sampler_name": "euler", + "scheduler": "normal", + "denoise": 1, + "model": [ + "4", + 0 + ], + "positive": [ + "6", + 0 + ], + "negative": [ + "7", + 0 + ], + "latent_image": [ + "5", + 0 + ] + }, + "class_type": "KSampler" + }, + "4": { + "inputs": { + "ckpt_name": "(修复)512-inpainting-ema.safetensors" + }, + "class_type": "CheckpointLoaderSimple" + }, + "5": { + "inputs": { + "width": 512, + "height": 512, + "batch_size": 1 + }, + "class_type": "EmptyLatentImage" + }, + "6": { + "inputs": { + "text": "beautiful scenery nature glass bottle landscape, , purple galaxy bottle,dress, ", + "clip": [ + "4", + 1 + ] + }, + "class_type": "CLIPTextEncode" + }, + "7": { + "inputs": { + "text": "text, watermark", + "clip": [ + "4", + 1 + ] + }, + "class_type": "CLIPTextEncode" + }, + "8": { + "inputs": { + "samples": [ + "3", + 0 + ], + "vae": [ + "4", + 2 + ] + }, + "class_type": "VAEDecode" + }, + "9": { + "inputs": { + "filename_prefix": "ComfyUI", + "images": [ + "8", + 0 + ] + }, + "class_type": "SaveImage" + } + } +} \ No newline at end of file diff --git a/tool/code_kernel.py b/tool/code_kernel.py new file mode 100644 index 0000000..e8e6304 --- /dev/null +++ b/tool/code_kernel.py @@ -0,0 +1,193 @@ +import base64 +from io import BytesIO +import os +from pprint import pprint +import queue +import re +from subprocess import PIPE + +import jupyter_client +from PIL import Image +import time + +IPYKERNEL = os.environ.get('IPYKERNEL', 'chatglm3') +class CodeKernel(object): + def __init__(self, + kernel_name='kernel', + kernel_id=None, + kernel_config_path="", + python_path=None, + ipython_path=None, + init_file_path="./startup.py", + verbose=1): + + self.kernel_name = kernel_name + self.kernel_id = kernel_id + self.kernel_config_path = kernel_config_path + self.python_path = python_path + self.ipython_path = ipython_path + self.init_file_path = init_file_path + self.verbose = verbose + + if python_path is None and ipython_path is None: + env = None + else: + env = {"PATH": self.python_path + ":$PATH", "PYTHONPATH": self.python_path} + + # Initialize the backend kernel + self.kernel_manager = jupyter_client.KernelManager(kernel_name=IPYKERNEL, + connection_file=self.kernel_config_path, + exec_files=[self.init_file_path], + env=env) + if self.kernel_config_path: + self.kernel_manager.load_connection_file() + self.kernel_manager.start_kernel(stdout=PIPE, stderr=PIPE) + print("Backend kernel started with the configuration: {}".format( + self.kernel_config_path)) + else: + self.kernel_manager.start_kernel(stdout=PIPE, stderr=PIPE) + print("Backend kernel started with the configuration: {}".format( + self.kernel_manager.connection_file)) + + if verbose: + print(self.kernel_manager.get_connection_info()) + + # Initialize the code kernel + self.kernel = self.kernel_manager.blocking_client() + # self.kernel.load_connection_file() + self.kernel.start_channels() + print("Code kernel started.") + + def execute(self, code): + self.kernel.execute(code) + try: + shell_msg = self.kernel.get_shell_msg(timeout=40) + io_msg_content = self.kernel.get_iopub_msg(timeout=40)['content'] + while True: + msg_out = io_msg_content + ### Poll the message + try: + io_msg_content = self.kernel.get_iopub_msg(timeout=40)['content'] + if 'execution_state' in io_msg_content and io_msg_content['execution_state'] == 'idle': + break + except queue.Empty: + break + + return shell_msg, msg_out + except Exception as e: + print(e) + return None + + def execute_interactive(self, code, verbose=False): + shell_msg = self.kernel.execute_interactive(code) + if shell_msg is queue.Empty: + if verbose: + print("Timeout waiting for shell message.") + self.check_msg(shell_msg, verbose=verbose) + + return shell_msg + + def inspect(self, code, verbose=False): + msg_id = self.kernel.inspect(code) + shell_msg = self.kernel.get_shell_msg(timeout=30) + if shell_msg is queue.Empty: + if verbose: + print("Timeout waiting for shell message.") + self.check_msg(shell_msg, verbose=verbose) + + return shell_msg + + def get_error_msg(self, msg, verbose=False) -> str | None: + if msg['content']['status'] == 'error': + try: + error_msg = msg['content']['traceback'] + except: + try: + error_msg = msg['content']['traceback'][-1].strip() + except: + error_msg = "Traceback Error" + if verbose: + print("Error: ", error_msg) + return error_msg + return None + + def check_msg(self, msg, verbose=False): + status = msg['content']['status'] + if status == 'ok': + if verbose: + print("Execution succeeded.") + elif status == 'error': + for line in msg['content']['traceback']: + if verbose: + print(line) + + def shutdown(self): + # Shutdown the backend kernel + self.kernel_manager.shutdown_kernel() + print("Backend kernel shutdown.") + # Shutdown the code kernel + self.kernel.shutdown() + print("Code kernel shutdown.") + + def restart(self): + # Restart the backend kernel + self.kernel_manager.restart_kernel() + # print("Backend kernel restarted.") + + def interrupt(self): + # Interrupt the backend kernel + self.kernel_manager.interrupt_kernel() + # print("Backend kernel interrupted.") + + def is_alive(self): + return self.kernel.is_alive() + +def b64_2_img(data): + buff = BytesIO(base64.b64decode(data)) + return Image.open(buff) + +def clean_ansi_codes(input_string): + ansi_escape = re.compile(r'(\x9B|\x1B\[|\u001b\[)[0-?]*[ -/]*[@-~]') + return ansi_escape.sub('', input_string) + +def execute(code, kernel: CodeKernel) -> tuple[str, str | Image.Image]: + res = "" + res_type = None + code = code.replace("<|observation|>", "") + code = code.replace("<|assistant|>interpreter", "") + code = code.replace("<|assistant|>", "") + code = code.replace("<|user|>", "") + code = code.replace("<|system|>", "") + msg, output = kernel.execute(code) + + if msg['metadata']['status'] == "timeout": + return res_type, 'Timed out' + elif msg['metadata']['status'] == 'error': + return res_type, clean_ansi_codes('\n'.join(kernel.get_error_msg(msg, verbose=True))) + + if 'text' in output: + res_type = "text" + res = output['text'] + elif 'data' in output: + for key in output['data']: + if 'image/png' in key: + res_type = "image" + res = output['data'][key] + break + elif 'text/plain' in key: + res_type = "text" + res = output['data'][key] + + + if res_type == "image": + return res_type, b64_2_img(res) + elif res_type == "text" or res_type == "traceback": + res = res + + return res_type, res + +def extract_code(text: str) -> str: + pattern = r'```([^\n]*)\n(.*?)```' + matches = re.findall(pattern, text, re.DOTALL) + return matches[-1][1] + diff --git a/tool/comfyUI_api.py b/tool/comfyUI_api.py new file mode 100644 index 0000000..033448a --- /dev/null +++ b/tool/comfyUI_api.py @@ -0,0 +1,177 @@ +#This is an example that uses the websockets api to know when a prompt execution is done +#Once the prompt execution is done it downloads the images using the /history endpoint + +import websocket #NOTE: websocket-client (https://github.com/websocket-client/websocket-client) +import uuid +import json +import requests +import urllib +import random +import io +from PIL import Image + + +class ComfyUIApi(): + def __init__(self, server_address="127.0.0.1:8188"): + self.server_address=server_address + self.client_id=str(uuid.uuid4()) + self.ws = websocket.WebSocket() + self.ws.connect("ws://{}/ws?clientId={}".format(server_address, self.client_id)) + + def queue_prompt(self,prompt): + p = {"prompt": prompt, "client_id": self.client_id} + data = json.dumps(p).encode('utf-8') + req = requests.post("http://{}/prompt".format(self.server_address), data=data) + print(req.text) + return json.loads(req.text) + + def get_image(self,filename, subfolder, folder_type): + data = {"filename": filename, "subfolder": subfolder, "type": folder_type} + url_values = urllib.parse.urlencode(data) + with requests.get("http://{}/view?{}".format(self.server_address, url_values)) as response: + image = Image.open(io.BytesIO(response.content)) + return image + + def get_image_url(self,filename, subfolder, folder_type): + data = {"filename": filename, "subfolder": subfolder, "type": folder_type} + url_values = urllib.parse.urlencode(data) + return "http://{}/view?{}".format(self.server_address, url_values) + + def get_history(self,prompt_id): + with requests.get("http://{}/history/{}".format(self.server_address, prompt_id)) as response: + return json.loads(response.text) + + def get_images(self,prompt,isUrl=False): + prompt_id = self.queue_prompt(prompt)['prompt_id'] + output_images = [] + while True: + out = self.ws.recv() + if isinstance(out, str): + message = json.loads(out) + if message['type'] == 'executing': + data = message['data'] + if data['node'] is None and data['prompt_id'] == prompt_id: + break #Execution is done + else: + continue #previews are binary data + + history = self.get_history(prompt_id)[prompt_id] + for o in history['outputs']: + for node_id in history['outputs']: + node_output = history['outputs'][node_id] + if 'images' in node_output: + for image in node_output['images']: + image_data =self.get_image_url(image['filename'], image['subfolder'], image['type']) if isUrl else self.get_image(image['filename'], image['subfolder'], image['type']) + image['image']=image_data + output_images.append(image) + + return output_images + +prompt_text = """ +{ + "3": { + "class_type": "KSampler", + "inputs": { + "cfg": 8, + "denoise": 1, + "latent_image": [ + "5", + 0 + ], + "model": [ + "4", + 0 + ], + "negative": [ + "7", + 0 + ], + "positive": [ + "6", + 0 + ], + "sampler_name": "euler", + "scheduler": "normal", + "seed": 8566257, + "steps": 20 + } + }, + "4": { + "class_type": "CheckpointLoaderSimple", + "inputs": { + "ckpt_name": "chilloutmix_NiPrunedFp32Fix.safetensors" + } + }, + "5": { + "class_type": "EmptyLatentImage", + "inputs": { + "batch_size": 1, + "height": 512, + "width": 512 + } + }, + "6": { + "class_type": "CLIPTextEncode", + "inputs": { + "clip": [ + "4", + 1 + ], + "text": "masterpiece best quality girl" + } + }, + "7": { + "class_type": "CLIPTextEncode", + "inputs": { + "clip": [ + "4", + 1 + ], + "text": "bad hands" + } + }, + "8": { + "class_type": "VAEDecode", + "inputs": { + "samples": [ + "3", + 0 + ], + "vae": [ + "4", + 2 + ] + } + }, + "9": { + "class_type": "SaveImage", + "inputs": { + "filename_prefix": "ComfyUI", + "images": [ + "8", + 0 + ] + } + } +} +""" +if __name__ == '__main__': + prompt = json.loads(prompt_text) + #set the text prompt for our positive CLIPTextEncode + prompt["6"]["inputs"]["text"] = "masterpiece best quality man" + + #set the seed for our KSampler node + prompt["3"]["inputs"]["seed"] = ''.join(random.sample('123456789012345678901234567890',14)) + + cfui=ComfyUIApi() + images = cfui.get_images(prompt) + + #Commented out code to display the output images: + + for node_id in images: + for image_data in images[node_id]: + from PIL import Image + import io + image = Image.open(io.BytesIO(image_data)) + image.show() + diff --git a/tool/tool_registry.py b/tool/tool_registry.py new file mode 100644 index 0000000..3a70ce8 --- /dev/null +++ b/tool/tool_registry.py @@ -0,0 +1,158 @@ +from copy import deepcopy +import inspect +from pprint import pformat +import traceback +from types import GenericAlias +from typing import get_origin, Annotated +import json +import requests +import random +import time +import re +from tool.comfyUI_api import ComfyUIApi +from func_news import News +from zhdate import ZhDate +from datetime import datetime + +_TOOL_HOOKS = {} +_TOOL_DESCRIPTIONS = {} + +def extract_code(text: str) -> str: + pattern = r'```([^\n]*)\n(.*?)```' + matches = re.findall(pattern, text, re.DOTALL) + return matches[-1][1] + + +def register_tool(func: callable): + tool_name = func.__name__ + tool_description = inspect.getdoc(func).strip() + python_params = inspect.signature(func).parameters + tool_params = [] + for name, param in python_params.items(): + annotation = param.annotation + if annotation is inspect.Parameter.empty: + raise TypeError(f"Parameter `{name}` missing type annotation") + if get_origin(annotation) != Annotated: + raise TypeError(f"Annotation type for `{name}` must be typing.Annotated") + + typ, (description, required) = annotation.__origin__, annotation.__metadata__ + typ: str = str(typ) if isinstance(typ, GenericAlias) else typ.__name__ + if not isinstance(description, str): + raise TypeError(f"Description for `{name}` must be a string") + if not isinstance(required, bool): + raise TypeError(f"Required for `{name}` must be a bool") + + tool_params.append({ + "name": name, + "description": description, + "type": typ, + "required": required + }) + tool_def = { + "name": tool_name, + "description": tool_description, + "params": tool_params + } + + #print("[registered tool] " + pformat(tool_def)) + _TOOL_HOOKS[tool_name] = func + _TOOL_DESCRIPTIONS[tool_name] = tool_def + + return func + +def dispatch_tool(tool_name: str, tool_params: dict) -> str: + if tool_name not in _TOOL_HOOKS: + return f"Tool `{tool_name}` not found. Please use a provided tool." + tool_call = _TOOL_HOOKS[tool_name] + try: + ret = tool_call(**tool_params) + except: + ret = traceback.format_exc() + return ret + +def get_tools() -> dict: + return deepcopy(_TOOL_DESCRIPTIONS) + +# Tool Definitions + +# @register_tool +# def random_number_generator( +# seed: Annotated[int, 'The random seed used by the generator', True], +# range: Annotated[tuple[int, int], 'The range of the generated numbers', True], +# ) -> int: +# """ +# Generates a random number x, s.t. range[0] <= x < range[1] +# """ +# if not isinstance(seed, int): +# raise TypeError("Seed must be an integer") +# if not isinstance(range, tuple): +# raise TypeError("Range must be a tuple") +# if not isinstance(range[0], int) or not isinstance(range[1], int): +# raise TypeError("Range must be a tuple of integers") + +# import random +# return random.Random(seed).randint(*range) + +@register_tool +def get_weather( + city_name: Annotated[str, 'The name of the city to be queried', True], +) -> str: + """ + Get the current weather for `city_name` + """ + if not isinstance(city_name, str): + raise TypeError("City name must be a string") + + key_selection = { + "current_condition": ["temp_C", "FeelsLikeC", "humidity", "weatherDesc", "observation_time"], + } + import requests + try: + resp = requests.get(f"https://wttr.in/{city_name}?format=j1") + resp.raise_for_status() + resp = resp.json() + ret = {k: {_v: resp[k][0][_v] for _v in v} for k, v in key_selection.items()} + except: + import traceback + ret = "Error encountered while fetching weather data!\n" + traceback.format_exc() + + return str(ret) + +@register_tool +def get_confyui_image(prompt: Annotated[str, '要生成图片的提示词,注意必须是英文', True]) -> dict: + ''' + 生成图片 + ''' + with open("tool\\base.json", "r", encoding="utf-8") as f: + data2 = json.load(f) + data2['prompt']['3']['inputs']['seed']=''.join(random.sample('123456789012345678901234567890',14)) + data2['prompt']['4']['inputs']['ckpt_name']='chilloutmix_NiPrunedFp32Fix.safetensors' #模型名称 + data2['prompt']['6']['inputs']['text']=prompt #正向提示词 + #data2['prompt']['7']['inputs']['text']='' #反向提示词 + cfui=ComfyUIApi(server_address="127.0.0.1:8188") #根据自己comfyUI地址修改 + images = cfui.get_images(data2['prompt']) + return {'res':images[0]['image'],'res_type':'image','filename':images[0]['filename']} + +@register_tool +def get_news() -> str: + ''' + 获取最新新闻 + ''' + news = News() + return news.get_important_news() + +@register_tool +def get_time() -> str: + ''' + 获取当前日期,时间,农历日期,星期几 + ''' + time=datetime.now() + date2 = ZhDate.from_datetime(time) + week_list = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"] + + return '{} {} {}'.format(time.strftime("%Y年%m月%d日 %H:%M:%S"),week_list[time.weekday()],'农历:'+date2.chinese()) + + +if __name__ == "__main__": + print(dispatch_tool("get_weather", {"city_name": "beijing"})) + print(get_tools()) From 3370cc245dba5402c6187a0d98d4057980f95804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=AA?= <757078144@qq.com> Date: Sun, 12 Nov 2023 16:09:54 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=BC=8F=E4=BA=86=E4=B8=AA=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- func_chatglm.py | 183 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 func_chatglm.py diff --git a/func_chatglm.py b/func_chatglm.py new file mode 100644 index 0000000..95fd0a8 --- /dev/null +++ b/func_chatglm.py @@ -0,0 +1,183 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +from datetime import datetime + +import openai +import json +import os +import random +from tool.tool_registry import get_tools, dispatch_tool,extract_code +from tool.code_kernel import execute,CodeKernel +from wcferry import Wcf, WxMsg + +functions=get_tools() + +class ChatGLM(): + + def __init__(self, wcf: Wcf, config={},max_retry=5) -> None: + openai.api_key = config.get('key') + # 自己搭建或第三方代理的接口 + openai.api_base = config.get('api') + if config.get('proxy',None): + openai.proxy = {"http": config.get('proxy',None), "https": config.get('proxy',None)} + self.conversation_list = {} + self.chat_type={} + self.max_retry=max_retry + self.wcf=wcf + self.filePath=config.get('file_path') + self.kernel = CodeKernel() + self.system_content_msg = {"chat":[{"role": "system", "content": config.get('prompt')}], + "tool":[{"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:"}], + "code":[{"role": "system", "content": "你是一位智能AI助手,你叫ChatGLM,你连接着一台电脑,但请注意不能联网。在使用Python解决任务时,你可以运行代码并得到结果,如果运行结果有错误,你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件,文件默认存储路径是{}。".format(self.filePath)}]} + code0=''' +import matplotlib.pyplot as plt +import numpy as np +x = np.linspace(-1, 1, 50) +y = x * x + 1 +plt.plot(x, y) +plt.show() + ''' + res_type, res = execute(code0, self.kernel) #第一次画图不返回图片问题 + print(res_type, res) + + def get_answer(self, question: str, wxid: str) -> str: + # wxid或者roomid,个人时为微信id,群消息时为群id + if '#帮助'==question: + return '本助手有三种模式,#聊天模式 = #1 ,#工具模式 = #2 ,#代码模式 = #3 , #清除模式会话 = #4 , #清除全部会话 = #5 可用发送#对应模式 或者 #编号 进行切换' + elif '#聊天模式'==question or '#1'==question: + self.chat_type[wxid]='chat' + return '已切换#聊天模式' + elif '#工具模式'==question or '#2'==question: + self.chat_type[wxid]='tool' + return '已切换#工具模式 \n工具有:查看天气,日期,新闻,comfyUI文生图。例如:\n帮我生成一张小鸟的图片,提示词必须是英文' + elif '#代码模式'==question or '#3'==question: + self.chat_type[wxid]='code' + return '已切换#代码模式 \n代码模式可以用于写python代码,例如:\n用python画一个爱心' + elif '#清除模式会话'==question or '#4'==question: + self.conversation_list[wxid][self.chat_type[wxid]]=self.system_content_msg[self.chat_type[wxid]] + return '已清除' + elif '#清除全部会话'==question or '#5'==question: + self.conversation_list[wxid]=self.system_content_msg[self.chat_type[wxid]] + return '已清除' + + self.updateMessage(wxid, question, "user") + + try: + params = dict(model="chatglm3",temperature=1.0, messages=self.conversation_list[wxid][self.chat_type[wxid]], stream=False) + if 'tool'==self.chat_type[wxid]: + params["functions"] = functions + response = openai.ChatCompletion.create(**params) + for _ in range(self.max_retry): + if response.choices[0].message.get("function_call"): + function_call = response.choices[0].message.function_call + print(f"Function Call Response: {function_call.to_dict_recursive()}") + + function_args = json.loads(function_call.arguments) + observation = dispatch_tool(function_call.name, function_args) + if isinstance(observation,dict): + res_type=observation['res_type'] if 'res_type' in observation else 'text' + res=observation['res'] if 'res_type' in observation else str(observation ) + if res_type == 'image': + filename= observation['filename'] + filePath=os.path.join(self.filePath,filename) + res.save(filePath) + self.wcf.send_image(filePath,wxid) + tool_response='[Image]' if res_type == 'image' else res + else: + tool_response=observation if isinstance(observation,str) else str(observation) + print(f"Tool Call Response: {tool_response}") + + params["messages"].append(response.choices[0].message) + params["messages"].append( + { + "role": "function", + "name": function_call.name, + "content": tool_response, # 调用函数返回结果 + } + ) + self.updateMessage(wxid, tool_response, "function") + response = openai.ChatCompletion.create(**params) + elif response.choices[0].message.content.find('interpreter')!=-1: + output_text=response.choices[0].message.content + code = extract_code(output_text) + self.wcf.send_text('代码如下:\n'+code,wxid) + self.wcf.send_text('执行代码...',wxid) + try: + res_type, res = execute(code, self.kernel) + except Exception as e: + rsp=f'代码执行错误: {e}' + break + if res_type == 'image': + filename= '{}.png'.format(''.join(random.sample('abcdefghijklmnopqrstuvwxyz1234567890',8))) + filePath=os.path.join(self.filePath,filename) + res.save(filePath) + self.wcf.send_image(filePath,wxid) + else: + self.wcf.send_text("执行结果:\n"+res,wxid) + tool_response='[Image]' if res_type == 'image' else res + print("Received:", res_type, res) + params["messages"].append(response.choices[0].message) + params["messages"].append( + { + "role": "function", + "name": "interpreter", + "content": tool_response, # 调用函数返回结果 + } + ) + self.updateMessage(wxid, tool_response, "function") + response = openai.ChatCompletion.create(**params) + else: + rsp = response.choices[0].message.content + break + + self.updateMessage(wxid, rsp, "assistant") + except Exception as e0: + rsp = "发生未知错误:" + str(e0) + + return rsp + + def updateMessage(self, wxid: str, question: str, role: str) -> None: + now_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + + # 初始化聊天记录,组装系统信息 + if wxid not in self.conversation_list.keys(): + self.conversation_list[wxid] = self.system_content_msg + if wxid not in self.chat_type.keys(): + self.chat_type[wxid]='chat' + + # 当前问题 + content_question_ = {"role": role, "content": question} + self.conversation_list[wxid][self.chat_type[wxid]].append(content_question_) + + # 只存储10条记录,超过滚动清除 + i = len(self.conversation_list[wxid][self.chat_type[wxid]]) + if i > 10: + print("滚动清除微信记录:" + wxid) + # 删除多余的记录,倒着删,且跳过第一个的系统消息 + del self.conversation_list[wxid][self.chat_type[wxid]][1] + + +if __name__ == "__main__": + from configuration import Config + config = Config().CHATGPT + if not config: + exit(0) + + key = config.get("key") + api = config.get("api") + proxy = config.get("proxy") + prompt = config.get("prompt") + + chat = ChatGLM(key, api, proxy, prompt) + + while True: + q = input(">>> ") + try: + time_start = datetime.now() # 记录开始时间 + print(chat.get_answer(q, "wxid")) + time_end = datetime.now() # 记录结束时间 + + print(f"{round((time_end - time_start).total_seconds(), 2)}s") # 计算的时间差为程序的执行时间,单位为秒/s + except Exception as e: + print(e) From db3ff14c7158dae6e0f43d14421ea8ca2a28950f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=AA?= <757078144@qq.com> Date: Sun, 12 Nov 2023 16:18:55 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- func_chatglm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/func_chatglm.py b/func_chatglm.py index 95fd0a8..13e2d9e 100644 --- a/func_chatglm.py +++ b/func_chatglm.py @@ -58,7 +58,7 @@ plt.show() self.conversation_list[wxid][self.chat_type[wxid]]=self.system_content_msg[self.chat_type[wxid]] return '已清除' elif '#清除全部会话'==question or '#5'==question: - self.conversation_list[wxid]=self.system_content_msg[self.chat_type[wxid]] + self.conversation_list[wxid]=self.system_content_msg return '已清除' self.updateMessage(wxid, question, "user") From 9ca9d31fee5c14bebf7fd017b2e78aaa5b647bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=AA?= <757078144@qq.com> Date: Mon, 13 Nov 2023 14:29:57 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chatglm/README.MD | 41 +++++++++ {tool => chatglm}/__init__.py | 0 {tool => chatglm}/base.json | 0 {tool => chatglm}/code_kernel.py | 45 ++++++---- {tool => chatglm}/comfyUI_api.py | 55 +++++++----- {tool => chatglm}/tool_registry.py | 55 +++++++----- config.yaml.template | 12 +-- func_chatglm.py | 139 ++++++++++++++--------------- requirements.txt | 4 + robot.py | 2 +- 10 files changed, 210 insertions(+), 143 deletions(-) create mode 100644 chatglm/README.MD rename {tool => chatglm}/__init__.py (100%) rename {tool => chatglm}/base.json (100%) rename {tool => chatglm}/code_kernel.py (91%) rename {tool => chatglm}/comfyUI_api.py (69%) rename {tool => chatglm}/tool_registry.py (75%) diff --git a/chatglm/README.MD b/chatglm/README.MD new file mode 100644 index 0000000..3003ca9 --- /dev/null +++ b/chatglm/README.MD @@ -0,0 +1,41 @@ +# ChatGLM3集成使用说明 + +* 1.需要取消配置中 chatglm 的注释, 并配置对应信息,使用ChatGLM3,启用最新版ChatGLM3根目录下openai_api.py获取api地址: +```yaml +# 如果要使用 chatglm,取消下面的注释并填写相关内容 +chatglm: + key: xxx #根据需要自己做key校验 + api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 + proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 + prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定 + file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 +``` +* 2.修改chatglm/tool_registry.py工具里面的一下配置,comfyUI地址或者根据需要自己配置一些工具,函数名上需要加@register_tool,函数里面需要叫'''函数描述''',参数需要用Annotated[str,'',True]修饰,分别是类型,参数说明,是否必填,再加->加上对应的返回类型 +```python +@register_tool +def get_confyui_image(prompt: Annotated[str, '要生成图片的提示词,注意必须是英文', True]) -> dict: + ''' + 生成图片 + ''' + with open("func_chatglm\\base.json", "r", encoding="utf-8") as f: + data2 = json.load(f) + data2['prompt']['3']['inputs']['seed'] = ''.join( + random.sample('123456789012345678901234567890', 14)) + # 模型名称 + data2['prompt']['4']['inputs']['ckpt_name'] = 'chilloutmix_NiPrunedFp32Fix.safetensors' + data2['prompt']['6']['inputs']['text'] = prompt # 正向提示词 + # data2['prompt']['7']['inputs']['text']='' #反向提示词 + cfui = ComfyUIApi(server_address="127.0.0.1:8188") # 根据自己comfyUI地址修改 + images = cfui.get_images(data2['prompt']) + return {'res': images[0]['image'], 'res_type': 'image', 'filename': images[0]['filename']} + +``` +* 3 使用 Code Interpreter 还需要安装 Jupyter 内核,默认名称叫chatglm3: +``` +ipython kernel install --name chatglm3 --user +``` +如果名称需要自定义,可以配置系统环境变量:IPYKERNEL 或者修改 chatglm/code_kernel.py +``` +IPYKERNEL = os.environ.get('IPYKERNEL', 'chatglm3') +``` +* 4 启动后,发送 #帮助 可以查看 模式和常用指令 diff --git a/tool/__init__.py b/chatglm/__init__.py similarity index 100% rename from tool/__init__.py rename to chatglm/__init__.py diff --git a/tool/base.json b/chatglm/base.json similarity index 100% rename from tool/base.json rename to chatglm/base.json diff --git a/tool/code_kernel.py b/chatglm/code_kernel.py similarity index 91% rename from tool/code_kernel.py rename to chatglm/code_kernel.py index e8e6304..48ab6a7 100644 --- a/tool/code_kernel.py +++ b/chatglm/code_kernel.py @@ -5,12 +5,14 @@ from pprint import pprint import queue import re from subprocess import PIPE - +from typing import Dict, Union, Optional, Tuple import jupyter_client from PIL import Image import time IPYKERNEL = os.environ.get('IPYKERNEL', 'chatglm3') + + class CodeKernel(object): def __init__(self, kernel_name='kernel', @@ -28,16 +30,18 @@ class CodeKernel(object): self.ipython_path = ipython_path self.init_file_path = init_file_path self.verbose = verbose - + if python_path is None and ipython_path is None: env = None else: - env = {"PATH": self.python_path + ":$PATH", "PYTHONPATH": self.python_path} + env = {"PATH": self.python_path + ":$PATH", + "PYTHONPATH": self.python_path} # Initialize the backend kernel - self.kernel_manager = jupyter_client.KernelManager(kernel_name=IPYKERNEL, + self.kernel_manager = jupyter_client.KernelManager(kernel_name=IPYKERNEL, connection_file=self.kernel_config_path, - exec_files=[self.init_file_path], + exec_files=[ + self.init_file_path], env=env) if self.kernel_config_path: self.kernel_manager.load_connection_file() @@ -65,14 +69,15 @@ class CodeKernel(object): io_msg_content = self.kernel.get_iopub_msg(timeout=40)['content'] while True: msg_out = io_msg_content - ### Poll the message + # Poll the message try: - io_msg_content = self.kernel.get_iopub_msg(timeout=40)['content'] + io_msg_content = self.kernel.get_iopub_msg(timeout=40)[ + 'content'] if 'execution_state' in io_msg_content and io_msg_content['execution_state'] == 'idle': break except queue.Empty: break - + return shell_msg, msg_out except Exception as e: print(e) @@ -97,14 +102,14 @@ class CodeKernel(object): return shell_msg - def get_error_msg(self, msg, verbose=False) -> str | None: + def get_error_msg(self, msg, verbose=False) -> Optional[str]: if msg['content']['status'] == 'error': try: error_msg = msg['content']['traceback'] - except: + except BaseException: try: error_msg = msg['content']['traceback'][-1].strip() - except: + except BaseException: error_msg = "Traceback Error" if verbose: print("Error: ", error_msg) @@ -141,16 +146,19 @@ class CodeKernel(object): def is_alive(self): return self.kernel.is_alive() - + + def b64_2_img(data): buff = BytesIO(base64.b64decode(data)) return Image.open(buff) + def clean_ansi_codes(input_string): ansi_escape = re.compile(r'(\x9B|\x1B\[|\u001b\[)[0-?]*[ -/]*[@-~]') return ansi_escape.sub('', input_string) - -def execute(code, kernel: CodeKernel) -> tuple[str, str | Image.Image]: + + +def execute(code, kernel: CodeKernel) -> tuple[str, Union[str, Image.Image]]: res = "" res_type = None code = code.replace("<|observation|>", "") @@ -159,12 +167,12 @@ def execute(code, kernel: CodeKernel) -> tuple[str, str | Image.Image]: code = code.replace("<|user|>", "") code = code.replace("<|system|>", "") msg, output = kernel.execute(code) - + if msg['metadata']['status'] == "timeout": return res_type, 'Timed out' elif msg['metadata']['status'] == 'error': return res_type, clean_ansi_codes('\n'.join(kernel.get_error_msg(msg, verbose=True))) - + if 'text' in output: res_type = "text" res = output['text'] @@ -177,17 +185,16 @@ def execute(code, kernel: CodeKernel) -> tuple[str, str | Image.Image]: elif 'text/plain' in key: res_type = "text" res = output['data'][key] - if res_type == "image": return res_type, b64_2_img(res) elif res_type == "text" or res_type == "traceback": res = res - + return res_type, res + def extract_code(text: str) -> str: pattern = r'```([^\n]*)\n(.*?)```' matches = re.findall(pattern, text, re.DOTALL) return matches[-1][1] - diff --git a/tool/comfyUI_api.py b/chatglm/comfyUI_api.py similarity index 69% rename from tool/comfyUI_api.py rename to chatglm/comfyUI_api.py index 033448a..e856f83 100644 --- a/tool/comfyUI_api.py +++ b/chatglm/comfyUI_api.py @@ -1,7 +1,8 @@ -#This is an example that uses the websockets api to know when a prompt execution is done -#Once the prompt execution is done it downloads the images using the /history endpoint +# This is an example that uses the websockets api to know when a prompt execution is done +# Once the prompt execution is done it downloads the images using the /history endpoint -import websocket #NOTE: websocket-client (https://github.com/websocket-client/websocket-client) +# NOTE: websocket-client (https://github.com/websocket-client/websocket-client) +import websocket import uuid import json import requests @@ -13,35 +14,39 @@ from PIL import Image class ComfyUIApi(): def __init__(self, server_address="127.0.0.1:8188"): - self.server_address=server_address - self.client_id=str(uuid.uuid4()) + self.server_address = server_address + self.client_id = str(uuid.uuid4()) self.ws = websocket.WebSocket() - self.ws.connect("ws://{}/ws?clientId={}".format(server_address, self.client_id)) + self.ws.connect( + "ws://{}/ws?clientId={}".format(server_address, self.client_id)) - def queue_prompt(self,prompt): + def queue_prompt(self, prompt): p = {"prompt": prompt, "client_id": self.client_id} data = json.dumps(p).encode('utf-8') - req = requests.post("http://{}/prompt".format(self.server_address), data=data) + req = requests.post( + "http://{}/prompt".format(self.server_address), data=data) print(req.text) return json.loads(req.text) - def get_image(self,filename, subfolder, folder_type): - data = {"filename": filename, "subfolder": subfolder, "type": folder_type} + def get_image(self, filename, subfolder, folder_type): + data = {"filename": filename, + "subfolder": subfolder, "type": folder_type} url_values = urllib.parse.urlencode(data) with requests.get("http://{}/view?{}".format(self.server_address, url_values)) as response: image = Image.open(io.BytesIO(response.content)) return image - def get_image_url(self,filename, subfolder, folder_type): - data = {"filename": filename, "subfolder": subfolder, "type": folder_type} + def get_image_url(self, filename, subfolder, folder_type): + data = {"filename": filename, + "subfolder": subfolder, "type": folder_type} url_values = urllib.parse.urlencode(data) return "http://{}/view?{}".format(self.server_address, url_values) - def get_history(self,prompt_id): + def get_history(self, prompt_id): with requests.get("http://{}/history/{}".format(self.server_address, prompt_id)) as response: return json.loads(response.text) - def get_images(self,prompt,isUrl=False): + def get_images(self, prompt, isUrl=False): prompt_id = self.queue_prompt(prompt)['prompt_id'] output_images = [] while True: @@ -51,9 +56,9 @@ class ComfyUIApi(): if message['type'] == 'executing': data = message['data'] if data['node'] is None and data['prompt_id'] == prompt_id: - break #Execution is done + break # Execution is done else: - continue #previews are binary data + continue # previews are binary data history = self.get_history(prompt_id)[prompt_id] for o in history['outputs']: @@ -61,12 +66,14 @@ class ComfyUIApi(): node_output = history['outputs'][node_id] if 'images' in node_output: for image in node_output['images']: - image_data =self.get_image_url(image['filename'], image['subfolder'], image['type']) if isUrl else self.get_image(image['filename'], image['subfolder'], image['type']) - image['image']=image_data + image_data = self.get_image_url(image['filename'], image['subfolder'], image['type']) if isUrl else self.get_image( + image['filename'], image['subfolder'], image['type']) + image['image'] = image_data output_images.append(image) return output_images + prompt_text = """ { "3": { @@ -157,16 +164,17 @@ prompt_text = """ """ if __name__ == '__main__': prompt = json.loads(prompt_text) - #set the text prompt for our positive CLIPTextEncode + # set the text prompt for our positive CLIPTextEncode prompt["6"]["inputs"]["text"] = "masterpiece best quality man" - #set the seed for our KSampler node - prompt["3"]["inputs"]["seed"] = ''.join(random.sample('123456789012345678901234567890',14)) + # set the seed for our KSampler node + prompt["3"]["inputs"]["seed"] = ''.join( + random.sample('123456789012345678901234567890', 14)) - cfui=ComfyUIApi() + cfui = ComfyUIApi() images = cfui.get_images(prompt) - #Commented out code to display the output images: + # Commented out code to display the output images: for node_id in images: for image_data in images[node_id]: @@ -174,4 +182,3 @@ if __name__ == '__main__': import io image = Image.open(io.BytesIO(image_data)) image.show() - diff --git a/tool/tool_registry.py b/chatglm/tool_registry.py similarity index 75% rename from tool/tool_registry.py rename to chatglm/tool_registry.py index 3a70ce8..5bdbdba 100644 --- a/tool/tool_registry.py +++ b/chatglm/tool_registry.py @@ -9,7 +9,7 @@ import requests import random import time import re -from tool.comfyUI_api import ComfyUIApi +from chatglm.comfyUI_api import ComfyUIApi from func_news import News from zhdate import ZhDate from datetime import datetime @@ -17,6 +17,7 @@ from datetime import datetime _TOOL_HOOKS = {} _TOOL_DESCRIPTIONS = {} + def extract_code(text: str) -> str: pattern = r'```([^\n]*)\n(.*?)```' matches = re.findall(pattern, text, re.DOTALL) @@ -33,8 +34,9 @@ def register_tool(func: callable): if annotation is inspect.Parameter.empty: raise TypeError(f"Parameter `{name}` missing type annotation") if get_origin(annotation) != Annotated: - raise TypeError(f"Annotation type for `{name}` must be typing.Annotated") - + raise TypeError( + f"Annotation type for `{name}` must be typing.Annotated") + typ, (description, required) = annotation.__origin__, annotation.__metadata__ typ: str = str(typ) if isinstance(typ, GenericAlias) else typ.__name__ if not isinstance(description, str): @@ -54,22 +56,24 @@ def register_tool(func: callable): "params": tool_params } - #print("[registered tool] " + pformat(tool_def)) + # print("[registered tool] " + pformat(tool_def)) _TOOL_HOOKS[tool_name] = func _TOOL_DESCRIPTIONS[tool_name] = tool_def return func + def dispatch_tool(tool_name: str, tool_params: dict) -> str: if tool_name not in _TOOL_HOOKS: return f"Tool `{tool_name}` not found. Please use a provided tool." tool_call = _TOOL_HOOKS[tool_name] try: - ret = tool_call(**tool_params) - except: + ret = tool_call(**tool_params) + except BaseException: ret = traceback.format_exc() return ret + def get_tools() -> dict: return deepcopy(_TOOL_DESCRIPTIONS) @@ -77,7 +81,7 @@ def get_tools() -> dict: # @register_tool # def random_number_generator( -# seed: Annotated[int, 'The random seed used by the generator', True], +# seed: Annotated[int, 'The random seed used by the generator', True], # range: Annotated[tuple[int, int], 'The range of the generated numbers', True], # ) -> int: # """ @@ -93,6 +97,7 @@ def get_tools() -> dict: # import random # return random.Random(seed).randint(*range) + @register_tool def get_weather( city_name: Annotated[str, 'The name of the city to be queried', True], @@ -104,34 +109,39 @@ def get_weather( raise TypeError("City name must be a string") key_selection = { - "current_condition": ["temp_C", "FeelsLikeC", "humidity", "weatherDesc", "observation_time"], + "current_condition": ["temp_C", "FeelsLikeC", "humidity", "weatherDesc", "observation_time"], } import requests try: resp = requests.get(f"https://wttr.in/{city_name}?format=j1") resp.raise_for_status() resp = resp.json() - ret = {k: {_v: resp[k][0][_v] for _v in v} for k, v in key_selection.items()} - except: + ret = {k: {_v: resp[k][0][_v] for _v in v} + for k, v in key_selection.items()} + except BaseException: import traceback - ret = "Error encountered while fetching weather data!\n" + traceback.format_exc() + ret = "Error encountered while fetching weather data!\n" + traceback.format_exc() return str(ret) + @register_tool def get_confyui_image(prompt: Annotated[str, '要生成图片的提示词,注意必须是英文', True]) -> dict: ''' 生成图片 ''' - with open("tool\\base.json", "r", encoding="utf-8") as f: + with open("chatglm\\base.json", "r", encoding="utf-8") as f: data2 = json.load(f) - data2['prompt']['3']['inputs']['seed']=''.join(random.sample('123456789012345678901234567890',14)) - data2['prompt']['4']['inputs']['ckpt_name']='chilloutmix_NiPrunedFp32Fix.safetensors' #模型名称 - data2['prompt']['6']['inputs']['text']=prompt #正向提示词 - #data2['prompt']['7']['inputs']['text']='' #反向提示词 - cfui=ComfyUIApi(server_address="127.0.0.1:8188") #根据自己comfyUI地址修改 + data2['prompt']['3']['inputs']['seed'] = ''.join( + random.sample('123456789012345678901234567890', 14)) + # 模型名称 + data2['prompt']['4']['inputs']['ckpt_name'] = 'chilloutmix_NiPrunedFp32Fix.safetensors' + data2['prompt']['6']['inputs']['text'] = prompt # 正向提示词 + # data2['prompt']['7']['inputs']['text']='' #反向提示词 + cfui = ComfyUIApi(server_address="127.0.0.1:8188") # 根据自己comfyUI地址修改 images = cfui.get_images(data2['prompt']) - return {'res':images[0]['image'],'res_type':'image','filename':images[0]['filename']} + return {'res': images[0]['image'], 'res_type': 'image', 'filename': images[0]['filename']} + @register_tool def get_news() -> str: @@ -141,16 +151,17 @@ def get_news() -> str: news = News() return news.get_important_news() + @register_tool def get_time() -> str: ''' 获取当前日期,时间,农历日期,星期几 ''' - time=datetime.now() + time = datetime.now() date2 = ZhDate.from_datetime(time) - week_list = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"] - - return '{} {} {}'.format(time.strftime("%Y年%m月%d日 %H:%M:%S"),week_list[time.weekday()],'农历:'+date2.chinese()) + week_list = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] + + return '{} {} {}'.format(time.strftime("%Y年%m月%d日 %H:%M:%S"), week_list[time.weekday()], '农历:' + date2.chinese()) if __name__ == "__main__": diff --git a/config.yaml.template b/config.yaml.template index 7de764e..4520880 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -54,12 +54,12 @@ report_reminder: # proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 # prompt: 你是智能聊天机器人,你叫wcferry # 根据需要对角色进行设定 -chatglm: - key: key - api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 - proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 - prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定 - file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 +#chatglm: +# key: key #暂时没有 +# api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 +# proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 +# prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定 +# file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 tigerbot: key: key diff --git a/func_chatglm.py b/func_chatglm.py index 13e2d9e..4cbb926 100644 --- a/func_chatglm.py +++ b/func_chatglm.py @@ -7,85 +7,84 @@ import openai import json import os import random -from tool.tool_registry import get_tools, dispatch_tool,extract_code -from tool.code_kernel import execute,CodeKernel -from wcferry import Wcf, WxMsg +from chatglm.tool_registry import get_tools, dispatch_tool, extract_code +from chatglm.code_kernel import execute, CodeKernel +from typing import Dict, Union, Optional, Tuple +from wcferry import Wcf + +functions = get_tools() -functions=get_tools() class ChatGLM(): - def __init__(self, wcf: Wcf, config={},max_retry=5) -> None: - openai.api_key = config.get('key') + def __init__(self, config={}, wcf: Optional[Wcf] = None, max_retry=5) -> None: + openai.api_key = config.get('key', 'XXX') # 自己搭建或第三方代理的接口 - openai.api_base = config.get('api') - if config.get('proxy',None): - openai.proxy = {"http": config.get('proxy',None), "https": config.get('proxy',None)} + openai.api_base = config.get('api', 'http://localhost:8000/v1') + if config.get('proxy', None): + openai.proxy = {"http": config.get( + 'proxy', None), "https": config.get('proxy', None)} self.conversation_list = {} - self.chat_type={} - self.max_retry=max_retry - self.wcf=wcf - self.filePath=config.get('file_path') + self.chat_type = {} + self.max_retry = max_retry + self.wcf = wcf + self.filePath = config.get('file_path', 'temp') self.kernel = CodeKernel() - self.system_content_msg = {"chat":[{"role": "system", "content": config.get('prompt')}], - "tool":[{"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:"}], - "code":[{"role": "system", "content": "你是一位智能AI助手,你叫ChatGLM,你连接着一台电脑,但请注意不能联网。在使用Python解决任务时,你可以运行代码并得到结果,如果运行结果有错误,你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件,文件默认存储路径是{}。".format(self.filePath)}]} - code0=''' -import matplotlib.pyplot as plt -import numpy as np -x = np.linspace(-1, 1, 50) -y = x * x + 1 -plt.plot(x, y) -plt.show() - ''' - res_type, res = execute(code0, self.kernel) #第一次画图不返回图片问题 - print(res_type, res) - + self.system_content_msg = {"chat": [{"role": "system", "content": config.get('prompt', '你是智能聊天机器人,你叫小薇')}], + "tool": [{"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:"}], + "code": [{"role": "system", "content": "你是一位智能AI助手,你叫ChatGLM,你连接着一台电脑,但请注意不能联网。在使用Python解决任务时,你可以运行代码并得到结果,如果运行结果有错误,你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件,文件默认存储路径是{}。".format(self.filePath)}]} + def get_answer(self, question: str, wxid: str) -> str: # wxid或者roomid,个人时为微信id,群消息时为群id - if '#帮助'==question: + if '#帮助' == question: return '本助手有三种模式,#聊天模式 = #1 ,#工具模式 = #2 ,#代码模式 = #3 , #清除模式会话 = #4 , #清除全部会话 = #5 可用发送#对应模式 或者 #编号 进行切换' - elif '#聊天模式'==question or '#1'==question: - self.chat_type[wxid]='chat' + elif '#聊天模式' == question or '#1' == question: + self.chat_type[wxid] = 'chat' return '已切换#聊天模式' - elif '#工具模式'==question or '#2'==question: - self.chat_type[wxid]='tool' + elif '#工具模式' == question or '#2' == question: + self.chat_type[wxid] = 'tool' return '已切换#工具模式 \n工具有:查看天气,日期,新闻,comfyUI文生图。例如:\n帮我生成一张小鸟的图片,提示词必须是英文' - elif '#代码模式'==question or '#3'==question: - self.chat_type[wxid]='code' + elif '#代码模式' == question or '#3' == question: + self.chat_type[wxid] = 'code' return '已切换#代码模式 \n代码模式可以用于写python代码,例如:\n用python画一个爱心' - elif '#清除模式会话'==question or '#4'==question: - self.conversation_list[wxid][self.chat_type[wxid]]=self.system_content_msg[self.chat_type[wxid]] + elif '#清除模式会话' == question or '#4' == question: + self.conversation_list[wxid][self.chat_type[wxid] + ] = self.system_content_msg[self.chat_type[wxid]] return '已清除' - elif '#清除全部会话'==question or '#5'==question: - self.conversation_list[wxid]=self.system_content_msg + elif '#清除全部会话' == question or '#5' == question: + self.conversation_list[wxid] = self.system_content_msg return '已清除' - + self.updateMessage(wxid, question, "user") try: - params = dict(model="chatglm3",temperature=1.0, messages=self.conversation_list[wxid][self.chat_type[wxid]], stream=False) - if 'tool'==self.chat_type[wxid]: + params = dict(model="chatglm3", temperature=1.0, + messages=self.conversation_list[wxid][self.chat_type[wxid]], stream=False) + if 'tool' == self.chat_type[wxid]: params["functions"] = functions response = openai.ChatCompletion.create(**params) for _ in range(self.max_retry): if response.choices[0].message.get("function_call"): function_call = response.choices[0].message.function_call - print(f"Function Call Response: {function_call.to_dict_recursive()}") + print( + f"Function Call Response: {function_call.to_dict_recursive()}") function_args = json.loads(function_call.arguments) - observation = dispatch_tool(function_call.name, function_args) - if isinstance(observation,dict): - res_type=observation['res_type'] if 'res_type' in observation else 'text' - res=observation['res'] if 'res_type' in observation else str(observation ) + observation = dispatch_tool( + function_call.name, function_args) + if isinstance(observation, dict): + res_type = observation['res_type'] if 'res_type' in observation else 'text' + res = observation['res'] if 'res_type' in observation else str( + observation) if res_type == 'image': - filename= observation['filename'] - filePath=os.path.join(self.filePath,filename) + filename = observation['filename'] + filePath = os.path.join(self.filePath, filename) res.save(filePath) - self.wcf.send_image(filePath,wxid) - tool_response='[Image]' if res_type == 'image' else res + self.wcf and self.wcf.send_image(filePath, wxid) + tool_response = '[Image]' if res_type == 'image' else res else: - tool_response=observation if isinstance(observation,str) else str(observation) + tool_response = observation if isinstance( + observation, str) else str(observation) print(f"Tool Call Response: {tool_response}") params["messages"].append(response.choices[0].message) @@ -98,24 +97,25 @@ plt.show() ) self.updateMessage(wxid, tool_response, "function") response = openai.ChatCompletion.create(**params) - elif response.choices[0].message.content.find('interpreter')!=-1: - output_text=response.choices[0].message.content + elif response.choices[0].message.content.find('interpreter') != -1: + output_text = response.choices[0].message.content code = extract_code(output_text) - self.wcf.send_text('代码如下:\n'+code,wxid) - self.wcf.send_text('执行代码...',wxid) + self.wcf and self.wcf.send_text('代码如下:\n' + code, wxid) + self.wcf and self.wcf.send_text('执行代码...', wxid) try: res_type, res = execute(code, self.kernel) except Exception as e: - rsp=f'代码执行错误: {e}' + rsp = f'代码执行错误: {e}' break if res_type == 'image': - filename= '{}.png'.format(''.join(random.sample('abcdefghijklmnopqrstuvwxyz1234567890',8))) - filePath=os.path.join(self.filePath,filename) + filename = '{}.png'.format(''.join(random.sample( + 'abcdefghijklmnopqrstuvwxyz1234567890', 8))) + filePath = os.path.join(self.filePath, filename) res.save(filePath) - self.wcf.send_image(filePath,wxid) + self.wcf and self.wcf.send_image(filePath, wxid) else: - self.wcf.send_text("执行结果:\n"+res,wxid) - tool_response='[Image]' if res_type == 'image' else res + self.wcf and self.wcf.send_text("执行结果:\n" + res, wxid) + tool_response = '[Image]' if res_type == 'image' else res print("Received:", res_type, res) params["messages"].append(response.choices[0].message) params["messages"].append( @@ -144,11 +144,12 @@ plt.show() if wxid not in self.conversation_list.keys(): self.conversation_list[wxid] = self.system_content_msg if wxid not in self.chat_type.keys(): - self.chat_type[wxid]='chat' + self.chat_type[wxid] = 'chat' # 当前问题 content_question_ = {"role": role, "content": question} - self.conversation_list[wxid][self.chat_type[wxid]].append(content_question_) + self.conversation_list[wxid][self.chat_type[wxid]].append( + content_question_) # 只存储10条记录,超过滚动清除 i = len(self.conversation_list[wxid][self.chat_type[wxid]]) @@ -160,16 +161,11 @@ plt.show() if __name__ == "__main__": from configuration import Config - config = Config().CHATGPT + config = Config().CHATGLM if not config: exit(0) - key = config.get("key") - api = config.get("api") - proxy = config.get("proxy") - prompt = config.get("prompt") - - chat = ChatGLM(key, api, proxy, prompt) + chat = ChatGLM(config) while True: q = input(">>> ") @@ -178,6 +174,7 @@ if __name__ == "__main__": print(chat.get_answer(q, "wxid")) time_end = datetime.now() # 记录结束时间 - print(f"{round((time_end - time_start).total_seconds(), 2)}s") # 计算的时间差为程序的执行时间,单位为秒/s + # 计算的时间差为程序的执行时间,单位为秒/s + print(f"{round((time_end - time_start).total_seconds(), 2)}s") except Exception as e: print(e) diff --git a/requirements.txt b/requirements.txt index 0394075..e82bf35 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,7 @@ schedule pyhandytools sparkdesk-api==1.3.0 wcferry>=39.0.3.0 +websocket +pillow +jupyter_client +zhdate diff --git a/robot.py b/robot.py index 53096ad..2447807 100644 --- a/robot.py +++ b/robot.py @@ -38,7 +38,7 @@ class Robot(Job): elif self.config.XINGHUO_WEB: self.chat = XinghuoWeb(self.config.XINGHUO_WEB) elif self.config.CHATGLM: - self.chat = ChatGLM(wcf, self.config.CHATGLM) + self.chat = ChatGLM(self.config.CHATGLM,wcf) else: self.chat = None From 3c3bb4748be00ad3863c55d46e37105a6ffa8c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=AA?= <757078144@qq.com> Date: Wed, 15 Nov 2023 21:10:39 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=8E=92=E5=88=97?= =?UTF-8?q?=E4=B8=80=E4=B8=8Bimport=EF=BC=8C=E6=B7=BB=E5=8A=A0=20ipykernel?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chatglm/code_kernel.py | 9 +++++---- chatglm/comfyUI_api.py | 16 +++++++++------- chatglm/tool_registry.py | 20 +++++++++++--------- func_chatglm.py | 13 +++++++------ requirements.txt | 1 + 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/chatglm/code_kernel.py b/chatglm/code_kernel.py index 48ab6a7..cc4c6ec 100644 --- a/chatglm/code_kernel.py +++ b/chatglm/code_kernel.py @@ -1,14 +1,15 @@ import base64 -from io import BytesIO import os -from pprint import pprint import queue import re +import time +from io import BytesIO +from pprint import pprint from subprocess import PIPE -from typing import Dict, Union, Optional, Tuple +from typing import Dict, Optional, Tuple, Union + import jupyter_client from PIL import Image -import time IPYKERNEL = os.environ.get('IPYKERNEL', 'chatglm3') diff --git a/chatglm/comfyUI_api.py b/chatglm/comfyUI_api.py index e856f83..aefe6cc 100644 --- a/chatglm/comfyUI_api.py +++ b/chatglm/comfyUI_api.py @@ -1,14 +1,15 @@ # This is an example that uses the websockets api to know when a prompt execution is done # Once the prompt execution is done it downloads the images using the /history endpoint +import io +import json +import random +import urllib +import uuid + +import requests # NOTE: websocket-client (https://github.com/websocket-client/websocket-client) import websocket -import uuid -import json -import requests -import urllib -import random -import io from PIL import Image @@ -178,7 +179,8 @@ if __name__ == '__main__': for node_id in images: for image_data in images[node_id]: - from PIL import Image import io + + from PIL import Image image = Image.open(io.BytesIO(image_data)) image.show() diff --git a/chatglm/tool_registry.py b/chatglm/tool_registry.py index 5bdbdba..8758b16 100644 --- a/chatglm/tool_registry.py +++ b/chatglm/tool_registry.py @@ -1,18 +1,20 @@ -from copy import deepcopy import inspect -from pprint import pformat -import traceback -from types import GenericAlias -from typing import get_origin, Annotated import json -import requests import random -import time import re -from chatglm.comfyUI_api import ComfyUIApi +import time +import traceback +from copy import deepcopy +from datetime import datetime +from pprint import pformat +from types import GenericAlias +from typing import Annotated, get_origin + +import requests from func_news import News from zhdate import ZhDate -from datetime import datetime + +from chatglm.comfyUI_api import ComfyUIApi _TOOL_HOOKS = {} _TOOL_DESCRIPTIONS = {} diff --git a/func_chatglm.py b/func_chatglm.py index 4cbb926..9fe57f3 100644 --- a/func_chatglm.py +++ b/func_chatglm.py @@ -1,17 +1,18 @@ #! /usr/bin/env python3 # -*- coding: utf-8 -*- -from datetime import datetime - -import openai import json import os import random -from chatglm.tool_registry import get_tools, dispatch_tool, extract_code -from chatglm.code_kernel import execute, CodeKernel -from typing import Dict, Union, Optional, Tuple +from datetime import datetime +from typing import Dict, Optional, Tuple, Union + +import openai from wcferry import Wcf +from chatglm.code_kernel import CodeKernel, execute +from chatglm.tool_registry import dispatch_tool, extract_code, get_tools + functions = get_tools() diff --git a/requirements.txt b/requirements.txt index e82bf35..5950519 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,4 @@ websocket pillow jupyter_client zhdate +ipykernel \ No newline at end of file