Extraction Engines

Contents

Extraction Engines#

Overview#

Extraction Engines enable the extraction of structured data from unstructured text. These Engines can be used with Structures via Extraction Tasks . As of now, Gen AI Builder supports two types of Extraction Engines: the CSV Extraction Engine and the JSON Extraction Engine.

CSV#

The CSV Extraction Engine extracts comma separated values from unstructured text.

from griptape.drivers.prompt.openai import OpenAiChatPromptDriver
from griptape.engines import CsvExtractionEngine

#  Initialize the CsvExtractionEngine instance

csv_engine = CsvExtractionEngine(
    prompt_driver=OpenAiChatPromptDriver(model="gpt-3.5-turbo"), column_names=["name", "age", "location"]
)

#  Define some unstructured data

sample_text = """
Alice, 28, lives in New
York.
Bob, 35 lives in California
Charlie is 40
Collin is 28 and lives in San Francisco
"""

#  Extract CSV rows using the engine

result = csv_engine.extract_text(sample_text)

for row in result:
    print(row.to_text())
name,age,location
Alice,28,New York
Bob,35,California
Charlie,40,
Collin,28,San Francisco

JSON#

The JSON Extraction Engine extracts JSON-formatted values from unstructured text.

import json

from schema import Literal, Schema

from griptape.artifacts import ListArtifact
from griptape.chunkers import TextChunker
from griptape.drivers.prompt.openai import OpenAiChatPromptDriver
from griptape.engines import JsonExtractionEngine
from griptape.loaders import WebLoader

#  Define a schema for extraction

json_engine = JsonExtractionEngine(
    prompt_driver=OpenAiChatPromptDriver(model="gpt-4.1"),
    template_schema=Schema(
        {
            Literal("model", description="Name of an LLM model."): str,
            Literal("notes", description="Any notes of substance about the model."): Schema([str]),
        }
    ).json_schema("ProductSchema"),
)

#  Load data from the web

web_data = WebLoader().load("https://en.wikipedia.org/wiki/Large_language_model")
chunks = TextChunker().chunk(web_data)

#  Extract data using the engine

result = json_engine.extract_artifacts(ListArtifact(chunks))

for artifact in result:
    print(json.dumps(artifact.value, indent=2))
{
  "model": "GPT-3.5",
  "notes": [
    "Part of OpenAIs GPT series.",
    "Used in ChatGPT and Microsoft Copilot."
  ]
}
{
  "model": "GPT-4",
  "notes": [
    "Part of OpenAIs GPT series.",
    "Praised for increased accuracy and multimodal capabilities.",
    "Architecture and number of parameters not revealed."
  ]
}
...Output truncated for brevity...