Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: 增强模型选择和错误处理机制

wizardchen884153aeb88b
diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue
index c5ecccf18..583459aa5 100644
--- a/frontend/src/components/Input-field.vue
+++ b/frontend/src/components/Input-field.vue
@@ -354,12 +354,14 @@ const loadConversationConfig = async () => {
try {
const response = await getConversationConfig();
conversationConfig.value = response.data;
+ const modelId = response.data?.summary_model_id || '';
settingsStore.updateConversationModels({
- summaryModelId: response.data?.summary_model_id || '',
+ summaryModelId: modelId,
+ selectedChatModelId: modelId,
rerankModelId: response.data?.rerank_model_id || '',
});
if (!selectedModelId.value) {
- selectedModelId.value = response.data?.summary_model_id || '';
+ selectedModelId.value = modelId;
}
ensureModelSelection();
} catch (error) {
@@ -437,6 +439,7 @@ const handleModelChange = async (value: string | number | Array<string | number>
// 同步到 store
settingsStore.updateConversationModels({
summaryModelId: val,
+ selectedChatModelId: val,
rerankModelId: conversationConfig.value?.rerank_model_id || '',
});
diff --git a/frontend/src/stores/menu.ts b/frontend/src/stores/menu.ts
index 5f008c8ec..2b165e6f1 100644
--- a/frontend/src/stores/menu.ts
+++ b/frontend/src/stores/menu.ts
@@ -34,6 +34,7 @@ export const useMenuStore = defineStore('menuStore', () => {
const isFirstSession = ref(false)
const firstQuery = ref('')
const firstMentionedItems = ref<any[]>([])
+ const firstModelId = ref('')
const applyMenuTranslations = () => {
menuArr.forEach(item => {
@@ -92,9 +93,10 @@ export const useMenuStore = defineStore('menuStore', () => {
isFirstSession.value = payload
}
- const changeFirstQuery = (payload: string, mentionedItems: any[] = []) => {
+ const changeFirstQuery = (payload: string, mentionedItems: any[] = [], modelId: string = '') => {
firstQuery.value = payload
firstMentionedItems.value = mentionedItems
+ firstModelId.value = modelId
}
return {
@@ -102,6 +104,7 @@ export const useMenuStore = defineStore('menuStore', () => {
isFirstSession,
firstQuery,
firstMentionedItems,
+ firstModelId,
clearMenuArr,
updatemenuArr,
updataMenuChildren,
diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue
index 88e60b417..ebe4bcd84 100644
--- a/frontend/src/views/chat/index.vue
+++ b/frontend/src/views/chat/index.vue
@@ -61,7 +61,7 @@ const useSettingsStoreInstance = useSettingsStore();
const uiStore = useUIStore();
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
const { t } = useI18n();
-const { menuArr, isFirstSession, firstQuery, firstMentionedItems } = storeToRefs(usemenuStore);
+const { menuArr, isFirstSession, firstQuery, firstMentionedItems, firstModelId } = storeToRefs(usemenuStore);
const { output, onChunk, isStreaming, isLoading, error, startStream, stopStream } = useStream();
const route = useRoute();
const router = useRouter();
@@ -672,13 +672,21 @@ const handleAgentChunk = (data) => {
// If this is an error response without tool data, handle it
if (data.response_type === 'error' && !toolName) {
- message.content = data.content || t('chat.processError');
+ const errorMsg = data.content || t('chat.processError');
+ message.content = errorMsg;
isReplying.value = false;
+ loading.value = false;
+ MessagePlugin.error(errorMsg);
+ console.error('[Chat Error]', errorMsg);
}
} else if (data.response_type === 'error') {
// Generic error without tool context
- message.content = data.content || t('chat.processError');
+ const errorMsg = data.content || t('chat.processError');
+ message.content = errorMsg;
isReplying.value = false;
+ loading.value = false;
+ MessagePlugin.error(errorMsg);
+ console.error('[Chat Error]', errorMsg);
}
break;
@@ -793,8 +801,8 @@ onMounted(async () => {
checkmenuTitle(session_id.value)
if (firstQuery.value) {
scrollLock.value = true;
- sendMsg(firstQuery.value, '', firstMentionedItems.value || []);
- usemenuStore.changeFirstQuery('');
+ sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || []);
+ usemenuStore.changeFirstQuery('', [], '');
} else {
scrollLock.value = false;
let data = {
diff --git a/frontend/src/views/creatChat/creatChat.vue b/frontend/src/views/creatChat/creatChat.vue
index 4fa855ea7..9b3de9039 100644
--- a/frontend/src/views/creatChat/creatChat.vue
+++ b/frontend/src/views/creatChat/creatChat.vue
@@ -40,10 +40,10 @@ const { t } = useI18n();
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
const sendMsg = (value: string, modelId: string, mentionedItems: any[]) => {
- createNewSession(value, mentionedItems);
+ createNewSession(value, modelId, mentionedItems);
}
-async function createNewSession(value: string, mentionedItems: any[] = []) {
+async function createNewSession(value: string, modelId: string, mentionedItems: any[] = []) {
const selectedKbs = settingsStore.settings.selectedKnowledgeBases || [];
const selectedFiles = settingsStore.settings.selectedFiles || [];
@@ -63,7 +63,7 @@ async function createNewSession(value: string, mentionedItems: any[] = []) {
try {
const res = await createSessions(sessionData);
if (res.data && res.data.id) {
- await navigateToSession(res.data.id, value, mentionedItems);
+ await navigateToSession(res.data.id, value, modelId, mentionedItems);
} else {
console.error('[createChat] Failed to create session');
MessagePlugin.error(t('createChat.messages.createFailed'));
@@ -74,7 +74,7 @@ async function createNewSession(value: string, mentionedItems: any[] = []) {
}
}
-const navigateToSession = async (sessionId: string, value: string, mentionedItems: any[]) => {
+const navigateToSession = async (sessionId: string, value: string, modelId: string, mentionedItems: any[]) => {
const now = new Date().toISOString();
let obj = {
title: t('createChat.newSessionTitle'),
@@ -87,7 +87,7 @@ const navigateToSession = async (sessionId: string, value: string, mentionedItem
};
usemenuStore.updataMenuChildren(obj);
usemenuStore.changeIsFirstSession(true);
- usemenuStore.changeFirstQuery(value, mentionedItems);
+ usemenuStore.changeFirstQuery(value, mentionedItems, modelId);
router.push(`/platform/chat/${sessionId}`);
}
diff --git a/internal/application/service/chat_pipline/chat_completion_stream.go b/internal/application/service/chat_pipline/chat_completion_stream.go
index 3c67cd3c5..507415e16 100644
--- a/internal/application/service/chat_pipline/chat_completion_stream.go
+++ b/internal/application/service/chat_pipline/chat_completion_stream.go
@@ -104,6 +104,24 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
var finalContent string
for response := range responseChan {
+ // Handle error responses from the stream
+ if response.ResponseType == types.ResponseTypeError {
+ logger.Errorf(ctx, "Stream error received: %s", response.Content)
+ if err := eventBus.Emit(ctx, types.Event{
+ ID: fmt.Sprintf("%s-error", uuid.New().String()[:8]),
+ Type: types.EventType(event.EventError),
+ SessionID: chatManage.SessionID,
+ Data: event.ErrorData{
+ Error: response.Content,
+ Stage: "chat_completion_stream",
+ SessionID: chatManage.SessionID,
+ },
+ }); err != nil {
+ logger.Errorf(ctx, "Failed to emit error event: %v", err)
+ }
+ continue
+ }
+
// Emit event for each answer chunk
if response.ResponseType == types.ResponseTypeAnswer {
finalContent += response.Content
diff --git a/internal/application/service/session.go b/internal/application/service/session.go
index 8da7e9812..d83136c46 100644
--- a/internal/application/service/session.go
+++ b/internal/application/service/session.go
@@ -461,8 +461,9 @@ func (s *sessionService) KnowledgeQA(
// Ensure defaults are set
customAgent.EnsureDefaults()
- // Override model ID
- if customAgent.Config.ModelID != "" {
+ // Override model ID only if request didn't specify summaryModelID
+ // Request's summaryModelID has highest priority
+ if summaryModelID == "" && customAgent.Config.ModelID != "" {
chatModelID = customAgent.Config.ModelID
logger.Infof(ctx, "Using custom agent's model_id: %s", chatModelID)
}
@@ -1055,11 +1056,13 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,
// AgentQA performs agent-based question answering with conversation history and streaming support
// customAgent is optional - if provided, uses custom agent configuration instead of tenant defaults
+// summaryModelID is optional - if provided, overrides the model from customAgent config
func (s *sessionService) AgentQA(
ctx context.Context,
session *types.Session,
query string,
assistantMessageID string,
+ summaryModelID string,
eventBus *event.EventBus,
customAgent *types.CustomAgent,
knowledgeBaseIDs []string,
@@ -1163,15 +1166,23 @@ func (s *sessionService) AgentQA(
agentConfig.SearchTargets = searchTargets
logger.Infof(ctx, "Agent search targets built: %d targets", len(searchTargets))
- // Get summary model from custom agent config
+ // Get summary model: prioritize request's summaryModelID, then custom agent config
// Note: tenantInfo.ConversationConfig is deprecated, all config comes from customAgent now
- summaryModelID := customAgent.Config.ModelID
- if summaryModelID == "" {
+ effectiveModelID := summaryModelID
+ if effectiveModelID == "" {
+ effectiveModelID = customAgent.Config.ModelID
+ }
+ if effectiveModelID == "" {
logger.Warnf(ctx, "No summary model configured for custom agent %s", customAgent.ID)
return errors.New("summary model (model_id) is not configured in custom agent settings")
}
+ if summaryModelID != "" {
+ logger.Infof(ctx, "Using request's summary model override: %s", effectiveModelID)
+ } else {
+ logger.Infof(ctx, "Using custom agent's model_id: %s", effectiveModelID)
+ }
- summaryModel, err := s.modelService.GetChatModel(ctx, summaryModelID)
+ summaryModel, err := s.modelService.GetChatModel(ctx, effectiveModelID)
if err != nil {
logger.Warnf(ctx, "Failed to get chat model: %v", err)
return fmt.Errorf("failed to get chat model: %w", err)
diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go
index 075bf7f1d..cb2ff2976 100644
--- a/internal/handler/session/qa.go
+++ b/internal/handler/session/qa.go
@@ -443,6 +443,7 @@ func (h *Handler) executeAgentModeQA(reqCtx *qaRequestContext) {
reqCtx.session,
reqCtx.query,
reqCtx.assistantMessage.ID,
+ reqCtx.summaryModelID,
streamCtx.eventBus,
reqCtx.customAgent,
reqCtx.knowledgeBaseIDs,
diff --git a/internal/models/chat/ollama.go b/internal/models/chat/ollama.go
index 647ef002a..e6791b1b5 100644
--- a/internal/models/chat/ollama.go
+++ b/internal/models/chat/ollama.go
@@ -189,7 +189,8 @@ func (c *OllamaChat) ChatStream(
logger.GetLogger(ctx).Errorf("流式聊天请求失败: %v", err)
// 发送错误响应
streamChan <- types.StreamResponse{
- ResponseType: types.ResponseTypeAnswer,
+ ResponseType: types.ResponseTypeError,
+ Content: err.Error(),
Done: true,
}
}
diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go
index 99ad70684..4193f7b16 100644
--- a/internal/models/chat/remote_api.go
+++ b/internal/models/chat/remote_api.go
@@ -419,12 +419,22 @@ func (c *RemoteAPIChat) ChatStream(ctx context.Context,
for {
response, err := stream.Recv()
if err != nil {
- // 发送最后一个响应,包含收集到的 tool calls
- streamChan <- types.StreamResponse{
- ResponseType: types.ResponseTypeAnswer,
- Content: "",
- Done: true,
- ToolCalls: buildOrderedToolCalls(),
+ // Check if it's a normal end of stream (io.EOF)
+ if err.Error() == "EOF" {
+ // Normal end of stream, send final response with collected tool calls
+ streamChan <- types.StreamResponse{
+ ResponseType: types.ResponseTypeAnswer,
+ Content: "",
+ Done: true,
+ ToolCalls: buildOrderedToolCalls(),
+ }
+ } else {
+ // Actual error, send error response
+ streamChan <- types.StreamResponse{
+ ResponseType: types.ResponseTypeError,
+ Content: err.Error(),
+ Done: true,
+ }
}
return
}
diff --git a/internal/types/interfaces/session.go b/internal/types/interfaces/session.go
index 1c326f3a1..30a7fbe85 100644
--- a/internal/types/interfaces/session.go
+++ b/internal/types/interfaces/session.go
@@ -49,11 +49,13 @@ type SessionService interface {
// AgentQA performs agent-based question answering with conversation history and streaming support
// eventBus is optional - if nil, uses service's default EventBus
// customAgent is optional - if provided, uses custom agent configuration instead of tenant defaults
+ // summaryModelID is optional - if provided, overrides the model from customAgent config
AgentQA(
ctx context.Context,
session *types.Session,
query string,
assistantMessageID string,
+ summaryModelID string,
eventBus *event.EventBus,
customAgent *types.CustomAgent,
knowledgeBaseIDs []string,