From 6dbed0199ddd57ee3b62aa551ffd68c378a71128 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Wed, 2 Oct 2024 19:58:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=EF=BC=8C=E8=AE=BE=E7=BD=AE=E5=8F=8A=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/imService.go | 30 ++++++++++++++++ worker/req.go | 82 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 worker/req.go diff --git a/service/imService.go b/service/imService.go index 617cf63..0b90290 100644 --- a/service/imService.go +++ b/service/imService.go @@ -46,6 +46,12 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content //在线,存入redis worker.PushRedisListWithExpire("user_"+strconv.Itoa(to_id)+"_msg_ids", strconv.Itoa(int(id)), time.Second*300) } + //判断接收方是否是机器人 + id_str := strconv.Itoa(to_id) + if worker.IsContainSet("robots_ids", id_str) == true { + go SetRobotMsg(from_id, to_id, content) + } + case proto.MSG_TYPE_GROUP: if from_id == 0 || group_id == 0 || content == "" { return errors.New("参数错误"), 0 @@ -325,3 +331,27 @@ func SetGroupCache(group_id int) bool { res := worker.SetRedisSet("group_"+strconv.Itoa(group_id)+"_users", ids, time.Hour*12) return res } + +// 设置机器人返回消息 +func SetRobotMsg(person_id, robot_id int, msg string) { + //发送 + url := "http://localhost:11434/api/generate" + //body := map[string]interface{}{ + // "model": "qwen2.5:0.5b", + // "prompt": msg, + // "stream": false, + //} + //body_str, _ := json.Marshal(body) + + data, err := worker.GenerateCompletion(url, msg, "qwen2.5:0.5b") + if err != nil { + return + } + //解析返回数据 + msg_res_content := data["response"].(string) + //存入数据库,及robot_id为发送者,person_id为接收者 + err, _ = CreateGeneralMessageService(robot_id, person_id, proto.MSG_TYPE_SIMPLE, 0, msg_res_content) + if err != nil { + return + } +} diff --git a/worker/req.go b/worker/req.go new file mode 100644 index 0000000..e271110 --- /dev/null +++ b/worker/req.go @@ -0,0 +1,82 @@ +package worker + +import ( + "bytes" + "encoding/json" + "io" + "io/ioutil" + "net/http" + "strings" +) + +var client *http.Client + +// 初始化 +func InitReq() { + client = &http.Client{} +} + +// 发起post请求 +func Post(url string, bodyType string, body string) (*http.Response, error) { + req, err := http.NewRequest("POST", url, nil) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", bodyType) + req.Body = io.NopCloser(strings.NewReader(body)) + return client.Do(req) +} + +// 发送到机器人 +func SendToRobot(url string, body string) (map[string]interface{}, error) { + resp, err := Post(url, "application/json", body) + if err != nil { + return nil, err + } + defer resp.Body.Close() + m := make(map[string]interface{}) + err = json.NewDecoder(resp.Body).Decode(&m) + if err != nil { + return nil, err + } + return m, nil +} + +// 生成补全的函数 +func GenerateCompletion(url, prompt string, model string) (map[string]interface{}, error) { + data := map[string]interface{}{ + "model": model, + "prompt": prompt, + "stream": false, + } + jsonData, err := json.Marshal(data) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + + client_ := &http.Client{} + resp, err := client_.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var result map[string]interface{} + err = json.Unmarshal(body, &result) + if err != nil { + return nil, err + } + + return result, nil +}