feat: 与ai直接交流
This commit is contained in:
parent
82149dfa5d
commit
cd5b42bed6
|
@ -8,7 +8,7 @@ import { useLocalStorage } from "react-use";
|
|||
// 一些工具函数导入
|
||||
import getArxivPapers from "./GetArxiv";
|
||||
import getSemanticPapers from "./GetSemantic";
|
||||
import { getTopicFromAI, sendMessageToOpenAI } from "./chatAI";
|
||||
import { getTopicFromAI, sendMessageToOpenAI, getFromAI } from "./chatAI";
|
||||
import {
|
||||
getTextBeforeCursor,
|
||||
convertToSuperscript,
|
||||
|
@ -52,6 +52,8 @@ const QEditor = () => {
|
|||
"semanticScholar",
|
||||
"semanticScholar"
|
||||
); // 默认选项
|
||||
//选择语言模型
|
||||
const [selectedModel, setSelectedModel] = useLocalStorage("gpt3.5", "gpt3.5"); // 默认选项
|
||||
//更新参考文献的部分
|
||||
const [references, setReferences] = useLocalStorage<Reference[]>(
|
||||
"referencesKey",
|
||||
|
@ -109,6 +111,13 @@ const QEditor = () => {
|
|||
setUserInput(event.target.value);
|
||||
};
|
||||
|
||||
// 处理AI写作
|
||||
const handleAIWrite = async () => {
|
||||
const prompt = "请帮助用户完成论文写作";
|
||||
await sendMessageToOpenAI(userInput, quill, selectedModel, prompt);
|
||||
};
|
||||
|
||||
// 处理paper2AI
|
||||
async function paper2AI(topic: string) {
|
||||
try {
|
||||
if (!topic) {
|
||||
|
@ -120,6 +129,10 @@ const QEditor = () => {
|
|||
console.log("topic in AI before removeSpecialCharacters", topic);
|
||||
topic = removeSpecialCharacters(topic);
|
||||
topic = topic.split(" ").slice(0, 2).join(" ");
|
||||
//如果超过十个字符就截断
|
||||
if (topic.length > 10) {
|
||||
topic = topic.slice(0, 10);
|
||||
}
|
||||
}
|
||||
console.log("topic in AI", topic);
|
||||
let rawData, dataString;
|
||||
|
@ -164,9 +177,18 @@ const QEditor = () => {
|
|||
})
|
||||
.join("");
|
||||
}
|
||||
// 其他数据源的处理
|
||||
|
||||
sendMessageToOpenAI(dataString, quill, getTextBeforeCursor(quill), topic);
|
||||
// 确保搜索到的论文不超过 3000 个字符
|
||||
const trimmedMessage =
|
||||
dataString.length > 3000 ? dataString.slice(0, 3000) : dataString;
|
||||
//slate的方法
|
||||
// const content = `需要完成的论文主题:${topic}, 搜索到的论文内容:${trimmedMessage},之前已经完成的内容上下文:${extractText(
|
||||
// editorValue
|
||||
// )}`;
|
||||
const content = `之前已经完成的内容上下文:${getTextBeforeCursor(
|
||||
quill,
|
||||
500
|
||||
)},搜索到的论文内容:${trimmedMessage},需要完成的论文主题:${topic},请根据搜索到的论文内容完成接下来的论文`;
|
||||
sendMessageToOpenAI(content, quill, selectedModel);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
// 错误处理
|
||||
|
@ -193,12 +215,12 @@ const QEditor = () => {
|
|||
onChange={handleInputChange}
|
||||
className="shadow appearance-none border rounded py-2 px-3 text-grey-darker"
|
||||
/>
|
||||
{/*<button
|
||||
onClick={handleAIClick}
|
||||
<button
|
||||
onClick={handleAIWrite}
|
||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
Insert AI Text
|
||||
</button>*/}
|
||||
AI Write
|
||||
</button>
|
||||
{/* <button
|
||||
onClick={() => insertPapers(userInput)}
|
||||
className="bg-indigo-500 hover:bg-indigo-700 text-black font-bold py-2 px-4 rounded"
|
||||
|
@ -220,8 +242,17 @@ const QEditor = () => {
|
|||
<option value="semanticScholar">semantic scholar</option>
|
||||
{/* 其他来源网站 */}
|
||||
</select>
|
||||
<select
|
||||
value={selectedModel}
|
||||
onChange={(e) => setSelectedModel(e.target.value)}
|
||||
className=" border border-gray-300 bg-white py-2 px-3 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
|
||||
>
|
||||
<option value="gpt3.5">gpt3.5</option>
|
||||
<option value="gpt4">gpt4</option>
|
||||
{/* 其他来源网站 */}
|
||||
</select>
|
||||
<button
|
||||
onClick={() =>formatTextInEditor(quill)} // 假设 updateIndex 是处理更新操作的函数
|
||||
onClick={() => formatTextInEditor(quill)} // 假设 updateIndex 是处理更新操作的函数
|
||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
|
||||
>
|
||||
更新索引
|
||||
|
|
|
@ -24,7 +24,7 @@ function ReferenceList({
|
|||
}: ReferenceListProps) {
|
||||
const [newTitle, setNewTitle] = useState("");
|
||||
const [newAuthor, setNewAuthor] = useState("");
|
||||
const [newYear, setNewYear] = useState(2020);
|
||||
const [newYear, setNewYear] = useState("");
|
||||
const [newPublisher, setNewPublisher] = useState("");
|
||||
const [newUrl, setNewUrl] = useState("");
|
||||
|
||||
|
@ -76,7 +76,7 @@ function ReferenceList({
|
|||
// 清空表单
|
||||
setNewTitle("");
|
||||
setNewAuthor("");
|
||||
setNewYear(2020);
|
||||
setNewYear("");
|
||||
setNewPublisher("");
|
||||
setNewUrl("");
|
||||
}}
|
||||
|
@ -101,7 +101,7 @@ function ReferenceList({
|
|||
className="border p-2 rounded"
|
||||
type="text"
|
||||
value={newYear}
|
||||
onChange={(e) => setNewYear(parseInt(e.target.value))}
|
||||
onChange={(e) => setNewYear(e.target.value)}
|
||||
placeholder="Year"
|
||||
/>
|
||||
<input
|
||||
|
@ -122,14 +122,14 @@ function ReferenceList({
|
|||
<div className="container mx-auto p-4">
|
||||
<div className="flex justify-between items-center mb-4">
|
||||
<button
|
||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-4"
|
||||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded "
|
||||
type="submit"
|
||||
>
|
||||
Add Reference
|
||||
</button>
|
||||
|
||||
<button
|
||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded mb-4"
|
||||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded "
|
||||
onClick={() =>
|
||||
copyToClipboard(formatAllReferencesForCopy(references))
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ function ReferenceList({
|
|||
复制所有引用
|
||||
</button>
|
||||
<button
|
||||
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded ml-2"
|
||||
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded "
|
||||
onClick={() => setReferences([])} // 设置引用列表为空数组
|
||||
>
|
||||
删除所有引用
|
||||
|
|
|
@ -14,20 +14,14 @@ interface ChatData {
|
|||
}
|
||||
|
||||
const sendMessageToOpenAI = async (
|
||||
userMessage: string,
|
||||
content: string,
|
||||
editor: Editor,
|
||||
editorValue: any,
|
||||
topic: string,
|
||||
selectedModel: "gpt3.5",
|
||||
prompt?: string
|
||||
) => {
|
||||
// 确保 userMessage 不超过 2000 个字符
|
||||
const trimmedMessage =
|
||||
userMessage.length > 3000 ? userMessage.slice(0, 3000) : userMessage;
|
||||
//slate的方法
|
||||
// const content = `需要完成的论文主题:${topic}, 搜索到的论文内容:${trimmedMessage},之前已经完成的内容上下文:${extractText(
|
||||
// editorValue
|
||||
// )}`;
|
||||
const content = `之前已经完成的内容上下文:${editorValue},搜索到的论文内容:${trimmedMessage},需要完成的论文主题:${topic}`;
|
||||
//识别应该使用的模型
|
||||
let model = selectedModel === "gpt3.5" ? "gpt-3.5-turbo" : "gpt4";
|
||||
|
||||
// 设置API请求参数
|
||||
const requestOptions = {
|
||||
method: "POST",
|
||||
|
@ -36,12 +30,12 @@ const sendMessageToOpenAI = async (
|
|||
Authorization: "Bearer " + process.env.NEXT_PUBLIC_OPENAI_API_KEY,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "gpt-3.5-turbo",
|
||||
model: model,
|
||||
stream: true,
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: `作为论文写作助手,您的主要任务是根据用户提供的研究主题和上下文,以及相关的研究论文,来撰写和完善学术论文。在撰写过程中,请注意以下要点:
|
||||
content: prompt || `作为论文写作助手,您的主要任务是根据用户提供的研究主题和上下文,以及相关的研究论文,来撰写和完善学术论文。在撰写过程中,请注意以下要点:
|
||||
1.学术格式:请采用标准的学术论文格式进行写作,包括清晰的段落结构、逻辑严谨的论点展开,以及恰当的专业术语使用。
|
||||
2.文献引用:只引用与主题紧密相关的论文。在引用文献时,文末应使用方括号内的数字来标注引用来源,如 [1]。请确保每个引用在文章中都有其对应的编号,*无需在文章末尾提供参考文献列表*。
|
||||
3.忽略无关文献:对于与主题无关的论文,请不要包含在您的写作中。只关注对理解和阐述主题有实质性帮助的资料。
|
||||
|
@ -107,6 +101,9 @@ const getTopicFromAI = async (userMessage: string, prompt: string) => {
|
|||
return topic; // 获取并返回回复
|
||||
};
|
||||
|
||||
// 给getTopicFromAI函数创建别名
|
||||
// export const getFromAI = sendMessageToOpenAI;
|
||||
|
||||
async function processResult(reader, decoder, editor) {
|
||||
let chunk = "";
|
||||
|
||||
|
|
4
utils/global.d.ts
vendored
4
utils/global.d.ts
vendored
|
@ -1,3 +1,5 @@
|
|||
import { string } from "slate";
|
||||
|
||||
export type JournalInfo = {
|
||||
name: string;
|
||||
pages: string;
|
||||
|
@ -7,7 +9,7 @@ export type JournalInfo = {
|
|||
export type Reference = {
|
||||
title: string;
|
||||
author: string;
|
||||
year: number;
|
||||
year: number|string;
|
||||
url: string;
|
||||
venue?: string;
|
||||
journal?: JournalInfo;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Reference } from "@/utils/global";
|
||||
|
||||
function getTextBeforeCursor(quill, length = 100) {
|
||||
function getTextBeforeCursor(quill, length = 500) {
|
||||
const cursorPosition = quill.getSelection().index;
|
||||
const start = Math.max(0, cursorPosition - length); // 确保开始位置不是负数
|
||||
return quill.getText(start, cursorPosition - start);
|
||||
|
|
Loading…
Reference in New Issue
Block a user