cauto il y a 1 an
Parent
commit
7b4df0d5d1
2 fichiers modifiés avec 10 ajouts et 6 suppressions
  1. 2 2
      api/v1/Chat.go
  2. 8 4
      internal/controller/chat.go

+ 2 - 2
api/v1/Chat.go

@@ -6,8 +6,8 @@ type ChatReq struct {
 	g.Meta         `path:"/chat" tags:"聊天请求" method:"post" summary:"AI聊天"`
 	Role           string `json:"role" v:"required"`
 	Content        string `json:"content" v:"required"`
-	ConversationID string `json:"conversation_id" doc:"这个一个会话ID唯一值"`
-	MessageId      string `json:"message_id,omitempty" doc:"这个一个消息ID用于保持上下文"`
+	ConversationID string `json:"conversation_id"`
+	MessageId      string `json:"message_id,omitempty"`
 }
 
 type ChatRes struct {

+ 8 - 4
internal/controller/chat.go

@@ -8,6 +8,7 @@ import (
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/os/glog"
 	"github.com/gogf/gf/v2/util/guid"
+	"github.com/google/uuid"
 	v1 "go-gpt/api/v1"
 	"go-gpt/gpt"
 )
@@ -32,15 +33,16 @@ func (c *sChat) Chat(ctx context.Context, req *v1.ChatReq) (res *v1.ChatRes, err
 	var chatRequst gpt.ChatCompletionRequest
 	var chatMessage []gpt.ChatCompletionRequestMessage
 
-	//re := g.RequestFromCtx(ctx)
-	//ConversationID := ""
+	re := g.RequestFromCtx(ctx)
+	ConversationID := res.ConversationID
+	MessageId := res.MessageId
 	chatRequst.Model = gpt.GPT3Dot5Turbo
 	chatRequst.MaxTokens = 1024
 	chatRequst.Temperature = 0
 
 	if req.ConversationID == "" || req.MessageId == "" {
-		//ConversationID = CreateSessionId(re)
-		//chatRequst.ConversationID = ConversationID
+		ConversationID = CreateSessionId(re)
+		MessageId = uuid.NewString()
 	}
 	chatMessage = append(chatMessage, gpt.ChatCompletionRequestMessage{
 		Role:    req.Role,
@@ -54,6 +56,8 @@ func (c *sChat) Chat(ctx context.Context, req *v1.ChatReq) (res *v1.ChatRes, err
 	}
 	res = new(v1.ChatRes)
 	if len(completion.Choices) > 0 {
+		res.ConversationID = ConversationID
+		res.MessageId = MessageId
 		res.Text = completion.Choices[0].Message.Content
 	}