-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add AI kit > generation > data-extraction example #8108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d345483
add generation > data-extraction
atierian 444b1b2
Update src/pages/[platform]/ai/generation/data-extraction/index.mdx
atierian 54d6ddd
Update src/pages/[platform]/ai/generation/data-extraction/index.mdx
atierian 0d81f5b
Update src/pages/[platform]/ai/generation/data-extraction/index.mdx
atierian 1cc16e1
Update src/pages/[platform]/ai/generation/data-extraction/index.mdx
atierian fe616e9
Update src/pages/[platform]/ai/generation/data-extraction/index.mdx
atierian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
117 changes: 117 additions & 0 deletions
117
src/pages/[platform]/ai/generation/data-extraction/index.mdx
This file contains hidden or 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,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()), | ||
}); | ||
``` | ||
|
||
<InlineFilter filters={["javascript","angular","vue"]}> | ||
```ts title="Data Client Request" | ||
import { generateClient } from "aws-amplify/api"; | ||
import { Schema } from "../amplify/data/resource"; | ||
atierian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
export const client = generateClient<Schema>(); | ||
|
||
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" | ||
} | ||
*/ | ||
``` | ||
</InlineFilter> | ||
|
||
|
||
<InlineFilter filters={['react','react-native','next-js']}> | ||
atierian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```ts title="React Hook" | ||
atierian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import { generateClient } from "aws-amplify/api"; | ||
import { createAIHooks } from "@aws-amplify/ui-react-ai"; | ||
import { Schema } from "../amplify/data/resource"; | ||
atierian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const client = generateClient<Schema>({ 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 | ||
}); | ||
}; | ||
} | ||
``` | ||
</InlineFilter> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.