mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	fix: fixed bug for chat request's token reach the max token limit for OpenAI
This commit is contained in:
		@@ -53,3 +53,10 @@ type ApiError struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const PromptMsg = "prompt" // prompt message
 | 
					const PromptMsg = "prompt" // prompt message
 | 
				
			||||||
const ReplyMsg = "reply"   // reply message
 | 
					const ReplyMsg = "reply"   // reply message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var ModelToTokens = map[string]int{
 | 
				
			||||||
 | 
						"gpt-3.5-turbo":     4096,
 | 
				
			||||||
 | 
						"gpt-3.5-turbo-16k": 16384,
 | 
				
			||||||
 | 
						"gpt-4":             8192,
 | 
				
			||||||
 | 
						"gpt-4-32k":         32768,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -180,21 +180,37 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
 | 
				
			|||||||
		if h.App.ChatContexts.Has(session.ChatId) {
 | 
							if h.App.ChatContexts.Has(session.ChatId) {
 | 
				
			||||||
			chatCtx = h.App.ChatContexts.Get(session.ChatId)
 | 
								chatCtx = h.App.ChatContexts.Get(session.ChatId)
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			// 加载角色信息
 | 
								// calculate the tokens of current request, to prevent to exceeding the max tokens num
 | 
				
			||||||
 | 
								tokens := req.MaxTokens
 | 
				
			||||||
 | 
								for _, f := range types.InnerFunctions {
 | 
				
			||||||
 | 
									tks, _ := utils.CalcTokens(utils.JsonEncode(f), req.Model)
 | 
				
			||||||
 | 
									tokens += tks
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// loading the role context
 | 
				
			||||||
			var messages []types.Message
 | 
								var messages []types.Message
 | 
				
			||||||
			err := utils.JsonDecode(role.Context, &messages)
 | 
								err := utils.JsonDecode(role.Context, &messages)
 | 
				
			||||||
			if err == nil {
 | 
								if err == nil {
 | 
				
			||||||
				for _, v := range messages {
 | 
									for _, v := range messages {
 | 
				
			||||||
 | 
										tks, _ := utils.CalcTokens(v.Content, req.Model)
 | 
				
			||||||
 | 
										if tokens+tks >= types.ModelToTokens[req.Model] {
 | 
				
			||||||
 | 
											break
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										tokens += tks
 | 
				
			||||||
					chatCtx = append(chatCtx, v)
 | 
										chatCtx = append(chatCtx, v)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// 加载最近的聊天记录作为聊天上下文
 | 
								// loading recent chat history as chat context
 | 
				
			||||||
			if chatConfig.ContextDeep > 0 {
 | 
								if chatConfig.ContextDeep > 0 {
 | 
				
			||||||
				var historyMessages []model.HistoryMessage
 | 
									var historyMessages []model.HistoryMessage
 | 
				
			||||||
				res := h.db.Where("chat_id = ? and use_context = 1", session.ChatId).Limit(2).Order("created_at desc").Find(&historyMessages)
 | 
									res := h.db.Where("chat_id = ? and use_context = 1", session.ChatId).Limit(chatConfig.ContextDeep).Order("created_at desc").Find(&historyMessages)
 | 
				
			||||||
				if res.Error == nil {
 | 
									if res.Error == nil {
 | 
				
			||||||
					for _, msg := range historyMessages {
 | 
										for _, msg := range historyMessages {
 | 
				
			||||||
 | 
											if tokens+msg.Tokens >= types.ModelToTokens[session.Model] {
 | 
				
			||||||
 | 
												break
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
											tokens += msg.Tokens
 | 
				
			||||||
						ms := types.Message{Role: "user", Content: msg.Content}
 | 
											ms := types.Message{Role: "user", Content: msg.Content}
 | 
				
			||||||
						if msg.Type == types.ReplyMsg {
 | 
											if msg.Type == types.ReplyMsg {
 | 
				
			||||||
							ms.Role = "assistant"
 | 
												ms.Role = "assistant"
 | 
				
			||||||
@@ -204,7 +220,6 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
		logger.Debugf("聊天上下文:%+v", chatCtx)
 | 
							logger.Debugf("聊天上下文:%+v", chatCtx)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	reqMgs := make([]interface{}, 0)
 | 
						reqMgs := make([]interface{}, 0)
 | 
				
			||||||
@@ -459,13 +474,9 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
 | 
				
			|||||||
		} else if strings.Contains(res.Error.Message, "You exceeded your current quota") {
 | 
							} else if strings.Contains(res.Error.Message, "You exceeded your current quota") {
 | 
				
			||||||
			replyMessage(ws, "请求 OpenAI API 失败:API KEY 触发并发限制,请稍后再试。")
 | 
								replyMessage(ws, "请求 OpenAI API 失败:API KEY 触发并发限制,请稍后再试。")
 | 
				
			||||||
		} else if strings.Contains(res.Error.Message, "This model's maximum context length") {
 | 
							} else if strings.Contains(res.Error.Message, "This model's maximum context length") {
 | 
				
			||||||
			replyMessage(ws, "当前会话上下文长度超出限制,已为您删减会话上下文!")
 | 
								logger.Error(res.Error.Message)
 | 
				
			||||||
			// 只保留最近的三条记录
 | 
								replyMessage(ws, "当前会话上下文长度超出限制,已为您清空会话上下文!")
 | 
				
			||||||
			chatContext := h.App.ChatContexts.Get(session.ChatId)
 | 
								h.App.ChatContexts.Delete(session.ChatId)
 | 
				
			||||||
			if len(chatContext) > 3 {
 | 
					 | 
				
			||||||
				chatContext = chatContext[len(chatContext)-3:]
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			h.App.ChatContexts.Put(session.ChatId, chatContext)
 | 
					 | 
				
			||||||
			return h.sendMessage(ctx, session, role, prompt, ws)
 | 
								return h.sendMessage(ctx, session, role, prompt, ws)
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			replyMessage(ws, "请求 OpenAI API 失败:"+res.Error.Message)
 | 
								replyMessage(ws, "请求 OpenAI API 失败:"+res.Error.Message)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -274,7 +274,7 @@ const title = ref('ChatGPT-智能助手');
 | 
				
			|||||||
const logo = 'images/logo.png';
 | 
					const logo = 'images/logo.png';
 | 
				
			||||||
const rewardImg = ref('images/reward.png')
 | 
					const rewardImg = ref('images/reward.png')
 | 
				
			||||||
const models = ref([])
 | 
					const models = ref([])
 | 
				
			||||||
const model = ref('gpt-3.5-turbo')
 | 
					const model = ref('gpt-3.5-turbo-16k')
 | 
				
			||||||
const chatData = ref([]);
 | 
					const chatData = ref([]);
 | 
				
			||||||
const allChats = ref([]); // 会话列表
 | 
					const allChats = ref([]); // 会话列表
 | 
				
			||||||
const chatList = ref(allChats.value);
 | 
					const chatList = ref(allChats.value);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user