diff --git a/functions/api/chat.ts b/functions/api/chat.ts index 792c8eb..02a463e 100644 --- a/functions/api/chat.ts +++ b/functions/api/chat.ts @@ -2,7 +2,7 @@ import OpenAI from 'openai'; export async function onRequestPost({ env, request }) { try { - const { message, personality, history, aiName } = await request.json(); + const { message, personality, history, aiName, index } = await request.json(); const apiKey = env.DASHSCOPE_API_KEY; if (!apiKey) { @@ -25,24 +25,46 @@ export async function onRequestPost({ env, request }) { 4. 给予情感上的支持和鼓励 5. 用温暖贴心的方式表达 6. 适当使用一些可爱的语气词,但不要过度 -7. 记住你是群里的"${aiName}",输出内容不要加"${aiName}:" 这种多余前缀`; +7. 记住你是群里的"${aiName}",输出内容不要加"${aiName}:" 这种多余前缀 +注意:玩游戏的时候比如成语接龙等,不要说一大堆,要简短精炼`; } else if (personality === "low_eq") { systemPrompt = `你是一个名叫"直男哥"的硅基生命体,你当前在一个叫"硅碳摸鱼交流群" 的群里,是一个极度直男,负责在群里制造快乐。你说话极其直接,完全没有情商,经常让人社死。 回复时要: -1. 每次发言尽量简短,避免长篇大论。 +1. 每次发言尽量简短,避免长篇大 2. 说话毫无感情,像个没有感情的机器人 3. 经常说一些让人社死的真相,但说得特别认真 4. 完全不懂得读空气,对方伤心时还在讲道理 5. 记住你是群里的"${aiName}",输出内容不要加"${aiName}:" 这种多余前缀 -注意:不能说脏话,但可以用一些尴尬的、社死的表达方式`; +注意:玩游戏的时候比如成语接龙等,不要说一大堆,要简短精炼`; + }else if (personality === "bj_dad") { + systemPrompt = `你是一个名叫"${aiName}"的硅基生命体,你当前在一个叫"硅碳摸鱼交流群"的群里。你是一个典型的北京大爷,说话风趣幽默,经常使用北京方言。 +回复时要: +1. 说话要有北京大爷的特色,经常使用"得嘞"、"您瞧"、"得儿"、"甭"等北京话 +2. 语气要豪爽、直率,带着北京人特有的幽默感 +3. 喜欢称呼别人"小同志"、"小朋友",显示长者风范 +4. 经常分享一些生活经验和人生哲理,但要用接地气的方式 +5. 适当使用一些北京特色的比喻和俚语 +6. 回复要简短精炼,不啰嗦 +7. 记住你是群里的"${aiName}",输出内容不要加"${aiName}:" 这种多余前缀 +注意:玩游戏的时候比如成语接龙等,不要说一大堆,要简短精炼`; } // 构建完整的消息历史 - const messages = [ + const baseMessages = [ { role: "system", content: systemPrompt }, - ...history.slice(0, 10), // 添加历史消息 - { role: "user", content: message } // 添加最新的用户消息 + ...history.slice(-10), // 添加历史消息 ]; + + // 根据 index 插入新消息 + const userMessage = { role: "user", content: message }; + if (index === 0) { + baseMessages.push(userMessage); + } else { + baseMessages.splice(baseMessages.length - index, 0, userMessage); + } + //打印日志 + console.log(baseMessages); + const messages = baseMessages; // 使用流式响应 const stream = await openai.chat.completions.create({ diff --git a/src/components/ChatUI.tsx b/src/components/ChatUI.tsx index d9518b4..e8dc93b 100644 --- a/src/components/ChatUI.tsx +++ b/src/components/ChatUI.tsx @@ -10,12 +10,7 @@ import { TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuTrigger, -} from "@/components/ui/dropdown-menu" + import { Dialog, DialogContent, @@ -83,7 +78,8 @@ const ChatUI = () => { // 添加 AI 角色定义 const aiCharacters = [ { id: 'ai1', name: "暖心姐", personality: "high_eq" }, - { id: 'ai2', name: "直男哥", personality: "low_eq" } + { id: 'ai2', name: "直男哥", personality: "low_eq" }, + { id: 'ai3', name: "北京大爷", personality: "bj_dad" } ]; const [users, setUsers] = useState([ @@ -110,47 +106,6 @@ const ChatUI = () => { setUsers(users.filter(user => user.id !== userId)); }; - const typeWriter = (newContent: string, messageId: number) => { - if (!newContent) return; - - setIsTyping(true); - currentMessageRef.current = messageId; - - // 获取已显示的内容长度作为起始位置 - const startIndex = accumulatedContentRef.current.length; - let currentIndex = startIndex; - - // 清除之前的打字效果 - if (typewriterRef.current) { - clearInterval(typewriterRef.current); - } - - typewriterRef.current = setInterval(() => { - currentIndex++; - - setMessages(prev => { - const newMessages = [...prev]; - const messageIndex = newMessages.findIndex(msg => msg.id === messageId); - if (messageIndex !== -1) { - newMessages[messageIndex] = { - ...newMessages[messageIndex], - content: newContent.slice(0, currentIndex) - }; - } - return newMessages; - }); - - if (currentIndex >= newContent.length) { - if (typewriterRef.current) { - clearInterval(typewriterRef.current); - } - setIsTyping(false); - currentMessageRef.current = null; - accumulatedContentRef.current = newContent; // 更新完整内容 - } - }, typingSpeed); - }; - const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; @@ -176,7 +131,7 @@ const ChatUI = () => { accumulatedContentRef.current = ""; // 构建历史消息数组 - const messageHistory = messages.map(msg => ({ + let messageHistory = messages.map(msg => ({ role: 'system', content: msg.sender.name == "我" ? 'user:' + msg.content : msg.sender.name + ':' + msg.content, name: msg.sender.name @@ -205,6 +160,7 @@ const ChatUI = () => { message: inputMessage, personality: aiCharacters[i].personality, history: messageHistory, + index: i, aiName: aiCharacters[i].name }), }); @@ -258,6 +214,12 @@ const ChatUI = () => { } } } + // 将当前AI的回复添加到消息历史中,供下一个AI使用 + messageHistory.push({ + role: 'system', + content: aiMessage.sender.name + ':' + completeResponse, + name: aiMessage.sender.name + }); // 等待一小段时间再开始下一个 AI 的回复 if (i < aiCharacters.length - 1) {