Skip to content

Commit 8f72e8c

Browse files
committed
formatting code
1 parent 3b8972c commit 8f72e8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1843
-1181
lines changed

.github/ISSUE_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
### 环境
2828

2929
- 操作系统类型 (Mac/Windows/Linux):
30-
- Python版本 ( 执行 `python3 -V` ):
30+
- Python版本 ( 执行 `python3 -V` ):
3131
- pip版本 ( 依赖问题此项必填,执行 `pip3 -V`):

.github/workflows/deploy-image.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ jobs:
4949
file: ./docker/Dockerfile.latest
5050
tags: ${{ steps.meta.outputs.tags }}
5151
labels: ${{ steps.meta.outputs.labels }}
52-
52+
5353
- uses: actions/delete-package-versions@v4
54-
with:
54+
with:
5555
package-name: 'chatgpt-on-wechat'
5656
package-type: 'container'
5757
min-versions-to-keep: 10

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ pip3 install azure-cognitiveservices-speech
120120

121121
```bash
122122
# config.json文件内容示例
123-
{
123+
{
124124
"open_ai_api_key": "YOUR API KEY", # 填入上面创建的 OpenAI API KEY
125125
"model": "gpt-3.5-turbo", # 模型名称。当use_azure_chatgpt为true时,其名称为Azure上model deployment名称
126126
"proxy": "127.0.0.1:7890", # 代理客户端的ip和端口
127127
"single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复
128128
"single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人
129129
"group_chat_prefix": ["@bot"], # 群聊时包含该前缀则会触发机器人回复
130130
"group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], # 开启自动回复的群名称列表
131-
"group_chat_in_one_session": ["ChatGPT测试群"], # 支持会话上下文共享的群名称
131+
"group_chat_in_one_session": ["ChatGPT测试群"], # 支持会话上下文共享的群名称
132132
"image_create_prefix": ["", "", ""], # 开启图片回复的前缀
133133
"conversation_max_tokens": 1000, # 支持上下文记忆的最多字符数
134134
"speech_recognition": false, # 是否开启语音识别
@@ -160,7 +160,7 @@ pip3 install azure-cognitiveservices-speech
160160
**4.其他配置**
161161

162162
+ `model`: 模型名称,目前支持 `gpt-3.5-turbo`, `text-davinci-003`, `gpt-4`, `gpt-4-32k` (其中gpt-4 api暂未开放)
163-
+ `temperature`,`frequency_penalty`,`presence_penalty`: Chat API接口参数,详情参考[OpenAI官方文档。](https://platform.openai.com/docs/api-reference/chat)
163+
+ `temperature`,`frequency_penalty`,`presence_penalty`: Chat API接口参数,详情参考[OpenAI官方文档。](https://platform.openai.com/docs/api-reference/chat)
164164
+ `proxy`:由于目前 `openai` 接口国内无法访问,需配置代理客户端的地址,详情参考 [#351](https://github.com/zhayujie/chatgpt-on-wechat/issues/351)
165165
+ 对于图像生成,在满足个人或群组触发条件外,还需要额外的关键词前缀来触发,对应配置 `image_create_prefix `
166166
+ 关于OpenAI对话及图片接口的参数配置(内容自由度、回复字数限制、图片大小等),可以参考 [对话接口](https://beta.openai.com/docs/api-reference/completions)[图像接口](https://beta.openai.com/docs/api-reference/completions) 文档直接在 [代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/bot/openai/open_ai_bot.py) `bot/openai/open_ai_bot.py` 中进行调整。
@@ -181,15 +181,15 @@ pip3 install azure-cognitiveservices-speech
181181
```bash
182182
python3 app.py
183183
```
184-
终端输出二维码后,使用微信进行扫码,当输出 "Start auto replying" 时表示自动回复程序已经成功运行了(注意:用于登录的微信需要在支付处已完成实名认证)。扫码登录后你的账号就成为机器人了,可以在微信手机端通过配置的关键词触发自动回复 (任意好友发送消息给你,或是自己发消息给好友),参考[#142](https://github.com/zhayujie/chatgpt-on-wechat/issues/142)
184+
终端输出二维码后,使用微信进行扫码,当输出 "Start auto replying" 时表示自动回复程序已经成功运行了(注意:用于登录的微信需要在支付处已完成实名认证)。扫码登录后你的账号就成为机器人了,可以在微信手机端通过配置的关键词触发自动回复 (任意好友发送消息给你,或是自己发消息给好友),参考[#142](https://github.com/zhayujie/chatgpt-on-wechat/issues/142)
185185

186186

187187
### 2.服务器部署
188188

189189
使用nohup命令在后台运行程序:
190190

191191
```bash
192-
touch nohup.out # 首次运行需要新建日志文件
192+
touch nohup.out # 首次运行需要新建日志文件
193193
nohup python3 app.py & tail -f nohup.out # 在后台运行程序并通过日志输出二维码
194194
```
195195
扫码登录后程序即可运行于服务器后台,此时可通过 `ctrl+c` 关闭日志,不会影响后台程序的运行。使用 `ps -ef | grep app.py | grep -v grep` 命令可查看运行于后台的进程,如果想要重新启动程序可以先 `kill` 掉对应的进程。日志关闭后如果想要再次打开只需输入 `tail -f nohup.out`。此外,`scripts` 目录下有一键运行、关闭程序的脚本供使用。

app.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
# encoding:utf-8
22

33
import os
4-
from config import conf, load_config
4+
import signal
5+
import sys
6+
57
from channel import channel_factory
68
from common.log import logger
9+
from config import conf, load_config
710
from plugins import *
8-
import signal
9-
import sys
11+
1012

1113
def sigterm_handler_wrap(_signo):
1214
old_handler = signal.getsignal(_signo)
15+
1316
def func(_signo, _stack_frame):
1417
logger.info("signal {} received, exiting...".format(_signo))
1518
conf().save_user_datas()
16-
if callable(old_handler): # check old_handler
19+
if callable(old_handler): # check old_handler
1720
return old_handler(_signo, _stack_frame)
1821
sys.exit(0)
22+
1923
signal.signal(_signo, func)
2024

25+
2126
def run():
2227
try:
2328
# load config
@@ -28,17 +33,17 @@ def run():
2833
sigterm_handler_wrap(signal.SIGTERM)
2934

3035
# create channel
31-
channel_name=conf().get('channel_type', 'wx')
36+
channel_name = conf().get("channel_type", "wx")
3237

3338
if "--cmd" in sys.argv:
34-
channel_name = 'terminal'
39+
channel_name = "terminal"
3540

36-
if channel_name == 'wxy':
37-
os.environ['WECHATY_LOG']="warn"
41+
if channel_name == "wxy":
42+
os.environ["WECHATY_LOG"] = "warn"
3843
# os.environ['WECHATY_PUPPET_SERVICE_ENDPOINT'] = '127.0.0.1:9001'
3944

4045
channel = channel_factory.create_channel(channel_name)
41-
if channel_name in ['wx','wxy','terminal','wechatmp','wechatmp_service']:
46+
if channel_name in ["wx", "wxy", "terminal", "wechatmp", "wechatmp_service"]:
4247
PluginManager().load_plugins()
4348

4449
# startup channel
@@ -47,5 +52,6 @@ def run():
4752
logger.error("App startup failed!")
4853
logger.exception(e)
4954

50-
if __name__ == '__main__':
51-
run()
55+
56+
if __name__ == "__main__":
57+
run()

bot/baidu/baidu_unit_bot.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# encoding:utf-8
22

33
import requests
4+
45
from bot.bot import Bot
56
from bridge.reply import Reply, ReplyType
67

@@ -9,20 +10,35 @@
910
class BaiduUnitBot(Bot):
1011
def reply(self, query, context=None):
1112
token = self.get_token()
12-
url = 'https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=' + token
13-
post_data = "{\"version\":\"3.0\",\"service_id\":\"S73177\",\"session_id\":\"\",\"log_id\":\"7758521\",\"skill_ids\":[\"1221886\"],\"request\":{\"terminal_id\":\"88888\",\"query\":\"" + query + "\", \"hyper_params\": {\"chat_custom_bot_profile\": 1}}}"
13+
url = (
14+
"https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token="
15+
+ token
16+
)
17+
post_data = (
18+
'{"version":"3.0","service_id":"S73177","session_id":"","log_id":"7758521","skill_ids":["1221886"],"request":{"terminal_id":"88888","query":"'
19+
+ query
20+
+ '", "hyper_params": {"chat_custom_bot_profile": 1}}}'
21+
)
1422
print(post_data)
15-
headers = {'content-type': 'application/x-www-form-urlencoded'}
23+
headers = {"content-type": "application/x-www-form-urlencoded"}
1624
response = requests.post(url, data=post_data.encode(), headers=headers)
1725
if response:
18-
reply = Reply(ReplyType.TEXT, response.json()['result']['context']['SYS_PRESUMED_HIST'][1])
26+
reply = Reply(
27+
ReplyType.TEXT,
28+
response.json()["result"]["context"]["SYS_PRESUMED_HIST"][1],
29+
)
1930
return reply
2031

2132
def get_token(self):
22-
access_key = 'YOUR_ACCESS_KEY'
23-
secret_key = 'YOUR_SECRET_KEY'
24-
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + access_key + '&client_secret=' + secret_key
33+
access_key = "YOUR_ACCESS_KEY"
34+
secret_key = "YOUR_SECRET_KEY"
35+
host = (
36+
"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="
37+
+ access_key
38+
+ "&client_secret="
39+
+ secret_key
40+
)
2541
response = requests.get(host)
2642
if response:
2743
print(response.json())
28-
return response.json()['access_token']
44+
return response.json()["access_token"]

bot/bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class Bot(object):
11-
def reply(self, query, context : Context =None) -> Reply:
11+
def reply(self, query, context: Context = None) -> Reply:
1212
"""
1313
bot auto-reply content
1414
:param req: received message

bot/bot_factory.py

+4
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,24 @@ def create_bot(bot_type):
1313
if bot_type == const.BAIDU:
1414
# Baidu Unit对话接口
1515
from bot.baidu.baidu_unit_bot import BaiduUnitBot
16+
1617
return BaiduUnitBot()
1718

1819
elif bot_type == const.CHATGPT:
1920
# ChatGPT 网页端web接口
2021
from bot.chatgpt.chat_gpt_bot import ChatGPTBot
22+
2123
return ChatGPTBot()
2224

2325
elif bot_type == const.OPEN_AI:
2426
# OpenAI 官方对话模型API
2527
from bot.openai.open_ai_bot import OpenAIBot
28+
2629
return OpenAIBot()
2730

2831
elif bot_type == const.CHATGPTONAZURE:
2932
# Azure chatgpt service https://azure.microsoft.com/en-in/products/cognitive-services/openai-service/
3033
from bot.chatgpt.chat_gpt_bot import AzureChatGPTBot
34+
3135
return AzureChatGPTBot()
3236
raise RuntimeError

0 commit comments

Comments
 (0)