添加机器人,设置及回复
This commit is contained in:
parent
a7742b304e
commit
6dbed0199d
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
Loading…
Reference in New Issue