From 28d63ea406826f48fc03390eadb9af64dcf83e2a Mon Sep 17 00:00:00 2001 From: liuweiqing Date: Thu, 22 Feb 2024 12:35:08 +0800 Subject: [PATCH] =?UTF-8?q?refractor:=20=E4=BD=BF=E5=BE=97=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=96=87=E7=8C=AE=E6=98=AF=E5=90=A6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=9B=B4=E6=9C=89=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/GetArxiv.tsx | 2 +- components/GetPubMed .tsx | 5 ++--- components/QuillEditor.tsx | 15 +++++++++----- components/Settings.tsx | 2 +- components/chatAI.tsx | 41 +++++++++++++++++++------------------- utils/others/aiutils.ts | 38 +++++++++++++++++++++++++---------- 6 files changed, 62 insertions(+), 41 deletions(-) diff --git a/components/GetArxiv.tsx b/components/GetArxiv.tsx index 04dc7f5d..720575f1 100644 --- a/components/GetArxiv.tsx +++ b/components/GetArxiv.tsx @@ -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]), }; }); diff --git a/components/GetPubMed .tsx b/components/GetPubMed .tsx index c88048cb..1985f019 100644 --- a/components/GetPubMed .tsx +++ b/components/GetPubMed .tsx @@ -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; @@ -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}`); } } diff --git a/components/QuillEditor.tsx b/components/QuillEditor.tsx index a931ebeb..c7154a64 100644 --- a/components/QuillEditor.tsx +++ b/components/QuillEditor.tsx @@ -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, @@ -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); @@ -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); @@ -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") { diff --git a/components/Settings.tsx b/components/Settings.tsx index 9012203f..bf4591de 100644 --- a/components/Settings.tsx +++ b/components/Settings.tsx @@ -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", }, diff --git a/components/chatAI.tsx b/components/chatAI.tsx index 1ba35365..fd71e577 100644 --- a/components/chatAI.tsx +++ b/components/chatAI.tsx @@ -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; @@ -47,7 +48,7 @@ const sendMessageToOpenAI = async ( }, body: JSON.stringify({ model: model, - stream: true, + stream: useEditorFlag, // 根据标志确定是否使用streaming messages: [ { role: "system", @@ -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); // 如果有响应,返回响应的原始内容 @@ -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}`); diff --git a/utils/others/aiutils.ts b/utils/others/aiutils.ts index adab29db..e43256c1 100644 --- a/utils/others/aiutils.ts +++ b/utils/others/aiutils.ts @@ -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[], @@ -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 结果,如果无法解析则直接使用结果字符串 @@ -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 }; }