feat: 如果AI多次引用同一文献则只返回第一个文献的引用数字

This commit is contained in:
liuweiqing 2024-02-18 15:48:18 +08:00
parent 07070bf253
commit 763a1062f9
4 changed files with 69 additions and 4 deletions

View File

@ -35,7 +35,7 @@ export default async function Login({
});
//sentry
const user = data?.user;
if (user) {
if (user && process.env.NODE_ENV === "production") {
Sentry.setUser({
email: user.email,
id: user.id,

View File

@ -366,7 +366,8 @@ const QEditor = ({ lng }) => {
selectedModel!,
apiKey,
upsreamUrl,
systemPrompt
systemPrompt,
cursorPosition!
);
setUserInput("");
// 重新获取更新后的内容并更新 Redux store

View File

@ -6,6 +6,7 @@ import { extractText } from "@/utils/others/slateutils";
import {
updateBracketNumbersInDeltaKeepSelection,
convertToSuperscript,
deleteSameBracketNumber,
} from "@/utils/others/quillutils";
//redux不能在普通函数使用
@ -26,7 +27,8 @@ const sendMessageToOpenAI = async (
selectedModel: string,
apiKey: string,
upsreamUrl: string,
prompt?: string
prompt: string,
cursorPosition?: number
) => {
//识别应该使用的模型
let model = selectedModel;
@ -87,8 +89,9 @@ const sendMessageToOpenAI = async (
// editor.focus();
editor.insertText(editor.getSelection(true).index, "\n");
await processResult(reader, decoder, editor);
//搜索是否有相同的括号编号,如果有相同的则删除到只剩一个
convertToSuperscript(editor);
deleteSameBracketNumber(editor, cursorPosition);
updateBracketNumbersInDeltaKeepSelection(editor);
} catch (error) {
console.error("Error:", error);

View File

@ -183,6 +183,67 @@ function convertToSuperscript(quill: Quill) {
}
}
function removeDuplicateBracketNumbersInDelta(delta: any) {
let seenNumbers = new Set(); // 用于记录已经看到的编号
const updatedOps = delta.ops.map((op: any) => {
if (typeof op.insert === "string") {
// 使用正则表达式和replace方法来找到并处理[数字]
return {
...op,
insert: op.insert.replace(/\[\d+\]/g, (match) => {
const number = match.slice(1, -1); // 提取括号中的数字
if (seenNumbers.has(number)) {
// 如果这个编号已经处理过,就删除它(用空字符串替换)
return "";
} else {
// 如果是首次见到这个编号,就记录并保留它
seenNumbers.add(number);
console.log("seenNumbers", seenNumbers);
return match;
}
}),
};
}
return op;
});
return { ops: updatedOps };
}
export function deleteSameBracketNumber(
quill: Quill,
cursorOldPosition: number
) {
//搜索是否有相同的括号编号,如果有相同的则删除到只剩一个
const selection = quill.getSelection(true);
if (selection) {
// 获取整个文档的内容
const delta = quill.getContents();
// 仅获取选区中的Delta片段
const selectionDelta = delta.slice(cursorOldPosition, selection.index);
console.log("cursorOldPosition", cursorOldPosition);
console.log("selection.index", selection.index);
console.log("selectionDelta", selectionDelta);
// 对选区中的Delta片段进行处理移除重复的括号编号
const updatedSelectionDelta =
removeDuplicateBracketNumbersInDelta(selectionDelta);
// 构建一个新的Delta包括未修改的选区前部分和处理后的选区后部分
const updatedDelta = delta
.slice(0, cursorOldPosition)
.concat(updatedSelectionDelta)
.concat(delta.slice(selection.index));
// 设置更新后的内容
quill.setContents(updatedDelta);
// 恢复选区
quill.setSelection(selection.index, selection.length);
}
}
function getRandomOffset(max: number) {
return Math.floor(Math.random() * max);
}