@@ -43,7 +43,7 @@ func (h *chat) HandleMessage(msg *Message) {
43
43
conversation = & AIConversation {
44
44
Id : id ,
45
45
Prompt : msg .Prompt ,
46
- HistoryRecords : make ([]string , 0 ),
46
+ Context : make ([]QARecord , 0 ),
47
47
InterruptCurrentChat : false ,
48
48
}
49
49
@@ -65,14 +65,16 @@ func (h *chat) HandleMessage(msg *Message) {
65
65
return
66
66
}
67
67
68
+ conversation .Question = msg .Data
69
+
68
70
openAIParam := & OpenAIParam {
69
71
AuthToken : h .termConf .GptApiKey ,
70
72
BaseURL : h .termConf .GptBaseUrl ,
71
73
Proxy : h .termConf .GptProxy ,
72
74
Model : h .termConf .GptModel ,
73
75
Prompt : conversation .Prompt ,
74
76
}
75
- conversation .HistoryRecords = append (conversation .HistoryRecords , msg .Data )
77
+ // conversation.HistoryRecords = append(conversation.HistoryRecords, msg.Data)
76
78
go h .chat (openAIParam , conversation )
77
79
}
78
80
@@ -90,30 +92,44 @@ func (h *chat) chat(
90
92
chatGPTParam .Proxy ,
91
93
)
92
94
93
- startIndex := len (conversation .HistoryRecords ) - 15
95
+ startIndex := len (conversation .Context ) - 8
94
96
if startIndex < 0 {
95
97
startIndex = 0
96
98
}
97
- contents := conversation .HistoryRecords [startIndex :]
99
+ conversation .Context = conversation .Context [startIndex :]
100
+ context := conversation .Context
101
+
102
+ chatContext := make ([]openai.ChatCompletionMessage , 0 )
103
+ for _ , record := range context {
104
+ chatContext = append (chatContext , openai.ChatCompletionMessage {
105
+ Role : openai .ChatMessageRoleUser ,
106
+ Content : record .Question ,
107
+ })
108
+ chatContext = append (chatContext , openai.ChatCompletionMessage {
109
+ Role : openai .ChatMessageRoleAssistant ,
110
+ Content : record .Answer ,
111
+ })
112
+ }
98
113
99
114
openAIConn := & srvconn.OpenAIConn {
100
115
Id : conversation .Id ,
101
116
Client : c ,
102
117
Prompt : chatGPTParam .Prompt ,
103
118
Model : chatGPTParam .Model ,
104
- Contents : contents ,
119
+ Question : conversation .Question ,
120
+ Context : chatContext ,
105
121
IsReasoning : false ,
106
122
AnswerCh : answerCh ,
107
123
DoneCh : doneCh ,
108
124
Type : h .termConf .ChatAIType ,
109
125
}
110
126
111
127
go openAIConn .Chat (& conversation .InterruptCurrentChat )
112
- return h .processChatMessages (openAIConn )
128
+ return h .processChatMessages (openAIConn , conversation )
113
129
}
114
130
115
131
func (h * chat ) processChatMessages (
116
- openAIConn * srvconn.OpenAIConn ,
132
+ openAIConn * srvconn.OpenAIConn , conversation * AIConversation ,
117
133
) string {
118
134
messageID := common .UUID ()
119
135
id := openAIConn .Id
@@ -123,6 +139,14 @@ func (h *chat) processChatMessages(
123
139
h .sendSessionMessage (id , answer , messageID , "message" , openAIConn .IsReasoning )
124
140
case answer := <- openAIConn .DoneCh :
125
141
h .sendSessionMessage (id , answer , messageID , "finish" , false )
142
+ runes := []rune (answer )
143
+ if len (runes ) > 100 {
144
+ answer = string (runes [:100 ])
145
+ }
146
+ conversation .Context = append (conversation .Context , QARecord {
147
+ Question : conversation .Question ,
148
+ Answer : answer ,
149
+ })
126
150
return answer
127
151
}
128
152
}
0 commit comments