feat: 增强模型选择和错误处理机制
优化模型ID传递逻辑,支持请求级别的模型覆盖;完善流式响应的错误处理
This commit is contained in:
@@ -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 || '',
|
||||
});
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user