-
Notifications
You must be signed in to change notification settings - Fork 305
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 aiContext prop to AIConversation (#6090)
- Loading branch information
1 parent
022b586
commit a25e5ed
Showing
7 changed files
with
160 additions
and
8 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,21 @@ | ||
--- | ||
"@aws-amplify/ui-react-ai": minor | ||
--- | ||
|
||
feat(ai): add aiContext prop to AIConversation | ||
|
||
```tsx | ||
<AIConversation | ||
messages={messages} | ||
isLoading={isLoading} | ||
handleSendMessage={sendMessage} | ||
// This will let the LLM know about the current state of this application | ||
// so it can better respond to questions, you can put any information | ||
// in this object that might be helpful | ||
aiContext={() => { | ||
return { | ||
currentTime: new Date().toLocaleTimeString(), | ||
}; | ||
}} | ||
/> | ||
``` |
98 changes: 98 additions & 0 deletions
98
examples/next/pages/ui/components/ai/ai-conversation/context.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,98 @@ | ||
import * as React from 'react'; | ||
import { Amplify } from 'aws-amplify'; | ||
import { signOut } from 'aws-amplify/auth'; | ||
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, Button, Card, Flex } from '@aws-amplify/ui-react'; | ||
|
||
const client = generateClient<Schema>({ authMode: 'userPool' }); | ||
const { useAIConversation } = createAIHooks(client); | ||
|
||
Amplify.configure(outputs); | ||
|
||
function Chat() { | ||
const { data } = React.useContext(AIContext); | ||
const [ | ||
{ | ||
data: { messages }, | ||
isLoading, | ||
}, | ||
sendMessage, | ||
] = useAIConversation('pirateChat'); | ||
|
||
return ( | ||
<AIConversation | ||
messages={messages} | ||
isLoading={isLoading} | ||
handleSendMessage={sendMessage} | ||
// This will let the LLM know about the current state of this application | ||
// so it can better respond to questions | ||
aiContext={() => { | ||
return { | ||
...data, | ||
currentTime: new Date().toLocaleTimeString(), | ||
}; | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
function Counter() { | ||
const { data, setData } = React.useContext(AIContext); | ||
const count = data.count ?? 0; | ||
return ( | ||
<Button onClick={() => setData({ ...data, count: count + 1 })}> | ||
{count} | ||
</Button> | ||
); | ||
} | ||
|
||
const AIContext = React.createContext<{ | ||
data: any; | ||
setData: (value: React.SetStateAction<any>) => void; | ||
}>({ data: {}, setData: () => {} }); | ||
|
||
const AIContextProvider = ({ | ||
children, | ||
}: { | ||
children?: React.ReactNode; | ||
}): JSX.Element => { | ||
const [data, setData] = React.useState({}); | ||
return ( | ||
<AIContext.Provider value={{ data, setData }}> | ||
{children} | ||
</AIContext.Provider> | ||
); | ||
}; | ||
|
||
export default function Example() { | ||
return ( | ||
<Authenticator> | ||
<AIContextProvider> | ||
<Flex direction="column" alignItems="flex-start"> | ||
<Button | ||
onClick={() => { | ||
signOut(); | ||
}} | ||
> | ||
Sign out | ||
</Button> | ||
<Card | ||
flex="1" | ||
variation="outlined" | ||
// height="400px" | ||
width="100%" | ||
margin="large" | ||
> | ||
<Chat /> | ||
</Card> | ||
<Counter /> | ||
</Flex> | ||
</AIContextProvider> | ||
</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
19 changes: 19 additions & 0 deletions
19
packages/react-ai/src/components/AIConversation/context/AIContextContext.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,19 @@ | ||
import React from 'react'; | ||
|
||
export const AIContextContext = React.createContext<(() => object) | undefined>( | ||
undefined | ||
); | ||
|
||
export const AIContextProvider = ({ | ||
children, | ||
aiContext, | ||
}: { | ||
children?: React.ReactNode; | ||
aiContext?: () => object; | ||
}): JSX.Element => { | ||
return ( | ||
<AIContextContext.Provider value={aiContext}> | ||
{children} | ||
</AIContextContext.Provider> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/react-ai/src/components/AIConversation/context/index.ts
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