fix message pointer error

This commit is contained in:
Zhang Minghan 2023-08-13 12:10:40 +08:00
parent d41931097e
commit 76e43b1d3e
4 changed files with 7 additions and 4 deletions

View File

@ -70,7 +70,7 @@ func ChatAPI(c *gin.Context) {
return
}
if instance.HandleMessage(db, message) {
keyword, segment := ChatWithWeb(instance.GetMessageSegment(12), true)
keyword, segment := ChatWithWeb(conversation.CopyMessage(instance.GetMessageSegment(12)), true)
SendSegmentMessage(conn, types.ChatGPTSegmentResponse{Keyword: keyword, End: false})
msg := ""

View File

@ -101,8 +101,7 @@ func CreateConversationTable(db *sql.DB) {
conversation_id INT UNIQUE,
conversation_name VARCHAR(255),
data TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES auth(id)
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`)
if err != nil {

View File

@ -63,6 +63,10 @@ func (c *Conversation) GetMessageSegment(length int) []types.ChatGPTMessage {
return c.Message[len(c.Message)-length:]
}
func CopyMessage(message []types.ChatGPTMessage) []types.ChatGPTMessage {
return utils.UnmarshalJson[[]types.ChatGPTMessage](utils.ToJson(message)) // deep copy
}
func (c *Conversation) GetLastMessage() types.ChatGPTMessage {
return c.Message[len(c.Message)-1]
}

View File

@ -74,7 +74,7 @@ func GetSegment[T any](arr []T, length int) []T {
func GetSegmentString(arr string, length int) string {
if length > len(arr) {
return ""
return arr
}
return arr[:length]
}