← Library AI AI-Powered

AIParseNumberOp

Converts natural-language or informal number text to a float64 — handles written numbers, currency, abbreviations, and embedded values.

Inputs

Field Type Description
Input *string Text containing a number, e.g. "two thousand", "$1.2k", "the price is 42"

Outputs

Field Type Description
Result float64 Parsed numeric value

Reasoning is captured by WithReasoningLog — no graph output needed.

Params

Key Type Default Description
operation string extract numeric value Optional custom instruction to override extraction behavior
max_retries string "3" Number of retries on parse error

Typical Use Cases

  • Parsing user-entered monetary amounts ("about fifty dollars")
  • Normalizing informal numbers from form inputs
  • Extracting a numeric value buried in a longer sentence
  • Converting written numbers to floats for math ops

Handles diverse formats. Written numbers ("two thousand"), currency ("$1.2k"), abbreviations ("3M"), and embedded numbers ("the discount is 15%") are all supported.

Complete Runnable Example

Parses a natural-language budget phrase into a float64 dollar amount.

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() {
	budgetText := "Our budget is roughly twelve thousand five hundred dollars."

	library.RegisterConst("budget_val", budgetText)

	g, err := graph.NewBuilder("parse_number_demo").
		Vertex("src").Op("budget_val").
		Output("Result", "text_wire").

		Vertex("parse").Op("AIParseNumberOp").
		Params(map[string]string{
			"operation": "extract the dollar amount as a number",
		}).
		Input("Input", "text_wire").
		Output("Result", "budget_float").

		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("budget_float"); ok {
		out["result"] = *raw.(*float64)
	}

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