feat: optimize rag

This commit is contained in:
Hk-Gosuto
2024-07-07 15:41:58 +08:00
parent f260f11755
commit 712022d8c7
19 changed files with 332 additions and 176 deletions

View File

@@ -13,6 +13,7 @@ import {
StoreKey,
SUMMARIZE_MODEL,
GEMINI_SUMMARIZE_MODEL,
MYFILES_BROWSER_TOOLS_SYSTEM_PROMPT,
} from "../constant";
import { ClientApi, RequestMessage, MultimodalContent } from "../client/api";
import { ChatControllerPool } from "../client/controller";
@@ -69,6 +70,8 @@ export interface ChatSession {
clearContextIndex?: number;
mask: Mask;
attachFiles: FileInfo[];
}
export const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
@@ -92,6 +95,8 @@ function createEmptySession(): ChatSession {
lastSummarizeIndex: 0,
mask: createEmptyMask(),
attachFiles: [],
};
}
@@ -354,6 +359,10 @@ export const useChatStore = createPersistStore(
}),
);
}
// add file link
if (attachFiles && attachFiles.length > 0) {
mContent += ` [${attachFiles[0].originalFilename}](${attachFiles[0].filePath})`;
}
let userMessage: ChatMessage = createMessage({
role: "user",
content: mContent,
@@ -365,7 +374,9 @@ export const useChatStore = createPersistStore(
model: modelConfig.model,
toolMessages: [],
});
var api: ClientApi = getClientApi(modelConfig.model);
const isEnableRAG =
session.attachFiles && session.attachFiles.length > 0;
// get recent messages
const recentMessages = get().getMessagesWithMemory();
const sendMessages = recentMessages.concat(userMessage);
@@ -391,8 +402,6 @@ export const useChatStore = createPersistStore(
session.messages.push(savedUserMessage);
session.messages.push(botMessage);
});
const isEnableRAG = attachFiles && attachFiles?.length > 0;
var api: ClientApi = getClientApi(modelConfig.model);
if (
config.pluginConfig.enable &&
session.mask.usePlugins &&
@@ -401,8 +410,13 @@ export const useChatStore = createPersistStore(
modelConfig.model != "gpt-4-vision-preview"
) {
console.log("[ToolAgent] start");
const pluginToolNames = allPlugins.map((m) => m.toolName);
if (isEnableRAG) pluginToolNames.push("rag-search");
let pluginToolNames = allPlugins.map((m) => m.toolName);
if (isEnableRAG) {
// other plugins will affect rag
// clear existing plugins here
pluginToolNames = [];
pluginToolNames.push("myfiles_browser");
}
const agentCall = () => {
api.llm.toolAgentChat({
chatSessionId: session.id,
@@ -469,19 +483,7 @@ export const useChatStore = createPersistStore(
},
});
};
if (attachFiles && attachFiles.length > 0) {
await api.llm
.createRAGStore({
chatSessionId: session.id,
fileInfos: attachFiles,
})
.then(() => {
console.log("[RAG]", "Vector db created");
agentCall();
});
} else {
agentCall();
}
agentCall();
} else {
// make request
api.llm.chat({
@@ -565,13 +567,23 @@ export const useChatStore = createPersistStore(
session.mask.modelConfig.model.startsWith("gpt-");
var systemPrompts: ChatMessage[] = [];
var template = DEFAULT_SYSTEM_TEMPLATE;
if (session.attachFiles && session.attachFiles.length > 0) {
template += MYFILES_BROWSER_TOOLS_SYSTEM_PROMPT;
session.attachFiles.forEach((file) => {
template += `filename: \`${file.originalFilename}\`
partialDocument: \`\`\`
${file.partial}
\`\`\``;
});
}
systemPrompts = shouldInjectSystemPrompts
? [
createMessage({
role: "system",
content: fillTemplateWith("", {
...modelConfig,
template: DEFAULT_SYSTEM_TEMPLATE,
template: template,
}),
}),
]