Spaces:
Configuration error
Configuration error
BOHE commited on
Commit ·
110a698
1
Parent(s): 6c2e83e
修改上下文处理方式测试
Browse files- api/main.go +114 -124
api/main.go
CHANGED
|
@@ -257,7 +257,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 257 |
// 打印OpenAI消息
|
| 258 |
fmt.Printf("\n=== 接收到的OpenAI消息 ===\n")
|
| 259 |
for i, msg := range openAIReq.Messages {
|
| 260 |
-
fmt.Printf("消息 %d: 角色=%s, 内容=%s\n", i
|
| 261 |
}
|
| 262 |
fmt.Printf("===================\n\n")
|
| 263 |
|
|
@@ -265,135 +265,125 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 265 |
var chatHistory []ChatEntry
|
| 266 |
var sources []map[string]interface{}
|
| 267 |
|
| 268 |
-
// 计算消息回合数(不包括最后一条)
|
| 269 |
-
var turnCount = len(openAIReq.Messages) / 2
|
| 270 |
-
fmt.Printf("消息回合数: %d\n", turnCount)
|
| 271 |
-
|
| 272 |
// 处理历史消息(不包括最后一条)
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
}
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
userShortFileName := generateShortFileName()
|
| 353 |
-
userTempFile := userShortFileName + ".txt"
|
| 354 |
-
|
| 355 |
-
// 写入文件
|
| 356 |
-
if err := os.WriteFile(userTempFile, addUTF8BOM(userMsg), 0644); err != nil {
|
| 357 |
-
fmt.Printf("创建用户临时文件失败: %v\n", err)
|
| 358 |
-
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 359 |
-
return
|
| 360 |
-
}
|
| 361 |
-
defer os.Remove(userTempFile)
|
| 362 |
-
|
| 363 |
-
// 上传用户消息文件
|
| 364 |
-
userUploadResp, err := uploadFile(dsToken, userTempFile)
|
| 365 |
-
if err != nil {
|
| 366 |
-
fmt.Printf("上传用户文件失败: %v\n", err)
|
| 367 |
-
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 368 |
-
return
|
| 369 |
-
}
|
| 370 |
-
|
| 371 |
-
// 添加用户文件源信息
|
| 372 |
-
sources = append(sources, map[string]interface{}{
|
| 373 |
-
"source_type": "user_file",
|
| 374 |
-
"filename": userUploadResp.Filename,
|
| 375 |
-
"user_filename": userUploadResp.UserFilename,
|
| 376 |
-
"size_bytes": len(userMsg),
|
| 377 |
-
})
|
| 378 |
-
|
| 379 |
-
fmt.Printf("使用文件形式添加长问题\n")
|
| 380 |
-
// 添加问答对到历史记录
|
| 381 |
-
chatHistory = append(chatHistory, ChatEntry{
|
| 382 |
-
Question: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(userUploadResp.UserFilename, ".txt")),
|
| 383 |
-
Answer: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(assistantUploadResp.UserFilename, ".txt")),
|
| 384 |
-
})
|
| 385 |
}
|
| 386 |
-
} else {
|
| 387 |
-
fmt.Printf("跳过消息索引 %d,它不是有效的用户-助手对\n", userMsgIndex)
|
| 388 |
}
|
| 389 |
}
|
| 390 |
|
| 391 |
-
//
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
fmt.Printf("
|
| 395 |
-
} else {
|
| 396 |
-
fmt.Printf("最后一条消息不是用户消息,跳过处理\n")
|
| 397 |
}
|
| 398 |
|
| 399 |
chatHistoryJSON, _ := json.Marshal(chatHistory)
|
|
|
|
| 257 |
// 打印OpenAI消息
|
| 258 |
fmt.Printf("\n=== 接收到的OpenAI消息 ===\n")
|
| 259 |
for i, msg := range openAIReq.Messages {
|
| 260 |
+
fmt.Printf("消息 %d: 角色=%s, 内容=%s\n", i, msg.Role, msg.Content)
|
| 261 |
}
|
| 262 |
fmt.Printf("===================\n\n")
|
| 263 |
|
|
|
|
| 265 |
var chatHistory []ChatEntry
|
| 266 |
var sources []map[string]interface{}
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
// 处理历史消息(不包括最后一条)
|
| 269 |
+
for i := 0; i < len(openAIReq.Messages)-1; i += 2 {
|
| 270 |
+
// 确保有足够的消息
|
| 271 |
+
if i+1 < len(openAIReq.Messages)-1 {
|
| 272 |
+
// 获取当前对话的用户和助手消息
|
| 273 |
+
userMsg := openAIReq.Messages[i]
|
| 274 |
+
assistantMsg := openAIReq.Messages[i+1]
|
| 275 |
+
|
| 276 |
+
fmt.Printf("处理对话对: 索引=%d-%d, 角色=%s-%s\n", i, i+1, userMsg.Role, assistantMsg.Role)
|
| 277 |
+
|
| 278 |
+
// 确保是用户-助手对
|
| 279 |
+
if userMsg.Role == "user" && assistantMsg.Role == "assistant" {
|
| 280 |
+
fmt.Printf("有效的用户-助手对话\n")
|
| 281 |
+
|
| 282 |
+
// 用户问题的内容
|
| 283 |
+
userContent := userMsg.Content
|
| 284 |
+
|
| 285 |
+
// 计算问题token数
|
| 286 |
+
questionTokenCount, _ := countTokens([]Message{userMsg})
|
| 287 |
+
fmt.Printf("用户问题token数: %d\n", questionTokenCount)
|
| 288 |
+
|
| 289 |
+
// 根据问题长度选择处理方式
|
| 290 |
+
if questionTokenCount < 30 {
|
| 291 |
+
// 短问题直接使用文字
|
| 292 |
+
fmt.Printf("短问题直接使用文字\n")
|
| 293 |
+
chatHistory = append(chatHistory, ChatEntry{
|
| 294 |
+
Question: userContent,
|
| 295 |
+
Answer: assistantMsg.Content,
|
| 296 |
+
})
|
| 297 |
+
} else {
|
| 298 |
+
// 长问题需要上传文件
|
| 299 |
+
fmt.Printf("长问题上传文件\n")
|
| 300 |
+
|
| 301 |
+
// 上传用户问题文件
|
| 302 |
+
_, err := getNonce(dsToken)
|
| 303 |
+
if err != nil {
|
| 304 |
+
fmt.Printf("获取nonce失败: %v\n", err)
|
| 305 |
+
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 306 |
+
return
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
// 创建用户问题临时文件
|
| 310 |
+
userShortFileName := generateShortFileName()
|
| 311 |
+
userTempFile := userShortFileName + ".txt"
|
| 312 |
+
|
| 313 |
+
if err := os.WriteFile(userTempFile, addUTF8BOM(userContent), 0644); err != nil {
|
| 314 |
+
fmt.Printf("创建用户问题文件失败: %v\n", err)
|
| 315 |
+
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 316 |
+
return
|
| 317 |
+
}
|
| 318 |
+
defer os.Remove(userTempFile)
|
| 319 |
+
|
| 320 |
+
// 上传用户文件
|
| 321 |
+
userUploadResp, err := uploadFile(dsToken, userTempFile)
|
| 322 |
+
if err != nil {
|
| 323 |
+
fmt.Printf("上传用户文件失败: %v\n", err)
|
| 324 |
+
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 325 |
+
return
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
// 添加用户文件源信息
|
| 329 |
+
sources = append(sources, map[string]interface{}{
|
| 330 |
+
"source_type": "user_file",
|
| 331 |
+
"filename": userUploadResp.Filename,
|
| 332 |
+
"user_filename": userUploadResp.UserFilename,
|
| 333 |
+
"size_bytes": len(userContent),
|
| 334 |
+
})
|
| 335 |
+
|
| 336 |
+
// 上传助手回答文件
|
| 337 |
+
_, err = getNonce(dsToken)
|
| 338 |
+
if err != nil {
|
| 339 |
+
fmt.Printf("获取nonce失败: %v\n", err)
|
| 340 |
+
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 341 |
+
return
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
// 创建助手回答临时文件
|
| 345 |
+
assistantShortFileName := generateShortFileName()
|
| 346 |
+
assistantTempFile := assistantShortFileName + ".txt"
|
| 347 |
+
|
| 348 |
+
if err := os.WriteFile(assistantTempFile, addUTF8BOM(assistantMsg.Content), 0644); err != nil {
|
| 349 |
+
fmt.Printf("创建助手回答文件失败: %v\n", err)
|
| 350 |
+
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 351 |
+
return
|
| 352 |
+
}
|
| 353 |
+
defer os.Remove(assistantTempFile)
|
| 354 |
+
|
| 355 |
+
// 上传助手文件
|
| 356 |
+
assistantUploadResp, err := uploadFile(dsToken, assistantTempFile)
|
| 357 |
+
if err != nil {
|
| 358 |
+
fmt.Printf("上传助手文件失败: %v\n", err)
|
| 359 |
+
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 360 |
+
return
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
// 添加助手文件源信息
|
| 364 |
+
sources = append(sources, map[string]interface{}{
|
| 365 |
+
"source_type": "user_file",
|
| 366 |
+
"filename": assistantUploadResp.Filename,
|
| 367 |
+
"user_filename": assistantUploadResp.UserFilename,
|
| 368 |
+
"size_bytes": len(assistantMsg.Content),
|
| 369 |
+
})
|
| 370 |
+
|
| 371 |
+
// 添加问答对
|
| 372 |
+
chatHistory = append(chatHistory, ChatEntry{
|
| 373 |
+
Question: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(userUploadResp.UserFilename, ".txt")),
|
| 374 |
+
Answer: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(assistantUploadResp.UserFilename, ".txt")),
|
| 375 |
+
})
|
| 376 |
}
|
| 377 |
+
} else {
|
| 378 |
+
fmt.Printf("跳过无效对话对: 角色=%s-%s\n", userMsg.Role, assistantMsg.Role)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
}
|
|
|
|
|
|
|
| 380 |
}
|
| 381 |
}
|
| 382 |
|
| 383 |
+
// 输出构建的聊天历史
|
| 384 |
+
fmt.Printf("聊天历史构建完成,共 %d 条记录\n", len(chatHistory))
|
| 385 |
+
for i, entry := range chatHistory {
|
| 386 |
+
fmt.Printf("历史 %d: Q=%s, A=%s\n", i, entry.Question, entry.Answer)
|
|
|
|
|
|
|
| 387 |
}
|
| 388 |
|
| 389 |
chatHistoryJSON, _ := json.Marshal(chatHistory)
|