← Library AI AI-Powered

AIScoreOp

Scores text against a criterion on a 0–1 scale using Claude.

Inputs

Field Type Description
Input *string Text to score

Outputs

Field Type Description
Result float64 Score in [0.0, 1.0]

Reasoning is captured by WithReasoningLog — no graph output needed.

Params

Key Type Default Description
criterion string required What to measure, e.g. "urgency", "clarity", "relevance to billing"
max_retries string "3" Number of retries on parse error

Typical Use Cases

  • Urgency scoring for support ticket prioritization
  • Sentiment scoring on a continuous scale
  • Quality assessment of generated content
  • Confidence estimation for downstream branching

Pairs with threshold ops. The float64 Result feeds naturally into IfFloatGeOp or BetweenFloatOp for score-based routing. For example, route tickets with urgency > 0.8 to an on-call queue.

Score is always in [0, 1]. The model is instructed to return a decimal in this range. If the response cannot be parsed as a float, the op retries up to max_retries times.

Complete Runnable Example

Scores a product review for customer satisfaction and positive sentiment on a 0–1 scale.

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() {
	review := "This blender is okay. It works but is a bit loud. The lid doesn't fit perfectly."

	library.RegisterConst("review_val", review)

	g, err := graph.NewBuilder("score_demo").
		Vertex("src").Op("review_val").
		Output("Result", "review_wire").

		Vertex("score").Op("AIScoreOp").
		Params(map[string]string{
			"criterion": "customer satisfaction and positive sentiment",
		}).
		Input("Input", "review_wire").
		Output("Result", "sentiment_score").

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

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