Add ChatGPT

This commit is contained in:
Changhua
2023-03-04 23:21:19 +08:00
parent eea263d85a
commit 1c06d0758f
5 changed files with 69 additions and 17 deletions

View File

@@ -40,3 +40,6 @@ logging:
groups:
enable: [] # 允许响应的群 roomId可以通过 WxGetContacts 获取
chatgpt:
key: your key

View File

@@ -27,3 +27,4 @@ class Config(object):
yconfig = self._load_config()
logging.config.dictConfig(yconfig["logging"])
self.GROUPS = yconfig["groups"]["enable"]
self.CHAT_KEY = yconfig["chatgpt"]["key"]

40
func_chatgpt.py Normal file
View File

@@ -0,0 +1,40 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import openai
class ChatGPT():
def __init__(self, key) -> None:
openai.api_key = key
def get_answer(self, question: str) -> str:
try:
ret = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": question},
],
temperature=0.5,
)
rsp = ret["choices"][0]["message"]["content"]
rsp = rsp[2:] if rsp.startswith("\n\n") else rsp
rsp = rsp.replace("\n\n", "\n")
except Exception as e:
ret = ""
return rsp
if __name__ == "__main__":
from datetime import datetime
chat = ChatGPT("your key")
while True:
q = input(">>> ")
try:
t1 = datetime.now()
print(chat.get_answer(q))
t2 = datetime.now()
print(f"{t2-t1}s")
except Exception as e:
print(e)

View File

@@ -23,7 +23,7 @@ def weather_report(robot: Robot) -> None:
def main():
wcf = Wcf()
wcf = Wcf(debug=True)
def handler(sig, frame):
wcf.cleanup() # 退出前清理环境

View File

@@ -9,6 +9,7 @@ from wcferry import Wcf
from configuration import Config
from func_chengyu import cy
from func_chatgpt import ChatGPT
from job_mgmt import Job
@@ -22,6 +23,7 @@ class Robot(Job):
self.LOG = logging.getLogger("Robot")
self.wxid = self.wcf.get_self_wxid()
self.allContacts = self.getAllContacts()
self.chat = ChatGPT(self.config.CHAT_KEY)
def toAt(self, msg: Wcf.WxMsg) -> bool:
"""
@@ -29,11 +31,7 @@ class Robot(Job):
:param msg: 微信消息结构
:return: 处理状态,`True` 成功,`False` 失败
"""
status = True
rsp = "你@我干嘛?"
self.sendTextMsg(rsp, msg.roomid, msg.sender)
return status
return self.toChitchat(msg)
def toChengyu(self, msg: Wcf.WxMsg) -> bool:
"""
@@ -62,10 +60,21 @@ class Robot(Job):
return status
def toChitchat(self, msg: Wcf.WxMsg) -> None:
"""闲聊,目前未实现
def toChitchat(self, msg: Wcf.WxMsg) -> bool:
"""闲聊,接入 ChatGPT
"""
pass
q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "")
rsp = self.chat.get_answer(q)
if rsp:
if msg.from_group():
self.sendTextMsg(rsp, msg.roomid, msg.sender)
else:
self.sendTextMsg(rsp, msg.sender)
return True
else:
self.LOG.error(f"无法从ChatGPT获得答案")
return False
def processMsg(self, msg: Wcf.WxMsg) -> None:
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
@@ -88,8 +97,10 @@ class Robot(Job):
else: # 其他消息
self.toChengyu(msg)
# 非群聊信息
elif msg.type == 37: # 好友请求
return # 处理完群聊信息,后面就不需要处理了
# 非群聊信息,按消息类型进行处理
if msg.type == 37: # 好友请求
self.autoAcceptFriendRequest(msg)
elif msg.type == 10000: # 系统信息
@@ -100,15 +111,12 @@ class Robot(Job):
if msg.from_self() and msg.content == "^更新$":
self.config.reload()
self.LOG.info("已更新")
return
# 闲聊
self.toChitchat(msg)
else:
self.toChitchat(msg) # 闲聊
def onMsg(self, msg: Wcf.WxMsg) -> int:
self.LOG.info(msg) # 打印信息
try:
self.LOG.info(msg) # 打印信息
self.processMsg(msg)
except Exception as e:
self.LOG.error(e)