Modus automatically creates an external API based on the endpoints defined in
your app manifest. Modus generates the API
signature based on the functions you export from your app.
Exporting functions
Modus uses the default conventions for each language.
Functions written in Go use starting capital letters to expose functions as public. Modus
creates an external API for public functions from any file that belongs to the main
package.
The functions below generate an API endpoint with the signature
type Query {
classifyText(text: String!, threshold: Float!): String!
}
Since the classify
function isn’t capitalized, Modus doesn’t include it in the
generated GraphQL API.
package main
import (
"errors"
"fmt"
"github.com/hypermodeAI/functions-go/pkg/models"
"github.com/hypermodeAI/functions-go/pkg/models/experimental"
)
const modelName = "my-classifier"
func ClassifyText(text string, threshold float32) (string, error) {
predictions, err:= classify(text)
if err != nil {
return "", err
}
prediction := predictions[0]
if prediction.Confidence < threshold {
return "", nil
}
return prediction.Label, nil
}
func classify(texts ...string) ([]experimental.ClassifierResult, error) {
model, err := models.GetModel[experimental.ClassificationModel](modelName)
if err != nil {
return nil, err
}
input, err := model.CreateInput(texts...)
if err != nil {
return nil, err
}
output, err := model.Invoke(input)
if err != nil {
return nil, err
}
if len(output.Predictions) != len(texts) {
word := "prediction"
if len(texts) > 1 {
word += "s"
}
return nil, fmt.Errorf("expected %d %s, got %d", len(texts), word, len(output.Predictions))
}
return output.Predictions, nil
}