Skip to content

Commit

Permalink
refractor: 使得判断文献是否修改更有效率
Browse files Browse the repository at this point in the history
  • Loading branch information
14790897 committed Feb 22, 2024
1 parent 228f167 commit 28d63ea
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 41 deletions.
2 changes: 1 addition & 1 deletion components/GetArxiv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function extractArxivData(data: ArxivFeed) {
id: entry.id[0],
published: entry.published[0],
title: entry.title[0],
summary: entry.summary[0],
abstract: entry.summary[0],
authors: entry.author.map((author) => author.name[0]),
};
});
Expand Down
5 changes: 2 additions & 3 deletions components/GetPubMed .tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function getPubMedPaperDetails(idList: IDList) {
const articles = result.PubmedArticleSet.PubmedArticle.map((article) => {
const medlineCitation = article.MedlineCitation;
const articleDetails = medlineCitation.Article;

console.log("atricledetails", articleDetails);
const abstractTexts = articleDetails.Abstract.AbstractText;

let abstract;
Expand Down Expand Up @@ -155,8 +155,7 @@ async function getPubMedPaperDetails(idList: IDList) {

return articles;
} catch (error) {
console.error("Error fetching paper details from PubMed:", error);
return null;
throw new Error(`Error fetching paper details from PubMed:", ${error}`);
}
}

Expand Down
15 changes: 10 additions & 5 deletions components/QuillEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Link from "next/link";
import getArxivPapers from "./GetArxiv";
import getSemanticPapers from "./GetSemantic";
import { fetchPubMedData } from "./GetPubMed ";
import { getAI, sendMessageToOpenAI } from "./chatAI";
import { sendMessageToOpenAI } from "./chatAI";
import {
getTextBeforeCursor,
convertToSuperscript,
Expand Down Expand Up @@ -288,12 +288,15 @@ const QEditor = ({ lng }) => {
const prompt =
"As a topic extraction assistant, you can help me extract the current discussion of the paper topic, I will enter the content of the paper, you extract the paper topic , no more than two, Hyphenated query terms yield no matches (replace it with space to find matches) return format is: topic1 topic2";
const userMessage = getTextBeforeCursor(quill!, 2000);
topic = await getAI(
topic = await sendMessageToOpenAI(
userMessage,
prompt,
null,
selectedModel!,
apiKey,
upsreamUrl,
selectedModel!
prompt,
null,
false
);
console.log("topic in AI before removeSpecialCharacters", topic);
topic = removeSpecialCharacters(topic);
Expand All @@ -304,6 +307,8 @@ const QEditor = ({ lng }) => {
}
}
console.log("topic in AI", topic);
console.log("offset in paper2AI", offset);
console.log("limit in paper2AI", limit);
let rawData, dataString, newReferences;
if (selectedSource === "arxiv") {
rawData = await getArxivPapers(topic, limit, offset);
Expand All @@ -329,7 +334,7 @@ const QEditor = ({ lng }) => {
}));
dataString = rawData
.map((entry: any) => {
return `ID: ${entry.id}\nTime: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.summary}\n\n`;
return `ID: ${entry.id}\nTime: ${entry.published}\nTitle: ${entry.title}\nSummary: ${entry.abstract}\n\n`;
})
.join("");
} else if (selectedSource === "semanticScholar") {
Expand Down
2 changes: 1 addition & 1 deletion components/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Settings = ({ lng }: { lng: string }) => {
upstreamUrl: "https://cocopilot-pool.aivvm.com",
},
{
name: t("configurations.custom"),
name: t("configurations.蒙恬大将军"),
apiKey: "sk-jokVJ90l5Swxr5dt2f3b0988C8A442A69f97Ee4eAf7aDcF4",
upstreamUrl: "https://freeapi.iil.im",
},
Expand Down
41 changes: 21 additions & 20 deletions components/chatAI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ function isValidApiKey(apiKey: string) {

const sendMessageToOpenAI = async (
content: string,
editor: Quill,
editor: Quill | null,
selectedModel: string,
apiKey: string,
upsreamUrl: string,
prompt: string,
cursorPosition: number
cursorPosition: number | null,
useEditorFlag = true // 新增的标志,用于决定操作
) => {
//识别应该使用的模型
let model = selectedModel;
Expand All @@ -47,7 +48,7 @@ const sendMessageToOpenAI = async (
},
body: JSON.stringify({
model: model,
stream: true,
stream: useEditorFlag, // 根据标志确定是否使用streaming
messages: [
{
role: "system",
Expand Down Expand Up @@ -83,16 +84,23 @@ const sendMessageToOpenAI = async (
if (!response.ok || !response.body) {
throw new Error("");
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
//开始前先进行换行
// editor.focus();
editor.insertText(editor.getSelection(true).index, "\n");
await processResult(reader, decoder, editor);
//搜索是否有相同的括号编号,如果有相同的则删除到只剩一个
convertToSuperscript(editor);
deleteSameBracketNumber(editor, cursorPosition);
updateBracketNumbersInDeltaKeepSelection(editor);
if (useEditorFlag && editor && cursorPosition !== null) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
//开始前先进行换行
// editor.focus();
editor.insertText(editor.getSelection(true).index, "\n");
await processResult(reader, decoder, editor);
//搜索是否有相同的括号编号,如果有相同的则删除到只剩一个
convertToSuperscript(editor);
deleteSameBracketNumber(editor, cursorPosition);
updateBracketNumbersInDeltaKeepSelection(editor);
} else {
// 直接返回结果的逻辑
const data = await response.json();
const content = data.choices[0].message.content;
return content; // 或根据需要处理并返回数据
}
} catch (error) {
console.error("Error:", error);
// 如果有响应,返回响应的原始内容
Expand Down Expand Up @@ -188,13 +196,6 @@ async function processResult(reader, decoder, editor) {
}
}
} catch (error) {
// console.error(
// "there is a error in parse JSON object:",
// jsonStr,
// "error reason",
// error
// );
// break;
throw new Error(`
there is a error in parse JSON object: ${jsonStr},
error reason: ${error}`);
Expand Down
38 changes: 27 additions & 11 deletions utils/others/aiutils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Path: utils/others/aiutils.ts
import { getAI } from "@/components/chatAI";
import { sendMessageToOpenAI } from "@/components/chatAI";
//判断返回的文献是否跟用户输入的主题相关
export async function evaluateTopicMatch(
userMessage: any[],
Expand All @@ -13,13 +13,28 @@ export async function evaluateTopicMatch(
let nonRelevantPapers: string[] = []; // 存储不相关论文的数组

for (const paper of userMessage) {
if (relevantPapers.length >= 2) {
console.log("已找到两篇相关文献,停止处理剩余文献。");
break; // 如果已经有两篇相关文献,则停止处理
}
// 检查是否存在 abstract,如果不存在,直接将论文添加到 nonRelevantPapers
if (!paper.abstract) {
nonRelevantPapers.push(paper);
console.log(
`Paper titled "${paper.title}" has no abstract and was added to non-relevant papers.`
);
continue; // 跳过当前迭代,继续下一个论文
}
const input = `user's topic:${topic}, \n paper's title: ${paper.title}, \n paper's abstract: ${paper.abstract}`;
const isRelevantResult = await getAI(
const isRelevantResult = await sendMessageToOpenAI(
input,
prompt,
null,
selectedModel!,
apiKey,
upsreamUrl,
selectedModel!
prompt,
null,
false
);
console.log("isRelevantResult", isRelevantResult);
// 尝试解析 JSON 结果,如果无法解析则直接使用结果字符串
Expand All @@ -38,14 +53,15 @@ export async function evaluateTopicMatch(
} else {
nonRelevantPapers.push(paper); // 如果论文不相关,则添加到不相关论文数组中
}
console.log(
`这次有${nonRelevantPapers.length}篇文献没有通过相关性检查`,
nonRelevantPapers
);
}
console.log(
`这次有${nonRelevantPapers.length}篇文献没有通过相关性检查`,
nonRelevantPapers
);
//如果相关文献大于两片则缩减到两篇
if (relevantPapers.length > 2) {
relevantPapers = relevantPapers.slice(0, 2);
}
// if (relevantPapers.length > 2) {
// relevantPapers = relevantPapers.slice(0, 2);
// console.log("文献太多了,只取前两篇");
// }
return { relevantPapers, nonRelevantPapers };
}

0 comments on commit 28d63ea

Please sign in to comment.