Skip to content

From Go

You’ll end with a Go program that asks one yes-or-no question about each of three passages and sorts them by the probability of yes. You need Go 1.27 or later and a key for one provider.

  1. Terminal window
    mkdir rank && cd rank
    go mod init example.com/rank
    go get github.com/deepnoodle-ai/decide
  2. Save this as main.go. It asks whether each passage answers the query, then reranks the passages with the rank pattern:

    main.go
    // Command rank reranks a retrieved shortlist by asking, for each passage,
    // whether it answers the query, then sorting by the answers.
    package main
    import (
    "context"
    "fmt"
    "log"
    "github.com/deepnoodle-ai/decide"
    "github.com/deepnoodle-ai/decide/patterns/rank"
    )
    const query = "How do I recover an expired session?"
    var shortlist = []string{
    "A general account of account access.",
    "The session recovery procedure: sign in again and resume from the saved draft.",
    "Refresh tokens rotate when a session is refreshed.",
    }
    func main() {
    client, err := decide.NewClient()
    if err != nil {
    log.Fatal(err)
    }
    var observations []rank.Observation
    for _, passage := range shortlist {
    req := decide.NewRequest(map[string]string{"query": query, "passage": passage})
    decide.Ask(req, "answers", decide.Noul("Does this passage directly answer the query?"))
    resp, err := client.SystemOne(context.Background(), req)
    if err != nil {
    log.Fatal(err)
    }
    o, err := rank.FromResponse(resp, "answers")
    if err != nil {
    log.Fatal(err)
    }
    observations = append(observations, o)
    }
    // Ties break on the passage text so the order is deterministic.
    order, err := rank.Rerank(shortlist, observations,
    func(_ int, passage string) string { return passage })
    if err != nil {
    log.Fatal(err)
    }
    for _, e := range order.Entries {
    fmt.Printf("%.2f %s\n", e.Value, e.Item)
    }
    }

    decide.NewClient reads TYPESAFE_API_KEY. For Clef or GPT-6 Luna, create the client with backend.NewClient, as the package documentation shows. The rest of the program stays the same.

  3. Terminal window
    go run .

    The passage that answers the query comes first, with the highest probability of yes:

    0.93 The session recovery procedure: sign in again and resume from the saved draft.
    0.10 Refresh tokens rotate when a session is refreshed.
    0.05 A general account of account access.

    Your probabilities may differ a little.

    • Ask several questions in one request with decide.Ask, and ask a built-in template’s questions with the templates package.
    • Gate an agent’s action, screen in stages, or calibrate a cutoff with the packages under patterns/. Each has a short program in examples/.
    • Test without a key using fakes from decidetest.
decide is open source under Apache 2.0.Made by Deep Noodle