123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package xiaoice
- import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/zeromicro/go-zero/core/errorx"
- "io"
- "net/http"
- "net/url"
- "strconv"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GptbotsChatLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewGptbotsChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GptbotsChatLogic {
- return &GptbotsChatLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *GptbotsChatLogic) GptbotsChat(req *types.ChatReq) (resp *types.BaseDataInfo, err error) {
- apikeyMap := map[string]string{
- "VHPF0W063": "app-hQL7oVq57McK5VBHlsMfhtUD",
- "VHPTL3UAP": "app-2T9KkC9CTwxjzRKQSSM1REmJ",
- }
- apikey := apikeyMap[*req.AvatarId]
- if apikey == "" {
- return nil, fmt.Errorf("未知虚拟人id")
- }
- conversationId, err := l.GetConversation(apikey, strconv.FormatUint(*req.UserId, 10))
- if conversationId == nil || err != nil {
- return nil, err
- }
- baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation/message")
- if err != nil {
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
- }
- // 构建请求体
- requestBody := map[string]string{
- "text": *req.Text,
- "conversation_id": *conversationId,
- "response_mode": "blocking",
- }
- jsonBody, err := json.Marshal(requestBody)
- if err != nil {
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
- }
- // 创建HTTP请求
- httpReq, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
- if err != nil {
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
- }
- // 添加必要的Header信息
- httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
- httpReq.Header.Set("Content-Type", "application/json")
- // 创建HTTP客户端并执行请求
- client := &http.Client{}
- response, err := client.Do(httpReq)
- if err != nil {
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
- }
- defer func(Body io.ReadCloser) {
- err := Body.Close()
- if err != nil {
- l.Error("生成内容失败: %v", err)
- }
- }(response.Body)
- // 读取和输出响应
- body, err := io.ReadAll(response.Body)
- if err != nil {
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
- }
- // 检查响应状态
- if response.StatusCode != http.StatusOK {
- //log.Fatalf("请求失败,状态码:%d,响应: %s", response.StatusCode, string(body))
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败:%d,响应: %s", response.StatusCode, string(body)))
- }
- // 解析 JSON 响应
- var responseMap types.GptbotsMessageResp
- if err := json.Unmarshal(body, &responseMap); err != nil {
- return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
- }
- data := ""
- if responseMap.FlowOutput != nil && len(responseMap.FlowOutput) > 0 {
- data = TrimHtml(Markdown2Html(responseMap.FlowOutput[0].Content))
- }
- return &types.BaseDataInfo{Msg: errormsg.Success, Data: data}, nil
- }
- func (l *GptbotsChatLogic) GetConversation(apikey string, userId string) (conversationId *string, err error) {
- val, _ := l.svcCtx.Rds.HGet(l.ctx, "xiaoice_conversation", userId).Result()
- if val == "" {
- baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation")
- if err != nil {
- return nil, err
- }
- // 构建请求体
- requestBody := map[string]string{
- "user_id": userId,
- }
- jsonBody, err := json.Marshal(requestBody)
- if err != nil {
- return nil, err
- }
- // 创建HTTP请求
- req, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
- if err != nil {
- return nil, err
- }
- // 添加必要的Header信息
- req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
- req.Header.Set("Content-Type", "application/json")
- // 创建HTTP客户端并执行请求
- client := &http.Client{}
- response, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer func(Body io.ReadCloser) {
- err := Body.Close()
- if err != nil {
- l.Error("创建会话失败败: %v", err)
- }
- }(response.Body)
- // 读取和输出响应
- body, err := io.ReadAll(response.Body)
- if err != nil {
- return nil, err
- }
- // 检查响应状态
- if response.StatusCode != http.StatusOK {
- //log.Fatalf("请求失败,状态码:%d,响应: %s", response.StatusCode, string(body))
- return nil, errorx.NewDefaultError(fmt.Sprintf("创建会话失败:%d,响应: %s", response.StatusCode, string(body)))
- }
- // 解析 JSON 响应
- var responseMap types.ConversationResp
- if err := json.Unmarshal(body, &responseMap); err != nil {
- return nil, err
- }
- l.svcCtx.Rds.HSet(l.ctx, "xiaoice_conversation", userId, *responseMap.ConversationId)
- return responseMap.ConversationId, nil
- }
- return &val, nil
- }
|