GuidesRecipesAPI ReferenceChangelogDiscussions
Log In

Golang (Alpha)

Modzy's Golang SDK provides a convenient wrapper around many of Modzy's most popular API routes. SDK functions include querying models, submitting inference jobs, and returning results directly to your IDE. Full documentation for this hot-off-the-press SDK is coming soon. In the meantime, check out the links below to get started quickly:

GitHub Repo: https://github.com/modzy/sdk-go
Go.dev Package Details: https://pkg.go.dev/github.com/modzy/sdk-go

Installation

To install the Golang SDK, you'll need to first install Go. With Go installed you can install the SDK right from your terminal using the following command:

$ go get github.com/modzy/sdk-go

Basic Usage

Retrieve your API key

You can find and download your API key in your user profile. Click on your name in the menu at the top left of the screen and then click Profile & API Keys. Clicking "Get key" will download a text file with both the public and private portions of your API key. Store this file somewhere safe as you can only download it once.

Choose a Model

Now let's submit our first job. Choose a model and version from the available algorithms in your Modzy Library. Click on the API tab to retrieve the model identifier from the model's bio page.

2116

Compose main.go

In this example, we'll use version 1.0.1 of the Sentiment Analysis model with identifier ed542963de. In the example below, you will need to replace placeholder values with valid inputs:

  • Replace BASE_URL with the URL of your instance of Modzy, such as https://trial.app.modzy.com
  • Replace API_KEY with a valid API key string

A good practice is to set these as environment variables then references them when needed with os.Getenv("BASEURL") and os.Getenv("APIKEY").

🚧

No Error Handling

For the sake of clarity, error handling has been omitted in the example below. This is not recommended for more than some light experimentation.

package main

import (
	"context"
	"log"
	"time"

	modzy "github.com/modzy/sdk-go"
)

func main() {
	ctx := context.TODO()
	// Replace BASE_URL and API_KEY with valid values
  	baseURL := BASE_URL
	apiKey := API_KEY

  // Initialize the API client
	client := modzy.NewClient(baseURL).WithAPIKey(apiKey)

  // Create a map with a single text input
	mapSource := map[string]modzy.TextInputItem{
		"my-input": {
			"input.txt": "The Modzy API is very easy to use and the documentation is delightful",
		},
	}

  // Send your input to the sentiment analysis model by specifying ID and version number
	submitResponse, _ := client.Jobs().SubmitJobText(ctx, &modzy.SubmitJobTextInput{
		ModelIdentifier: "ed542963de",
		ModelVersion:    "1.0.1",
		Inputs:          mapSource,
	})

  // Check on the status of your request to analyze the sentiment of your input once a second
	jobDetails, _ := submitResponse.WaitForCompletion(ctx, time.Second)

  //When the input is done processing, retrive the results from Modzy, and log them to your terminal
	if jobDetails.Details.Status == modzy.JobStatusCompleted {
		results, _ := submitResponse.GetResults(ctx)
		for key := range mapSource {
			if result, exists := results.Results.Results[key]; exists {
				log.Printf("	%s:\n", key)
				modelRes := result.Data["results.json"].(map[string]interface{})
				for key2, val2 := range modelRes {
					log.Printf("	%s:%f\n", key2, val2)
				}
			}
		}
	}
}

Modzy runs inferences asynchronously, so this example uses the submitResonse.WaitForCompletion type to block the rest of the code from running until the job is complete. The average latency for the Sentiment Analysis model is 491ms, so the resulting analysis will be available nearly immediately, but some models can run much longer so this is a helpful way to make sure the model you've selected has returned a response before doing something else with it.

Run main.go

Once you've created main.go with your code, just navigate to the directory where your code is located from your terminal and run $ go run main.go. Below is an example of what your output should look like.

$ go run main.go
2021/12/27 12:37:11 	my-input:
2021/12/27 12:37:11 	data:map[%!f(string=drift):<nil> %!f(string=explanation):<nil> %!f(string=result):map[%!f(string=classPredictions):[map[%!f(string=class):%!f(string=neutral) %!f(string=score):0.611000] map[%!f(string=class):%!f(string=positive) %!f(string=score):0.389000] map[%!f(string=class):%!f(string=negative) %!f(string=score):0.000000]]]]

And that is it: you have run an inference using Modzy's Golang SDK! Next you might try submitting a file for analysis or running a batch job with many inputs.

Functions & Types

Currently the Golang SDK support the following API Routes

Function or TypeDescription
client.Models().ListModelsRetrieve all the models
Retrieve some models
client.Models().GetModelDetails()Retrieve model details by identifier
client.Models().GetModelDetailsByName()Retrieve model details by name
client.Models().GetRelatedVersions()Retrieve the list of related models
client.Models().GetModelVersions()Retrieve the versions of a model by identifier
client.Models().GetModelVersionDetails()Retrieve the version details
Retrieve the raw JSON input sample of a model and version
Retrieve the output sample of a model and version
client.Jobs.GetJobsHistory()Get the job history
client.Jobs().SubmitJobText()Submit a job using text as inputs
client.Jobs().SubmitJobEmbedded()Submit a job using files as inputs
Submit a job using bytes or byte arrays as inputs
client.Jobs().SubmitJobS3()Submit a job using aws s3 files as inputs
client.Jobs().SubmitJobJDBC()Submits a job using a SQL query with JDBC access params
client.Jobs().GetJobDetails()Retrieve the job information from Modzy
client.Jobs().CancelJobSend a request to cancel the job processing
client.Jobs().GetJobResultsRetrieve the results of a job