package aiSearch import ( "aiChat/api/aiSearch/v1" "context" "errors" "fmt" "io/ioutil" "log" "strings" "time" "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" ) func (c *ControllerV1) Chat(ctx context.Context, req *v1.ChatReq) (res *v1.ChatRes, err error) { content := fmt.Sprintf(g.Cfg("ai_search.yaml").MustGet(ctx, "prompt").String(), req.Content) query, largeModelReply, err := c.doubao(ctx, content) if err != nil { query, largeModelReply, err = c.zhipu(ctx, content) } log.Println(query, largeModelReply, err) return nil, nil } //调用豆包大模型 func (c *ControllerV1) doubao(ctx context.Context, content string) (string, string, error) { count, err := g.Redis("main").Incr(ctx, fmt.Sprintf("aiSearch_doubaoCall_%s", gtime.Now().Format("YYYYmmdd"))) if err != nil { g.Log().Error(ctx, "从redis获取doubao调用次数出错", err) return "", "", err } else if doubaoCallMax := g.Cfg("ai_search.yaml").MustGet(ctx, "doubaoCallMax").Int64(); count > doubaoCallMax { g.Log().Info(ctx, "doubao调用次数达到上限", doubaoCallMax, count) return "", "", errors.New("doubao调用次数达到上限") } // 构造请求数据 messages := []map[string]interface{}{} messages = append(messages, map[string]interface{}{ "role": "user", "content": content, }) //glm-4-air glm-4-0520 glm-4-flash requestData := map[string]interface{}{ "model": "ep-20250207170552-g8dsx", "temperature": 0.1, "top_p": 0.7, "messages": messages, } return c.post(ctx, "doubao", "https://ark.cn-beijing.volces.com/api/v3/chat/completions", "3dd861bf-b8a7-41d4-bb0b-5076362c572d", requestData) } //调用智普大模型 func (c *ControllerV1) zhipu(ctx context.Context, content string) (string, string, error) { count, err := g.Redis("main").Incr(ctx, fmt.Sprintf("aiSearch_doubaoCall_%s", gtime.Now().Format("YYYYmmdd"))) if err != nil { g.Log().Error(ctx, "从redis获取zhipu调用次数出错", err) return "", "", err } else if doubaoCallMax := g.Cfg("ai_search.yaml").MustGet(ctx, "zhipuCallMax").Int64(); count > doubaoCallMax { g.Log().Info(ctx, "zhipu调用次数达到上限", doubaoCallMax, count) return "", "", errors.New("zhipu调用次数达到上限") } // 构造请求数据 messages := []map[string]interface{}{} messages = append(messages, map[string]interface{}{ "role": "user", "content": content, }) //glm-4-air glm-4-0520 glm-4-flash requestData := map[string]interface{}{ "model": "glm-4-flash", "messages": messages, "temperature": 0.1, "max_tokens": 4096, } return c.post(ctx, "zhipu", "https://open.bigmodel.cn/api/paas/v4/chat/completions", "3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR", requestData) } func (c *ControllerV1) post(ctx context.Context, t, apiURL, pass string, requestData map[string]interface{}) (string, string, error) { resp, err := g.Client().Timeout(time.Duration(g.Cfg("ai_search.yaml").MustGet(ctx, "timeout").Int())*time.Second). SetHeader("Authorization", fmt.Sprintf("Bearer %s", pass)). ContentType("application/json"). Post(ctx, apiURL, requestData) if err != nil { g.Log().Error(ctx, t, "请求出错", err) return "", "", err } defer resp.Body.Close() b, be := ioutil.ReadAll(resp.Body) if be != nil { g.Log().Error(ctx, t, "gjson.LoadJson出错", be) return "", "", err } largeModelReply := string(b) g.Log().Error(ctx, t, "请求回复", largeModelReply) r, re := gjson.LoadJson(b) if re != nil { g.Log().Error(ctx, t, largeModelReply, "gjson.LoadJson出错", re) return "", largeModelReply, err } content := "" choices := r.GetJsons("choices") if len(choices) == 0 { return "", largeModelReply, err } message := choices[0].GetJson("message") if message == nil { return "", largeModelReply, err } content = message.Get("content").String() content = strings.ReplaceAll(content, "```json", "") content = strings.ReplaceAll(content, "```", "") return content, largeModelReply, nil }