-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sense): basic llm threads support (#1316)
Co-authored-by: Cheslav Zhuravsky <[email protected]>
- Loading branch information
1 parent
4857bd5
commit 67d8e7b
Showing
30 changed files
with
2,302 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useState } from 'react'; | ||
import { ActionBar, Button, Input } from 'src/components'; | ||
import { useAppDispatch, useAppSelector } from 'src/redux/hooks'; | ||
import { | ||
createLLMThread, | ||
addLLMMessageToThread, | ||
replaceLastLLMMessageInThread, | ||
} from 'src/features/sense/redux/sense.redux'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import styles from './ActionBar.module.scss'; | ||
import { llmRequest } from '../../../../containers/Search/LLMSpark/LLMSpark'; | ||
import VoiceInteraction from '../VoiceInteraction/VoiceInteraction'; | ||
|
||
function ActionBarLLM() { | ||
const [text, setText] = useState(''); | ||
const currentThreadId = useAppSelector( | ||
(state) => state.sense.llm.currentThreadId | ||
); | ||
const dispatch = useAppDispatch(); | ||
|
||
const sendMessage = async () => { | ||
if (!text.trim()) { | ||
return; | ||
} | ||
|
||
let threadId = currentThreadId; | ||
if (!threadId) { | ||
// Create new thread if none selected | ||
threadId = uuidv4(); | ||
dispatch(createLLMThread({ id: threadId })); | ||
} | ||
|
||
// Add user's message to the thread | ||
const userMessage = { | ||
text, | ||
sender: 'user', | ||
timestamp: Date.now(), | ||
}; | ||
dispatch(addLLMMessageToThread({ threadId, message: userMessage })); | ||
|
||
// Clear the input field | ||
setText(''); | ||
|
||
// Add "waiting..." message | ||
const waitingMessage = { | ||
text: 'waiting...', | ||
sender: 'llm', | ||
timestamp: Date.now(), | ||
}; | ||
dispatch(addLLMMessageToThread({ threadId, message: waitingMessage })); | ||
|
||
// Send message to LLM API | ||
try { | ||
const responseText = await llmRequest(text); | ||
|
||
// Replace "waiting..." message with the actual response | ||
const llmMessage = { | ||
text: responseText, | ||
sender: 'llm', | ||
timestamp: Date.now(), | ||
}; | ||
dispatch( | ||
replaceLastLLMMessageInThread({ threadId, message: llmMessage }) | ||
); | ||
} catch (error) { | ||
// Handle error: Remove the "waiting..." message | ||
dispatch( | ||
replaceLastLLMMessageInThread({ | ||
threadId, | ||
message: { | ||
text: 'Error: Failed to get response.', | ||
sender: 'llm', | ||
timestamp: Date.now(), | ||
}, | ||
}) | ||
); | ||
console.error('LLM request failed:', error); | ||
} | ||
}; | ||
|
||
return ( | ||
<ActionBar> | ||
<Input | ||
placeholder="Ask the model" | ||
value={text} | ||
onChange={(e) => setText(e.target.value)} | ||
className={styles.input} | ||
/> | ||
<Button onClick={sendMessage}>Send</Button> | ||
<VoiceInteraction /> | ||
</ActionBar> | ||
); | ||
} | ||
|
||
export default ActionBarLLM; |
Oops, something went wrong.