← Library AI AI-Powered

AIComputeMathOperandsToFloat64Op

AI-powered fallback for binary float64 math — applies a natural-language math operation to two float64 operands.

Inputs

Field Type Description
Input *MathOperands Struct with fields A float64 and B float64 — use PackMathOperandsOp to build it

Outputs

Field Type Description
Result float64 Computed result

Reasoning is captured by WithReasoningLog — no graph output needed.

Params

Key Type Default Description
operation string required Math instruction referring to A and B, e.g. "compute A to the power of B"
max_retries string "3" Number of retries on parse error

Typical Use Cases

  • Non-standard arithmetic: power, log, modulo
  • AI fallback in a det-first coalesce pattern
  • Domain-specific math that lacks a deterministic op

Use PackMathOperandsOp first. The input type is MathOperands, not two separate floats. Wire PackMathOperandsOp.Result into AIComputeMathOperandsToFloat64Op.Input.

AI fallback pattern. Typically the AI branch in a coalesce where the deterministic branch (e.g. DivOp) failed. Use the op name and behavior to guide DefaultFloat64Op coalescing.

Complete Runnable Example

Uses AI to compute 2 raised to the power of 10 by packing operands with PackMathOperandsOp then applying a natural-language math instruction.

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() {
	base := 2.0
	exponent := 10.0

	library.RegisterConst("base_val", base)
	library.RegisterConst("exponent_val", exponent)

	g, err := graph.NewBuilder("power_demo").
		Vertex("base_src").Op("base_val").
		Output("Result", "base_wire").

		Vertex("exp_src").Op("exponent_val").
		Output("Result", "exponent_wire").

		Vertex("pack").Op("PackMathOperandsOp").
		Input("A", "base_wire").
		Input("B", "exponent_wire").
		Output("Result", "operands_wire").

		Vertex("power").Op("AIComputeMathOperandsToFloat64Op").
		Params(map[string]string{
			"operation": "compute A raised to the power of B",
		}).
		Input("Input", "operands_wire").
		Output("Result", "power_result").

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

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