feat: 增强模型选择和错误处理机制
优化模型ID传递逻辑,支持请求级别的模型覆盖;完善流式响应的错误处理
This commit is contained in:
@@ -354,12 +354,14 @@ const loadConversationConfig = async () => {
|
|||||||
try {
|
try {
|
||||||
const response = await getConversationConfig();
|
const response = await getConversationConfig();
|
||||||
conversationConfig.value = response.data;
|
conversationConfig.value = response.data;
|
||||||
|
const modelId = response.data?.summary_model_id || '';
|
||||||
settingsStore.updateConversationModels({
|
settingsStore.updateConversationModels({
|
||||||
summaryModelId: response.data?.summary_model_id || '',
|
summaryModelId: modelId,
|
||||||
|
selectedChatModelId: modelId,
|
||||||
rerankModelId: response.data?.rerank_model_id || '',
|
rerankModelId: response.data?.rerank_model_id || '',
|
||||||
});
|
});
|
||||||
if (!selectedModelId.value) {
|
if (!selectedModelId.value) {
|
||||||
selectedModelId.value = response.data?.summary_model_id || '';
|
selectedModelId.value = modelId;
|
||||||
}
|
}
|
||||||
ensureModelSelection();
|
ensureModelSelection();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -437,6 +439,7 @@ const handleModelChange = async (value: string | number | Array<string | number>
|
|||||||
// 同步到 store
|
// 同步到 store
|
||||||
settingsStore.updateConversationModels({
|
settingsStore.updateConversationModels({
|
||||||
summaryModelId: val,
|
summaryModelId: val,
|
||||||
|
selectedChatModelId: val,
|
||||||
rerankModelId: conversationConfig.value?.rerank_model_id || '',
|
rerankModelId: conversationConfig.value?.rerank_model_id || '',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ export const useMenuStore = defineStore('menuStore', () => {
|
|||||||
const isFirstSession = ref(false)
|
const isFirstSession = ref(false)
|
||||||
const firstQuery = ref('')
|
const firstQuery = ref('')
|
||||||
const firstMentionedItems = ref<any[]>([])
|
const firstMentionedItems = ref<any[]>([])
|
||||||
|
const firstModelId = ref('')
|
||||||
|
|
||||||
const applyMenuTranslations = () => {
|
const applyMenuTranslations = () => {
|
||||||
menuArr.forEach(item => {
|
menuArr.forEach(item => {
|
||||||
@@ -92,9 +93,10 @@ export const useMenuStore = defineStore('menuStore', () => {
|
|||||||
isFirstSession.value = payload
|
isFirstSession.value = payload
|
||||||
}
|
}
|
||||||
|
|
||||||
const changeFirstQuery = (payload: string, mentionedItems: any[] = []) => {
|
const changeFirstQuery = (payload: string, mentionedItems: any[] = [], modelId: string = '') => {
|
||||||
firstQuery.value = payload
|
firstQuery.value = payload
|
||||||
firstMentionedItems.value = mentionedItems
|
firstMentionedItems.value = mentionedItems
|
||||||
|
firstModelId.value = modelId
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -102,6 +104,7 @@ export const useMenuStore = defineStore('menuStore', () => {
|
|||||||
isFirstSession,
|
isFirstSession,
|
||||||
firstQuery,
|
firstQuery,
|
||||||
firstMentionedItems,
|
firstMentionedItems,
|
||||||
|
firstModelId,
|
||||||
clearMenuArr,
|
clearMenuArr,
|
||||||
updatemenuArr,
|
updatemenuArr,
|
||||||
updataMenuChildren,
|
updataMenuChildren,
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ const useSettingsStoreInstance = useSettingsStore();
|
|||||||
const uiStore = useUIStore();
|
const uiStore = useUIStore();
|
||||||
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
|
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
|
||||||
const { t } = useI18n();
|
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 { output, onChunk, isStreaming, isLoading, error, startStream, stopStream } = useStream();
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -672,13 +672,21 @@ const handleAgentChunk = (data) => {
|
|||||||
|
|
||||||
// If this is an error response without tool data, handle it
|
// If this is an error response without tool data, handle it
|
||||||
if (data.response_type === 'error' && !toolName) {
|
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;
|
isReplying.value = false;
|
||||||
|
loading.value = false;
|
||||||
|
MessagePlugin.error(errorMsg);
|
||||||
|
console.error('[Chat Error]', errorMsg);
|
||||||
}
|
}
|
||||||
} else if (data.response_type === 'error') {
|
} else if (data.response_type === 'error') {
|
||||||
// Generic error without tool context
|
// 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;
|
isReplying.value = false;
|
||||||
|
loading.value = false;
|
||||||
|
MessagePlugin.error(errorMsg);
|
||||||
|
console.error('[Chat Error]', errorMsg);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -793,8 +801,8 @@ onMounted(async () => {
|
|||||||
checkmenuTitle(session_id.value)
|
checkmenuTitle(session_id.value)
|
||||||
if (firstQuery.value) {
|
if (firstQuery.value) {
|
||||||
scrollLock.value = true;
|
scrollLock.value = true;
|
||||||
sendMsg(firstQuery.value, '', firstMentionedItems.value || []);
|
sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || []);
|
||||||
usemenuStore.changeFirstQuery('');
|
usemenuStore.changeFirstQuery('', [], '');
|
||||||
} else {
|
} else {
|
||||||
scrollLock.value = false;
|
scrollLock.value = false;
|
||||||
let data = {
|
let data = {
|
||||||
|
|||||||
@@ -40,10 +40,10 @@ const { t } = useI18n();
|
|||||||
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
|
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
|
||||||
|
|
||||||
const sendMsg = (value: string, modelId: string, mentionedItems: any[]) => {
|
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 selectedKbs = settingsStore.settings.selectedKnowledgeBases || [];
|
||||||
const selectedFiles = settingsStore.settings.selectedFiles || [];
|
const selectedFiles = settingsStore.settings.selectedFiles || [];
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ async function createNewSession(value: string, mentionedItems: any[] = []) {
|
|||||||
try {
|
try {
|
||||||
const res = await createSessions(sessionData);
|
const res = await createSessions(sessionData);
|
||||||
if (res.data && res.data.id) {
|
if (res.data && res.data.id) {
|
||||||
await navigateToSession(res.data.id, value, mentionedItems);
|
await navigateToSession(res.data.id, value, modelId, mentionedItems);
|
||||||
} else {
|
} else {
|
||||||
console.error('[createChat] Failed to create session');
|
console.error('[createChat] Failed to create session');
|
||||||
MessagePlugin.error(t('createChat.messages.createFailed'));
|
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();
|
const now = new Date().toISOString();
|
||||||
let obj = {
|
let obj = {
|
||||||
title: t('createChat.newSessionTitle'),
|
title: t('createChat.newSessionTitle'),
|
||||||
@@ -87,7 +87,7 @@ const navigateToSession = async (sessionId: string, value: string, mentionedItem
|
|||||||
};
|
};
|
||||||
usemenuStore.updataMenuChildren(obj);
|
usemenuStore.updataMenuChildren(obj);
|
||||||
usemenuStore.changeIsFirstSession(true);
|
usemenuStore.changeIsFirstSession(true);
|
||||||
usemenuStore.changeFirstQuery(value, mentionedItems);
|
usemenuStore.changeFirstQuery(value, mentionedItems, modelId);
|
||||||
router.push(`/platform/chat/${sessionId}`);
|
router.push(`/platform/chat/${sessionId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -104,6 +104,24 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
|||||||
var finalContent string
|
var finalContent string
|
||||||
|
|
||||||
for response := range responseChan {
|
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
|
// Emit event for each answer chunk
|
||||||
if response.ResponseType == types.ResponseTypeAnswer {
|
if response.ResponseType == types.ResponseTypeAnswer {
|
||||||
finalContent += response.Content
|
finalContent += response.Content
|
||||||
|
|||||||
@@ -461,8 +461,9 @@ func (s *sessionService) KnowledgeQA(
|
|||||||
// Ensure defaults are set
|
// Ensure defaults are set
|
||||||
customAgent.EnsureDefaults()
|
customAgent.EnsureDefaults()
|
||||||
|
|
||||||
// Override model ID
|
// Override model ID only if request didn't specify summaryModelID
|
||||||
if customAgent.Config.ModelID != "" {
|
// Request's summaryModelID has highest priority
|
||||||
|
if summaryModelID == "" && customAgent.Config.ModelID != "" {
|
||||||
chatModelID = customAgent.Config.ModelID
|
chatModelID = customAgent.Config.ModelID
|
||||||
logger.Infof(ctx, "Using custom agent's model_id: %s", chatModelID)
|
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
|
// 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
|
// 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(
|
func (s *sessionService) AgentQA(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
session *types.Session,
|
session *types.Session,
|
||||||
query string,
|
query string,
|
||||||
assistantMessageID string,
|
assistantMessageID string,
|
||||||
|
summaryModelID string,
|
||||||
eventBus *event.EventBus,
|
eventBus *event.EventBus,
|
||||||
customAgent *types.CustomAgent,
|
customAgent *types.CustomAgent,
|
||||||
knowledgeBaseIDs []string,
|
knowledgeBaseIDs []string,
|
||||||
@@ -1163,15 +1166,23 @@ func (s *sessionService) AgentQA(
|
|||||||
agentConfig.SearchTargets = searchTargets
|
agentConfig.SearchTargets = searchTargets
|
||||||
logger.Infof(ctx, "Agent search targets built: %d targets", len(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
|
// Note: tenantInfo.ConversationConfig is deprecated, all config comes from customAgent now
|
||||||
summaryModelID := customAgent.Config.ModelID
|
effectiveModelID := summaryModelID
|
||||||
if summaryModelID == "" {
|
if effectiveModelID == "" {
|
||||||
|
effectiveModelID = customAgent.Config.ModelID
|
||||||
|
}
|
||||||
|
if effectiveModelID == "" {
|
||||||
logger.Warnf(ctx, "No summary model configured for custom agent %s", customAgent.ID)
|
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")
|
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 {
|
if err != nil {
|
||||||
logger.Warnf(ctx, "Failed to get chat model: %v", err)
|
logger.Warnf(ctx, "Failed to get chat model: %v", err)
|
||||||
return fmt.Errorf("failed to get chat model: %w", err)
|
return fmt.Errorf("failed to get chat model: %w", err)
|
||||||
|
|||||||
@@ -443,6 +443,7 @@ func (h *Handler) executeAgentModeQA(reqCtx *qaRequestContext) {
|
|||||||
reqCtx.session,
|
reqCtx.session,
|
||||||
reqCtx.query,
|
reqCtx.query,
|
||||||
reqCtx.assistantMessage.ID,
|
reqCtx.assistantMessage.ID,
|
||||||
|
reqCtx.summaryModelID,
|
||||||
streamCtx.eventBus,
|
streamCtx.eventBus,
|
||||||
reqCtx.customAgent,
|
reqCtx.customAgent,
|
||||||
reqCtx.knowledgeBaseIDs,
|
reqCtx.knowledgeBaseIDs,
|
||||||
|
|||||||
@@ -189,7 +189,8 @@ func (c *OllamaChat) ChatStream(
|
|||||||
logger.GetLogger(ctx).Errorf("流式聊天请求失败: %v", err)
|
logger.GetLogger(ctx).Errorf("流式聊天请求失败: %v", err)
|
||||||
// 发送错误响应
|
// 发送错误响应
|
||||||
streamChan <- types.StreamResponse{
|
streamChan <- types.StreamResponse{
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
ResponseType: types.ResponseTypeError,
|
||||||
|
Content: err.Error(),
|
||||||
Done: true,
|
Done: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -419,12 +419,22 @@ func (c *RemoteAPIChat) ChatStream(ctx context.Context,
|
|||||||
for {
|
for {
|
||||||
response, err := stream.Recv()
|
response, err := stream.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 发送最后一个响应,包含收集到的 tool calls
|
// Check if it's a normal end of stream (io.EOF)
|
||||||
streamChan <- types.StreamResponse{
|
if err.Error() == "EOF" {
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
// Normal end of stream, send final response with collected tool calls
|
||||||
Content: "",
|
streamChan <- types.StreamResponse{
|
||||||
Done: true,
|
ResponseType: types.ResponseTypeAnswer,
|
||||||
ToolCalls: buildOrderedToolCalls(),
|
Content: "",
|
||||||
|
Done: true,
|
||||||
|
ToolCalls: buildOrderedToolCalls(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Actual error, send error response
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeError,
|
||||||
|
Content: err.Error(),
|
||||||
|
Done: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,11 +49,13 @@ type SessionService interface {
|
|||||||
// AgentQA performs agent-based question answering with conversation history and streaming support
|
// AgentQA performs agent-based question answering with conversation history and streaming support
|
||||||
// eventBus is optional - if nil, uses service's default EventBus
|
// eventBus is optional - if nil, uses service's default EventBus
|
||||||
// customAgent is optional - if provided, uses custom agent configuration instead of tenant defaults
|
// 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(
|
AgentQA(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
session *types.Session,
|
session *types.Session,
|
||||||
query string,
|
query string,
|
||||||
assistantMessageID string,
|
assistantMessageID string,
|
||||||
|
summaryModelID string,
|
||||||
eventBus *event.EventBus,
|
eventBus *event.EventBus,
|
||||||
customAgent *types.CustomAgent,
|
customAgent *types.CustomAgent,
|
||||||
knowledgeBaseIDs []string,
|
knowledgeBaseIDs []string,
|
||||||
|
|||||||
Reference in New Issue
Block a user