runway

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2020 License: MIT Imports: 10 Imported by: 0

README

RunwayML Go

A small Go module for interfacing with RunwayML. It currently supports the new Hosted Models functionality, modeled after the official Hosted Models JavaScript SDK.

go get -u github.com/brannondorsey/go-runway

Examples

Several examples live in the examples/ directory.

Basic
package main

import (
	"fmt"
	"math/rand"
	"time"

	runway "github.com/brannondorsey/go-runway"
)

func main() {

	// Replace this with the URL of your hosted model (https://learn.runwayml.com/#/how-to/hosted-models)
	url := "https://example-text-generator.hosted-models.runwayml.cloud/v1"

	// Paste your secret token in here. Leave as empty string if the model is public.
	token := ""

	model, err := runway.NewHostedModel(url, token)
	if err != nil {
		panic(err)
	}

	rand.Seed(time.Now().UnixNano())
	input := runway.JSONObject{
		"prompt":         "Four score and seven years ago",
		"seed":           rand.Intn(1000),
		"max_characters": 512,
	}

	fmt.Println("Querying model...")
	output, err := model.Query(input)
	if err != nil {
		panic(err)
	}

	fmt.Println(output["generated_text"])
}

The hosted-models example

The hosted-models example is a standalone utility for interfacing with hosted models. You can download it from the releases page.

# Call the /v1/info endpoint of the hosted model
hosted-models --url https://my-example-model.hosted-models.runwayml.cloud --token XXXX info
# {
#     "name": "generate_batch",
#     "description": "Generate text conditioned on prompt",
#     "inputs": [
#         {
#             "default": "",
#             "description": null,
#             "minLength": 0,
#             "name": "prompt",
#             "type": "text"
#         },
#         ...
#     ],
#     "outputs": [
#         {
#             "default": "",
#             "description": null,
#             "minLength": 0,
#             "name": "generated_text",
#             "type": "text"
#         },
#         ...
#     ]
# }
hosted-models --url https://my-example-model.hosted-models.runwayml.cloud --token XXXX \
  query '{"prompt": "Four score and seven years ago"}'
# {
#     "encountered_end": false,
#     "generated_text": "Four score and seven years ago the sorcerer Anor stood before the king, as a new wizard of the"
# }

Docs

The Go docs for this package live here on go.dev.

See the Usage section of the Hosted Models JavaScript SDK for general information about the methods available for the HostedModel object, as they are identical to the ones provided by this package.

Dev

Running make will build all examples/ and place their executables in build/bin.

git clone https://github.com/brannondorsey/go-runway
cd runway

make

Documentation

Overview

runway is a Go library for interfacing with RunwayML Hosted Models.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPermissionDenied = errors.New("Permission denied, this model is private. Did you include the correct token?")
	ErrNotFound         = errors.New("Model not found. Make sure the url is correct and that the model is \"active\".")
	ErrInvalidURL       = errors.New("The hosted model url you've provided is invalid. It must be in a format like: https://my-model.hosted-models.runwayml.cloud/v1.")
	ErrModelError       = errors.New("The model experienced an error while processing your input. Double-check that you are sending properly formed input parameters in HostedModel.Query(). You can use the HostedModel.Info() method to check the input parameters the model expects. If the error persists, contact support (https://support.runwayml.com).")
)

Functions

This section is empty.

Types

type ErrInvalidArgument

type ErrInvalidArgument struct {
	ArgumentName string
	Details      string
	Err          error
}

func (*ErrInvalidArgument) Error added in v0.2.0

func (err *ErrInvalidArgument) Error() string

type ErrNetworkError

type ErrNetworkError struct {
	Err error
}

func (*ErrNetworkError) Error added in v0.2.0

func (err *ErrNetworkError) Error() string

func (*ErrNetworkError) Unwrap added in v0.2.0

func (err *ErrNetworkError) Unwrap() error

type ErrUnexpectedError

type ErrUnexpectedError struct {
	Err error
}

func (*ErrUnexpectedError) Error added in v0.2.0

func (err *ErrUnexpectedError) Error() string

func (*ErrUnexpectedError) Unwrap added in v0.2.0

func (err *ErrUnexpectedError) Unwrap() error

type HostedModel

type HostedModel struct {
	// contains filtered or unexported fields
}

HostedModel represents a RunwayML Hosted Model. See learn.runwayml.com/#/how-to/hosted-models for more details.

func NewHostedModel

func NewHostedModel(url, token string) (*HostedModel, error)

NewHostedModel instantiates a HostedModel object and returns a pointer to it. "url" is the full URL of your hosted model in the format "https://my-model.hosted-models.runwayml.cloud/v1". "token" is the secret token associated with this model, if the model is private. Use an empty string "" if the model has no token.

func (*HostedModel) GetURL added in v0.2.0

func (model *HostedModel) GetURL() string

GetURL returns the hosted model's url with trailing slashes removed if they were present during creation

func (*HostedModel) Info

func (model *HostedModel) Info() (JSONObject, error)

Info returns a JSONObject containing the input/output spec provided by the model. It makes a GET request to the /v1/info route of a hosted model under the hood.

func (*HostedModel) IsAwake

func (model *HostedModel) IsAwake() (bool, error)

IsAwake returns true if this model is awake, and false if it is still waking up. See the Awake, Awakening, and Awake in the Hosted Models docs for more info: https://learn.runwayml.com/#/how-to/hosted-models?id=asleep-awakening-and-awake-states.

func (*HostedModel) Query

func (model *HostedModel) Query(input JSONObject) (JSONObject, error)

Query runs the model on your input and produce an output. This is how you "run" the model. "input" is an object containing input parameters to be sent to the model. Use the HostedModel.Info() method to get the correct format for this JSONobject, as each model expects different inputs.

func (*HostedModel) WaitUntilAwake

func (model *HostedModel) WaitUntilAwake(pollIntervalMillis int) error

WaitUntilAwake returns once the model is awake. This method is never required, as HostedModel.Info() and HostedModel.Query() will always return results eventually, but it can be useful for managing UI if you want to postpone making Info() and Query() requests until you know that they will resolve more quickly. pollIntervalMillis controls the frequency this method will make HTTP requests to the underlying Hosted Model to check if it is awake yet.

type JSONObject

type JSONObject map[string]interface{}

JSONObject is used to represent JSON structures which are variable or unknown at compile time.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL