Spaces:
Configuration error
Configuration error
BOHE
commited on
Commit
·
42a0ec7
1
Parent(s):
a18777c
修改上下文处理方式测试
Browse files- api/main.go +149 -146
api/main.go
CHANGED
|
@@ -266,154 +266,157 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 266 |
var sources []map[string]interface{}
|
| 267 |
|
| 268 |
// 处理历史消息(不包括最后一条)
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
//
|
| 279 |
-
if
|
| 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 |
-
if err := os.WriteFile(assistantTempFile, addUTF8BOM(assistantMsg.Content), 0644); err != nil {
|
| 307 |
-
fmt.Printf("创建助手回答文件失败: %v\n", err)
|
| 308 |
-
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 309 |
-
return
|
| 310 |
-
}
|
| 311 |
-
defer os.Remove(assistantTempFile)
|
| 312 |
-
|
| 313 |
-
// 上传助手文件
|
| 314 |
-
assistantUploadResp, err := uploadFile(dsToken, assistantTempFile)
|
| 315 |
-
if err != nil {
|
| 316 |
-
fmt.Printf("上传助手文件失败: %v\n", err)
|
| 317 |
-
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 318 |
-
return
|
| 319 |
-
}
|
| 320 |
-
|
| 321 |
-
// 添加助手文件源信息
|
| 322 |
-
sources = append(sources, map[string]interface{}{
|
| 323 |
-
"source_type": "user_file",
|
| 324 |
-
"filename": assistantUploadResp.Filename,
|
| 325 |
-
"user_filename": assistantUploadResp.UserFilename,
|
| 326 |
-
"size_bytes": len(assistantMsg.Content),
|
| 327 |
-
})
|
| 328 |
-
|
| 329 |
-
// 短问题+上传的助手回答
|
| 330 |
-
chatHistory = append(chatHistory, ChatEntry{
|
| 331 |
-
Question: userContent,
|
| 332 |
-
Answer: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(assistantUploadResp.UserFilename, ".txt")),
|
| 333 |
-
})
|
| 334 |
-
} else {
|
| 335 |
-
// 长问题需要上传文件
|
| 336 |
-
fmt.Printf("长问题上传文件\n")
|
| 337 |
-
|
| 338 |
-
// 上传用户问题文件
|
| 339 |
-
_, err := getNonce(dsToken)
|
| 340 |
-
if err != nil {
|
| 341 |
-
fmt.Printf("获取nonce失败: %v\n", err)
|
| 342 |
-
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 343 |
-
return
|
| 344 |
-
}
|
| 345 |
-
|
| 346 |
-
// 创建用户问题临时文件
|
| 347 |
-
userShortFileName := generateShortFileName()
|
| 348 |
-
userTempFile := userShortFileName + ".txt"
|
| 349 |
-
|
| 350 |
-
if err := os.WriteFile(userTempFile, addUTF8BOM(userContent), 0644); err != nil {
|
| 351 |
-
fmt.Printf("创建用户问题文件失败: %v\n", err)
|
| 352 |
-
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 353 |
-
return
|
| 354 |
-
}
|
| 355 |
-
defer os.Remove(userTempFile)
|
| 356 |
-
|
| 357 |
-
// 上传用户文件
|
| 358 |
-
userUploadResp, err := uploadFile(dsToken, userTempFile)
|
| 359 |
-
if err != nil {
|
| 360 |
-
fmt.Printf("上传用户文件失败: %v\n", err)
|
| 361 |
-
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 362 |
-
return
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
// 添加用户文件源信息
|
| 366 |
-
sources = append(sources, map[string]interface{}{
|
| 367 |
-
"source_type": "user_file",
|
| 368 |
-
"filename": userUploadResp.Filename,
|
| 369 |
-
"user_filename": userUploadResp.UserFilename,
|
| 370 |
-
"size_bytes": len(userContent),
|
| 371 |
-
})
|
| 372 |
-
|
| 373 |
-
// 上传助手回答文件
|
| 374 |
-
_, err = getNonce(dsToken)
|
| 375 |
-
if err != nil {
|
| 376 |
-
fmt.Printf("获取nonce失败: %v\n", err)
|
| 377 |
-
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 378 |
-
return
|
| 379 |
-
}
|
| 380 |
-
|
| 381 |
-
// 创建助手回答临时文件
|
| 382 |
-
assistantShortFileName := generateShortFileName()
|
| 383 |
-
assistantTempFile := assistantShortFileName + ".txt"
|
| 384 |
-
|
| 385 |
-
if err := os.WriteFile(assistantTempFile, addUTF8BOM(assistantMsg.Content), 0644); err != nil {
|
| 386 |
-
fmt.Printf("创建助手回答文件失败: %v\n", err)
|
| 387 |
-
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 388 |
-
return
|
| 389 |
-
}
|
| 390 |
-
defer os.Remove(assistantTempFile)
|
| 391 |
-
|
| 392 |
-
// 上传助手文件
|
| 393 |
-
assistantUploadResp, err := uploadFile(dsToken, assistantTempFile)
|
| 394 |
-
if err != nil {
|
| 395 |
-
fmt.Printf("上传助手文件失败: %v\n", err)
|
| 396 |
-
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 397 |
-
return
|
| 398 |
-
}
|
| 399 |
-
|
| 400 |
-
// 添加助手文件源信息
|
| 401 |
-
sources = append(sources, map[string]interface{}{
|
| 402 |
-
"source_type": "user_file",
|
| 403 |
-
"filename": assistantUploadResp.Filename,
|
| 404 |
-
"user_filename": assistantUploadResp.UserFilename,
|
| 405 |
-
"size_bytes": len(assistantMsg.Content),
|
| 406 |
-
})
|
| 407 |
-
|
| 408 |
-
// 添加问答对
|
| 409 |
-
chatHistory = append(chatHistory, ChatEntry{
|
| 410 |
-
Question: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(userUploadResp.UserFilename, ".txt")),
|
| 411 |
-
Answer: fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(assistantUploadResp.UserFilename, ".txt")),
|
| 412 |
-
})
|
| 413 |
-
}
|
| 414 |
} else {
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
}
|
| 418 |
}
|
| 419 |
|
|
|
|
| 266 |
var sources []map[string]interface{}
|
| 267 |
|
| 268 |
// 处理历史消息(不包括最后一条)
|
| 269 |
+
var currentQuestion string
|
| 270 |
+
var currentAnswer string
|
| 271 |
+
var hasQuestion bool
|
| 272 |
+
var hasAnswer bool
|
| 273 |
+
|
| 274 |
+
for i := 0; i < len(openAIReq.Messages)-1; i++ {
|
| 275 |
+
msg := openAIReq.Messages[i]
|
| 276 |
+
|
| 277 |
+
if msg.Role == "user" {
|
| 278 |
+
// 如果已经有问题和回答,添加到历史
|
| 279 |
+
if hasQuestion && hasAnswer {
|
| 280 |
+
chatHistory = append(chatHistory, ChatEntry{
|
| 281 |
+
Question: currentQuestion,
|
| 282 |
+
Answer: currentAnswer,
|
| 283 |
+
})
|
| 284 |
+
// 重置状态
|
| 285 |
+
currentQuestion = msg.Content
|
| 286 |
+
currentAnswer = ""
|
| 287 |
+
hasQuestion = true
|
| 288 |
+
hasAnswer = false
|
| 289 |
+
} else if hasQuestion {
|
| 290 |
+
// 如果已经有问题但没有回答,合并问题
|
| 291 |
+
currentQuestion += "\n" + msg.Content
|
| 292 |
+
} else {
|
| 293 |
+
// 新的问题
|
| 294 |
+
currentQuestion = msg.Content
|
| 295 |
+
hasQuestion = true
|
| 296 |
+
}
|
| 297 |
+
} else if msg.Role == "assistant" {
|
| 298 |
+
if hasQuestion {
|
| 299 |
+
// 如果有问题,设置回答
|
| 300 |
+
currentAnswer = msg.Content
|
| 301 |
+
hasAnswer = true
|
| 302 |
+
} else if hasAnswer {
|
| 303 |
+
// 如果已经有回答但没有问题,合并回答
|
| 304 |
+
currentAnswer += "\n" + msg.Content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
} else {
|
| 306 |
+
// 没有问题的回答,创建空问题
|
| 307 |
+
currentQuestion = ""
|
| 308 |
+
currentAnswer = msg.Content
|
| 309 |
+
hasQuestion = true
|
| 310 |
+
hasAnswer = true
|
| 311 |
+
}
|
| 312 |
+
}
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
// 添加最后一对问答(如果有)
|
| 316 |
+
if hasQuestion {
|
| 317 |
+
if hasAnswer {
|
| 318 |
+
chatHistory = append(chatHistory, ChatEntry{
|
| 319 |
+
Question: currentQuestion,
|
| 320 |
+
Answer: currentAnswer,
|
| 321 |
+
})
|
| 322 |
+
} else {
|
| 323 |
+
// 有问题但没有回答,添加空回答
|
| 324 |
+
chatHistory = append(chatHistory, ChatEntry{
|
| 325 |
+
Question: currentQuestion,
|
| 326 |
+
Answer: "",
|
| 327 |
+
})
|
| 328 |
+
}
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
// 处理聊天历史中的每个条目,上传文件
|
| 332 |
+
for i := range chatHistory {
|
| 333 |
+
entry := &chatHistory[i]
|
| 334 |
+
|
| 335 |
+
// 处理问题
|
| 336 |
+
if entry.Question != "" {
|
| 337 |
+
questionTokenCount, _ := countTokens([]Message{{Role: "user", Content: entry.Question}})
|
| 338 |
+
|
| 339 |
+
// 如果问题较长,上传为文件
|
| 340 |
+
if questionTokenCount >= 30 {
|
| 341 |
+
// 获取nonce
|
| 342 |
+
_, err := getNonce(dsToken)
|
| 343 |
+
if err != nil {
|
| 344 |
+
fmt.Printf("获取nonce失败: %v\n", err)
|
| 345 |
+
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 346 |
+
return
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
// 创建问题临时文件
|
| 350 |
+
questionShortFileName := generateShortFileName()
|
| 351 |
+
questionTempFile := questionShortFileName + ".txt"
|
| 352 |
+
|
| 353 |
+
if err := os.WriteFile(questionTempFile, addUTF8BOM(entry.Question), 0644); err != nil {
|
| 354 |
+
fmt.Printf("创建问题文件失败: %v\n", err)
|
| 355 |
+
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 356 |
+
return
|
| 357 |
+
}
|
| 358 |
+
defer os.Remove(questionTempFile)
|
| 359 |
+
|
| 360 |
+
// 上传问题文件
|
| 361 |
+
questionUploadResp, err := uploadFile(dsToken, questionTempFile)
|
| 362 |
+
if err != nil {
|
| 363 |
+
fmt.Printf("上传问题文件失败: %v\n", err)
|
| 364 |
+
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 365 |
+
return
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
// 添加问题文件源信息
|
| 369 |
+
sources = append(sources, map[string]interface{}{
|
| 370 |
+
"source_type": "user_file",
|
| 371 |
+
"filename": questionUploadResp.Filename,
|
| 372 |
+
"user_filename": questionUploadResp.UserFilename,
|
| 373 |
+
"size_bytes": len(entry.Question),
|
| 374 |
+
})
|
| 375 |
+
|
| 376 |
+
// 更新问题为文件引用
|
| 377 |
+
entry.Question = fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(questionUploadResp.UserFilename, ".txt"))
|
| 378 |
+
}
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
// 处理回答
|
| 382 |
+
if entry.Answer != "" {
|
| 383 |
+
// 获取nonce
|
| 384 |
+
_, err := getNonce(dsToken)
|
| 385 |
+
if err != nil {
|
| 386 |
+
fmt.Printf("获取nonce失败: %v\n", err)
|
| 387 |
+
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 388 |
+
return
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
// 创建回答临时文件
|
| 392 |
+
answerShortFileName := generateShortFileName()
|
| 393 |
+
answerTempFile := answerShortFileName + ".txt"
|
| 394 |
+
|
| 395 |
+
if err := os.WriteFile(answerTempFile, addUTF8BOM(entry.Answer), 0644); err != nil {
|
| 396 |
+
fmt.Printf("创建回答文件失败: %v\n", err)
|
| 397 |
+
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 398 |
+
return
|
| 399 |
}
|
| 400 |
+
defer os.Remove(answerTempFile)
|
| 401 |
+
|
| 402 |
+
// 上传回答文件
|
| 403 |
+
answerUploadResp, err := uploadFile(dsToken, answerTempFile)
|
| 404 |
+
if err != nil {
|
| 405 |
+
fmt.Printf("上传回答文件失败: %v\n", err)
|
| 406 |
+
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 407 |
+
return
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
// 添加回答文件源信息
|
| 411 |
+
sources = append(sources, map[string]interface{}{
|
| 412 |
+
"source_type": "user_file",
|
| 413 |
+
"filename": answerUploadResp.Filename,
|
| 414 |
+
"user_filename": answerUploadResp.UserFilename,
|
| 415 |
+
"size_bytes": len(entry.Answer),
|
| 416 |
+
})
|
| 417 |
+
|
| 418 |
+
// 更新回答为文件引用
|
| 419 |
+
entry.Answer = fmt.Sprintf("查看这个文件并且直接与文件内容进行聊天:%s.txt", strings.TrimSuffix(answerUploadResp.UserFilename, ".txt"))
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|