-
Notifications
You must be signed in to change notification settings - Fork 301
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 message renderer (#5873)
- Loading branch information
1 parent
7f42be2
commit 3a697ea
Showing
12 changed files
with
190 additions
and
53 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,16 @@ | ||
--- | ||
"@aws-amplify/ui-react-ai": minor | ||
--- | ||
|
||
feat(ai): add message renderer | ||
|
||
```tsx | ||
<AIConversation | ||
messages={messages} | ||
handleSendMessage={sendMessage} | ||
isLoading={isLoading} | ||
messageRenderer={{ | ||
text: ({text}) => <ReactMarkdown>{text}</ReactMarkdown>, | ||
}} | ||
/> | ||
``` |
2 changes: 2 additions & 0 deletions
2
examples/next/pages/ui/components/ai/ai-conversation-renderer/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; |
89 changes: 89 additions & 0 deletions
89
examples/next/pages/ui/components/ai/ai-conversation-renderer/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,89 @@ | ||
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 '@aws-amplify/ui-react-ai/ai-conversation-styles.css'; | ||
|
||
import outputs from './amplify_outputs'; | ||
import type { Schema } from '@environments/ai/gen2/amplify/data/resource'; | ||
import { Authenticator, Card, Text } from '@aws-amplify/ui-react'; | ||
import Image from 'next/image'; | ||
|
||
const client = generateClient<Schema>({ authMode: 'userPool' }); | ||
const { useAIConversation } = createAIHooks(client); | ||
|
||
Amplify.configure(outputs); | ||
|
||
function arrayBufferToBase64(buffer: ArrayBuffer) { | ||
let binary = ''; | ||
const bytes = new Uint8Array(buffer); | ||
const len = bytes.byteLength; | ||
for (let i = 0; i < len; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
return window.btoa(binary); | ||
} | ||
|
||
function convertBufferToBase64(buffer: ArrayBuffer, format: string): string { | ||
let base64string = ''; | ||
// Use node-based buffer if available | ||
// fall back on browser if not | ||
if (typeof Buffer !== 'undefined') { | ||
base64string = Buffer.from(new Uint8Array(buffer)).toString('base64'); | ||
} else { | ||
base64string = arrayBufferToBase64(buffer); | ||
} | ||
return `data:image/${format};base64,${base64string}`; | ||
} | ||
|
||
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 | ||
messageRenderer={{ | ||
text: ({ text }) => <Text className="testing">{text}</Text>, | ||
image: ({ image }) => ( | ||
<Image | ||
className="testing" | ||
width={200} | ||
height={200} | ||
src={convertBufferToBase64(image.source.bytes, image.format)} | ||
alt="" | ||
/> | ||
), | ||
}} | ||
suggestedPrompts={[ | ||
{ | ||
inputText: 'hello', | ||
header: 'hello', | ||
}, | ||
{ | ||
inputText: 'how are you?', | ||
header: 'how are you?', | ||
}, | ||
]} | ||
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
13 changes: 13 additions & 0 deletions
13
packages/react-ai/src/components/AIConversation/context/MessageRenderContext.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,13 @@ | ||
import { createContextUtilities } from '@aws-amplify/ui-react-core'; | ||
import { MessageRenderer } from '../types'; | ||
|
||
export const { | ||
MessageRendererContext, | ||
MessageRendererProvider, | ||
useMessageRenderer, | ||
} = createContextUtilities<MessageRenderer>({ | ||
contextName: 'MessageRenderer', | ||
defaultValue: undefined, | ||
errorMessage: | ||
'`useMessageRenderer` must be used with an AIConversation component', | ||
}); |
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
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