From 11cb7bc81af7a6f6b457c74275521ab3ab8a88ad Mon Sep 17 00:00:00 2001 From: ryangyuan Date: Tue, 29 Apr 2025 11:49:04 +0800 Subject: [PATCH 1/3] fix: fix playgroud param error --- .../app/dashboard/playground/chat/page.tsx | 44 ++++++------------- lpm_frontend/src/utils/chatStorage.ts | 20 +++++++-- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lpm_frontend/src/app/dashboard/playground/chat/page.tsx b/lpm_frontend/src/app/dashboard/playground/chat/page.tsx index 921f9d53..4cdbe4c8 100644 --- a/lpm_frontend/src/app/dashboard/playground/chat/page.tsx +++ b/lpm_frontend/src/app/dashboard/playground/chat/page.tsx @@ -20,7 +20,7 @@ type Message = StorageMessage; type ModelType = 'chat' | 'thinking'; -interface PlaygroundSettings { +export interface PlaygroundSettings { enableL0Retrieval: boolean; enableL1Retrieval: boolean; enableHelperModel: boolean; @@ -30,9 +30,6 @@ interface PlaygroundSettings { temperature: number; } -// Constants -const STORAGE_KEY_SETTINGS = 'playgroundSettings'; - // Function to generate unique ID const generateMessageId = () => { return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; @@ -92,16 +89,6 @@ export default function PlaygroundChat() { const [settings, setSettings] = useState(originSettings); const messagesEndRef = useRef(null); - useEffect(() => { - setSettings((prev) => { - const newSettings = { ...prev, systemPrompt: originPrompt }; - - localStorage.setItem(STORAGE_KEY_SETTINGS, JSON.stringify(newSettings)); - - return newSettings; - }); - }, [originPrompt]); - useEffect(() => { getTrainingParams() .then((res) => { @@ -156,28 +143,17 @@ export default function PlaygroundChat() { } else { setActiveSessionId(storedSessions[0].id); setMessages(storedSessions[0].messages); - } - }, []); - - // Load sessions, messages, and settings from storage on mount - useEffect(() => { - const storedSettings = localStorage.getItem(STORAGE_KEY_SETTINGS); - - if (storedSettings) { - try { - setSettings(JSON.parse(storedSettings)); - } catch (error) { - console.error('Failed to parse stored settings:', error); - } + setSettings(storedSessions[0].setting); } }, []); const handleNewChat = () => { - const newSession = chatStorage.createSession(); + const newSession = chatStorage.createSession({ originPrompt: originPrompt }); setSessions((prev) => [newSession, ...prev]); setActiveSessionId(newSession.id); setMessages([]); + setSettings(originSettings); }; const handleSessionClick = (sessionId: string) => { @@ -188,12 +164,18 @@ export default function PlaygroundChat() { if (session) { setMessages(session.messages); + setSettings(session.setting); } }; const handleSettingsChange = (newSettings: PlaygroundSettings) => { setSettings(newSettings); - localStorage.setItem(STORAGE_KEY_SETTINGS, JSON.stringify(newSettings)); + + if (activeSessionId) { + chatStorage.updateSession(activeSessionId, { + setting: newSettings + }); + } }; const handleSendMessage = async (content: string) => { @@ -224,7 +206,7 @@ export default function PlaygroundChat() { const systemMessage: Message = { id: generateMessageId(), - content: originPrompt, + content: settings.systemPrompt, role: 'system', timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', @@ -237,7 +219,7 @@ export default function PlaygroundChat() { } else { newMessages = newMessages.map((msg) => { if (msg.role === 'system') { - return { ...msg, content: originPrompt }; + return { ...msg, content: settings.systemPrompt }; } return msg; diff --git a/lpm_frontend/src/utils/chatStorage.ts b/lpm_frontend/src/utils/chatStorage.ts index 684bd3f1..301ad27c 100644 --- a/lpm_frontend/src/utils/chatStorage.ts +++ b/lpm_frontend/src/utils/chatStorage.ts @@ -1,3 +1,5 @@ +import type { PlaygroundSettings } from '@/app/dashboard/playground/chat/page'; + // Storage keys for different chat types const STORAGE_KEYS = { PLAYGROUND: 'playgroundChat', @@ -18,6 +20,7 @@ export interface ChatSession { lastMessage: string; timestamp: string; messages: ChatMessage[]; + setting: PlaygroundSettings; } // Type for playground chat which only stores messages @@ -136,13 +139,24 @@ export const chatWithUploadStorage = { return data.sessions; }, - createSession: (title: string = 'New Conversation'): ChatSession => { + createSession: (props: { title?: string; originPrompt?: string }): ChatSession => { + const { title = 'New Conversation', originPrompt = "You are User's Second Me" } = props; + const newSession: ChatSession = { id: Date.now().toString(), title, lastMessage: '', timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }), - messages: [] + messages: [], + setting: { + enableL0Retrieval: true, + enableL1Retrieval: true, + enableHelperModel: false, + selectedModel: 'ollama', + apiKey: 'http://localhost:11434', + systemPrompt: originPrompt, + temperature: 0.3 + } }; const sessions = chatWithUploadStorage.getSessions(); @@ -218,7 +232,7 @@ export const roleplayChatStorage = { getMessages: (role_id: string): ChatMessage[] => { const data = getStorageData(STORAGE_KEYS.ROLEPLAY, {}); - return data[role_id]?.messages || []; + return data[role_id].messages || []; }, saveMessages: (role_id: string, messages: ChatMessage[]) => { From 46ee16b8fe64f1752f56d4e947fdb92f39c259e0 Mon Sep 17 00:00:00 2001 From: ryangyuan Date: Tue, 29 Apr 2025 12:05:50 +0800 Subject: [PATCH 2/3] fix: fix first inside page error --- lpm_frontend/src/app/dashboard/playground/chat/page.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lpm_frontend/src/app/dashboard/playground/chat/page.tsx b/lpm_frontend/src/app/dashboard/playground/chat/page.tsx index 4cdbe4c8..8758ba83 100644 --- a/lpm_frontend/src/app/dashboard/playground/chat/page.tsx +++ b/lpm_frontend/src/app/dashboard/playground/chat/page.tsx @@ -48,10 +48,6 @@ export default function PlaygroundChat() { const originPrompt = useMemo(() => { const name = loadInfo?.name || 'user'; - if (modelType === 'chat') { - return `You are ${name}'s "Second Me", which is a personalized AI created by ${name}. You can help ${name} answer questions based on your understanding of ${name}'s background information and past records.`; - } - if (modelType === 'thinking') { return `You are ${name}'s "Second Me", and you are currently in conversation with ${name}. Your task is to help ${name} answer relevant questions based on your understanding of ${name}'s background information and past records. @@ -72,7 +68,7 @@ export default function PlaygroundChat() { `; } - return ''; + return `You are ${name}'s "Second Me", which is a personalized AI created by ${name}. You can help ${name} answer questions based on your understanding of ${name}'s background information and past records.`; }, [loadInfo, modelType]); const originSettings = useMemo(() => { return { From 3167276453f9786e3f49b1aa0e9ed83ab53893aa Mon Sep 17 00:00:00 2001 From: ryangyuan Date: Tue, 29 Apr 2025 14:34:37 +0800 Subject: [PATCH 3/3] fix: fix init prompt error --- .../app/dashboard/playground/chat/page.tsx | 59 +++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/lpm_frontend/src/app/dashboard/playground/chat/page.tsx b/lpm_frontend/src/app/dashboard/playground/chat/page.tsx index 8758ba83..baf70d61 100644 --- a/lpm_frontend/src/app/dashboard/playground/chat/page.tsx +++ b/lpm_frontend/src/app/dashboard/playground/chat/page.tsx @@ -44,10 +44,15 @@ export default function PlaygroundChat() { const [activeSessionId, setActiveSessionId] = useState(null); const [messages, setMessages] = useState([]); const [modelType, setModelType] = useState(undefined); + const [initLoading, setInitLoading] = useState(true); const originPrompt = useMemo(() => { const name = loadInfo?.name || 'user'; + if (modelType === 'chat') { + return `You are ${name}'s "Second Me", which is a personalized AI created by ${name}. You can help ${name} answer questions based on your understanding of ${name}'s background information and past records.`; + } + if (modelType === 'thinking') { return `You are ${name}'s "Second Me", and you are currently in conversation with ${name}. Your task is to help ${name} answer relevant questions based on your understanding of ${name}'s background information and past records. @@ -68,7 +73,7 @@ export default function PlaygroundChat() { `; } - return `You are ${name}'s "Second Me", which is a personalized AI created by ${name}. You can help ${name} answer questions based on your understanding of ${name}'s background information and past records.`; + return ''; }, [loadInfo, modelType]); const originSettings = useMemo(() => { return { @@ -85,8 +90,8 @@ export default function PlaygroundChat() { const [settings, setSettings] = useState(originSettings); const messagesEndRef = useRef(null); - useEffect(() => { - getTrainingParams() + const fetchModelType = () => { + return getTrainingParams() .then((res) => { if (res.data.code === 0) { const data = res.data.data; @@ -100,7 +105,7 @@ export default function PlaygroundChat() { .catch((error) => { console.error(error.message); }); - }, []); + }; const scrollToBottom = () => { if (messagesEndRef.current) { @@ -123,13 +128,7 @@ export default function PlaygroundChat() { } }; - // When messages are updated, scroll to the bottom - useEffect(() => { - scrollToBottom(); - }, [messages, streamContent]); - - // First initialization - useEffect(() => { + const firstLoadSessions = () => { const storedSessions = chatStorage.getSessions(); setSessions(storedSessions); @@ -141,8 +140,26 @@ export default function PlaygroundChat() { setMessages(storedSessions[0].messages); setSettings(storedSessions[0].setting); } + }; + + // When messages are updated, scroll to the bottom + useEffect(() => { + scrollToBottom(); + }, [messages, streamContent]); + + // First initialization + useEffect(() => { + fetchModelType().then(() => { + setInitLoading(false); + }); }, []); + useEffect(() => { + if (!initLoading) { + firstLoadSessions(); + } + }, [initLoading]); + const handleNewChat = () => { const newSession = chatStorage.createSession({ originPrompt: originPrompt }); @@ -297,6 +314,26 @@ export default function PlaygroundChat() { } }; + if (initLoading) { + return ( +
+
+
+
+
+
+

Loading Chat...

+
+
+ ); + } + return (