← Library AI AI-Powered

AIExtractStringSliceOp

Extracts a list of items from unstructured text and returns them as a string slice.

Inputs

Field Type Description
Input *string Unstructured text to extract items from

Outputs

Field Type Description
Result []string Extracted items as a slice of strings

Reasoning is captured by WithReasoningLog — no graph output needed.

Params

Key Type Default Description
operation string required Plain-English extraction instruction, e.g. "extract all ingredient names"
max_retries string "3" Number of retries on parse error

Typical Use Cases

  • Ingredient extraction from recipe text
  • Keyword or topic extraction from articles
  • Named entity listing from prose
  • Structured list extraction from freeform descriptions

Built on AIComputeOp. The operation param is the full instruction sent to the model. Be specific: "extract the first and last name of every person mentioned" outperforms "extract names".

Connects to slice ops. The []string result feeds naturally into SliceLenOp, SliceJoinOp, SliceFilterEqOp, and Map vertices for per-item processing.

Complete Runnable Example

Extracts all ingredient names from a recipe text into a string slice.

main.go
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"
	"time"

	"github.com/akennis/sparsi-go/library"
	_ "github.com/akennis/dagor/operator/builtin"

	"github.com/panjf2000/ants/v2"
	"github.com/akennis/dagor"
	"github.com/akennis/dagor/graph"
)

func main() {
	recipe := "Whisk together 2 eggs, 1 cup flour, 1/2 cup milk, and a pinch of salt. Add butter to the pan before cooking."

	library.RegisterConst("recipe_val", recipe)

	g, err := graph.NewBuilder("ingredient_extractor").
		Vertex("src").Op("recipe_val").
		Output("Result", "recipe_wire").

		Vertex("extract").Op("AIExtractStringSliceOp").
		Params(map[string]string{
			"operation": "extract all ingredient names from this recipe",
		}).
		Input("Input", "recipe_wire").
		Output("Result", "ingredient_list").

		Build()
	if err != nil {
		log.Fatalf("build: %v", err)
	}

	pool, err := ants.NewPool(4)
	if err != nil {
		log.Fatalf("pool: %v", err)
	}
	defer pool.Release()

	eng, err := dagor.NewEngine(g, pool)
	if err != nil {
		log.Fatalf("engine: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	if err := eng.Run(ctx); err != nil {
		log.Fatalf("run: %v", err)
	}

	out := map[string]any{}
	if raw, ok := eng.GetOutput("ingredient_list"); ok {
		out["ingredients"] = *raw.(*[]string)
	}

	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}