14
14
limitations under the License.
15
15
*/
16
16
17
- import { useState , useRef , useCallback , useEffect , useMemo } from 'react' ;
17
+ import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
18
18
import { useAuth } from 'react-oidc-context' ;
19
19
import Form from '@cloudscape-design/components/form' ;
20
20
import Container from '@cloudscape-design/components/container' ;
21
21
import Box from '@cloudscape-design/components/box' ;
22
22
import { v4 as uuidv4 } from 'uuid' ;
23
23
import SpaceBetween from '@cloudscape-design/components/space-between' ;
24
- import { Grid , TextContent , PromptInput , Autosuggest , ButtonGroup } from '@cloudscape-design/components' ;
24
+ import {
25
+ Autosuggest ,
26
+ ButtonGroup ,
27
+ ButtonGroupProps ,
28
+ Grid ,
29
+ PromptInput ,
30
+ TextContent ,
31
+ } from '@cloudscape-design/components' ;
25
32
import StatusIndicator from '@cloudscape-design/components/status-indicator' ;
26
33
27
34
import Message from './Message' ;
28
- import { LisaChatMessage , LisaChatSession , LisaChatMessageMetadata } from '../types' ;
29
- import { RESTAPI_URI , formatDocumentsAsString , RESTAPI_VERSION } from '../utils' ;
35
+ import { LisaChatMessage , LisaChatMessageMetadata , LisaChatSession } from '../types' ;
36
+ import { formatDocumentsAsString , RESTAPI_URI , RESTAPI_VERSION } from '../utils' ;
30
37
import { LisaChatMessageHistory } from '../adapters/lisa-chat-history' ;
31
38
import { ChatPromptTemplate , MessagesPlaceholder , PromptTemplate } from '@langchain/core/prompts' ;
32
39
import { RunnableSequence } from '@langchain/core/runnables' ;
33
40
import { StringOutputParser } from '@langchain/core/output_parsers' ;
34
- import { BufferWindowMemory } from 'langchain/memory' ;
35
41
import RagControls , { RagConfig } from './RagOptions' ;
36
42
import { ContextUploadModal , RagUploadModal } from './FileUploadModals' ;
37
43
import { ChatOpenAI } from '@langchain/openai' ;
@@ -41,7 +47,7 @@ import { useLazyGetConfigurationQuery } from '../../shared/reducers/configuratio
41
47
import {
42
48
useGetSessionHealthQuery ,
43
49
useLazyGetSessionByIdQuery ,
44
- useUpdateSessionMutation
50
+ useUpdateSessionMutation ,
45
51
} from '../../shared/reducers/session.reducer' ;
46
52
import { useAppDispatch } from '../../config/store' ;
47
53
import { useNotificationService } from '../../shared/util/hooks' ;
@@ -51,6 +57,7 @@ import { baseConfig, GenerateLLMRequestParams, IChatConfiguration } from '../../
51
57
import { useLazyGetRelevantDocumentsQuery } from '../../shared/reducers/rag.reducer' ;
52
58
import { IConfiguration } from '../../shared/model/configuration.model' ;
53
59
import { DocumentSummarizationModal } from './DocumentSummarizationModal' ;
60
+ import { ChatMemory } from '../../shared/util/chat-memory' ;
54
61
55
62
export default function Chat ( { sessionId } ) {
56
63
const dispatch = useAppDispatch ( ) ;
@@ -94,7 +101,7 @@ export default function Chat ({ sessionId }) {
94
101
const [ useRag , setUseRag ] = useState ( false ) ;
95
102
const [ ragConfig , setRagConfig ] = useState < RagConfig > ( { } as RagConfig ) ;
96
103
const [ memory , setMemory ] = useState (
97
- new BufferWindowMemory ( {
104
+ new ChatMemory ( {
98
105
chatHistory : new LisaChatMessageHistory ( session ) ,
99
106
returnMessages : false ,
100
107
memoryKey : 'history' ,
@@ -157,12 +164,11 @@ export default function Chat ({ sessionId }) {
157
164
const handleRagConfiguration = async ( chainSteps : any [ ] ) => {
158
165
const ragStep = {
159
166
input : ( { input } : { input : string } ) => input ,
160
- chatHistory : ( ) => memory . loadMemoryVariables ( { } ) ,
167
+ chatHistory : ( ) => memory . loadMemoryVariables ( ) ,
161
168
context : async ( input : { input : string ; chatHistory ?: LisaChatMessage [ ] } ) => {
162
169
const question = await getContextualizedQuestion ( input ) ;
163
170
const relevantDocs = await fetchRelevantDocuments ( question ) ;
164
- const serialized = await updateSessionWithRagContext ( relevantDocs ) ;
165
- return serialized ;
171
+ return await updateSessionWithRagContext ( relevantDocs ) ;
166
172
} ,
167
173
} ;
168
174
@@ -218,7 +224,7 @@ export default function Chat ({ sessionId }) {
218
224
const handleNonRagConfiguration = ( chainSteps : any [ ] ) => {
219
225
const nonRagStep = {
220
226
input : ( initialInput : any ) => initialInput . input ,
221
- memory : ( ) => memory . loadMemoryVariables ( { } ) ,
227
+ memory : ( ) => memory . loadMemoryVariables ( ) ,
222
228
context : ( ) => fileContext || '' ,
223
229
humanPrefix : ( initialInput : any ) => initialInput . humanPrefix ,
224
230
aiPrefix : ( initialInput : any ) => initialInput . aiPrefix ,
@@ -376,7 +382,7 @@ export default function Chat ({ sessionId }) {
376
382
377
383
useEffect ( ( ) => {
378
384
setMemory (
379
- new BufferWindowMemory ( {
385
+ new ChatMemory ( {
380
386
chatHistory : new LisaChatMessageHistory ( session ) ,
381
387
returnMessages : false ,
382
388
memoryKey : 'history' ,
@@ -390,7 +396,7 @@ export default function Chat ({ sessionId }) {
390
396
391
397
useEffect ( ( ) => {
392
398
if ( selectedModel && auth . isAuthenticated ) {
393
- memory . loadMemoryVariables ( { } ) . then ( async ( formattedHistory ) => {
399
+ memory . loadMemoryVariables ( ) . then ( async ( formattedHistory ) => {
394
400
const promptValues = {
395
401
input : userPrompt ,
396
402
history : formattedHistory . history ,
@@ -543,9 +549,9 @@ export default function Chat ({ sessionId }) {
543
549
< div ref = { bottomRef } />
544
550
</ SpaceBetween >
545
551
</ div >
546
- < div className = 'fixed bottom-8' style = { { width : 'calc(var(--awsui-layout-width-g964ok) - 500px)' } } >
552
+ < div className = 'sticky bottom-8' >
547
553
< form onSubmit = { ( e ) => e . preventDefault ( ) } >
548
- < Form variant = 'embedded' >
554
+ < Form >
549
555
< Container >
550
556
< SpaceBetween size = 'm' direction = 'vertical' >
551
557
< Grid
@@ -562,6 +568,7 @@ export default function Chat ({ sessionId }) {
562
568
empty = { < div className = 'text-gray-500' > No models available.</ div > }
563
569
filteringType = 'auto'
564
570
value = { selectedModel ?. modelId ?? '' }
571
+ enteredTextLabel = { ( text ) => `Use: "${ text } "` }
565
572
onChange = { ( { detail : { value } } ) => {
566
573
if ( ! value || value . length === 0 ) {
567
574
setSelectedModel ( undefined ) ;
@@ -635,20 +642,20 @@ export default function Chat ({ sessionId }) {
635
642
id : 'upload-to-rag' ,
636
643
iconName : 'upload' ,
637
644
text : 'Upload to RAG'
638
- } ] : [ ] ) ,
645
+ } ] as ButtonGroupProps . Item [ ] : [ ] ) ,
639
646
...( config ?. configuration . enabledComponents . uploadContextDocs ?
640
647
[ {
641
648
type : 'icon-button' ,
642
649
id : 'add-file-to-context' ,
643
650
iconName : 'insert-row' ,
644
651
text : 'Add file to context'
645
- } ] : [ ] ) ,
652
+ } ] as ButtonGroupProps . Item [ ] : [ ] ) ,
646
653
...( config ?. configuration . enabledComponents . documentSummarization ? [ {
647
654
type : 'icon-button' ,
648
655
id : 'summarize-document' ,
649
656
iconName : 'transcript' ,
650
657
text : 'Summarize Document'
651
- } ] : [ ] ) ,
658
+ } ] as ButtonGroupProps . Item [ ] : [ ] ) ,
652
659
...( config ?. configuration . enabledComponents . editPromptTemplate ?
653
660
[ {
654
661
type : 'menu-dropdown' ,
@@ -661,7 +668,7 @@ export default function Chat ({ sessionId }) {
661
668
text : 'Edit Prompt Template'
662
669
} ,
663
670
]
664
- } ] : [ ] )
671
+ } ] as ButtonGroupProps . Item [ ] : [ ] )
665
672
] }
666
673
variant = 'icon'
667
674
/>
0 commit comments