BOHE commited on
Commit
88916e2
·
1 Parent(s): fb2b2c5

修改上下文处理方式测试

Browse files
Files changed (1) hide show
  1. api/main.go +125 -69
api/main.go CHANGED
@@ -245,84 +245,144 @@ func Handler(w http.ResponseWriter, r *http.Request) {
245
  // 构建 You.com 聊天历史
246
  var chatHistory []map[string]interface{}
247
  var sources []map[string]interface{}
248
- var lastUserMessage string
249
- var lastAssistantMessage string
250
 
251
- // 处理历史消息
252
- for i := 0; i < len(openAIReq.Messages); i++ {
253
- msg := openAIReq.Messages[i]
254
 
 
255
  if msg.Role == "user" {
256
- lastUserMessage = msg.Content
257
- } else if msg.Role == "assistant" {
258
- lastAssistantMessage = msg.Content
259
 
260
- // 当我们找到一个assistant消息,将其与前面最近的user消息配对
261
- if lastUserMessage != "" {
262
- // 添加一条完整的问答对
263
- chatHistory = append(chatHistory, map[string]interface{}{
264
- "question": lastUserMessage,
265
- "answer": lastAssistantMessage,
266
- })
267
 
268
- // 重置消息,防止重复添加
269
- lastUserMessage = ""
270
- lastAssistantMessage = ""
 
 
 
 
271
  }
272
- }
273
- }
274
 
275
- // 如果还有未配对的用户消息(最后一条),将其添加到历史记录中
276
- if lastUserMessage != "" {
277
- chatHistory = append(chatHistory, map[string]interface{}{
278
- "question": lastUserMessage,
279
- "answer": "",
280
- })
281
- }
 
 
 
 
 
 
 
 
 
282
 
283
- // 将聊天历史转换为JSON
284
- chatHistoryJSON, _ := json.Marshal(chatHistory)
 
 
 
 
 
285
 
286
- // 使用文件上传方式处理所有历史消息
287
- var chatFileURL string
288
- if len(chatHistory) > 0 {
289
- // 获取 nonce
290
- nonceResp, err := getNonce(dsToken)
291
- if err != nil {
292
- fmt.Printf("获取历史消息nonce失败: %v\n", err)
293
- http.Error(w, "Failed to get nonce for chat history", http.StatusInternalServerError)
294
- return
295
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
 
297
- // 创建临时文件存储历史消息
298
- tempChatFile := fmt.Sprintf("temp_chat_%s.txt", nonceResp.Uuid)
299
- if err := os.WriteFile(tempChatFile, chatHistoryJSON, 0644); err != nil {
300
- fmt.Printf("创建历史消息临时文件失败: %v\n", err)
301
- http.Error(w, "Failed to create temp file for chat history", http.StatusInternalServerError)
302
- return
303
- }
304
- defer os.Remove(tempChatFile)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
306
- // 上传历史消息文件
307
- uploadChatResp, err := uploadFile(dsToken, tempChatFile)
308
- if err != nil {
309
- fmt.Printf("上传历史消息文件失败: %v\n", err)
310
- http.Error(w, "Failed to upload chat history file", http.StatusInternalServerError)
311
- return
312
  }
313
-
314
- // 添加历史消息文件源信息
315
- sources = append(sources, map[string]interface{}{
316
- "source_type": "chat_history_file",
317
- "filename": uploadChatResp.Filename,
318
- "user_filename": uploadChatResp.UserFilename,
319
- "size_bytes": len(string(chatHistoryJSON)),
320
- })
321
-
322
- // 保存历史消息文件URL
323
- chatFileURL = uploadChatResp.Filename
324
  }
325
 
 
 
326
  // 创建 You.com API 请求
327
  youReq, _ := http.NewRequest("GET", "https://you.com/api/streamingSearch", nil)
328
 
@@ -360,11 +420,6 @@ func Handler(w http.ResponseWriter, r *http.Request) {
360
  q.Add("traceId", traceId)
361
  q.Add("use_nested_youchat_updates", "true")
362
 
363
- // 如果有聊天历史文件,添加到查询参数
364
- if chatFileURL != "" {
365
- q.Add("chat", chatFileURL)
366
- }
367
-
368
  // 如果最后一条消息超过限制,使用文件上传
369
  if lastMessageTokens > MaxContextTokens {
370
  // 获取 nonce
@@ -415,6 +470,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
415
  q.Add("q", lastMessage.Content)
416
  }
417
 
 
418
  youReq.URL.RawQuery = q.Encode()
419
 
420
  fmt.Printf("\n=== 完整请求信息 ===\n")
 
245
  // 构建 You.com 聊天历史
246
  var chatHistory []map[string]interface{}
247
  var sources []map[string]interface{}
 
 
248
 
249
+ // 处理历史消息(不包括最后一条)
250
+ var currentQuestion string
251
+ var currentAnswer string
252
 
253
+ for i, msg := range openAIReq.Messages[:len(openAIReq.Messages)-1] {
254
  if msg.Role == "user" {
255
+ currentQuestion = msg.Content
256
+ currentAnswer = "" // 初始化当前答案为空
 
257
 
258
+ // 如果下一条消息是助手消息,则跳过当前循环,等待处理助手消息
259
+ if i+1 < len(openAIReq.Messages)-1 && openAIReq.Messages[i+1].Role == "assistant" {
260
+ continue
261
+ }
 
 
 
262
 
263
+ // 如果没有对应的助手消息,则创建一个问答对
264
+ // 获取 nonce 用于上传文件
265
+ nonceResp, err := getNonce(dsToken)
266
+ if err != nil {
267
+ fmt.Printf("获取 nonce 失败: %v\n", err)
268
+ http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
269
+ return
270
  }
 
 
271
 
272
+ // 创建用户消息临时文件并上传
273
+ userTempFile := fmt.Sprintf("temp_user_%s.txt", nonceResp.Uuid)
274
+ if err := os.WriteFile(userTempFile, []byte(currentQuestion), 0644); err != nil {
275
+ fmt.Printf("创建用户临时文件失败: %v\n", err)
276
+ http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
277
+ return
278
+ }
279
+ defer os.Remove(userTempFile)
280
+
281
+ // 上传用户消息文件
282
+ userUploadResp, err := uploadFile(dsToken, userTempFile)
283
+ if err != nil {
284
+ fmt.Printf("上传用户文件失败: %v\n", err)
285
+ http.Error(w, "Failed to upload file", http.StatusInternalServerError)
286
+ return
287
+ }
288
 
289
+ // 添加用户文件源信息
290
+ sources = append(sources, map[string]interface{}{
291
+ "source_type": "user_file",
292
+ "filename": userUploadResp.Filename,
293
+ "user_filename": userUploadResp.UserFilename,
294
+ "size_bytes": len(currentQuestion),
295
+ })
296
 
297
+ // 在历史记录中添加问答对
298
+ chatHistory = append(chatHistory, map[string]interface{}{
299
+ "question": fmt.Sprintf("Please review the attached file: %s", userUploadResp.UserFilename),
300
+ "answer": currentAnswer,
301
+ })
302
+ } else if msg.Role == "assistant" {
303
+ currentAnswer = msg.Content
304
+
305
+ // 获取上一条用户消息
306
+ if i > 0 && openAIReq.Messages[i-1].Role == "user" {
307
+ currentQuestion = openAIReq.Messages[i-1].Content
308
+
309
+ // 获取 nonce 用于上传用户消息文件
310
+ userNonceResp, err := getNonce(dsToken)
311
+ if err != nil {
312
+ fmt.Printf("获取用户消息 nonce 失败: %v\n", err)
313
+ http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
314
+ return
315
+ }
316
+
317
+ // 创建用户消息临时文件并上传
318
+ userTempFile := fmt.Sprintf("temp_user_%s.txt", userNonceResp.Uuid)
319
+ if err := os.WriteFile(userTempFile, []byte(currentQuestion), 0644); err != nil {
320
+ fmt.Printf("创建用户临时文件失败: %v\n", err)
321
+ http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
322
+ return
323
+ }
324
+ defer os.Remove(userTempFile)
325
+
326
+ // 上传用户消息文件
327
+ userUploadResp, err := uploadFile(dsToken, userTempFile)
328
+ if err != nil {
329
+ fmt.Printf("上传用户文件失败: %v\n", err)
330
+ http.Error(w, "Failed to upload file", http.StatusInternalServerError)
331
+ return
332
+ }
333
+
334
+ // 添加用户文件源信息
335
+ sources = append(sources, map[string]interface{}{
336
+ "source_type": "user_file",
337
+ "filename": userUploadResp.Filename,
338
+ "user_filename": userUploadResp.UserFilename,
339
+ "size_bytes": len(currentQuestion),
340
+ })
341
 
342
+ // 获取 nonce 用于上传助手消息文件
343
+ assistantNonceResp, err := getNonce(dsToken)
344
+ if err != nil {
345
+ fmt.Printf("获取助手消息 nonce 失败: %v\n", err)
346
+ http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
347
+ return
348
+ }
349
+
350
+ // 创建助手消息临时文件并上传
351
+ assistantTempFile := fmt.Sprintf("temp_assistant_%s.txt", assistantNonceResp.Uuid)
352
+ if err := os.WriteFile(assistantTempFile, []byte(currentAnswer), 0644); err != nil {
353
+ fmt.Printf("创建助手临时文件失败: %v\n", err)
354
+ http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
355
+ return
356
+ }
357
+ defer os.Remove(assistantTempFile)
358
+
359
+ // 上传助手消息文件
360
+ assistantUploadResp, err := uploadFile(dsToken, assistantTempFile)
361
+ if err != nil {
362
+ fmt.Printf("上传助手文件失败: %v\n", err)
363
+ http.Error(w, "Failed to upload file", http.StatusInternalServerError)
364
+ return
365
+ }
366
+
367
+ // 添加助手文件源信息
368
+ sources = append(sources, map[string]interface{}{
369
+ "source_type": "user_file",
370
+ "filename": assistantUploadResp.Filename,
371
+ "user_filename": assistantUploadResp.UserFilename,
372
+ "size_bytes": len(currentAnswer),
373
+ })
374
 
375
+ // 在历史记录中添加问答对
376
+ chatHistory = append(chatHistory, map[string]interface{}{
377
+ "question": fmt.Sprintf("Please review the attached file: %s", userUploadResp.UserFilename),
378
+ "answer": fmt.Sprintf("Please review the attached file: %s", assistantUploadResp.UserFilename),
379
+ })
380
+ }
381
  }
 
 
 
 
 
 
 
 
 
 
 
382
  }
383
 
384
+ chatHistoryJSON, _ := json.Marshal(chatHistory)
385
+
386
  // 创建 You.com API 请求
387
  youReq, _ := http.NewRequest("GET", "https://you.com/api/streamingSearch", nil)
388
 
 
420
  q.Add("traceId", traceId)
421
  q.Add("use_nested_youchat_updates", "true")
422
 
 
 
 
 
 
423
  // 如果最后一条消息超过限制,使用文件上传
424
  if lastMessageTokens > MaxContextTokens {
425
  // 获取 nonce
 
470
  q.Add("q", lastMessage.Content)
471
  }
472
 
473
+ q.Add("chat", string(chatHistoryJSON))
474
  youReq.URL.RawQuery = q.Encode()
475
 
476
  fmt.Printf("\n=== 完整请求信息 ===\n")