forked from aws-amplify/amplify-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai): add fallback response component (aws-amplify#5968)
Co-authored-by: dindjarinjs <[email protected]>
- Loading branch information
1 parent
90cb443
commit 3b96ff7
Showing
11 changed files
with
213 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
"@aws-amplify/ui-react-ai": minor | ||
--- | ||
|
||
feat(ai): add fallback response component | ||
|
||
```tsx | ||
<AIConversation | ||
FallBackResponseComponent={(props) => <>{JSON.stringify(props)}</>} | ||
//... | ||
/> | ||
``` |
2 changes: 2 additions & 0 deletions
2
examples/next/pages/ui/components/ai/ai-conversation-response-components/amplify_outputs.js
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,2 @@ | ||
import amplifyOutputs from '@environments/ai/gen2/amplify_outputs'; | ||
export default amplifyOutputs; |
51 changes: 51 additions & 0 deletions
51
examples/next/pages/ui/components/ai/ai-conversation-response-components/fallback.page.tsx
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,51 @@ | ||
import { AIConversation, ConversationMessage } from '@aws-amplify/ui-react-ai'; | ||
import '@aws-amplify/ui-react/styles.css'; | ||
|
||
const messages: ConversationMessage[] = [ | ||
{ | ||
role: 'user', | ||
content: [ | ||
{ | ||
text: 'hello', | ||
}, | ||
], | ||
conversationId: '1', | ||
id: '2', | ||
createdAt: new Date(2023, 4, 21, 15, 24).toDateString(), | ||
}, | ||
{ | ||
role: 'assistant', | ||
content: [ | ||
{ | ||
toolUse: { | ||
name: 'AMPLIFY_UI_foobar', | ||
input: { foo: 'bar' }, | ||
toolUseId: '1234', | ||
}, | ||
}, | ||
], | ||
conversationId: '1', | ||
id: '2', | ||
createdAt: new Date(2023, 4, 21, 15, 24).toDateString(), | ||
}, | ||
]; | ||
|
||
// Note: because response components are sent in the message | ||
// there could be cases where the AIConversation component | ||
// gets rendered with an existing conversation and does not | ||
// have the React component needed to render the response | ||
// component. For example in the Amplify console, we don't | ||
// have customers' React code running in our console. | ||
// Because of this, this example page isn't actually | ||
// using a live conversation route. | ||
export default function Example() { | ||
return ( | ||
<AIConversation | ||
messages={messages} | ||
handleSendMessage={() => {}} | ||
FallbackResponseComponent={(props) => { | ||
return <div>{JSON.stringify(props)}</div>; | ||
}} | ||
/> | ||
); | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/next/pages/ui/components/ai/ai-conversation-response-components/index.page.tsx
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,58 @@ | ||
import { Amplify } from 'aws-amplify'; | ||
import { createAIHooks, AIConversation } from '@aws-amplify/ui-react-ai'; | ||
import { generateClient } from 'aws-amplify/api'; | ||
import '@aws-amplify/ui-react/styles.css'; | ||
|
||
import outputs from './amplify_outputs'; | ||
import type { Schema } from '@environments/ai/gen2/amplify/data/resource'; | ||
import { Authenticator, Card } from '@aws-amplify/ui-react'; | ||
|
||
const client = generateClient<Schema>({ authMode: 'userPool' }); | ||
const { useAIConversation } = createAIHooks(client); | ||
|
||
Amplify.configure(outputs); | ||
|
||
function Chat() { | ||
const [ | ||
{ | ||
data: { messages }, | ||
isLoading, | ||
}, | ||
sendMessage, | ||
] = useAIConversation('pirateChat'); | ||
|
||
return ( | ||
<Card variation="outlined" width="50%" height="300px" margin="0 auto"> | ||
<AIConversation | ||
messages={messages} | ||
handleSendMessage={sendMessage} | ||
isLoading={isLoading} | ||
allowAttachments | ||
responseComponents={{ | ||
WeatherCard: { | ||
description: | ||
'Used to display the weather of a given city to the user', | ||
component: ({ city }) => { | ||
return <Card>{city}</Card>; | ||
}, | ||
props: { | ||
city: { | ||
type: 'string', | ||
required: true, | ||
}, | ||
}, | ||
}, | ||
}} | ||
variant="bubble" | ||
/> | ||
</Card> | ||
); | ||
} | ||
|
||
export default function Example() { | ||
return ( | ||
<Authenticator> | ||
<Chat /> | ||
</Authenticator> | ||
); | ||
} |
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
20 changes: 20 additions & 0 deletions
20
packages/react-ai/src/components/AIConversation/context/FallbackComponentContext.tsx
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,20 @@ | ||
import React from 'react'; | ||
import { AIConversationInput } from '../types'; | ||
|
||
export const FallbackComponentContext = React.createContext< | ||
AIConversationInput['FallbackResponseComponent'] | undefined | ||
>(undefined); | ||
|
||
export const FallbackComponentProvider = ({ | ||
children, | ||
FallbackComponent, | ||
}: { | ||
children?: React.ReactNode; | ||
FallbackComponent?: AIConversationInput['FallbackResponseComponent']; | ||
}): JSX.Element => { | ||
return ( | ||
<FallbackComponentContext.Provider value={FallbackComponent}> | ||
{children} | ||
</FallbackComponentContext.Provider> | ||
); | ||
}; |
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