Skip to content

Commit d69d025

Browse files
committed
add generation > data-extraction
wip extract data
1 parent d57e3ee commit d69d025

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

Diff for: src/directory/directory.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,12 @@ export const directory = {
771771
]
772772
},
773773
{
774-
path: 'src/pages/[platform]/ai/generation/index.mdx'
774+
path: 'src/pages/[platform]/ai/generation/index.mdx',
775+
children: [
776+
{
777+
path: 'src/pages/[platform]/ai/generation/data-extraction/index.mdx'
778+
}
779+
]
775780
}
776781
]
777782
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { getCustomStaticPath } from "@/utils/getCustomStaticPath";
2+
3+
export const meta = {
4+
title: "Data Extraction",
5+
description:
6+
"How to extract data from unstructured text.",
7+
platforms: [
8+
"javascript",
9+
"react-native",
10+
"angular",
11+
"nextjs",
12+
"react",
13+
"vue",
14+
],
15+
};
16+
17+
export const getStaticPaths = async () => {
18+
return getCustomStaticPath(meta.platforms);
19+
};
20+
21+
export function getStaticProps(context) {
22+
return {
23+
props: {
24+
platform: context.params.platform,
25+
meta,
26+
showBreadcrumbs: false,
27+
},
28+
};
29+
}
30+
31+
Data extraction allows you to parse unstructured text and extract structured data using AI. This is useful for converting free-form text into typed objects that can be used in your application.
32+
33+
The following example shows how to extract product details from an unstructured product description. The AI model will analyze the text and return a structured object containing the product name, summary, price, and category.
34+
35+
```typescript title="Schema Definition"
36+
const schema = a.schema({
37+
ProductDetails: a.customType({
38+
name: a.string().required(),
39+
summary: a.string().required(),
40+
price: a.float().required(),
41+
category: a.string().required(),
42+
}),
43+
44+
extractProductDetails: a.generation({
45+
aiModel: a.ai.model('Claude 3 Haiku'),
46+
systemPrompt: 'Extract the property details from the text provided',
47+
})
48+
.arguments({
49+
productDescription: a.string()
50+
})
51+
.returns(a.ref('ProductDetails'))
52+
.authorization((allow) => allow.authenticated()),
53+
});
54+
```
55+
56+
<InlineFilter filters={["javascript","angular","vue"]}>
57+
```ts title="Data Client Request"
58+
import { generateClient } from "aws-amplify/api";
59+
import { Schema } from "../amplify/data/resource";
60+
61+
export const client = generateClient<Schema>();
62+
63+
const productDescription = `The NBA Official Game Basketball is a premium
64+
regulation-size basketball crafted with genuine leather and featuring
65+
official NBA specifications. This professional-grade ball offers superior grip
66+
and durability, with deep channels and a moisture-wicking surface that ensures
67+
consistent performance during intense gameplay. Priced at $159.99, this high-end
68+
basketball belongs in our Professional Sports Equipment category and is the same model
69+
used in NBA games.`
70+
71+
const { data, errors } = await client.generations
72+
.extractProductDetails({ productDescription })
73+
74+
/**
75+
Example response:
76+
{
77+
"name": "NBA Official Game Basketball",
78+
"summary": "Premium regulation-size NBA basketball made with genuine leather. Features official NBA specifications, superior grip, deep channels, and moisture-wicking surface for consistent gameplay performance.",
79+
"price": 159.99,
80+
"category": "Professional Sports Equipment"
81+
}
82+
*/
83+
```
84+
</InlineFilter>
85+
86+
87+
<InlineFilter filters={['react','react-native','next-js']}>
88+
89+
```ts title="React Hook"
90+
import { generateClient } from "aws-amplify/api";
91+
import { createAIHooks } from "@aws-amplify/ui-react-ai";
92+
import { Schema } from "../amplify/data/resource";
93+
94+
const client = generateClient<Schema>({ authMode: "userPool" });
95+
const { useAIGeneration } = createAIHooks(client);
96+
97+
export default function Example() {
98+
const productDescription = `The NBA Official Game Basketball is a premium
99+
regulation-size basketball crafted with genuine leather and featuring
100+
official NBA specifications. This professional-grade ball offers superior grip
101+
and durability, with deep channels and a moisture-wicking surface that ensures
102+
consistent performance during intense gameplay. Priced at $159.99, this high-end
103+
basketball belongs in our Professional Sports Equipment category and is the same model
104+
used in NBA games.`
105+
106+
// data is React state and will be populated when the generation is returned
107+
const [{ data, isLoading }, extractProductDetails] =
108+
useAIGeneration("extractProductDetails");
109+
110+
const productDetails = async () => {
111+
extractProductDetails({
112+
productDescription
113+
});
114+
};
115+
}
116+
```
117+
</InlineFilter>

0 commit comments

Comments
 (0)