From d3454835213dad6900e7fbc6cc9443a8b3f48989 Mon Sep 17 00:00:00 2001
From: Ian Saultz <52051793+atierian@users.noreply.github.com>
Date: Sat, 16 Nov 2024 14:57:45 -0500
Subject: [PATCH] add generation > data-extraction
wip
extract data
---
src/directory/directory.mjs | 7 +-
.../ai/generation/data-extraction/index.mdx | 117 ++++++++++++++++++
2 files changed, 123 insertions(+), 1 deletion(-)
create mode 100644 src/pages/[platform]/ai/generation/data-extraction/index.mdx
diff --git a/src/directory/directory.mjs b/src/directory/directory.mjs
index d6337968899..12a1af8698d 100644
--- a/src/directory/directory.mjs
+++ b/src/directory/directory.mjs
@@ -771,7 +771,12 @@ export const directory = {
]
},
{
- path: 'src/pages/[platform]/ai/generation/index.mdx'
+ path: 'src/pages/[platform]/ai/generation/index.mdx',
+ children: [
+ {
+ path: 'src/pages/[platform]/ai/generation/data-extraction/index.mdx'
+ }
+ ]
}
]
},
diff --git a/src/pages/[platform]/ai/generation/data-extraction/index.mdx b/src/pages/[platform]/ai/generation/data-extraction/index.mdx
new file mode 100644
index 00000000000..9c6b5295218
--- /dev/null
+++ b/src/pages/[platform]/ai/generation/data-extraction/index.mdx
@@ -0,0 +1,117 @@
+import { getCustomStaticPath } from "@/utils/getCustomStaticPath";
+
+export const meta = {
+ title: "Data Extraction",
+ description:
+ "How to extract data from unstructured text.",
+ platforms: [
+ "javascript",
+ "react-native",
+ "angular",
+ "nextjs",
+ "react",
+ "vue",
+ ],
+};
+
+export const getStaticPaths = async () => {
+ return getCustomStaticPath(meta.platforms);
+};
+
+export function getStaticProps(context) {
+ return {
+ props: {
+ platform: context.params.platform,
+ meta,
+ showBreadcrumbs: false,
+ },
+ };
+}
+
+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.
+
+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.
+
+```typescript title="Schema Definition"
+const schema = a.schema({
+ ProductDetails: a.customType({
+ name: a.string().required(),
+ summary: a.string().required(),
+ price: a.float().required(),
+ category: a.string().required(),
+ }),
+
+ extractProductDetails: a.generation({
+ aiModel: a.ai.model('Claude 3 Haiku'),
+ systemPrompt: 'Extract the property details from the text provided',
+ })
+ .arguments({
+ productDescription: a.string()
+ })
+ .returns(a.ref('ProductDetails'))
+ .authorization((allow) => allow.authenticated()),
+});
+```
+
+
+```ts title="Data Client Request"
+import { generateClient } from "aws-amplify/api";
+import { Schema } from "../amplify/data/resource";
+
+export const client = generateClient();
+
+const productDescription = `The NBA Official Game Basketball is a premium
+regulation-size basketball crafted with genuine leather and featuring
+official NBA specifications. This professional-grade ball offers superior grip
+and durability, with deep channels and a moisture-wicking surface that ensures
+consistent performance during intense game play. Priced at $159.99, this high-end
+basketball belongs in our Professional Sports Equipment category and is the same model
+used in NBA games.`
+
+const { data, errors } = await client.generations
+ .extractProductDetails({ productDescription })
+
+/**
+Example response:
+{
+ "name": "NBA Official Game Basketball",
+ "summary": "Premium regulation-size NBA basketball made with genuine leather. Features official NBA specifications, superior grip, deep channels, and moisture-wicking surface for consistent game play performance.",
+ "price": 159.99,
+ "category": "Professional Sports Equipment"
+}
+*/
+```
+
+
+
+
+
+```ts title="React Hook"
+import { generateClient } from "aws-amplify/api";
+import { createAIHooks } from "@aws-amplify/ui-react-ai";
+import { Schema } from "../amplify/data/resource";
+
+const client = generateClient({ authMode: "userPool" });
+const { useAIGeneration } = createAIHooks(client);
+
+export default function Example() {
+ const productDescription = `The NBA Official Game Basketball is a premium
+ regulation-size basketball crafted with genuine leather and featuring
+ official NBA specifications. This professional-grade ball offers superior grip
+ and durability, with deep channels and a moisture-wicking surface that ensures
+ consistent performance during intense game play. Priced at $159.99, this high-end
+ basketball belongs in our Professional Sports Equipment category and is the same model
+ used in NBA games.`
+
+ // data is React state and will be populated when the generation is returned
+ const [{ data, isLoading }, extractProductDetails] =
+ useAIGeneration("extractProductDetails");
+
+ const productDetails = async () => {
+ extractProductDetails({
+ productDescription
+ });
+ };
+}
+```
+