sanbo
commited on
Commit
·
833813a
1
Parent(s):
3b9a1cf
update sth at 2025-10-10 01:21:40
Browse files
main.go
CHANGED
|
@@ -1433,6 +1433,31 @@ func handleModels(w http.ResponseWriter, r *http.Request) {
|
|
| 1433 |
json.NewEncoder(w).Encode(response)
|
| 1434 |
}
|
| 1435 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1436 |
func handleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
| 1437 |
startTime := time.Now()
|
| 1438 |
path := r.URL.Path
|
|
@@ -1502,7 +1527,20 @@ func handleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
|
| 1502 |
debugLog("客户端未指定stream参数,使用默认值: %v", DEFAULT_STREAM)
|
| 1503 |
}
|
| 1504 |
|
| 1505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1506 |
|
| 1507 |
// 生成会话相关ID
|
| 1508 |
chatID := fmt.Sprintf("%d-%d", time.Now().UnixNano(), time.Now().Unix())
|
|
@@ -1518,7 +1556,7 @@ func handleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
|
| 1518 |
ChatID: chatID,
|
| 1519 |
ID: msgID,
|
| 1520 |
Model: getUpstreamModelID(req.Model), // 使用用户请求中的模型名称
|
| 1521 |
-
Messages:
|
| 1522 |
Params: map[string]interface{}{},
|
| 1523 |
Features: map[string]interface{}{
|
| 1524 |
"enable_thinking": enableThinking,
|
|
|
|
| 1433 |
json.NewEncoder(w).Encode(response)
|
| 1434 |
}
|
| 1435 |
|
| 1436 |
+
// 总结多条消息为一条上下文信息
|
| 1437 |
+
func summarizeMessages(messages []Message) Message {
|
| 1438 |
+
// 构建消息摘要文本
|
| 1439 |
+
var summaryBuilder strings.Builder
|
| 1440 |
+
summaryBuilder.WriteString("以下是前30轮对话的总结:\n\n")
|
| 1441 |
+
|
| 1442 |
+
for _, msg := range messages {
|
| 1443 |
+
if msg.Role == "system" {
|
| 1444 |
+
summaryBuilder.WriteString("系统提示:")
|
| 1445 |
+
} else if msg.Role == "user" {
|
| 1446 |
+
summaryBuilder.WriteString("用户:")
|
| 1447 |
+
} else if msg.Role == "assistant" {
|
| 1448 |
+
summaryBuilder.WriteString("助手:")
|
| 1449 |
+
}
|
| 1450 |
+
summaryBuilder.WriteString(msg.Content)
|
| 1451 |
+
summaryBuilder.WriteString("\n\n")
|
| 1452 |
+
}
|
| 1453 |
+
|
| 1454 |
+
// 生成最终的系统消息作为上下文摘要
|
| 1455 |
+
return Message{
|
| 1456 |
+
Role: "system",
|
| 1457 |
+
Content: summaryBuilder.String() + "请基于以上对话历史,继续回复用户的最新问题。",
|
| 1458 |
+
}
|
| 1459 |
+
}
|
| 1460 |
+
|
| 1461 |
func handleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
| 1462 |
startTime := time.Now()
|
| 1463 |
path := r.URL.Path
|
|
|
|
| 1527 |
debugLog("客户端未指定stream参数,使用默认值: %v", DEFAULT_STREAM)
|
| 1528 |
}
|
| 1529 |
|
| 1530 |
+
// 处理对话次数超限情况:超过30次自动总结
|
| 1531 |
+
var processedMessages []Message
|
| 1532 |
+
if len(req.Messages) > 30 {
|
| 1533 |
+
debugLog("对话次数超过30次,自动进行总结")
|
| 1534 |
+
// 总结前30条消息
|
| 1535 |
+
summaryMessage := summarizeMessages(req.Messages[:30])
|
| 1536 |
+
// 保留后面的消息
|
| 1537 |
+
processedMessages = append([]Message{summaryMessage}, req.Messages[30:]...)
|
| 1538 |
+
debugLog("总结完成,消息数从%d减少到%d", len(req.Messages), len(processedMessages))
|
| 1539 |
+
} else {
|
| 1540 |
+
processedMessages = req.Messages
|
| 1541 |
+
}
|
| 1542 |
+
|
| 1543 |
+
debugLog("请求解析成功 - 模型: %s, 流式: %v, 消息数: %d", req.Model, req.Stream, len(processedMessages))
|
| 1544 |
|
| 1545 |
// 生成会话相关ID
|
| 1546 |
chatID := fmt.Sprintf("%d-%d", time.Now().UnixNano(), time.Now().Unix())
|
|
|
|
| 1556 |
ChatID: chatID,
|
| 1557 |
ID: msgID,
|
| 1558 |
Model: getUpstreamModelID(req.Model), // 使用用户请求中的模型名称
|
| 1559 |
+
Messages: processedMessages, // 使用处理后的消息列表
|
| 1560 |
Params: map[string]interface{}{},
|
| 1561 |
Features: map[string]interface{}{
|
| 1562 |
"enable_thinking": enableThinking,
|