Spaces:
Configuration error
Configuration error
BOHE
commited on
Commit
·
432aa95
1
Parent(s):
88916e2
修改上下文处理方式测试
Browse files- api/main.go +45 -16
api/main.go
CHANGED
|
@@ -6,6 +6,7 @@ import (
|
|
| 6 |
"encoding/json"
|
| 7 |
"fmt"
|
| 8 |
"io"
|
|
|
|
| 9 |
"mime/multipart"
|
| 10 |
"net/http"
|
| 11 |
"os"
|
|
@@ -16,6 +17,11 @@ import (
|
|
| 16 |
"github.com/google/uuid"
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
// TokenCount 定义了 token 计数的结构
|
| 20 |
type TokenCount struct {
|
| 21 |
PromptTokens int `json:"prompt_tokens"`
|
|
@@ -261,16 +267,19 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 261 |
}
|
| 262 |
|
| 263 |
// 如果没有对应的助手消息,则创建一个问答对
|
| 264 |
-
// 获取 nonce 用于上传文件
|
| 265 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -306,16 +315,19 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 306 |
if i > 0 && openAIReq.Messages[i-1].Role == "user" {
|
| 307 |
currentQuestion = openAIReq.Messages[i-1].Content
|
| 308 |
|
| 309 |
-
// 获取 nonce 用于上传用户消息文件
|
| 310 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -339,16 +351,19 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 339 |
"size_bytes": len(currentQuestion),
|
| 340 |
})
|
| 341 |
|
| 342 |
-
// 获取 nonce 用于上传助手消息文件
|
| 343 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -422,16 +437,19 @@ func Handler(w http.ResponseWriter, r *http.Request) {
|
|
| 422 |
|
| 423 |
// 如果最后一条消息超过限制,使用文件上传
|
| 424 |
if lastMessageTokens > MaxContextTokens {
|
| 425 |
-
// 获取 nonce
|
| 426 |
-
|
| 427 |
if err != nil {
|
| 428 |
fmt.Printf("获取 nonce 失败: %v\n", err)
|
| 429 |
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 430 |
return
|
| 431 |
}
|
| 432 |
|
| 433 |
-
//
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
| 435 |
if err := os.WriteFile(tempFile, []byte(lastMessage.Content), 0644); err != nil {
|
| 436 |
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 437 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
|
@@ -693,6 +711,17 @@ func getNonce(dsToken string) (*NonceResponse, error) {
|
|
| 693 |
}, nil
|
| 694 |
}
|
| 695 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
// 上传文件
|
| 697 |
func uploadFile(dsToken, filePath string) (*UploadResponse, error) {
|
| 698 |
file, err := os.Open(filePath)
|
|
|
|
| 6 |
"encoding/json"
|
| 7 |
"fmt"
|
| 8 |
"io"
|
| 9 |
+
"math/rand"
|
| 10 |
"mime/multipart"
|
| 11 |
"net/http"
|
| 12 |
"os"
|
|
|
|
| 17 |
"github.com/google/uuid"
|
| 18 |
)
|
| 19 |
|
| 20 |
+
func init() {
|
| 21 |
+
// 初始化随机数生成器
|
| 22 |
+
rand.Seed(time.Now().UnixNano())
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
// TokenCount 定义了 token 计数的结构
|
| 26 |
type TokenCount struct {
|
| 27 |
PromptTokens int `json:"prompt_tokens"`
|
|
|
|
| 267 |
}
|
| 268 |
|
| 269 |
// 如果没有对应的助手消息,则创建一个问答对
|
| 270 |
+
// 获取 nonce 用于上传文件 - 不再需要nonce
|
| 271 |
+
_, err := getNonce(dsToken) // 仍然调用以保持API流程,但不使用返回值
|
| 272 |
if err != nil {
|
| 273 |
fmt.Printf("获取 nonce 失败: %v\n", err)
|
| 274 |
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 275 |
return
|
| 276 |
}
|
| 277 |
|
| 278 |
+
// 创建用户消息临时文件并上传,使用短文件名
|
| 279 |
+
shortFileName := generateShortFileName()
|
| 280 |
+
userTempFile := shortFileName + ".txt"
|
| 281 |
+
|
| 282 |
+
// 确保使用UTF-8编码写入文件
|
| 283 |
if err := os.WriteFile(userTempFile, []byte(currentQuestion), 0644); err != nil {
|
| 284 |
fmt.Printf("创建用户临时文件失败: %v\n", err)
|
| 285 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
|
|
|
| 315 |
if i > 0 && openAIReq.Messages[i-1].Role == "user" {
|
| 316 |
currentQuestion = openAIReq.Messages[i-1].Content
|
| 317 |
|
| 318 |
+
// 获取 nonce 用于上传用户消息文件 - 不再需要nonce
|
| 319 |
+
_, err := getNonce(dsToken) // 仍然调用以保持API流程,但不使用返回值
|
| 320 |
if err != nil {
|
| 321 |
fmt.Printf("获取用户消息 nonce 失败: %v\n", err)
|
| 322 |
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 323 |
return
|
| 324 |
}
|
| 325 |
|
| 326 |
+
// 创建用户消息临时文件并上传,使用短文件名
|
| 327 |
+
userShortFileName := generateShortFileName()
|
| 328 |
+
userTempFile := userShortFileName + ".txt"
|
| 329 |
+
|
| 330 |
+
// 确保使用UTF-8编码写入文件
|
| 331 |
if err := os.WriteFile(userTempFile, []byte(currentQuestion), 0644); err != nil {
|
| 332 |
fmt.Printf("创建用户临时文件失败: %v\n", err)
|
| 333 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
|
|
|
| 351 |
"size_bytes": len(currentQuestion),
|
| 352 |
})
|
| 353 |
|
| 354 |
+
// 获取 nonce 用于上传助手消息文件 - 不再需要nonce
|
| 355 |
+
_, err = getNonce(dsToken) // 仍然调用以保持API流程,但不使用返回值
|
| 356 |
if err != nil {
|
| 357 |
fmt.Printf("获取助手消息 nonce 失败: %v\n", err)
|
| 358 |
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 359 |
return
|
| 360 |
}
|
| 361 |
|
| 362 |
+
// 创建助手消息临时文件并上传,使用短文件名
|
| 363 |
+
assistantShortFileName := generateShortFileName()
|
| 364 |
+
assistantTempFile := assistantShortFileName + ".txt"
|
| 365 |
+
|
| 366 |
+
// 确保使用UTF-8编码写入文件
|
| 367 |
if err := os.WriteFile(assistantTempFile, []byte(currentAnswer), 0644); err != nil {
|
| 368 |
fmt.Printf("创建助手临时文件失败: %v\n", err)
|
| 369 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
|
|
|
| 437 |
|
| 438 |
// 如果最后一条消息超过限制,使用文件上传
|
| 439 |
if lastMessageTokens > MaxContextTokens {
|
| 440 |
+
// 获取 nonce - 不再需要nonce
|
| 441 |
+
_, err := getNonce(dsToken) // 仍然调用以保持API流程,但不使用返回值
|
| 442 |
if err != nil {
|
| 443 |
fmt.Printf("获取 nonce 失败: %v\n", err)
|
| 444 |
http.Error(w, "Failed to get nonce", http.StatusInternalServerError)
|
| 445 |
return
|
| 446 |
}
|
| 447 |
|
| 448 |
+
// 创建临时文件,使用短文件名
|
| 449 |
+
shortFileName := generateShortFileName()
|
| 450 |
+
tempFile := shortFileName + ".txt"
|
| 451 |
+
|
| 452 |
+
// 确保使用UTF-8编码写入文件
|
| 453 |
if err := os.WriteFile(tempFile, []byte(lastMessage.Content), 0644); err != nil {
|
| 454 |
fmt.Printf("创建临时文件失败: %v\n", err)
|
| 455 |
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
|
|
|
|
| 711 |
}, nil
|
| 712 |
}
|
| 713 |
|
| 714 |
+
// 生成短文件名
|
| 715 |
+
func generateShortFileName() string {
|
| 716 |
+
// 生成10位随机字母数字字符串
|
| 717 |
+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
| 718 |
+
result := make([]byte, 10)
|
| 719 |
+
for i := range result {
|
| 720 |
+
result[i] = charset[rand.Intn(len(charset))]
|
| 721 |
+
}
|
| 722 |
+
return string(result)
|
| 723 |
+
}
|
| 724 |
+
|
| 725 |
// 上传文件
|
| 726 |
func uploadFile(dsToken, filePath string) (*UploadResponse, error) {
|
| 727 |
file, err := os.Open(filePath)
|