mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-03 19:17:10 +08:00
Compare commits
60 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2db4673392 | ||
|
|
38619db629 | ||
|
|
930fd436ea | ||
|
|
98b8ff2fc8 | ||
|
|
d0662683f9 | ||
|
|
957f2574a9 | ||
|
|
109b362ebd | ||
|
|
ff3fdfa738 | ||
|
|
e2636ed54a | ||
|
|
dbe2f17e1a | ||
|
|
4dc535673f | ||
|
|
f414b6408e | ||
|
|
3aa2e6a04d | ||
|
|
1963ff273f | ||
|
|
bb737a71d5 | ||
|
|
a582a46ce9 | ||
|
|
abf80a3266 | ||
|
|
d768f5c66d | ||
|
|
b25e843351 | ||
|
|
419a3e518e | ||
|
|
d1b867a7c0 | ||
|
|
c34d70b3cb | ||
|
|
a33df9312f | ||
|
|
ebf8db0b37 | ||
|
|
e539ae3b69 | ||
|
|
4c5e8850aa | ||
|
|
94c0af3037 | ||
|
|
165182c68f | ||
|
|
65b9542599 | ||
|
|
d01d1f8830 | ||
|
|
ad3e9f3d42 | ||
|
|
4589974095 | ||
|
|
ed4553ddf8 | ||
|
|
ff97ae73f1 | ||
|
|
f96b4d2781 | ||
|
|
ce32cfffdb | ||
|
|
f66df8531e | ||
|
|
dfe1c23e76 | ||
|
|
07fd81919f | ||
|
|
210042bb81 | ||
|
|
12dc7427e9 | ||
|
|
b476085110 | ||
|
|
776cdaf63c | ||
|
|
69b6855745 | ||
|
|
3590babd8b | ||
|
|
c29d391c1d | ||
|
|
50e44dbb2a | ||
|
|
34277a3940 | ||
|
|
f1a00d58ca | ||
|
|
d1a5f17ae8 | ||
|
|
6409f49609 | ||
|
|
9ee0ea88b5 | ||
|
|
a3819d8673 | ||
|
|
2d7dd71a3d | ||
|
|
0e8195ae61 | ||
|
|
3e92d07618 | ||
|
|
e59597280d | ||
|
|
f2e3d69d8a | ||
|
|
9d2cb75c84 | ||
|
|
f971505c4a |
10
.github/workflows/deploy-image.yml
vendored
10
.github/workflows/deploy-image.yml
vendored
@@ -28,6 +28,12 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Log in to the Container registry
|
- name: Log in to the Container registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
@@ -39,7 +45,9 @@ jobs:
|
|||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@v4
|
uses: docker/metadata-action@v4
|
||||||
with:
|
with:
|
||||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
images: |
|
||||||
|
${{ env.IMAGE_NAME }}
|
||||||
|
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||||
|
|
||||||
- name: Build and push Docker image
|
- name: Build and push Docker image
|
||||||
uses: docker/build-push-action@v3
|
uses: docker/build-push-action@v3
|
||||||
|
|||||||
27
README.md
27
README.md
@@ -14,8 +14,9 @@
|
|||||||
> 欢迎接入更多应用,参考 [Terminal代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/terminal/terminal_channel.py)实现接收和发送消息逻辑即可接入。 同时欢迎增加新的插件,参考 [插件说明文档](https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins)。
|
> 欢迎接入更多应用,参考 [Terminal代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/terminal/terminal_channel.py)实现接收和发送消息逻辑即可接入。 同时欢迎增加新的插件,参考 [插件说明文档](https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins)。
|
||||||
|
|
||||||
**一键部署:**
|
**一键部署:**
|
||||||
|
- 个人微信
|
||||||
[](https://railway.app/template/qApznZ?referralCode=RC3znh)
|
|
||||||
|
[](https://railway.app/template/qApznZ?referralCode=RC3znh)
|
||||||
|
|
||||||
# 演示
|
# 演示
|
||||||
|
|
||||||
@@ -23,9 +24,17 @@ https://user-images.githubusercontent.com/26161723/233777277-e3b9928e-b88f-43e2-
|
|||||||
|
|
||||||
Demo made by [Visionn](https://www.wangpc.cc/)
|
Demo made by [Visionn](https://www.wangpc.cc/)
|
||||||
|
|
||||||
|
# 交流群
|
||||||
|
|
||||||
|
添加小助手微信进群:
|
||||||
|
|
||||||
|
<img width="240" src="./docs/images/contact.jpg">
|
||||||
|
|
||||||
# 更新日志
|
# 更新日志
|
||||||
|
|
||||||
>**2023.04.26:** 支持企业微信应用号部署,兼容插件,并支持语音图片交互,支持Railway部署,[使用文档](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/wechatcom/README.md)。(contributed by [@lanvent](https://github.com/lanvent) in [#944](https://github.com/zhayujie/chatgpt-on-wechat/pull/944))
|
>**2023.06.12:** 接入 [LinkAI](https://chat.link-ai.tech/console) 平台,可在线创建 个人知识库,并接入微信中。Beta版本欢迎体验,使用参考 [接入文档](https://link-ai.tech/platform/link-app/wechat)。
|
||||||
|
|
||||||
|
>**2023.04.26:** 支持企业微信应用号部署,兼容插件,并支持语音图片交互,私人助理理想选择,[使用文档](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/wechatcom/README.md)。(contributed by [@lanvent](https://github.com/lanvent) in [#944](https://github.com/zhayujie/chatgpt-on-wechat/pull/944))
|
||||||
|
|
||||||
>**2023.04.05:** 支持微信公众号部署,兼容插件,并支持语音图片交互,[使用文档](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/wechatmp/README.md)。(contributed by [@JS00000](https://github.com/JS00000) in [#686](https://github.com/zhayujie/chatgpt-on-wechat/pull/686))
|
>**2023.04.05:** 支持微信公众号部署,兼容插件,并支持语音图片交互,[使用文档](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/wechatmp/README.md)。(contributed by [@JS00000](https://github.com/JS00000) in [#686](https://github.com/zhayujie/chatgpt-on-wechat/pull/686))
|
||||||
|
|
||||||
@@ -116,6 +125,7 @@ pip3 install azure-cognitiveservices-speech
|
|||||||
"speech_recognition": false, # 是否开启语音识别
|
"speech_recognition": false, # 是否开启语音识别
|
||||||
"group_speech_recognition": false, # 是否开启群组语音识别
|
"group_speech_recognition": false, # 是否开启群组语音识别
|
||||||
"use_azure_chatgpt": false, # 是否使用Azure ChatGPT service代替openai ChatGPT service. 当设置为true时需要设置 open_ai_api_base,如 https://xxx.openai.azure.com/
|
"use_azure_chatgpt": false, # 是否使用Azure ChatGPT service代替openai ChatGPT service. 当设置为true时需要设置 open_ai_api_base,如 https://xxx.openai.azure.com/
|
||||||
|
"azure_deployment_id": "", # 采用Azure ChatGPT时,模型部署名称
|
||||||
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", # 人格描述
|
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", # 人格描述
|
||||||
# 订阅消息,公众号和企业微信channel中请填写,当被订阅时会自动回复,可使用特殊占位符。目前支持的占位符有{trigger_prefix},在程序中它会自动替换成bot的触发词。
|
# 订阅消息,公众号和企业微信channel中请填写,当被订阅时会自动回复,可使用特殊占位符。目前支持的占位符有{trigger_prefix},在程序中它会自动替换成bot的触发词。
|
||||||
"subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持角色扮演和文字冒险等丰富插件。\n输入{trigger_prefix}#help 查看详细指令。"
|
"subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持角色扮演和文字冒险等丰富插件。\n输入{trigger_prefix}#help 查看详细指令。"
|
||||||
@@ -143,11 +153,11 @@ pip3 install azure-cognitiveservices-speech
|
|||||||
|
|
||||||
**4.其他配置**
|
**4.其他配置**
|
||||||
|
|
||||||
+ `model`: 模型名称,目前支持 `gpt-3.5-turbo`, `text-davinci-003`, `gpt-4`, `gpt-4-32k` (其中gpt-4 api暂未开放)
|
+ `model`: 模型名称,目前支持 `gpt-3.5-turbo`, `text-davinci-003`, `gpt-4`, `gpt-4-32k` (其中gpt-4 api暂未完全开放,申请通过后可使用)
|
||||||
+ `temperature`,`frequency_penalty`,`presence_penalty`: Chat API接口参数,详情参考[OpenAI官方文档。](https://platform.openai.com/docs/api-reference/chat)
|
+ `temperature`,`frequency_penalty`,`presence_penalty`: Chat API接口参数,详情参考[OpenAI官方文档。](https://platform.openai.com/docs/api-reference/chat)
|
||||||
+ `proxy`:由于目前 `openai` 接口国内无法访问,需配置代理客户端的地址,详情参考 [#351](https://github.com/zhayujie/chatgpt-on-wechat/issues/351)
|
+ `proxy`:由于目前 `openai` 接口国内无法访问,需配置代理客户端的地址,详情参考 [#351](https://github.com/zhayujie/chatgpt-on-wechat/issues/351)
|
||||||
+ 对于图像生成,在满足个人或群组触发条件外,还需要额外的关键词前缀来触发,对应配置 `image_create_prefix `
|
+ 对于图像生成,在满足个人或群组触发条件外,还需要额外的关键词前缀来触发,对应配置 `image_create_prefix `
|
||||||
+ 关于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` 中进行调整。
|
+ 关于OpenAI对话及图片接口的参数配置(内容自由度、回复字数限制、图片大小等),可以参考 [对话接口](https://beta.openai.com/docs/api-reference/completions) 和 [图像接口](https://beta.openai.com/docs/api-reference/completions) 文档,在[`config.py`](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/config.py)中检查哪些参数在本项目中是可配置的。
|
||||||
+ `conversation_max_tokens`:表示能够记忆的上下文最大字数(一问一答为一组对话,如果累积的对话字数超出限制,就会优先移除最早的一组对话)
|
+ `conversation_max_tokens`:表示能够记忆的上下文最大字数(一问一答为一组对话,如果累积的对话字数超出限制,就会优先移除最早的一组对话)
|
||||||
+ `rate_limit_chatgpt`,`rate_limit_dalle`:每分钟最高问答速率、画图速率,超速后排队按序处理。
|
+ `rate_limit_chatgpt`,`rate_limit_dalle`:每分钟最高问答速率、画图速率,超速后排队按序处理。
|
||||||
+ `clear_memory_commands`: 对话内指令,主动清空前文记忆,字符串数组可自定义指令别名。
|
+ `clear_memory_commands`: 对话内指令,主动清空前文记忆,字符串数组可自定义指令别名。
|
||||||
@@ -155,7 +165,7 @@ pip3 install azure-cognitiveservices-speech
|
|||||||
+ `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的设定,你可以为他定制任何人格 (关于会话上下文的更多内容参考该 [issue](https://github.com/zhayujie/chatgpt-on-wechat/issues/43))
|
+ `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的设定,你可以为他定制任何人格 (关于会话上下文的更多内容参考该 [issue](https://github.com/zhayujie/chatgpt-on-wechat/issues/43))
|
||||||
+ `subscribe_msg`:订阅消息,公众号和企业微信channel中请填写,当被订阅时会自动回复, 可使用特殊占位符。目前支持的占位符有{trigger_prefix},在程序中它会自动替换成bot的触发词。
|
+ `subscribe_msg`:订阅消息,公众号和企业微信channel中请填写,当被订阅时会自动回复, 可使用特殊占位符。目前支持的占位符有{trigger_prefix},在程序中它会自动替换成bot的触发词。
|
||||||
|
|
||||||
**所有可选的配置项均在该[文件](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/config.py)中列出。**
|
**本说明文档可能会未及时更新,当前所有可选的配置项均在该[`config.py`](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/config.py)中列出。**
|
||||||
|
|
||||||
## 运行
|
## 运行
|
||||||
|
|
||||||
@@ -198,9 +208,12 @@ nohup python3 app.py & tail -f nohup.out # 在后台运行程序并通
|
|||||||
|
|
||||||
FAQs: <https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs>
|
FAQs: <https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs>
|
||||||
|
|
||||||
|
或直接在线咨询 [项目小助手](https://chat.link-ai.tech/app/Kv2fXJcH) (beta版本,语料完善中,回复仅供参考)
|
||||||
|
|
||||||
## 联系
|
## 联系
|
||||||
|
|
||||||
欢迎提交PR、Issues,以及Star支持一下。程序运行遇到问题优先查看 [常见问题列表](https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs) ,其次前往 [Issues](https://github.com/zhayujie/chatgpt-on-wechat/issues) 中搜索。如果你想了解更多项目细节,并与开发者们交流更多关于AI技术的实践,欢迎加入星球:
|
欢迎提交PR、Issues,以及Star支持一下。程序运行遇到问题可以查看 [常见问题列表](https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs) ,其次前往 [Issues](https://github.com/zhayujie/chatgpt-on-wechat/issues) 中搜索。
|
||||||
|
|
||||||
|
如果你想了解更多项目细节,与开发者们交流更多关于AI技术的实践,欢迎加入星球:
|
||||||
|
|
||||||
<a href="https://public.zsxq.com/groups/88885848842852.html"><img width="360" src="./docs/images/planet.jpg"></a>
|
<a href="https://public.zsxq.com/groups/88885848842852.html"><img width="360" src="./docs/images/planet.jpg"></a>
|
||||||
|
|||||||
@@ -33,4 +33,9 @@ def create_bot(bot_type):
|
|||||||
from bot.chatgpt.chat_gpt_bot import AzureChatGPTBot
|
from bot.chatgpt.chat_gpt_bot import AzureChatGPTBot
|
||||||
|
|
||||||
return AzureChatGPTBot()
|
return AzureChatGPTBot()
|
||||||
|
|
||||||
|
elif bot_type == const.LINKAI:
|
||||||
|
from bot.linkai.link_ai_bot import LinkAIBot
|
||||||
|
return LinkAIBot()
|
||||||
|
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class ChatGPTBot(Bot, OpenAIImage):
|
|||||||
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
|
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
|
||||||
"temperature": conf().get("temperature", 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
|
"temperature": conf().get("temperature", 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
|
||||||
# "max_tokens":4096, # 回复最大的字符数
|
# "max_tokens":4096, # 回复最大的字符数
|
||||||
"top_p": 1,
|
"top_p": conf().get("top_p", 1),
|
||||||
"frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
"frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
"presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
"presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
"request_timeout": conf().get("request_timeout", None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间
|
"request_timeout": conf().get("request_timeout", None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间
|
||||||
@@ -66,12 +66,16 @@ class ChatGPTBot(Bot, OpenAIImage):
|
|||||||
logger.debug("[CHATGPT] session query={}".format(session.messages))
|
logger.debug("[CHATGPT] session query={}".format(session.messages))
|
||||||
|
|
||||||
api_key = context.get("openai_api_key")
|
api_key = context.get("openai_api_key")
|
||||||
|
model = context.get("gpt_model")
|
||||||
|
new_args = None
|
||||||
|
if model:
|
||||||
|
new_args = self.args.copy()
|
||||||
|
new_args["model"] = model
|
||||||
# if context.get('stream'):
|
# if context.get('stream'):
|
||||||
# # reply in stream
|
# # reply in stream
|
||||||
# return self.reply_text_stream(query, new_query, session_id)
|
# return self.reply_text_stream(query, new_query, session_id)
|
||||||
|
|
||||||
reply_content = self.reply_text(session, api_key)
|
reply_content = self.reply_text(session, api_key, args=new_args)
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"[CHATGPT] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(
|
"[CHATGPT] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(
|
||||||
session.messages,
|
session.messages,
|
||||||
@@ -102,7 +106,7 @@ class ChatGPTBot(Bot, OpenAIImage):
|
|||||||
reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
|
reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
def reply_text(self, session: ChatGPTSession, api_key=None, retry_count=0) -> dict:
|
def reply_text(self, session: ChatGPTSession, api_key=None, args=None, retry_count=0) -> dict:
|
||||||
"""
|
"""
|
||||||
call openai's ChatCompletion to get the answer
|
call openai's ChatCompletion to get the answer
|
||||||
:param session: a conversation session
|
:param session: a conversation session
|
||||||
@@ -114,7 +118,10 @@ class ChatGPTBot(Bot, OpenAIImage):
|
|||||||
if conf().get("rate_limit_chatgpt") and not self.tb4chatgpt.get_token():
|
if conf().get("rate_limit_chatgpt") and not self.tb4chatgpt.get_token():
|
||||||
raise openai.error.RateLimitError("RateLimitError: rate limit exceeded")
|
raise openai.error.RateLimitError("RateLimitError: rate limit exceeded")
|
||||||
# if api_key == None, the default openai.api_key will be used
|
# if api_key == None, the default openai.api_key will be used
|
||||||
response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **self.args)
|
if args is None:
|
||||||
|
args = self.args
|
||||||
|
response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args)
|
||||||
|
# logger.debug("[CHATGPT] response={}".format(response))
|
||||||
# logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"]))
|
# logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"]))
|
||||||
return {
|
return {
|
||||||
"total_tokens": response["usage"]["total_tokens"],
|
"total_tokens": response["usage"]["total_tokens"],
|
||||||
@@ -134,18 +141,23 @@ class ChatGPTBot(Bot, OpenAIImage):
|
|||||||
result["content"] = "我没有收到你的消息"
|
result["content"] = "我没有收到你的消息"
|
||||||
if need_retry:
|
if need_retry:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
elif isinstance(e, openai.error.APIError):
|
||||||
|
logger.warn("[CHATGPT] Bad Gateway: {}".format(e))
|
||||||
|
result["content"] = "请再问我一次"
|
||||||
|
if need_retry:
|
||||||
|
time.sleep(10)
|
||||||
elif isinstance(e, openai.error.APIConnectionError):
|
elif isinstance(e, openai.error.APIConnectionError):
|
||||||
logger.warn("[CHATGPT] APIConnectionError: {}".format(e))
|
logger.warn("[CHATGPT] APIConnectionError: {}".format(e))
|
||||||
need_retry = False
|
need_retry = False
|
||||||
result["content"] = "我连接不到你的网络"
|
result["content"] = "我连接不到你的网络"
|
||||||
else:
|
else:
|
||||||
logger.warn("[CHATGPT] Exception: {}".format(e))
|
logger.exception("[CHATGPT] Exception: {}".format(e))
|
||||||
need_retry = False
|
need_retry = False
|
||||||
self.sessions.clear_session(session.session_id)
|
self.sessions.clear_session(session.session_id)
|
||||||
|
|
||||||
if need_retry:
|
if need_retry:
|
||||||
logger.warn("[CHATGPT] 第{}次重试".format(retry_count + 1))
|
logger.warn("[CHATGPT] 第{}次重试".format(retry_count + 1))
|
||||||
return self.reply_text(session, api_key, retry_count + 1)
|
return self.reply_text(session, api_key, args, retry_count + 1)
|
||||||
else:
|
else:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
@@ -57,24 +57,25 @@ def num_tokens_from_messages(messages, model):
|
|||||||
"""Returns the number of tokens used by a list of messages."""
|
"""Returns the number of tokens used by a list of messages."""
|
||||||
import tiktoken
|
import tiktoken
|
||||||
|
|
||||||
|
if model in ["gpt-3.5-turbo-0301", "gpt-35-turbo"]:
|
||||||
|
return num_tokens_from_messages(messages, model="gpt-3.5-turbo")
|
||||||
|
elif model in ["gpt-4-0314", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613"]:
|
||||||
|
return num_tokens_from_messages(messages, model="gpt-4")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
encoding = tiktoken.encoding_for_model(model)
|
encoding = tiktoken.encoding_for_model(model)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
logger.debug("Warning: model not found. Using cl100k_base encoding.")
|
logger.debug("Warning: model not found. Using cl100k_base encoding.")
|
||||||
encoding = tiktoken.get_encoding("cl100k_base")
|
encoding = tiktoken.get_encoding("cl100k_base")
|
||||||
if model == "gpt-3.5-turbo" or model == "gpt-35-turbo":
|
if model == "gpt-3.5-turbo":
|
||||||
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301")
|
|
||||||
elif model == "gpt-4":
|
|
||||||
return num_tokens_from_messages(messages, model="gpt-4-0314")
|
|
||||||
elif model == "gpt-3.5-turbo-0301":
|
|
||||||
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
||||||
tokens_per_name = -1 # if there's a name, the role is omitted
|
tokens_per_name = -1 # if there's a name, the role is omitted
|
||||||
elif model == "gpt-4-0314":
|
elif model == "gpt-4":
|
||||||
tokens_per_message = 3
|
tokens_per_message = 3
|
||||||
tokens_per_name = 1
|
tokens_per_name = 1
|
||||||
else:
|
else:
|
||||||
logger.warn(f"num_tokens_from_messages() is not implemented for model {model}. Returning num tokens assuming gpt-3.5-turbo-0301.")
|
logger.warn(f"num_tokens_from_messages() is not implemented for model {model}. Returning num tokens assuming gpt-3.5-turbo.")
|
||||||
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301")
|
return num_tokens_from_messages(messages, model="gpt-3.5-turbo")
|
||||||
num_tokens = 0
|
num_tokens = 0
|
||||||
for message in messages:
|
for message in messages:
|
||||||
num_tokens += tokens_per_message
|
num_tokens += tokens_per_message
|
||||||
|
|||||||
108
bot/linkai/link_ai_bot.py
Normal file
108
bot/linkai/link_ai_bot.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
# access LinkAI knowledge base platform
|
||||||
|
# docs: https://link-ai.tech/platform/link-app/wechat
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from bot.bot import Bot
|
||||||
|
from bot.chatgpt.chat_gpt_session import ChatGPTSession
|
||||||
|
from bot.openai.open_ai_image import OpenAIImage
|
||||||
|
from bot.session_manager import SessionManager
|
||||||
|
from bridge.context import Context, ContextType
|
||||||
|
from bridge.reply import Reply, ReplyType
|
||||||
|
from common.log import logger
|
||||||
|
from config import conf
|
||||||
|
|
||||||
|
|
||||||
|
class LinkAIBot(Bot, OpenAIImage):
|
||||||
|
# authentication failed
|
||||||
|
AUTH_FAILED_CODE = 401
|
||||||
|
NO_QUOTA_CODE = 406
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.base_url = "https://api.link-ai.chat/v1"
|
||||||
|
self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "gpt-3.5-turbo")
|
||||||
|
|
||||||
|
def reply(self, query, context: Context = None) -> Reply:
|
||||||
|
if context.type == ContextType.TEXT:
|
||||||
|
return self._chat(query, context)
|
||||||
|
elif context.type == ContextType.IMAGE_CREATE:
|
||||||
|
ok, retstring = self.create_img(query, 0)
|
||||||
|
reply = None
|
||||||
|
if ok:
|
||||||
|
reply = Reply(ReplyType.IMAGE_URL, retstring)
|
||||||
|
else:
|
||||||
|
reply = Reply(ReplyType.ERROR, retstring)
|
||||||
|
return reply
|
||||||
|
else:
|
||||||
|
reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
|
||||||
|
return reply
|
||||||
|
|
||||||
|
def _chat(self, query, context, retry_count=0):
|
||||||
|
if retry_count >= 2:
|
||||||
|
# exit from retry 2 times
|
||||||
|
logger.warn("[LINKAI] failed after maximum number of retry times")
|
||||||
|
return Reply(ReplyType.ERROR, "请再问我一次吧")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# load config
|
||||||
|
if context.get("generate_breaked_by"):
|
||||||
|
logger.info(f"[LINKAI] won't set appcode because a plugin ({context['generate_breaked_by']}) affected the context")
|
||||||
|
app_code = None
|
||||||
|
else:
|
||||||
|
app_code = conf().get("linkai_app_code")
|
||||||
|
linkai_api_key = conf().get("linkai_api_key")
|
||||||
|
|
||||||
|
session_id = context["session_id"]
|
||||||
|
|
||||||
|
session = self.sessions.session_query(query, session_id)
|
||||||
|
|
||||||
|
# remove system message
|
||||||
|
if app_code and session.messages[0].get("role") == "system":
|
||||||
|
session.messages.pop(0)
|
||||||
|
|
||||||
|
logger.info(f"[LINKAI] query={query}, app_code={app_code}")
|
||||||
|
|
||||||
|
body = {
|
||||||
|
"appCode": app_code,
|
||||||
|
"messages": session.messages,
|
||||||
|
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
|
||||||
|
"temperature": conf().get("temperature"),
|
||||||
|
"top_p": conf().get("top_p", 1),
|
||||||
|
"frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
|
"presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
|
||||||
|
}
|
||||||
|
headers = {"Authorization": "Bearer " + linkai_api_key}
|
||||||
|
|
||||||
|
# do http request
|
||||||
|
res = requests.post(url=self.base_url + "/chat/completion", json=body, headers=headers).json()
|
||||||
|
|
||||||
|
if not res or not res["success"]:
|
||||||
|
if res.get("code") == self.AUTH_FAILED_CODE:
|
||||||
|
logger.exception(f"[LINKAI] please check your linkai_api_key, res={res}")
|
||||||
|
return Reply(ReplyType.ERROR, "请再问我一次吧")
|
||||||
|
|
||||||
|
elif res.get("code") == self.NO_QUOTA_CODE:
|
||||||
|
logger.exception(f"[LINKAI] please check your account quota, https://chat.link-ai.tech/console/account")
|
||||||
|
return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧")
|
||||||
|
|
||||||
|
else:
|
||||||
|
# retry
|
||||||
|
time.sleep(2)
|
||||||
|
logger.warn(f"[LINKAI] do retry, times={retry_count}")
|
||||||
|
return self._chat(query, context, retry_count + 1)
|
||||||
|
|
||||||
|
# execute success
|
||||||
|
reply_content = res["data"]["content"]
|
||||||
|
logger.info(f"[LINKAI] reply={reply_content}")
|
||||||
|
self.sessions.session_reply(reply_content, session_id)
|
||||||
|
return Reply(ReplyType.TEXT, reply_content)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(e)
|
||||||
|
# retry
|
||||||
|
time.sleep(2)
|
||||||
|
logger.warn(f"[LINKAI] do retry, times={retry_count}")
|
||||||
|
return self._chat(query, context, retry_count + 1)
|
||||||
@@ -23,6 +23,8 @@ class Bridge(object):
|
|||||||
self.btype["chat"] = const.OPEN_AI
|
self.btype["chat"] = const.OPEN_AI
|
||||||
if conf().get("use_azure_chatgpt", False):
|
if conf().get("use_azure_chatgpt", False):
|
||||||
self.btype["chat"] = const.CHATGPTONAZURE
|
self.btype["chat"] = const.CHATGPTONAZURE
|
||||||
|
if conf().get("use_linkai") and conf().get("linkai_api_key"):
|
||||||
|
self.btype["chat"] = const.LINKAI
|
||||||
self.bots = {}
|
self.bots = {}
|
||||||
|
|
||||||
def get_bot(self, typename):
|
def get_bot(self, typename):
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ class ChatChannel(Channel):
|
|||||||
cmsg = context["msg"]
|
cmsg = context["msg"]
|
||||||
user_data = conf().get_user_data(cmsg.from_user_id)
|
user_data = conf().get_user_data(cmsg.from_user_id)
|
||||||
context["openai_api_key"] = user_data.get("openai_api_key")
|
context["openai_api_key"] = user_data.get("openai_api_key")
|
||||||
|
context["gpt_model"] = user_data.get("gpt_model")
|
||||||
if context.get("isgroup", False):
|
if context.get("isgroup", False):
|
||||||
group_name = cmsg.other_user_nickname
|
group_name = cmsg.other_user_nickname
|
||||||
group_id = cmsg.other_user_id
|
group_id = cmsg.other_user_id
|
||||||
@@ -161,6 +162,8 @@ class ChatChannel(Channel):
|
|||||||
reply = e_context["reply"]
|
reply = e_context["reply"]
|
||||||
if not e_context.is_pass():
|
if not e_context.is_pass():
|
||||||
logger.debug("[WX] ready to handle context: type={}, content={}".format(context.type, context.content))
|
logger.debug("[WX] ready to handle context: type={}, content={}".format(context.type, context.content))
|
||||||
|
if e_context.is_break():
|
||||||
|
context["generate_breaked_by"] = e_context["breaked_by"]
|
||||||
if context.type == ContextType.TEXT or context.type == ContextType.IMAGE_CREATE: # 文字和图片消息
|
if context.type == ContextType.TEXT or context.type == ContextType.IMAGE_CREATE: # 文字和图片消息
|
||||||
reply = super().build_reply_content(context.content, context)
|
reply = super().build_reply_content(context.content, context)
|
||||||
elif context.type == ContextType.VOICE: # 语音消息
|
elif context.type == ContextType.VOICE: # 语音消息
|
||||||
@@ -219,10 +222,10 @@ class ChatChannel(Channel):
|
|||||||
reply = super().build_text_to_voice(reply.content)
|
reply = super().build_text_to_voice(reply.content)
|
||||||
return self._decorate_reply(context, reply)
|
return self._decorate_reply(context, reply)
|
||||||
if context.get("isgroup", False):
|
if context.get("isgroup", False):
|
||||||
reply_text = "@" + context["msg"].actual_user_nickname + " " + reply_text.strip()
|
reply_text = "@" + context["msg"].actual_user_nickname + "\n" + reply_text.strip()
|
||||||
reply_text = conf().get("group_chat_reply_prefix", "") + reply_text
|
reply_text = conf().get("group_chat_reply_prefix", "") + reply_text + conf().get("group_chat_reply_suffix", "")
|
||||||
else:
|
else:
|
||||||
reply_text = conf().get("single_chat_reply_prefix", "") + reply_text
|
reply_text = conf().get("single_chat_reply_prefix", "") + reply_text + conf().get("single_chat_reply_suffix", "")
|
||||||
reply.content = reply_text
|
reply.content = reply_text
|
||||||
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
|
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
|
||||||
reply.content = "[" + str(reply.type) + "]\n" + reply.content
|
reply.content = "[" + str(reply.type) + "]\n" + reply.content
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ from common.time_check import time_checker
|
|||||||
from config import conf, get_appdata_dir
|
from config import conf, get_appdata_dir
|
||||||
from lib import itchat
|
from lib import itchat
|
||||||
from lib.itchat.content import *
|
from lib.itchat.content import *
|
||||||
from plugins import *
|
|
||||||
|
|
||||||
|
|
||||||
@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE])
|
@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE])
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# 企业微信应用号channel
|
# 企业微信应用号channel
|
||||||
|
|
||||||
企业微信官方提供了客服、应用等API,本channel使用的是企业微信的应用API的能力。
|
企业微信官方提供了客服、应用等API,本channel使用的是企业微信的自建应用API的能力。
|
||||||
|
|
||||||
因为未来可能还会开发客服能力,所以本channel的类型名叫作`wechatcom_app`。
|
因为未来可能还会开发客服能力,所以本channel的类型名叫作`wechatcom_app`。
|
||||||
|
|
||||||
@@ -72,13 +72,11 @@ Error code: 60020, message: "not allow to access from your ip, ...from ip: xx.xx
|
|||||||
|
|
||||||
意思是IP不可信,需要参考上一步的`企业可信IP`配置,把这里的IP加进去。
|
意思是IP不可信,需要参考上一步的`企业可信IP`配置,把这里的IP加进去。
|
||||||
|
|
||||||
### Railway部署方式
|
~~### Railway部署方式~~(2023-06-08已失效)
|
||||||
|
|
||||||
公众号不能在`Railway`上部署,但企业微信应用[可以](https://railway.app/template/-FHS--?referralCode=RC3znh)!
|
~~公众号不能在`Railway`上部署,但企业微信应用[可以](https://railway.app/template/-FHS--?referralCode=RC3znh)!~~
|
||||||
|
|
||||||
[](https://railway.app/template/-FHS--?referralCode=RC3znh)
|
~~填写配置后,将部署完成后的网址```**.railway.app/wxcomapp```,填写在上一步的URL中。发送信息后观察日志,把报错的IP加入到可信IP。(每次重启后都需要加入可信IP)~~
|
||||||
|
|
||||||
填写配置后,将部署完成后的网址```**.railway.app/wxcomapp```,填写在上一步的URL中。发送信息后观察日志,把报错的IP加入到可信IP。(每次重启后都需要加入可信IP)
|
|
||||||
|
|
||||||
## 测试体验
|
## 测试体验
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,6 @@ OPEN_AI = "openAI"
|
|||||||
CHATGPT = "chatGPT"
|
CHATGPT = "chatGPT"
|
||||||
BAIDU = "baidu"
|
BAIDU = "baidu"
|
||||||
CHATGPTONAZURE = "chatGPTOnAzure"
|
CHATGPTONAZURE = "chatGPTOnAzure"
|
||||||
|
LINKAI = "linkai"
|
||||||
|
|
||||||
VERSION = "1.3.0"
|
VERSION = "1.3.0"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
"open_ai_api_key": "YOUR API KEY",
|
"open_ai_api_key": "YOUR API KEY",
|
||||||
"model": "gpt-3.5-turbo",
|
"model": "gpt-3.5-turbo",
|
||||||
"proxy": "",
|
"proxy": "",
|
||||||
|
"hot_reload": false,
|
||||||
"single_chat_prefix": [
|
"single_chat_prefix": [
|
||||||
"bot",
|
"bot",
|
||||||
"@bot"
|
"@bot"
|
||||||
@@ -28,5 +29,8 @@
|
|||||||
"conversation_max_tokens": 1000,
|
"conversation_max_tokens": 1000,
|
||||||
"expires_in_seconds": 3600,
|
"expires_in_seconds": 3600,
|
||||||
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
|
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
|
||||||
"subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。"
|
"subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
|
||||||
|
"use_linkai": false,
|
||||||
|
"linkai_api_key": "",
|
||||||
|
"linkai_app_code": ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,10 @@ available_setting = {
|
|||||||
# Bot触发配置
|
# Bot触发配置
|
||||||
"single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复
|
"single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复
|
||||||
"single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人
|
"single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人
|
||||||
"group_chat_prefix": ["@bot"], # 群聊时包含该前缀则会触发机器人回复
|
"single_chat_reply_suffix": "", # 私聊时自动回复的后缀,\n 可以换行
|
||||||
|
"group_chat_prefix": ["@bot"], # 群聊时包含该前缀则会触发机器人回复
|
||||||
"group_chat_reply_prefix": "", # 群聊时自动回复的前缀
|
"group_chat_reply_prefix": "", # 群聊时自动回复的前缀
|
||||||
|
"group_chat_reply_suffix": "", # 群聊时自动回复的后缀,\n 可以换行
|
||||||
"group_chat_keyword": [], # 群聊时包含该关键词则会触发机器人回复
|
"group_chat_keyword": [], # 群聊时包含该关键词则会触发机器人回复
|
||||||
"group_at_off": False, # 是否关闭群聊时@bot的触发
|
"group_at_off": False, # 是否关闭群聊时@bot的触发
|
||||||
"group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], # 开启自动回复的群名称列表
|
"group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], # 开启自动回复的群名称列表
|
||||||
@@ -99,6 +101,10 @@ available_setting = {
|
|||||||
"appdata_dir": "", # 数据目录
|
"appdata_dir": "", # 数据目录
|
||||||
# 插件配置
|
# 插件配置
|
||||||
"plugin_trigger_prefix": "$", # 规范插件提供聊天相关指令的前缀,建议不要和管理员指令前缀"#"冲突
|
"plugin_trigger_prefix": "$", # 规范插件提供聊天相关指令的前缀,建议不要和管理员指令前缀"#"冲突
|
||||||
|
# 知识库平台配置
|
||||||
|
"use_linkai": False,
|
||||||
|
"linkai_api_key": "",
|
||||||
|
"linkai_app_code": ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ RUN chmod +x /entrypoint.sh \
|
|||||||
&& mkdir -p /home/noroot \
|
&& mkdir -p /home/noroot \
|
||||||
&& groupadd -r noroot \
|
&& groupadd -r noroot \
|
||||||
&& useradd -r -g noroot -s /bin/bash -d /home/noroot noroot \
|
&& useradd -r -g noroot -s /bin/bash -d /home/noroot noroot \
|
||||||
&& chown -R noroot:noroot /home/noroot ${BUILD_PREFIX}
|
&& chown -R noroot:noroot /home/noroot ${BUILD_PREFIX} /usr/local/lib
|
||||||
|
|
||||||
USER noroot
|
USER noroot
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ ARG TZ='Asia/Shanghai'
|
|||||||
ARG CHATGPT_ON_WECHAT_VER
|
ARG CHATGPT_ON_WECHAT_VER
|
||||||
|
|
||||||
RUN echo /etc/apt/sources.list
|
RUN echo /etc/apt/sources.list
|
||||||
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
|
# RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
|
||||||
ENV BUILD_PREFIX=/app
|
ENV BUILD_PREFIX=/app
|
||||||
|
|
||||||
ADD . ${BUILD_PREFIX}
|
ADD . ${BUILD_PREFIX}
|
||||||
@@ -28,8 +28,8 @@ RUN chmod +x /entrypoint.sh \
|
|||||||
&& mkdir -p /home/noroot \
|
&& mkdir -p /home/noroot \
|
||||||
&& groupadd -r noroot \
|
&& groupadd -r noroot \
|
||||||
&& useradd -r -g noroot -s /bin/bash -d /home/noroot noroot \
|
&& useradd -r -g noroot -s /bin/bash -d /home/noroot noroot \
|
||||||
&& chown -R noroot:noroot /home/noroot ${BUILD_PREFIX}
|
&& chown -R noroot:noroot /home/noroot ${BUILD_PREFIX} /usr/local/lib
|
||||||
|
|
||||||
USER noroot
|
USER noroot
|
||||||
|
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
version: '2.0'
|
version: '2.0'
|
||||||
services:
|
services:
|
||||||
chatgpt-on-wechat:
|
chatgpt-on-wechat:
|
||||||
build:
|
|
||||||
context: ./
|
|
||||||
dockerfile: Dockerfile.alpine
|
|
||||||
image: zhayujie/chatgpt-on-wechat
|
image: zhayujie/chatgpt-on-wechat
|
||||||
container_name: sample-chatgpt-on-wechat
|
container_name: chatgpt-on-wechat
|
||||||
environment:
|
environment:
|
||||||
OPEN_AI_API_KEY: 'YOUR API KEY'
|
OPEN_AI_API_KEY: 'YOUR API KEY'
|
||||||
|
MODEL: 'gpt-3.5-turbo'
|
||||||
OPEN_AI_PROXY: ''
|
OPEN_AI_PROXY: ''
|
||||||
SINGLE_CHAT_PREFIX: '["bot", "@bot"]'
|
SINGLE_CHAT_PREFIX: '["bot", "@bot"]'
|
||||||
SINGLE_CHAT_REPLY_PREFIX: '"[bot] "'
|
SINGLE_CHAT_REPLY_PREFIX: '"[bot] "'
|
||||||
@@ -15,6 +13,9 @@ services:
|
|||||||
GROUP_NAME_WHITE_LIST: '["ChatGPT测试群", "ChatGPT测试群2"]'
|
GROUP_NAME_WHITE_LIST: '["ChatGPT测试群", "ChatGPT测试群2"]'
|
||||||
IMAGE_CREATE_PREFIX: '["画", "看", "找"]'
|
IMAGE_CREATE_PREFIX: '["画", "看", "找"]'
|
||||||
CONVERSATION_MAX_TOKENS: 1000
|
CONVERSATION_MAX_TOKENS: 1000
|
||||||
SPEECH_RECOGNITION: "False"
|
SPEECH_RECOGNITION: 'False'
|
||||||
CHARACTER_DESC: '你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。'
|
CHARACTER_DESC: '你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。'
|
||||||
EXPIRES_IN_SECONDS: 3600
|
EXPIRES_IN_SECONDS: 3600
|
||||||
|
USE_LINKAI: 'False'
|
||||||
|
LINKAI_API_KEY: ''
|
||||||
|
LINKAI_APP_CODE: ''
|
||||||
BIN
docs/images/contact.jpg
Normal file
BIN
docs/images/contact.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 151 KiB |
@@ -1,7 +1,7 @@
|
|||||||
providers = ['python']
|
|
||||||
|
|
||||||
[phases.setup]
|
[phases.setup]
|
||||||
nixPkgs = ['python310']
|
nixPkgs = ['python310']
|
||||||
cmds = ['apt-get update','apt-get install -y --no-install-recommends ffmpeg espeak libavcodec-extra','python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements-optional.txt']
|
cmds = ['apt-get update','apt-get install -y --no-install-recommends ffmpeg espeak libavcodec-extra']
|
||||||
|
[phases.install]
|
||||||
|
cmds = ['python -m venv /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt && pip install -r requirements-optional.txt']
|
||||||
[start]
|
[start]
|
||||||
cmd = "python ./app.py"
|
cmd = "python ./app.py"
|
||||||
@@ -64,7 +64,7 @@ class Dungeon(Plugin):
|
|||||||
if e_context["context"].type != ContextType.TEXT:
|
if e_context["context"].type != ContextType.TEXT:
|
||||||
return
|
return
|
||||||
bottype = Bridge().get_bot_type("chat")
|
bottype = Bridge().get_bot_type("chat")
|
||||||
if bottype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE]:
|
if bottype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI]:
|
||||||
return
|
return
|
||||||
bot = Bridge().get_bot("chat")
|
bot = Bridge().get_bot("chat")
|
||||||
content = e_context["context"].content[:]
|
content = e_context["context"].content[:]
|
||||||
|
|||||||
@@ -50,3 +50,6 @@ class EventContext:
|
|||||||
|
|
||||||
def is_pass(self):
|
def is_pass(self):
|
||||||
return self.action == EventAction.BREAK_PASS
|
return self.action == EventAction.BREAK_PASS
|
||||||
|
|
||||||
|
def is_break(self):
|
||||||
|
return self.action == EventAction.BREAK or self.action == EventAction.BREAK_PASS
|
||||||
|
|||||||
@@ -41,6 +41,18 @@ COMMANDS = {
|
|||||||
"alias": ["reset_openai_api_key"],
|
"alias": ["reset_openai_api_key"],
|
||||||
"desc": "重置为默认的api_key",
|
"desc": "重置为默认的api_key",
|
||||||
},
|
},
|
||||||
|
"set_gpt_model": {
|
||||||
|
"alias": ["set_gpt_model"],
|
||||||
|
"desc": "设置你的私有模型",
|
||||||
|
},
|
||||||
|
"reset_gpt_model": {
|
||||||
|
"alias": ["reset_gpt_model"],
|
||||||
|
"desc": "重置你的私有模型",
|
||||||
|
},
|
||||||
|
"gpt_model": {
|
||||||
|
"alias": ["gpt_model"],
|
||||||
|
"desc": "查询你使用的模型",
|
||||||
|
},
|
||||||
"id": {
|
"id": {
|
||||||
"alias": ["id", "用户"],
|
"alias": ["id", "用户"],
|
||||||
"desc": "获取用户id", # wechaty和wechatmp的用户id不会变化,可用于绑定管理员
|
"desc": "获取用户id", # wechaty和wechatmp的用户id不会变化,可用于绑定管理员
|
||||||
@@ -264,8 +276,28 @@ class Godcmd(Plugin):
|
|||||||
ok, result = True, "你的OpenAI私有api_key已清除"
|
ok, result = True, "你的OpenAI私有api_key已清除"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ok, result = False, "你没有设置私有api_key"
|
ok, result = False, "你没有设置私有api_key"
|
||||||
|
elif cmd == "set_gpt_model":
|
||||||
|
if len(args) == 1:
|
||||||
|
user_data = conf().get_user_data(user)
|
||||||
|
user_data["gpt_model"] = args[0]
|
||||||
|
ok, result = True, "你的GPT模型已设置为" + args[0]
|
||||||
|
else:
|
||||||
|
ok, result = False, "请提供一个GPT模型"
|
||||||
|
elif cmd == "gpt_model":
|
||||||
|
user_data = conf().get_user_data(user)
|
||||||
|
model = conf().get("model")
|
||||||
|
if "gpt_model" in user_data:
|
||||||
|
model = user_data["gpt_model"]
|
||||||
|
ok, result = True, "你的GPT模型为" + str(model)
|
||||||
|
elif cmd == "reset_gpt_model":
|
||||||
|
try:
|
||||||
|
user_data = conf().get_user_data(user)
|
||||||
|
user_data.pop("gpt_model")
|
||||||
|
ok, result = True, "你的GPT模型已重置"
|
||||||
|
except Exception as e:
|
||||||
|
ok, result = False, "你没有设置私有GPT模型"
|
||||||
elif cmd == "reset":
|
elif cmd == "reset":
|
||||||
if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE]:
|
if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI]:
|
||||||
bot.sessions.clear_session(session_id)
|
bot.sessions.clear_session(session_id)
|
||||||
channel.cancel_session(session_id)
|
channel.cancel_session(session_id)
|
||||||
ok, result = True, "会话已重置"
|
ok, result = True, "会话已重置"
|
||||||
@@ -288,7 +320,7 @@ class Godcmd(Plugin):
|
|||||||
load_config()
|
load_config()
|
||||||
ok, result = True, "配置已重载"
|
ok, result = True, "配置已重载"
|
||||||
elif cmd == "resetall":
|
elif cmd == "resetall":
|
||||||
if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE]:
|
if bottype in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI]:
|
||||||
channel.cancel_all_session()
|
channel.cancel_all_session()
|
||||||
bot.sessions.clear_all_session()
|
bot.sessions.clear_all_session()
|
||||||
ok, result = True, "重置所有会话成功"
|
ok, result = True, "重置所有会话成功"
|
||||||
|
|||||||
@@ -34,14 +34,14 @@ class Hello(Plugin):
|
|||||||
e_context["context"].type = ContextType.TEXT
|
e_context["context"].type = ContextType.TEXT
|
||||||
msg: ChatMessage = e_context["context"]["msg"]
|
msg: ChatMessage = e_context["context"]["msg"]
|
||||||
e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。'
|
e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。'
|
||||||
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
|
||||||
return
|
return
|
||||||
|
|
||||||
if e_context["context"].type == ContextType.PATPAT:
|
if e_context["context"].type == ContextType.PATPAT:
|
||||||
e_context["context"].type = ContextType.TEXT
|
e_context["context"].type = ContextType.TEXT
|
||||||
msg: ChatMessage = e_context["context"]["msg"]
|
msg: ChatMessage = e_context["context"]["msg"]
|
||||||
e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
|
e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
|
||||||
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
|
||||||
return
|
return
|
||||||
|
|
||||||
content = e_context["context"].content
|
content = e_context["context"].content
|
||||||
|
|||||||
@@ -163,6 +163,9 @@ class PluginManager:
|
|||||||
logger.debug("Plugin %s triggered by event %s" % (name, e_context.event))
|
logger.debug("Plugin %s triggered by event %s" % (name, e_context.event))
|
||||||
instance = self.instances[name]
|
instance = self.instances[name]
|
||||||
instance.handlers[e_context.event](e_context, *args, **kwargs)
|
instance.handlers[e_context.event](e_context, *args, **kwargs)
|
||||||
|
if e_context.is_break():
|
||||||
|
e_context["breaked_by"] = name
|
||||||
|
logger.debug("Plugin %s breaked event %s" % (name, e_context.event))
|
||||||
return e_context
|
return e_context
|
||||||
|
|
||||||
def set_plugin_priority(self, name: str, priority: int):
|
def set_plugin_priority(self, name: str, priority: int):
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ class Role(Plugin):
|
|||||||
if e_context["context"].type != ContextType.TEXT:
|
if e_context["context"].type != ContextType.TEXT:
|
||||||
return
|
return
|
||||||
btype = Bridge().get_bot_type("chat")
|
btype = Bridge().get_bot_type("chat")
|
||||||
if btype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE]:
|
if btype not in [const.OPEN_AI, const.CHATGPT, const.CHATGPTONAZURE, const.LINKAI]:
|
||||||
return
|
return
|
||||||
bot = Bridge().get_bot("chat")
|
bot = Bridge().get_bot("chat")
|
||||||
content = e_context["context"].content[:]
|
content = e_context["context"].content[:]
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ $tool reset: 重置工具。
|
|||||||
|
|
||||||
### 6. news 新闻类工具集合
|
### 6. news 新闻类工具集合
|
||||||
|
|
||||||
|
> news更新:0.4版本对新闻类工具做了整合,配置文件只要加入`news`一个工具名就会自动加载所有新闻类工具
|
||||||
|
|
||||||
#### 6.1. news-api *
|
#### 6.1. news-api *
|
||||||
###### 从全球 80,000 多个信息源中获取当前和历史新闻文章
|
###### 从全球 80,000 多个信息源中获取当前和历史新闻文章
|
||||||
|
|
||||||
@@ -75,7 +77,7 @@ $tool reset: 重置工具。
|
|||||||
|
|
||||||
> 该工具需要解决browser tool 的google-chrome依赖安装
|
> 该工具需要解决browser tool 的google-chrome依赖安装
|
||||||
|
|
||||||
> news更新:0.4版本对news工具做了整合,只要加入news一个工具就会自动加载所有新闻类工具
|
|
||||||
|
|
||||||
### 7. bing-search *
|
### 7. bing-search *
|
||||||
###### bing搜索引擎,从此你不用再烦恼搜索要用哪些关键词
|
###### bing搜索引擎,从此你不用再烦恼搜索要用哪些关键词
|
||||||
@@ -112,24 +114,25 @@ $tool reset: 重置工具。
|
|||||||
---
|
---
|
||||||
|
|
||||||
###### 注1:带*工具需要获取api-key才能使用(在config.json内的kwargs添加项),部分工具需要外网支持
|
###### 注1:带*工具需要获取api-key才能使用(在config.json内的kwargs添加项),部分工具需要外网支持
|
||||||
#### [申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)
|
## [工具的api申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)
|
||||||
|
|
||||||
## config.json 配置说明
|
## config.json 配置说明
|
||||||
###### 默认工具无需配置,其它工具需手动配置,一个例子:
|
###### 默认工具无需配置,其它工具需手动配置,以增加morning-news和bing-search两个工具为例:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"tools": ["wikipedia", "你想要添加的其他工具"], // 填入你想用到的额外工具名
|
"tools": ["bing-search", "news", "你想要添加的其他工具"], // 填入你想用到的额外工具名,这里加入了工具"bing-search"和工具"news"(news工具会自动加载morning-news、finance-news等子工具)
|
||||||
"kwargs": {
|
"kwargs": {
|
||||||
"debug": true, // 当你遇到问题求助时,需要配置
|
"debug": true, // 当你遇到问题求助时,需要配置
|
||||||
"request_timeout": 120, // openai接口超时时间
|
"request_timeout": 120, // openai接口超时时间
|
||||||
"no_default": false, // 是否不使用默认的4个工具
|
"no_default": false, // 是否不使用默认的4个工具
|
||||||
// 带*工具需要申请api-key,在这里填入,api_name参考前述`申请方法`
|
"bing_subscription_key": "4871f273a4804743",//带*工具需要申请api-key,这里填入了工具bing-search对应的api,api_name参考前述`工具的api申请方法`
|
||||||
|
"morning_news_api_key": "5w1kjNh9VQlUc",// 这里填入了morning-news对应的api,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
注:config.json文件非必须,未创建仍可使用本tool;带*工具需在kwargs填入对应api-key键值对
|
注:config.json文件非必须,未创建仍可使用本tool;带*工具需在kwargs填入对应api-key键值对
|
||||||
- `tools`:本插件初始化时加载的工具, 上述标题即是对应工具名称,带*工具必须在kwargs中配置相应api-key
|
- `tools`:本插件初始化时加载的工具, 上述一级标题即是对应工具名称,带*工具必须在kwargs中配置相应api-key
|
||||||
- `kwargs`:工具执行时的配置,一般在这里存放**api-key**,或环境配置
|
- `kwargs`:工具执行时的配置,一般在这里存放**api-key**,或环境配置
|
||||||
- `debug`: 输出chatgpt-tool-hub额外信息用于调试
|
- `debug`: 输出chatgpt-tool-hub额外信息用于调试
|
||||||
- `request_timeout`: 访问openai接口的超时时间,默认与wechat-on-chatgpt配置一致,可单独配置
|
- `request_timeout`: 访问openai接口的超时时间,默认与wechat-on-chatgpt配置一致,可单独配置
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ class Tool(Plugin):
|
|||||||
const.CHATGPT,
|
const.CHATGPT,
|
||||||
const.OPEN_AI,
|
const.OPEN_AI,
|
||||||
const.CHATGPTONAZURE,
|
const.CHATGPTONAZURE,
|
||||||
|
const.LINKAI,
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -25,4 +25,4 @@ wechatpy
|
|||||||
# chatgpt-tool-hub plugin
|
# chatgpt-tool-hub plugin
|
||||||
|
|
||||||
--extra-index-url https://pypi.python.org/simple
|
--extra-index-url https://pypi.python.org/simple
|
||||||
chatgpt_tool_hub==0.4.3
|
chatgpt_tool_hub==0.4.4
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
openai==0.27.2
|
openai>=0.27.8
|
||||||
HTMLParser>=0.0.2
|
HTMLParser>=0.0.2
|
||||||
PyQRCode>=1.2.1
|
PyQRCode>=1.2.1
|
||||||
qrcode>=7.4.2
|
qrcode>=7.4.2
|
||||||
requests>=2.28.2
|
requests>=2.28.2
|
||||||
chardet>=5.1.0
|
chardet>=5.1.0
|
||||||
Pillow
|
Pillow
|
||||||
pre-commit
|
pre-commit
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ class BaiduTranslator(Translator):
|
|||||||
self.url = endpoint + path
|
self.url = endpoint + path
|
||||||
self.appid = conf().get("baidu_translate_app_id")
|
self.appid = conf().get("baidu_translate_app_id")
|
||||||
self.appkey = conf().get("baidu_translate_app_key")
|
self.appkey = conf().get("baidu_translate_app_key")
|
||||||
|
if not self.appid or not self.appkey:
|
||||||
|
raise Exception("baidu translate appid or appkey not set")
|
||||||
|
|
||||||
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`, need to convert to ISO 639-1 codes
|
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`, need to convert to ISO 639-1 codes
|
||||||
def translate(self, query: str, from_lang: str = "", to_lang: str = "en") -> str:
|
def translate(self, query: str, from_lang: str = "", to_lang: str = "en") -> str:
|
||||||
if not from_lang:
|
if not from_lang:
|
||||||
from_lang = "auto" # baidu suppport auto detect
|
from_lang = "auto" # baidu suppport auto detect
|
||||||
salt = random.randint(32768, 65536)
|
salt = random.randint(32768, 65536)
|
||||||
sign = self.make_md5(self.appid + query + str(salt) + self.appkey)
|
sign = self.make_md5("{}{}{}{}".format(self.appid, query, salt, self.appkey))
|
||||||
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
||||||
payload = {"appid": self.appid, "q": query, "from": from_lang, "to": to_lang, "salt": salt, "sign": sign}
|
payload = {"appid": self.appid, "q": query, "from": from_lang, "to": to_lang, "salt": salt, "sign": sign}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
import shutil
|
import shutil
|
||||||
import wave
|
import wave
|
||||||
|
|
||||||
import pysilk
|
from common.log import logger
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pysilk
|
||||||
|
except ImportError:
|
||||||
|
logger.warn("import pysilk failed, wechaty voice message will not be supported.")
|
||||||
|
|
||||||
from pydub import AudioSegment
|
from pydub import AudioSegment
|
||||||
|
|
||||||
sil_supports = [8000, 12000, 16000, 24000, 32000, 44100, 48000] # slk转wav时,支持的采样率
|
sil_supports = [8000, 12000, 16000, 24000, 32000, 44100, 48000] # slk转wav时,支持的采样率
|
||||||
|
|||||||
@@ -43,9 +43,9 @@ class BaiduVoice(Voice):
|
|||||||
with open(config_path, "r") as fr:
|
with open(config_path, "r") as fr:
|
||||||
bconf = json.load(fr)
|
bconf = json.load(fr)
|
||||||
|
|
||||||
self.app_id = conf().get("baidu_app_id")
|
self.app_id = str(conf().get("baidu_app_id"))
|
||||||
self.api_key = conf().get("baidu_api_key")
|
self.api_key = str(conf().get("baidu_api_key"))
|
||||||
self.secret_key = conf().get("baidu_secret_key")
|
self.secret_key = str(conf().get("baidu_secret_key"))
|
||||||
self.dev_id = conf().get("baidu_dev_pid")
|
self.dev_id = conf().get("baidu_dev_pid")
|
||||||
self.lang = bconf["lang"]
|
self.lang = bconf["lang"]
|
||||||
self.ctp = bconf["ctp"]
|
self.ctp = bconf["ctp"]
|
||||||
|
|||||||
Reference in New Issue
Block a user