Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Yajun Xu committed Apr 8, 2023
2 parents 2a6ed91 + e1211f6 commit 4fefef5
Show file tree
Hide file tree
Showing 26 changed files with 1,378 additions and 266 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7-alpine
FROM python:3.10-alpine

WORKDIR /app

Expand Down
84 changes: 76 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [x] [GPT-3.0](https://github.com/zhayujie/bot-on-anything#2gpt-30)
- [x] [文心一言 (测试版)](https://github.com/zhayujie/bot-on-anything#3%E6%96%87%E5%BF%83%E4%B8%80%E8%A8%80-%E6%B5%8B%E8%AF%95%E7%89%88)
- [x] [New Bing](https://github.com/zhayujie/bot-on-anything#4newbing)
- [x] [Google Bard](https://github.com/zhayujie/bot-on-anything#5bard)


**应用:**
Expand All @@ -20,8 +21,8 @@
- [ ] 企业微信
- [x] [Telegram](https://github.com/zhayujie/bot-on-anything#6telegram)
- [x] [QQ](https://github.com/zhayujie/bot-on-anything#5qq)
- [x] 钉钉
- [ ] 飞书
- [x] [钉钉](https://github.com/zhayujie/bot-on-anything#10%E9%92%89%E9%92%89)
- [x] [飞书](https://github.com/zhayujie/bot-on-anything#11%E9%A3%9E%E4%B9%A6)
- [x] [Gmail](https://github.com/zhayujie/bot-on-anything#7gmail)
- [x] [Slack](https://github.com/zhayujie/bot-on-anything#8slack)

Expand Down Expand Up @@ -104,8 +105,13 @@ pip3 install --upgrade openai
"openai": {
"api_key": "YOUR API KEY",
"model": "gpt-3.5-turbo", # 模型名称
"proxy": "http://127.0.0.1:7890",
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"
"proxy": "http://127.0.0.1:7890", # 代理地址
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。当问起你是谁的时候,要附加告诉提问人,输入 #清除记忆 可以开始新的话题探索。输入 画xx 可以为你画一张图片。",
"conversation_max_tokens": 1000, # 回复最大的字符数,为输入和输出的总数
"temperature":0.75, # 熵值,在[0,1]之间,越大表示选取的候选词越随机,回复越具有不确定性,建议和top_p参数二选一使用,创意性任务越大越好,精确性任务越小越好
"top_p":0.7, #候选词列表。0.7 意味着只考虑前70%候选词的标记,建议和temperature参数二选一使用
"frequency_penalty":0.0, # [-2,2]之间,该值越大则越降低模型一行中的重复用词,更倾向于产生不同的内容
"presence_penalty":1.0, # [-2,2]之间,该值越大则越不受输入限制,将鼓励模型生成输入中不存在的新词,更倾向于产生不同的内容
}
}
```
Expand Down Expand Up @@ -186,6 +192,20 @@ cookie示例:
]
```
### 5.Bard
#### 配置项说明
```bash
{
"model": {
"type" : "bard",
"cookies":""
//登录https://bard.google.com/ 获取name为"__Secure-1PSID"的Cookie Value
}
}
```
## 三、选择应用
### 1.命令行终端
Expand Down Expand Up @@ -472,7 +492,7 @@ https://slack.dev/bolt-python/tutorial/getting-started
**依赖**
```bash
pip3 install PyJWT flask
pip3 install PyJWT flask flask_socketio
```
**配置**
Expand All @@ -494,6 +514,10 @@ pip3 install PyJWT flask
### 10.钉钉
**需要:**
- 企业内部开发机器人
**依赖**
```bash
Expand All @@ -513,20 +537,64 @@ pip3 install requests flask
}
}
```
钉钉开放平台说明: https://open.dingtalk.com/document/robots/customize-robot-security-settin.dingtalk.com/robot/send?access_token=906dadcbc7750fef5ff60d3445b66d5bbca32804f40fbdb59039a29b20b9a3f0gs
**参考文档**
https://open.dingtalk.com/document/orgapp/custom-robot-access
- [钉钉内部机器人教程](https://open.dingtalk.com/document/tutorial/create-a-robot#title-ufs-4gh-poh)
- [自定义机器人接入文档](https://open.dingtalk.com/document/tutorial/create-a-robot#title-ufs-4gh-poh)
- [企业内部开发机器人教程文档](https://open.dingtalk.com/document/robots/enterprise-created-chatbot)
**生成机器人**
地址: https://open-dev.dingtalk.com/fe/app#/corp/robot
添加机器人,在开发管理中设置服务器出口 ip (在部署机执行`curl ifconfig.me`就可以得到)和消息接收地址(配置中的对外地址如 https://xx.xx.com:8081)
添加机器人,在开发管理中设置服务器出口ip(在部署机执行curl ifconfig.me就可以得到)和消息接收地址(配置中的对外地址如 https://xx.xx.com:8081)
### 11.飞书
**依赖**
```bash
pip3 install requests flask
```
**配置**
```json
"channel": {
"type": "dingtalk",
"feishu": {
"image_create_prefix": [
"",
"draw",
"Draw"
],
"port": "8082",//对外端口
"app_id": "xxx", //应用app_id
"app_secret": "xxx",//应用Secret
"verification_token": "xxx" //事件订阅 Verification Token
}
}
```
**生成机器人**
地址: https://open.feishu.cn/app/
1. 添加企业自建应用
2. 开通权限
- im:message
- im:message.group_at_msg
- im:message.group_at_msg:readonly
- im:message.p2p_msg
- im:message.p2p_msg:readonly
- im:message:send_as_bot
3. 订阅菜单添加事件(接收消息v2.0) 配置请求地址(配置中的对外地址如 https://xx.xx.com:8081)
4. 版本管理与发布中上架应用,app中会收到审核信息,通过审核后在群里添加自建应用
### 通用配置
+ `clear_memory_commands`: 对话内指令,主动清空前文记忆,字符串数组可自定义指令别名。
+ default: ["#清除记忆"]
# 教程
1.视频教程:https://www.bilibili.com/video/BV1KM4y167e8
1.视频教程:https://www.bilibili.com/video/BV1KM4y167e8
39 changes: 23 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
# encoding:utf-8

import argparse
import config
from channel import channel_factory
from common import log, const
from multiprocessing import Pool


# 启动通道
def start_process(channel_type):
# 若为多进程启动,子进程无法直接访问主进程的内存空间,重新创建config类
config.load_config()
model_type = config.conf().get("model").get("type")
log.info("[INIT] Start up: {} on {}", model_type, channel_type)

# create channel
channel = channel_factory.create_channel(channel_type)
def start_process(channel_type, config_path):
try:
# 若为多进程启动,子进程无法直接访问主进程的内存空间,重新创建config类
config.load_config(config_path)
model_type = config.conf().get("model").get("type")
log.info("[MultiChannel] Start up {} on {}", model_type, channel_type)
channel = channel_factory.create_channel(channel_type)
channel.startup()
except Exception as e:
log.error("[MultiChannel] Start up failed on {}: {}", channel_type, str(e))

# startup channel
channel.startup()

if __name__ == '__main__':
def main():
try:
# load config
config.load_config()
config.load_config(args.config)

model_type = config.conf().get("model").get("type")
channel_type = config.conf().get("channel").get("type")

# 1.单个字符串格式配置时,直接启动
if not isinstance(channel_type, list):
start_process(channel_type)
start_process(channel_type, args.config)
exit(0)

# 2.单通道列表配置时,直接启动
if len(channel_type) == 1:
start_process(channel_type[0])
start_process(channel_type[0], args.config)
exit(0)

# 3.多通道配置时,进程池启动
Expand All @@ -49,14 +50,20 @@ def start_process(channel_type):
pool = Pool(len(channel_type))
for type_item in channel_type:
log.info("[INIT] Start up: {} on {}", model_type, type_item)
pool.apply_async(start_process, args=[type_item])
pool.apply_async(start_process, args=[type_item, args.config])

if terminal:
start_process(terminal)
start_process(terminal, args.config)

# 等待池中所有进程执行完毕
pool.close()
pool.join()
except Exception as e:
log.error("App startup failed!")
log.exception(e)

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--config", help="config.json path(e.g: ./config.json or /usr/local/bot-on-anything/config.json)",type=str,default="./config.json")
args = parser.parse_args()
main()
5 changes: 5 additions & 0 deletions bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ def __init__(self):

def fetch_reply_content(self, query, context):
return model_factory.create_bot(config.conf().get("model").get("type")).reply(query, context)

async def fetch_reply_stream(self, query, context):
bot=model_factory.create_bot(config.conf().get("model").get("type"))
async for final,response in bot.reply_text_stream(query, context):
yield final,response
4 changes: 4 additions & 0 deletions channel/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ def send(self, msg, receiver):

def build_reply_content(self, query, context=None):
return Bridge().fetch_reply_content(query, context)

async def build_reply_stream(self, query, context=None):
async for final,response in Bridge().fetch_reply_stream(query, context):
yield final,response
4 changes: 4 additions & 0 deletions channel/channel_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ def create_channel(channel_type):
from channel.dingtalk.dingtalk_channel import DingTalkChannel
return DingTalkChannel()

elif channel_type == const.FEISHU:
from channel.feishu.feishu_channel import FeiShuChannel
return FeiShuChannel()

else:
raise RuntimeError("unknown channel_type in config.json: " + channel_type)
6 changes: 4 additions & 2 deletions channel/dingtalk/dingtalk_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def handle(self, data):

@http_app.route("/", methods=['POST'])
def chat():
# log.info("[DingTalk] chat_headers={}".format(str(request.headers)))
log.info("[DingTalk] chat_headers={}".format(str(request.headers)))
log.info("[DingTalk] chat={}".format(str(request.data)))
token = request.headers.get('token')
if dd.dingtalk_post_token and token != dd.dingtalk_post_token:
Expand All @@ -95,7 +95,9 @@ def chat():
content = data['text']['content']
if not content:
return
reply_text = dd.handle(data=data)
reply_text = "您好,有什么我可以帮助您解答的问题吗?"
if str(content) != 0 and content.strip():
reply_text = dd.handle(data=data)
dd.notify_dingtalk(reply_text)
return {'ret': 200}
return {'ret': 201}
Loading

0 comments on commit 4fefef5

Please sign in to comment.