开发消息分流demo

This commit is contained in:
Cool 2024-12-06 19:11:50 +08:00
parent 78a6b04a7c
commit 407c3c7b96
4 changed files with 16 additions and 6 deletions

4
ai.py
View File

@ -3,8 +3,6 @@ import json
from zhipuai import ZhipuAI from zhipuai import ZhipuAI
import flask import flask
# client = ZhipuAI(api_key="73bdeed728677bc80efc6956478a2315.VerNWJMCwN9L5gTi") # 请填写您自己的APIKey # client = ZhipuAI(api_key="73bdeed728677bc80efc6956478a2315.VerNWJMCwN9L5gTi") # 请填写您自己的APIKey
# response = client.chat.completions.create( # response = client.chat.completions.create(
# model="glm-4", # 请填写您要调用的模型名称 # model="glm-4", # 请填写您要调用的模型名称
@ -21,9 +19,9 @@ def app_chat():
data = json.loads(flask.globals.request.get_data()) data = json.loads(flask.globals.request.get_data())
# print(data) # print(data)
uid = data["user_id"] uid = data["user_id"]
if not data["text"][-1] in ['?', '', '.', '', ',', '', '!', '']: if not data["text"][-1] in ['?', '', '.', '', ',', '', '!', '']:
data["text"] += "" data["text"] += ""
data["text"] += ""
# 使用ZhipuAI库调用模型生成回复 # 使用ZhipuAI库调用模型生成回复
client = ZhipuAI(api_key="73bdeed728677bc80efc6956478a2315.VerNWJMCwN9L5gTi") # 请填写您自己的APIKey client = ZhipuAI(api_key="73bdeed728677bc80efc6956478a2315.VerNWJMCwN9L5gTi") # 请填写您自己的APIKey

BIN
examples/SchoolCalendar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

BIN
examples/classExample.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

View File

@ -18,7 +18,10 @@ func Use(vals ...interface{}) {
_ = val _ = val
} }
} }
var keyWordMap = map[string]string{
"生成课程表": "./examples/classExample.jpg",
"生成校历": "./examples/SchoolCalendar.png",
}
type SendTextRequest struct { type SendTextRequest struct {
InGroup bool `json:"in_group"` //本来想用于区分在群聊和非群聊时的上下文记忆规则,但是最终没有实现... InGroup bool `json:"in_group"` //本来想用于区分在群聊和非群聊时的上下文记忆规则,但是最终没有实现...
UserID string `json:"user_id"` UserID string `json:"user_id"`
@ -94,7 +97,7 @@ func main() {
func sendMessage(msg *openwechat.Message, self *openwechat.Self) { func sendMessage(msg *openwechat.Message, self *openwechat.Self) {
if msg.IsTickledMe() { if msg.IsTickledMe() {
msg.ReplyText("别拍了,机器人是会被拍坏掉的。") msg.ReplyText("你好。我是大数据分析与知识处理课程的AI助手专门为学习相关课程的学生提供帮助包括解答疑问、提供学习资料、辅助分析和理解复杂概念等。如果您有任何与大数据分析、知识处理相关的问题欢迎随时向我提问。")
return return
} }
@ -147,7 +150,16 @@ func sendMessage(msg *openwechat.Message, self *openwechat.Self) {
} }
} }
} else { }else {
// 遍历 map
for keyword, value := range keyWordMap {
if strings.Contains(msg.Content, keyword) {
img, _ := os.Open(value)
defer img.Close()
msg.ReplyImage(img)
return
}
}
// 调用GPT // 调用GPT
sender, _ := msg.Sender() sender, _ := msg.Sender()