Primitives.org.ai

extract

Extract structured data from unstructured text

Extract structured data from unstructured text. Supports streaming.

Import

import { extract } from 'ai-functions'

Syntax

extract`items from ${text}`
extract`items from ${text}${{ schema }}`
for await (const item of extract`...`) { }

Examples

// Simple extraction
const names = await extract`person names from ${article}`
// ['John Smith', 'Jane Doe', 'Bob Wilson']

// With schema
const companies = await extract`companies from ${text}${{
  schema: {
    name: 'Company name',
    role: 'mentioned as: competitor | partner | customer',
  },
}}`
// [{ name: 'Acme', role: 'competitor' }, ...]

// Stream extraction
for await (const email of extract`email addresses from ${document}`) {
  await sendNotification(email)
}

Schema Extraction

Define the shape of extracted items:

const people = await extract`people from ${article}${{
  schema: {
    name: 'Full name',
    title: 'Job title (if mentioned)',
    company: 'Company name (if mentioned)',
  },
}}`

Use Cases

// Data extraction
const emails = await extract`email addresses from ${text}`
const dates = await extract`dates from ${document}`
const prices = await extract`prices from ${webpage}`

// Entity extraction
const companies = await extract`company names from ${article}`
const products = await extract`product mentions from ${review}`

// Research processing
const research = await research`competitors in ${market}`
for await (const competitor of extract`company names from ${research}`) {
  const analysis = await research`${competitor} vs ${ourProduct}`
}

Streaming Pattern

Process extractions one at a time:

for await (const mention of extract`product mentions from ${text}`) {
  const sentiment = await is`${mention} positive sentiment?`
  await trackMention(mention, sentiment)
}
Was this page helpful?

On this page