Skip to content

Commit be470e0

Browse files
docs(ai): add initial setup-ai page (#7960)
Co-authored-by: Danny Banks <[email protected]>
1 parent b5db1bf commit be470e0

File tree

1 file changed

+157
-0
lines changed
  • src/pages/[platform]/build-a-backend/ai/set-up-ai

1 file changed

+157
-0
lines changed

src/pages/[platform]/build-a-backend/ai/set-up-ai/index.mdx

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,160 @@ export function getStaticProps(context) {
3232
Amplify AI sections are under construction
3333

3434
</Callout>
35+
36+
In this guide, you will learn how to set up Amplify AI. This includes building Conversation and Generation routes, and securing access to these routes with authorization rules.
37+
38+
Before you begin, you will need:
39+
40+
- [Node.js](https://nodejs.org/) v18.16.0 or later
41+
- [npm](https://www.npmjs.com/) v6.14.4 or later
42+
- [git](https://git-scm.com/) v2.14.1 or later
43+
44+
You will also need an AWS Account with access to the Bedrock Foundation Model(s) you want to use. You can do that by going in to the [Bedrock console and requesting access](https://console.aws.amazon.com/bedrock/home#/modelaccess).
45+
46+
With Amplify AI, you can define Conversation and Generation routes backed by an AWS AppSync API and Amazon Bedrock.
47+
48+
## Building your AI backend
49+
50+
If you've run `npm create amplify@latest` already, you should see an `amplify/data/resource/ts` file, which is the central location to configure your AI backend. Within the `schema` object, you define Conversation routes (`a.conversation({})`) and Generation routes (`a.generation({})`), and can integrate them seamlessly with existing Amplify Data schema objects.
51+
52+
```ts title="amplify/data/resources.ts"
53+
import { a, defineData, type ClientSchema } from '@aws-amplify/backend';
54+
55+
const schema = a.schema({
56+
// This will add a new conversation route to your Amplify Data backend.
57+
chat: a.conversation({
58+
aiModel: a.ai.model('Claude 3 Haiku'),
59+
systemPrompt: 'You are a helpful assistant',
60+
}),
61+
62+
// This adds a new generation route to your Amplify Data backend.
63+
generateRecipe: a.generation({
64+
aiModel: a.ai.model('Claude 3 Haiku'),
65+
systemPrompt: 'You are a helpful assistant that generates recipes.',
66+
})
67+
.arguments({
68+
description: a.string(),
69+
})
70+
// This specifies the return type of the generation route.
71+
.returns(
72+
a.customType({
73+
name: a.string(),
74+
ingredients: a.string().array(),
75+
instructions: a.string(),
76+
})
77+
)
78+
// only logged in users can access this
79+
.authorization((allow) => allow.authenticated()),
80+
});
81+
```
82+
83+
To deploy these resources to your cloud sandbox, run the following CLI command in your terminal:
84+
85+
```bash title="Terminal"
86+
npx ampx sandbox
87+
```
88+
89+
## Connect your application code the the AI routes
90+
91+
Once the cloud sandbox is up and running, it will also create an `amplify_outputs.json` file, which includes relevant connection information to your AI routes.
92+
93+
To connect your frontend code to your backend, you need to:
94+
95+
1. Configure the Amplify library with the Amplify client configuration file (`amplify_outputs.json`).
96+
2. Generate a new API client from the Amplify library.
97+
3. Make an API request with end-to-end type-safety.
98+
99+
## AI Route UI
100+
101+
_NOTE: If you are not using React in your frontend, check out the section for using the Amplify Javascript libraries._
102+
103+
First, install the Amplify client library to your project:
104+
105+
```bash title="Terminal"
106+
npm add aws-amplify @aws-amplify/ui-react @aws-amplify/ui-react-ai
107+
```
108+
109+
### Conversation Route UI
110+
Then in your React code, import and use the `AIConversation` component to start a conversation with your AI route:
111+
112+
```tsx title="src/page.tsx"
113+
import { generateClient } from "aws-amplify/data";
114+
import { Authenticator } from "@aws-amplify/ui-react";
115+
import { createAIHooks, AIConversation } from '@aws-amplify/ui-react-ai';
116+
import type { Schema } from "../amplify/data/resource";
117+
118+
const client = generateClient<Schema>({ authMode: 'userPool' });
119+
const { useAIConversation } = createAIHooks(client);
120+
121+
export default function Page() {
122+
const [
123+
{
124+
data: { messages },
125+
},
126+
sendMessage,
127+
] = useAIConversation('chat');
128+
// 'chat' is based on the key for the conversation route in your schema.
129+
130+
return (
131+
<Authenticator>
132+
<AIConversation
133+
messages={messages}
134+
handleSendMessage={sendMessage}
135+
/>
136+
</Authenticator>
137+
);
138+
}
139+
```
140+
141+
### Generation Route UI
142+
143+
```tsx title="src/page.tsx"
144+
import { createAIHooks } from "@aws-amplify/ui-react-ai";
145+
import { generateClient } from "aws-amplify/api";
146+
import type { Schema } from "../amplify/data/resource";
147+
148+
const client = generateClient<Schema>({ authMode: "userPool" });
149+
const { useAIGeneration, useAIConversation } = createAIHooks(client);
150+
151+
export default function Page() {
152+
const [description, setDescription] = React.useState("");
153+
const [{ data, isLoading, hasError }, generateRecipe] =
154+
useAIGeneration("generateRecipe");
155+
156+
const handleClick = async () => {
157+
generateRecipe({ description });
158+
};
159+
160+
return (
161+
<Flex direction="column">
162+
<Flex direction="row">
163+
<TextAreaField
164+
autoResize
165+
value={description}
166+
onChange={(e) => setDescription(e.target.value)}
167+
label="Description"
168+
/>
169+
<Button onClick={handleClick}>Generate recipe</Button>
170+
</Flex>
171+
{isLoading ? (
172+
<Loader variation="linear" />
173+
) : (
174+
<>
175+
<Heading level={2}>{data?.name}</Heading>
176+
<View as="ul">
177+
{data?.ingredients?.map((ingredient) => (
178+
<Text as="li" key={ingredient}>
179+
{ingredient}
180+
</Text>
181+
))}
182+
</View>
183+
<Text>{data?.instructions}</Text>
184+
</>
185+
)}
186+
</Flex>
187+
);
188+
}
189+
```
190+
191+
## Next steps

0 commit comments

Comments
 (0)