Spaces:
Configuration error
Configuration error
BOHE
commited on
Commit
·
fed4dbc
1
Parent(s):
6a67df5
修改上下文处理方式
Browse files- api/main.go +76 -105
api/main.go
CHANGED
|
@@ -245,18 +245,14 @@ 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 lastAssistantMessage string
|
| 249 |
|
| 250 |
// 处理历史消息(不包括最后一条)
|
| 251 |
-
for
|
| 252 |
-
if
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
http.Error(w, "Failed to count tokens", http.StatusInternalServerError)
|
| 256 |
-
return
|
| 257 |
-
}
|
| 258 |
|
| 259 |
-
if
|
| 260 |
// 获取 nonce
|
| 261 |
nonceResp, err := getNonce(dsToken)
|
| 262 |
if err != nil {
|
|
@@ -267,7 +263,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 267 |
|
| 268 |
// 创建临时文件
|
| 269 |
tempFile := fmt.Sprintf("temp_%s.txt", nonceResp.Uuid)
|
| 270 |
-
if err := os.WriteFile(tempFile, []byte(
|
| 271 |
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 272 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 273 |
return
|
|
@@ -287,34 +283,51 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 287 |
"source_type": "user_file",
|
| 288 |
"filename": uploadResp.Filename,
|
| 289 |
"user_filename": uploadResp.UserFilename,
|
| 290 |
-
"size_bytes": len(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
})
|
| 292 |
|
| 293 |
// 在历史记录中使用文件引用
|
| 294 |
chatHistory = append(chatHistory, map[string]interface{}{
|
| 295 |
"question": fmt.Sprintf("Please review the attached file: %s", uploadResp.UserFilename),
|
| 296 |
-
"answer": "",
|
| 297 |
-
})
|
| 298 |
-
} else {
|
| 299 |
-
chatHistory = append(chatHistory, map[string]interface{}{
|
| 300 |
-
"question": msg.Content,
|
| 301 |
-
"answer": "",
|
| 302 |
})
|
| 303 |
}
|
| 304 |
-
} else if msg.Role == "assistant" {
|
| 305 |
-
// 只保存最后一条 assistant 消息
|
| 306 |
-
lastAssistantMessage = msg.Content
|
| 307 |
}
|
| 308 |
}
|
| 309 |
|
| 310 |
-
// 如果有最后一条 assistant 消息,添加到历史记录中
|
| 311 |
-
if lastAssistantMessage != "" {
|
| 312 |
-
chatHistory = append(chatHistory, map[string]interface{}{
|
| 313 |
-
"question": "",
|
| 314 |
-
"answer": lastAssistantMessage,
|
| 315 |
-
})
|
| 316 |
-
}
|
| 317 |
-
|
| 318 |
chatHistoryJSON, _ := json.Marshal(chatHistory)
|
| 319 |
|
| 320 |
// 创建 You.com API 请求
|
|
@@ -327,12 +340,40 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 327 |
|
| 328 |
// 处理最后一条消息
|
| 329 |
lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1]
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
if err != nil {
|
| 332 |
-
|
|
|
|
| 333 |
return
|
| 334 |
}
|
| 335 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
// 构建查询参数
|
| 337 |
q := youReq.URL.Query()
|
| 338 |
|
|
@@ -354,56 +395,12 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 354 |
q.Add("traceId", traceId)
|
| 355 |
q.Add("use_nested_youchat_updates", "true")
|
| 356 |
|
| 357 |
-
//
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
nonceResp, err := getNonce(dsToken)
|
| 361 |
-
if err != nil {
|
| 362 |
-
fmt.Printf("获取 nonce 失败: %v\n", err)
|
| 363 |
-
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 364 |
-
return
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
// 创建临时文件
|
| 368 |
-
tempFile := fmt.Sprintf("temp_%s.txt", nonceResp.Uuid)
|
| 369 |
-
if err := os.WriteFile(tempFile, []byte(lastMessage.Content), 0644); err != nil {
|
| 370 |
-
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 371 |
-
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 372 |
-
return
|
| 373 |
-
}
|
| 374 |
-
defer os.Remove(tempFile)
|
| 375 |
-
|
| 376 |
-
// 上传文件
|
| 377 |
-
uploadResp, err := uploadFile(dsToken, tempFile)
|
| 378 |
-
if err != nil {
|
| 379 |
-
fmt.Printf("上传文件失败: %v\n", err)
|
| 380 |
-
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 381 |
-
return
|
| 382 |
-
}
|
| 383 |
-
|
| 384 |
-
// 添加文件源信息
|
| 385 |
-
sources = append(sources, map[string]interface{}{
|
| 386 |
-
"source_type": "user_file",
|
| 387 |
-
"filename": uploadResp.Filename,
|
| 388 |
-
"user_filename": uploadResp.UserFilename,
|
| 389 |
-
"size_bytes": len(lastMessage.Content),
|
| 390 |
-
})
|
| 391 |
-
|
| 392 |
-
// 添加 sources 参数
|
| 393 |
-
sourcesJSON, _ := json.Marshal(sources)
|
| 394 |
-
q.Add("sources", string(sourcesJSON))
|
| 395 |
-
|
| 396 |
-
// 使用文件引用作为查询
|
| 397 |
-
q.Add("q", fmt.Sprintf("Please review the attached file: %s", uploadResp.UserFilename))
|
| 398 |
-
} else {
|
| 399 |
-
// 如果有之前上传的文件,添加 sources
|
| 400 |
-
if len(sources) > 0 {
|
| 401 |
-
sourcesJSON, _ := json.Marshal(sources)
|
| 402 |
-
q.Add("sources", string(sourcesJSON))
|
| 403 |
-
}
|
| 404 |
-
q.Add("q", lastMessage.Content)
|
| 405 |
-
}
|
| 406 |
|
|
|
|
|
|
|
| 407 |
q.Add("chat", string(chatHistoryJSON))
|
| 408 |
youReq.URL.RawQuery = q.Encode()
|
| 409 |
|
|
@@ -664,32 +661,6 @@ func uploadFile(dsToken, filePath string) (*UploadResponse, error) {
|
|
| 664 |
return &uploadResp, nil
|
| 665 |
}
|
| 666 |
|
| 667 |
-
// 计算消息的 token 数(使用字符估算方法)
|
| 668 |
-
func countTokens(messages []Message) (int, error) {
|
| 669 |
-
totalTokens := 0
|
| 670 |
-
for _, msg := range messages {
|
| 671 |
-
content := msg.Content
|
| 672 |
-
englishCount := 0
|
| 673 |
-
chineseCount := 0
|
| 674 |
-
|
| 675 |
-
// 遍历每个字符
|
| 676 |
-
for _, r := range content {
|
| 677 |
-
if r <= 127 { // ASCII 字符(英文和符号)
|
| 678 |
-
englishCount++
|
| 679 |
-
} else { // 非 ASCII 字符(中文等)
|
| 680 |
-
chineseCount++
|
| 681 |
-
}
|
| 682 |
-
}
|
| 683 |
-
|
| 684 |
-
// 计算 tokens:英文字符 * 0.3 + 中文字符 * 0.6
|
| 685 |
-
tokens := int(float64(englishCount)*0.3 + float64(chineseCount)*1)
|
| 686 |
-
|
| 687 |
-
// 加上角色名的 token(约 2 个)
|
| 688 |
-
totalTokens += tokens + 2
|
| 689 |
-
}
|
| 690 |
-
return totalTokens, nil
|
| 691 |
-
}
|
| 692 |
-
|
| 693 |
// 将 system 消息转换为第一条 user 消息
|
| 694 |
func convertSystemToUser(messages []Message) []Message {
|
| 695 |
if len(messages) == 0 {
|
|
|
|
| 245 |
// 构建 You.com 聊天历史
|
| 246 |
var chatHistory []map[string]interface{}
|
| 247 |
var sources []map[string]interface{}
|
|
|
|
| 248 |
|
| 249 |
// 处理历史消息(不包括最后一条)
|
| 250 |
+
for i := 0; i < len(openAIReq.Messages)-1; i += 2 {
|
| 251 |
+
if i+1 < len(openAIReq.Messages) {
|
| 252 |
+
userMsg := openAIReq.Messages[i]
|
| 253 |
+
assistantMsg := openAIReq.Messages[i+1]
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
+
if userMsg.Role == "user" && assistantMsg.Role == "assistant" {
|
| 256 |
// 获取 nonce
|
| 257 |
nonceResp, err := getNonce(dsToken)
|
| 258 |
if err != nil {
|
|
|
|
| 263 |
|
| 264 |
// 创建临时文件
|
| 265 |
tempFile := fmt.Sprintf("temp_%s.txt", nonceResp.Uuid)
|
| 266 |
+
if err := os.WriteFile(tempFile, []byte(userMsg.Content), 0644); err != nil {
|
| 267 |
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 268 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 269 |
return
|
|
|
|
| 283 |
"source_type": "user_file",
|
| 284 |
"filename": uploadResp.Filename,
|
| 285 |
"user_filename": uploadResp.UserFilename,
|
| 286 |
+
"size_bytes": len(userMsg.Content),
|
| 287 |
+
})
|
| 288 |
+
|
| 289 |
+
// 获取 nonce for assistant message
|
| 290 |
+
nonceRespAssistant, err := getNonce(dsToken)
|
| 291 |
+
if err != nil {
|
| 292 |
+
fmt.Printf("获取 nonce 失败: %v\n", err)
|
| 293 |
+
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 294 |
+
return
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
// 创建临时文件
|
| 298 |
+
tempFileAssistant := fmt.Sprintf("temp_%s.txt", nonceRespAssistant.Uuid)
|
| 299 |
+
if err := os.WriteFile(tempFileAssistant, []byte(assistantMsg.Content), 0644); err != nil {
|
| 300 |
+
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 301 |
+
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 302 |
+
return
|
| 303 |
+
}
|
| 304 |
+
defer os.Remove(tempFileAssistant)
|
| 305 |
+
|
| 306 |
+
// 上传文件
|
| 307 |
+
uploadRespAssistant, err := uploadFile(dsToken, tempFileAssistant)
|
| 308 |
+
if err != nil {
|
| 309 |
+
fmt.Printf("上传文件失败: %v\n", err)
|
| 310 |
+
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 311 |
+
return
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
// 添加文件源信息
|
| 315 |
+
sources = append(sources, map[string]interface{}{
|
| 316 |
+
"source_type": "user_file",
|
| 317 |
+
"filename": uploadRespAssistant.Filename,
|
| 318 |
+
"user_filename": uploadRespAssistant.UserFilename,
|
| 319 |
+
"size_bytes": len(assistantMsg.Content),
|
| 320 |
})
|
| 321 |
|
| 322 |
// 在历史记录中使用文件引用
|
| 323 |
chatHistory = append(chatHistory, map[string]interface{}{
|
| 324 |
"question": fmt.Sprintf("Please review the attached file: %s", uploadResp.UserFilename),
|
| 325 |
+
"answer": fmt.Sprintf("Please review the attached file: %s", uploadRespAssistant.UserFilename),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
})
|
| 327 |
}
|
|
|
|
|
|
|
|
|
|
| 328 |
}
|
| 329 |
}
|
| 330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
chatHistoryJSON, _ := json.Marshal(chatHistory)
|
| 332 |
|
| 333 |
// 创建 You.com API 请求
|
|
|
|
| 340 |
|
| 341 |
// 处理最后一条消息
|
| 342 |
lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1]
|
| 343 |
+
|
| 344 |
+
// 获取 nonce
|
| 345 |
+
nonceResp, err := getNonce(dsToken)
|
| 346 |
+
if err != nil {
|
| 347 |
+
fmt.Printf("获取 nonce 失败: %v\n", err)
|
| 348 |
+
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 349 |
+
return
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
// 创建临时文件
|
| 353 |
+
tempFile := fmt.Sprintf("temp_%s.txt", nonceResp.Uuid)
|
| 354 |
+
if err := os.WriteFile(tempFile, []byte(lastMessage.Content), 0644); err != nil {
|
| 355 |
+
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 356 |
+
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
| 357 |
+
return
|
| 358 |
+
}
|
| 359 |
+
defer os.Remove(tempFile)
|
| 360 |
+
|
| 361 |
+
// 上传文件
|
| 362 |
+
uploadResp, err := uploadFile(dsToken, tempFile)
|
| 363 |
if err != nil {
|
| 364 |
+
fmt.Printf("上传文件失败: %v\n", err)
|
| 365 |
+
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
| 366 |
return
|
| 367 |
}
|
| 368 |
|
| 369 |
+
// 添加文件源信息
|
| 370 |
+
sources = append(sources, map[string]interface{}{
|
| 371 |
+
"source_type": "user_file",
|
| 372 |
+
"filename": uploadResp.Filename,
|
| 373 |
+
"user_filename": uploadResp.UserFilename,
|
| 374 |
+
"size_bytes": len(lastMessage.Content),
|
| 375 |
+
})
|
| 376 |
+
|
| 377 |
// 构建查询参数
|
| 378 |
q := youReq.URL.Query()
|
| 379 |
|
|
|
|
| 395 |
q.Add("traceId", traceId)
|
| 396 |
q.Add("use_nested_youchat_updates", "true")
|
| 397 |
|
| 398 |
+
// 添加 sources 参数
|
| 399 |
+
sourcesJSON, _ := json.Marshal(sources)
|
| 400 |
+
q.Add("sources", string(sourcesJSON))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
// 使用文件引用作为查询
|
| 403 |
+
q.Add("q", fmt.Sprintf("Please review the attached file: %s", uploadResp.UserFilename))
|
| 404 |
q.Add("chat", string(chatHistoryJSON))
|
| 405 |
youReq.URL.RawQuery = q.Encode()
|
| 406 |
|
|
|
|
| 661 |
return &uploadResp, nil
|
| 662 |
}
|
| 663 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
// 将 system 消息转换为第一条 user 消息
|
| 665 |
func convertSystemToUser(messages []Message) []Message {
|
| 666 |
if len(messages) == 0 {
|