手机端网站 优帮云,网页设计公司创业计划书,门户网站建设报告,免费咨询广东律师事务所引言
在这个数字化的时代#xff0c;历史学习常常给人一种距离感。教科书中的历史人物似乎永远停留在文字里#xff0c;我们无法真正理解他们的思想和智慧。如何让这些伟大的历史人物活起来#xff1f;如何让历史学习变得生动有趣#xff1f;带着这些思考历史学习常常给人一种距离感。教科书中的历史人物似乎永远停留在文字里我们无法真正理解他们的思想和智慧。如何让这些伟大的历史人物活起来如何让历史学习变得生动有趣带着这些思考我们开发了一个简单的穿越时空对话机。
源码已经放在最后啦 人物选择界面 聊天界面 1. 项目背景
1.1 创意起源
在传统的历史学习中学生们往往需要通过记忆大量的史实和年代来了解历史。这种学习方式不仅枯燥而且难以激发学习兴趣。我们注意到
学生对历史人物的故事最感兴趣互动式学习效果最好AI技术为教育带来新可能
基于这些观察我萌生了开发一个能让用户直接与历史人物对话的应用的想法。
1.2 项目目标 教育价值 让历史学习变得生动有趣深入理解历史人物的思想培养历史思维能力 技术创新 运用先进的AI对话技术打造流畅的用户体验实现智能的角色扮演 文化传承 弘扬优秀传统文化促进文化交流连接古今智慧
2. 核心功能概述
2.1 人物库设计
我们精心挑选了来自不同时期、不同领域的历史名人 中国古代名人 孔子儒家思想的代表强调仁义礼智信诸葛亮军事谋略家智慧与忠诚的化身李白浪漫主义诗人豪放不羁的文学天才苏轼文学艺术全才豁达乐观的人生导师李时珍医学大师严谨求实的科学精神 世界名人 达芬奇文艺复兴全才艺术与科学的完美结合爱因斯坦现代物理学家创新思维的代表莎士比亚戏剧大师人性洞察的大师莫扎特音乐天才艺术激情的化身
每个人物都经过精心设计包括
详细的背景故事独特的性格特征专业领域知识语言表达风格典型的思维方式
2.2 对话系统
基于DeepSeek API构建的智能对话系统具有以下特点 角色准确性 准确还原历史人物性格保持语言风格一致性符合历史背景 对话智能性 上下文理解能力灵活的问题处理深度的知识储备 交互自然性 流畅的对话节奏自然的语言表达适当的情感表现
3. 技术架构
3.1 技术栈选择
我们采用了现代化的技术栈 前端技术 - React 18
- TypeScript 4.x
- Vite
- CSS ModulesAI对话引擎 - DeepSeek API
- WebSocket
- Axios数据存储 - LocalStorage3.2 系统架构
采用清晰的分层架构 表现层 角色选择界面对话交互界面用户反馈组件 业务层 对话管理角色控制会话处理 数据层 API通信本地存储状态管理
3.3 数据流设计
实现了清晰的单向数据流 #mermaid-svg-RvoMVKJoABCNAvXH {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-RvoMVKJoABCNAvXH .error-icon{fill:#552222;}#mermaid-svg-RvoMVKJoABCNAvXH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RvoMVKJoABCNAvXH .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-RvoMVKJoABCNAvXH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RvoMVKJoABCNAvXH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RvoMVKJoABCNAvXH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RvoMVKJoABCNAvXH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RvoMVKJoABCNAvXH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RvoMVKJoABCNAvXH .marker.cross{stroke:#333333;}#mermaid-svg-RvoMVKJoABCNAvXH svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RvoMVKJoABCNAvXH .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RvoMVKJoABCNAvXH .cluster-label text{fill:#333;}#mermaid-svg-RvoMVKJoABCNAvXH .cluster-label span{color:#333;}#mermaid-svg-RvoMVKJoABCNAvXH .label text,#mermaid-svg-RvoMVKJoABCNAvXH span{fill:#333;color:#333;}#mermaid-svg-RvoMVKJoABCNAvXH .node rect,#mermaid-svg-RvoMVKJoABCNAvXH .node circle,#mermaid-svg-RvoMVKJoABCNAvXH .node ellipse,#mermaid-svg-RvoMVKJoABCNAvXH .node polygon,#mermaid-svg-RvoMVKJoABCNAvXH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RvoMVKJoABCNAvXH .node .label{text-align:center;}#mermaid-svg-RvoMVKJoABCNAvXH .node.clickable{cursor:pointer;}#mermaid-svg-RvoMVKJoABCNAvXH .arrowheadPath{fill:#333333;}#mermaid-svg-RvoMVKJoABCNAvXH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RvoMVKJoABCNAvXH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RvoMVKJoABCNAvXH .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-RvoMVKJoABCNAvXH .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-RvoMVKJoABCNAvXH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RvoMVKJoABCNAvXH .cluster text{fill:#333;}#mermaid-svg-RvoMVKJoABCNAvXH .cluster span{color:#333;}#mermaid-svg-RvoMVKJoABCNAvXH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RvoMVKJoABCNAvXH :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户输入 状态更新 API请求 响应处理 界面更新 4. DeepSeek API集成
4.1 API配置
const api axios.create({baseURL: https://api.deepseek.com/v1,headers: {Authorization: Bearer ${API_KEY},Content-Type: application/json,},
});4.2 对话生成
export const generateResponse async (messages: ChatMessage[]) {const response await api.post(/chat/completions, {model: deepseek-chat,messages,temperature: 0.7,max_tokens: 1000,});return response.data.choices[0].message.content;
};4.3 提示词系统
为每个历史人物设计了专门的提示词模板
interface HistoricalFigurePrompt {id: string;name: string;period: string;description: string;avatar: string;systemPrompt: string;sampleQuestions: string[];personality: string;background: string;
}5. 开发规范
5.1 代码规范
使用ESLint进行代码检查使用Prettier进行代码格式化遵循TypeScript严格模式采用函数式编程范式
5.2 命名规范
组件采用大驼峰命名函数采用小驼峰命名常量使用大写下划线CSS类名采用kebab-case
5.3 文件组织
src/
├── components/ # 通用组件
├── pages/ # 页面组件
├── services/ # 服务层
├── styles/ # 样式文件6. 用户界面设计理念
6.1 设计原则
在设计用户界面时我们遵循以下核心原则 简约而不简单 清晰的视觉层次重点内容突出减少视觉干扰 沉浸式体验 符合历史氛围富有时代特色细节打造意境 交互友好 操作直观反馈及时引导清晰
6.2 视觉风格
我们采用了现代简约与古典元素相结合的设计风格 配色方案 :root {--primary-color: #4A90E2;--secondary-color: #F5A623;--text-color: #2C3E50;--background-color: #F8F9FA;--border-color: #E2E8F0;
}字体选择 中文思源宋体英文Roboto代码JetBrains Mono 布局网格 基础单位8px内容区域最大宽度1200px响应式断点576px、768px、992px、1200px
7. 角色选择页面
7.1 布局设计
角色选择页面采用卡片式布局
const CharacterSelect: React.FC () {return (div classNamecharacter-selectheader classNameheaderh1穿越时空/h1p与历史对话/p/headerdiv classNamecharacter-grid{historicalFigures.map(figure (CharacterCardkey{figure.id}figure{figure}onClick{() handleSelect(figure)}/))}/div/div);
};7.2 角色卡片设计
每个角色卡片包含 视觉元素 头像/形象姓名/时代特色标签简介文字 交互效果 .character-card {transition: transform 0.3s ease;:hover {transform: translateY(-5px);box-shadow: 0 10px 20px rgba(0,0,0,0.1);}
}信息展示 const CharacterCard: React.FCProps ({ figure }) (div classNamecharacter-carddiv classNameavatar{figure.avatar}/divh3{figure.name}/h3p classNameperiod{figure.period}/pp classNamedescription{figure.description}/pdiv classNamepersonality-tags{figure.personality.split().map(tag (span classNametag{tag}/span))}/div/div
);8. 对话界面实现
8.1 整体布局
对话界面分为三个主要部分 顶部导航 返回按钮当前角色信息功能按钮 对话区域 消息气泡打字机效果滚动加载 输入区域 消息输入框发送按钮快捷操作
8.2 消息展示
实现了富有特色的消息气泡
const MessageBubble: React.FCProps ({ message, isUser }) (div className{message ${isUser ? user : assistant}}div classNameavatar{isUser ? : selectedFigure.avatar}/divdiv classNamecontentdiv classNamebubble{message.content}/divdiv classNametime{formatTime(message.timestamp)}/div/div/div
);8.3 打字机效果
实现了自然的打字机效果
const simulateTyping async (content: string) {await new Promise(resolve setTimeout(resolve, 500 Math.random() * 1000));return content;
};8.4 历史记录功能
实现了完整的对话历史管理
interface ChatHistory {characterId: string;messages: ChatMessage[];lastUpdated: string;
}export const saveChatHistory (characterId: string, messages: ChatMessage[]) {try {const existingHistory localStorage.getItem(CHAT_HISTORY_KEY);const history existingHistory ? JSON.parse(existingHistory) : {};history[characterId] {characterId,messages,lastUpdated: new Date().toISOString()};localStorage.setItem(CHAT_HISTORY_KEY, JSON.stringify(history));} catch (error) {console.error(Failed to save chat history:, error);}
};9. 动画效果
9.1 过渡动画
使用CSS transitions实现平滑过渡
.fade-enter {opacity: 0;transform: translateY(20px);
}.fade-enter-active {opacity: 1;transform: translateY(0);transition: opacity 300ms, transform 300ms;
}.fade-exit {opacity: 1;
}.fade-exit-active {opacity: 0;transition: opacity 300ms;
}9.2 加载动画
实现了优雅的加载效果
.typing-indicator {display: flex;gap: 4px;padding: 8px 12px;.dot {width: 8px;height: 8px;background: var(--primary-color);border-radius: 50%;animation: bounce 1.4s infinite ease-in-out;:nth-child(1) { animation-delay: -0.32s; }:nth-child(2) { animation-delay: -0.16s; }}
}keyframes bounce {0%, 80%, 100% { transform: scale(0); }40% { transform: scale(1); }
}10. 响应式设计
10.1 媒体查询
实现了完整的响应式布局
/* 移动端 */
media (max-width: 576px) {.character-grid {grid-template-columns: 1fr;}
}/* 平板 */
media (min-width: 577px) and (max-width: 992px) {.character-grid {grid-template-columns: repeat(2, 1fr);}
}/* 桌面端 */
media (min-width: 993px) {.character-grid {grid-template-columns: repeat(3, 1fr);}
}10.2 弹性布局
使用Flexbox实现灵活的布局
.chat-container {display: flex;flex-direction: column;height: 100vh;.chat-messages {flex: 1;overflow-y: auto;}.chat-input {padding: 1rem;border-top: 1px solid var(--border-color);}
}11. 性能优化
11.1 虚拟滚动
实现了消息列表的虚拟滚动
import { FixedSizeList } from react-window;const MessageList: React.FCProps ({ messages }) (FixedSizeListheight{600}itemCount{messages.length}itemSize{80}width100%{({ index, style }) (MessageItemmessage{messages[index]}style{style}/)}/FixedSizeList
);11.2 防抖处理
对输入和滚动事件进行防抖处理
const debouncedScroll debounce(() {const { scrollTop, scrollHeight, clientHeight } messagesRef.current;if (scrollHeight - scrollTop clientHeight) {loadMoreMessages();}
}, 200);11.3 懒加载
实现了图片和组件的懒加载
const LazyCharacterCard React.lazy(() import(./CharacterCard));const CharacterGrid () (Suspense fallback{Skeleton /}{figures.map(figure (LazyCharacterCardkey{figure.id}figure{figure}/))}/Suspense
);12. 性能优化策略
12.1 代码层面优化
12.1.1 React优化
组件优化
// 使用React.memo避免不必要的重渲染
const MessageBubble React.memoProps(({ message, isUser }) {return (div className{message ${isUser ? user : assistant}}{/* 消息内容 */}/div);
});// 使用useCallback优化事件处理函数
const handleSendMessage useCallback(() {// 处理发送消息
}, [dependencies]);状态管理优化
// 使用useReducer处理复杂状态
const [state, dispatch] useReducer(chatReducer, initialState);// 分离状态逻辑
const chatReducer (state, action) {switch (action.type) {case ADD_MESSAGE:return {...state,messages: [...state.messages, action.payload]};case CLEAR_HISTORY:return {...state,messages: []};default:return state;}
};12.1.2 资源加载优化
代码分割
// 路由级别的代码分割
const ChatInterface lazy(() import(./pages/ChatInterface));
const CharacterSelect lazy(() import(./pages/CharacterSelect));// 组件级别的代码分割
const MarkdownRenderer lazy(() import(./components/MarkdownRenderer));资源预加载
// 预加载关键资源
const prefetchResources () {const links [{ rel: prefetch, href: /assets/fonts/main.woff2 },{ rel: preload, href: /assets/images/sprites.png, as: image }];links.forEach(link {const linkElement document.createElement(link);Object.assign(linkElement, link);document.head.appendChild(linkElement);});
};12.2 网络优化
12.2.1 API请求优化
请求缓存
const cache new Map();const cachedFetch async (url: string) {if (cache.has(url)) {return cache.get(url);}const response await fetch(url);const data await response.json();cache.set(url, data);return data;
};请求合并
const batchRequest async (requests: Request[]) {const batchId generateBatchId();return await api.post(/batch, {batchId,requests});
};12.2.2 WebSocket优化
class WebSocketManager {private ws: WebSocket;private reconnectAttempts 0;private maxReconnectAttempts 5;constructor(url: string) {this.ws new WebSocket(url);this.setupEventHandlers();}private setupEventHandlers() {this.ws.onclose () {if (this.reconnectAttempts this.maxReconnectAttempts) {setTimeout(() this.reconnect(), 1000 * Math.pow(2, this.reconnectAttempts));}};}private reconnect() {this.reconnectAttempts;// 重新连接逻辑}
}12.3 存储优化
12.3.1 本地存储优化
数据压缩
import { compress, decompress } from lz-string;const saveCompressedData (key: string, data: any) {const compressed compress(JSON.stringify(data));localStorage.setItem(key, compressed);
};const loadCompressedData (key: string) {const compressed localStorage.getItem(key);if (!compressed) return null;return JSON.parse(decompress(compressed));
};存储管理
class StorageManager {private maxSize 5 * 1024 * 1024; // 5MBasync cleanup() {const items { ...localStorage };const totalSize Object.values(items).reduce((size, item) size item.length, 0);if (totalSize this.maxSize) {// 清理最旧的数据const oldestKey this.findOldestKey();localStorage.removeItem(oldestKey);}}
}13. 用户体验优化
13.1 交互优化
13.1.1 输入优化
智能提示
const AutoComplete: React.FCProps ({ suggestions, onSelect }) {return (div classNameautocomplete{suggestions.map(suggestion (divkey{suggestion.id}classNamesuggestiononClick{() onSelect(suggestion)}{suggestion.text}/div))}/div);
};输入防抖
const useDebounce (value: string, delay: number) {const [debouncedValue, setDebouncedValue] useState(value);useEffect(() {const handler setTimeout(() {setDebouncedValue(value);}, delay);return () {clearTimeout(handler);};}, [value, delay]);return debouncedValue;
};13.2 动画优化
13.2.1 性能优化的动画
.optimized-animation {will-change: transform;transform: translateZ(0);backface-visibility: hidden;
}keyframes smooth-entrance {from {opacity: 0;transform: translateY(20px);}to {opacity: 1;transform: translateY(0);}
}13.2.2 动画管理
class AnimationManager {private animations: Mapstring, Animation new Map();register(id: string, animation: Animation) {this.animations.set(id, animation);}play(id: string) {const animation this.animations.get(id);if (animation) {animation.play();}}pause(id: string) {const animation this.animations.get(id);if (animation) {animation.pause();}}
}结语
穿越时空对话机探索了如何将先进的AI技术与教育需求相结合的可能性。通过这个项目我们不仅实现了有趣的历史对话功能也积累了宝贵的技术经验。
未来我们将继续优化和扩展这个项目让更多人能够通过这种创新的方式学习历史、感受文化。我们相信技术的进步将为教育带来更多可能性。 Gitee源码 文章转载自: http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.zydr.cn.gov.cn.zydr.cn http://www.morning.plydc.cn.gov.cn.plydc.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.crfyr.cn.gov.cn.crfyr.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn