Spaces:
Configuration error
Configuration error
BOHE
commited on
Commit
·
d0ca2c0
1
Parent(s):
4e1ac28
测试
Browse files- api/main.go +14 -14
api/main.go
CHANGED
|
@@ -241,8 +241,9 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 241 |
// 转换 system 消息为 user 消息
|
| 242 |
openAIReq.Messages = convertSystemToUser(openAIReq.Messages)
|
| 243 |
|
| 244 |
-
//
|
| 245 |
-
|
|
|
|
| 246 |
if err != nil {
|
| 247 |
http.Error(w, "Failed to count tokens", http.StatusInternalServerError)
|
| 248 |
return
|
|
@@ -250,7 +251,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 250 |
|
| 251 |
// 构建 You.com 聊天历史
|
| 252 |
var chatHistory []map[string]interface{}
|
| 253 |
-
for _, msg := range openAIReq.Messages {
|
| 254 |
chatMsg := map[string]interface{}{
|
| 255 |
"question": msg.Content,
|
| 256 |
"answer": "",
|
|
@@ -273,8 +274,8 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 273 |
conversationTurnId := uuid.New().String()
|
| 274 |
traceId := fmt.Sprintf("%s|%s|%s", chatId, conversationTurnId, time.Now().Format(time.RFC3339))
|
| 275 |
|
| 276 |
-
//
|
| 277 |
-
if
|
| 278 |
// 1. 获取 nonce
|
| 279 |
nonceResp, err := getNonce(dsToken)
|
| 280 |
if err != nil {
|
|
@@ -310,12 +311,11 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 310 |
}
|
| 311 |
fmt.Printf("文件上传成功: filename=%s, user_filename=%s\n", uploadResp.Filename, uploadResp.UserFilename)
|
| 312 |
|
| 313 |
-
// 4.
|
| 314 |
-
openAIReq.Messages
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
},
|
| 319 |
}
|
| 320 |
|
| 321 |
// 5. 添加文件源信息
|
|
@@ -351,12 +351,12 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 351 |
q.Add("enable_worklow_generation_ux", "true")
|
| 352 |
q.Add("domain", "youchat")
|
| 353 |
q.Add("use_personalization_extraction", "true")
|
| 354 |
-
q.Add("pastChatLength", "
|
| 355 |
q.Add("selectedChatMode", "custom")
|
| 356 |
q.Add("selectedAiModel", mapModelName(openAIReq.Model))
|
| 357 |
q.Add("enable_agent_clarification_questions", "true")
|
| 358 |
q.Add("use_nested_youchat_updates", "true")
|
| 359 |
-
q.Add("chat",
|
| 360 |
youReq.URL.RawQuery = q.Encode()
|
| 361 |
|
| 362 |
fmt.Printf("构建的请求 URL: %s\n", youReq.URL.String())
|
|
@@ -431,7 +431,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 431 |
q.Add("enable_worklow_generation_ux", "true")
|
| 432 |
q.Add("domain", "youchat")
|
| 433 |
q.Add("use_personalization_extraction", "true")
|
| 434 |
-
q.Add("pastChatLength", fmt.Sprintf("%d", len(chatHistory)
|
| 435 |
q.Add("selectedChatMode", "custom")
|
| 436 |
q.Add("selectedAiModel", mapModelName(openAIReq.Model))
|
| 437 |
q.Add("enable_agent_clarification_questions", "true")
|
|
|
|
| 241 |
// 转换 system 消息为 user 消息
|
| 242 |
openAIReq.Messages = convertSystemToUser(openAIReq.Messages)
|
| 243 |
|
| 244 |
+
// 计算最后一条消息的 token 数(使用字符估算方法)
|
| 245 |
+
lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1]
|
| 246 |
+
lastMessageTokens, err := countTokens([]Message{lastMessage})
|
| 247 |
if err != nil {
|
| 248 |
http.Error(w, "Failed to count tokens", http.StatusInternalServerError)
|
| 249 |
return
|
|
|
|
| 251 |
|
| 252 |
// 构建 You.com 聊天历史
|
| 253 |
var chatHistory []map[string]interface{}
|
| 254 |
+
for _, msg := range openAIReq.Messages[:len(openAIReq.Messages)-1] { // 不包含最后一条消息
|
| 255 |
chatMsg := map[string]interface{}{
|
| 256 |
"question": msg.Content,
|
| 257 |
"answer": "",
|
|
|
|
| 274 |
conversationTurnId := uuid.New().String()
|
| 275 |
traceId := fmt.Sprintf("%s|%s|%s", chatId, conversationTurnId, time.Now().Format(time.RFC3339))
|
| 276 |
|
| 277 |
+
// 如果最后一条消息超过限制,使用文件上传
|
| 278 |
+
if lastMessageTokens > MaxContextTokens {
|
| 279 |
// 1. 获取 nonce
|
| 280 |
nonceResp, err := getNonce(dsToken)
|
| 281 |
if err != nil {
|
|
|
|
| 311 |
}
|
| 312 |
fmt.Printf("文件上传成功: filename=%s, user_filename=%s\n", uploadResp.Filename, uploadResp.UserFilename)
|
| 313 |
|
| 314 |
+
// 4. 修改消息列表,保留历史记录,只将最后一条消息改为文件引用
|
| 315 |
+
lastMsg := openAIReq.Messages[len(openAIReq.Messages)-1]
|
| 316 |
+
openAIReq.Messages[len(openAIReq.Messages)-1] = Message{
|
| 317 |
+
Role: lastMsg.Role,
|
| 318 |
+
Content: fmt.Sprintf("Please review the attached file: %s", uploadResp.UserFilename),
|
|
|
|
| 319 |
}
|
| 320 |
|
| 321 |
// 5. 添加文件源信息
|
|
|
|
| 351 |
q.Add("enable_worklow_generation_ux", "true")
|
| 352 |
q.Add("domain", "youchat")
|
| 353 |
q.Add("use_personalization_extraction", "true")
|
| 354 |
+
q.Add("pastChatLength", fmt.Sprintf("%d", len(chatHistory)))
|
| 355 |
q.Add("selectedChatMode", "custom")
|
| 356 |
q.Add("selectedAiModel", mapModelName(openAIReq.Model))
|
| 357 |
q.Add("enable_agent_clarification_questions", "true")
|
| 358 |
q.Add("use_nested_youchat_updates", "true")
|
| 359 |
+
q.Add("chat", string(chatHistoryJSON))
|
| 360 |
youReq.URL.RawQuery = q.Encode()
|
| 361 |
|
| 362 |
fmt.Printf("构建的请求 URL: %s\n", youReq.URL.String())
|
|
|
|
| 431 |
q.Add("enable_worklow_generation_ux", "true")
|
| 432 |
q.Add("domain", "youchat")
|
| 433 |
q.Add("use_personalization_extraction", "true")
|
| 434 |
+
q.Add("pastChatLength", fmt.Sprintf("%d", len(chatHistory)))
|
| 435 |
q.Add("selectedChatMode", "custom")
|
| 436 |
q.Add("selectedAiModel", mapModelName(openAIReq.Model))
|
| 437 |
q.Add("enable_agent_clarification_questions", "true")
|