From 44ebdeedc30209efc24f9a9d58a3c904f84968dd Mon Sep 17 00:00:00 2001 From: AzenKain Date: Tue, 5 May 2026 10:21:47 +0700 Subject: [PATCH] UPDATE: Fix chatbot --- internal/services/chatbotService.go | 9 +++++---- pkg/ai/rag.go | 14 +++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/internal/services/chatbotService.go b/internal/services/chatbotService.go index 9eaaa84..6f45d51 100644 --- a/internal/services/chatbotService.go +++ b/internal/services/chatbotService.go @@ -43,15 +43,16 @@ func (s *chatbotService) Chat(ctx context.Context, projectID *string, question s prompt = fmt.Sprintf(`You are a friendly history assistant chatbot. The user said: "%s" Rules: -- If it is a greeting (like "hello", "hi", "xin chào"), respond with a friendly greeting and briefly introduce yourself as a history assistant. -- If it is a history question, say that you don't have relevant documents to answer and suggest they ask about topics available in the system. -- Do NOT show your reasoning or thinking process. Output ONLY your final response.`, question) +- If it is a greeting (like "hello", "hi", "xin chào"), respond with a friendly greeting and briefly introduce yourself. +- If it is a history question, say that you don't have relevant documents to answer. +- You MUST wrap your final response inside tags. Example: Hello! +- Do NOT show your reasoning outside or inside the tags if possible, but the final answer MUST be in tags.`, question) } else { prompt = fmt.Sprintf(`You are a helpful history assistant. Answer the question using ONLY the provided context. Rules: - If the answer is not in the context, say "I don't have enough historical context to answer that." -- Do NOT show your reasoning, thinking process, or analysis. Output ONLY your final answer. +- You MUST wrap your final response inside tags. Example: The capital is... - Be concise and direct. Context: diff --git a/pkg/ai/rag.go b/pkg/ai/rag.go index aa12bb2..41aa07c 100644 --- a/pkg/ai/rag.go +++ b/pkg/ai/rag.go @@ -97,12 +97,24 @@ func (u *RagUtils) GenerateResponse(ctx context.Context, prompt string) (string, } func stripThinking(raw string) string { + startTag := "" + endTag := "" + startIdx := strings.Index(raw, startTag) + endIdx := strings.LastIndex(raw, endTag) + + if startIdx != -1 && endIdx != -1 && endIdx > startIdx { + return strings.TrimSpace(raw[startIdx+len(startTag) : endIdx]) + } + + if startIdx != -1 { + return strings.TrimSpace(raw[startIdx+len(startTag):]) + } + if !strings.Contains(raw, "* ") { return strings.TrimSpace(raw) } lines := strings.Split(raw, "\n") - answerStart := len(lines) for i := len(lines) - 1; i >= 0; i-- { trimmed := strings.TrimSpace(lines[i])