SDKsGo SDK

Go SDK

A hand-written client with no third-party dependencies. It requires Go 1.21 or later.

go get github.com/lucasmailland/mnemosyne/packages/sdk-go

The module lives in a subdirectory of the main repository and has no tagged release, so go get resolves a pseudo-version of the latest commit on main.

Construct and use

package main
 
import (
    "context"
    "fmt"
    "log"
 
    "github.com/lucasmailland/mnemosyne/packages/sdk-go/mnemo"
)
 
func main() {
    client, err := mnemo.NewClient("http://localhost:3939", "mns_live_...")
    if err != nil {
        log.Fatal(err)
    }
    ctx := context.Background()
 
    // Remember
    fact, err := client.CreateFact(ctx, mnemo.FactInput{
        Content: "Acme prefers communication in Spanish.",
        Tags:    []string{"client:acme"},
    })
    if err != nil {
        log.Fatal(err)
    }
    id := fact["id"].(string)
 
    // Recall
    result, err := client.Recall(ctx, mnemo.RecallInput{
        Query: "what language should we use with Acme?",
        TopK:  5,
    })
    if err != nil {
        log.Fatal(err)
    }
    for _, hit := range result.Hits {
        fmt.Println(hit["score"], hit["content"])
    }
 
    // Pin
    if _, err := client.PinFact(ctx, id); err != nil {
        log.Fatal(err)
    }
 
    // Forget: a soft delete, the fact is closed and not erased
    if err := client.ForgetFact(ctx, id); err != nil {
        log.Fatal(err)
    }
}

NewClient(baseURL, apiKey string) (*Client, error) returns an error when either argument is empty. The default timeout is 30 seconds, and client.SetTimeout changes it.

One Client struct holds every method. There are no sub-clients. Every method takes a context.Context first, and most return (map[string]interface{}, error). Recall returns a *RecallResponse with Hits []map[string]interface{} and Debug.

RecallInput has Query, Vector, TopK, AsOf, VectorWeight and SessionID. FactInput has Content, Attribution, Tags and SessionID. CreateFact posts to POST /remember and does not expose kind, scope, scopeRef or topicKey.

Lists

List methods take url.Values and return one page. There are no iterators:

page, err := client.ListFacts(ctx, url.Values{"limit": {"100"}})
if err != nil {
    log.Fatal(err)
}
fmt.Println(page["items"], page["nextCursor"])

Pass the returned nextCursor as cursor to get the next page.

Errors

A non-2xx response is returned as *mnemo.APIError:

result, err := client.Recall(ctx, mnemo.RecallInput{Query: "firmware release cadence"})
if err != nil {
    var apiErr *mnemo.APIError
    if errors.As(err, &apiErr) {
        fmt.Printf("HTTP %d: %s (code=%s, requestId=%s)\n",
            apiErr.Status, apiErr.Message, apiErr.Code, apiErr.RequestID)
    }
    log.Fatal(err)
}
fmt.Println(len(result.Hits))

APIError has Status, Message, Code, RequestID, Details, Retryable, RetryAfterSeconds and OutcomeUnknown. Transport failures and timeouts are separate error types and are not *APIError.

Retries and idempotency

The client retries 429, 500, 502, 503 and 504 up to three times, with exponential backoff and jitter starting at 200 ms. It stops when the context is cancelled. Reads are replayed automatically. A write is replayed only when it carries an idempotency key:

key, err := mnemo.NewIdempotencyKey()
if err != nil {
    log.Fatal(err)
}
 
fact, err := client.CreateFact(ctx,
    mnemo.FactInput{Content: "Larkspur Robotics ships firmware every two weeks."},
    mnemo.WithIdempotencyKey(key),
)

WithIdempotencyKey is the only request option today. NewIdempotencyKey returns a version 4 UUID from crypto/rand.

Several workspaces

With a user token (uk_live_*), client.ForWorkspace(workspaceID) returns an independent client that sends X-Workspace-Id. A workspace key always works on its own workspace.