Skip to content

Latest commit

 

History

History
164 lines (116 loc) · 5.75 KB

README.md

File metadata and controls

164 lines (116 loc) · 5.75 KB

Go SDK for Inferable

Go Reference License: MIT Documentation Go Report Card

Inferable Go Client is a Go package that provides a client for interacting with the Inferable API. It allows you to register your go functions against the Inferable control plane.

Installation

To install the Inferable Go Client, use the following command:

go get github.com/inferablehq/inferable/sdk-go

Quick Start

Initializing Inferable

To create a new Inferable client, use the New function:

import "github.com/inferablehq/inferable/sdk-go/inferable"

client, err := inferable.New("your-api-secret", "https://api.inferable.ai")

if err != nil {
    // Handle error
}

If you don't provide an API key or base URL, it will attempt to read them from the following environment variables:

  • INFERABLE_API_SECRET
  • INFERABLE_API_ENDPOINT

Registering a Function

Register a "SayHello" function with the control-plane.

type MyInput struct {
    Message string `json:"message"`
}

sayHello, err := client.Default.RegisterFunc(inferable.Function{
    Func:        myFunc,
    Name:        "SayHello",
    Description: "A simple greeting function",
})

if err != nil {
    // Handle error
}
👉 The Golang SDK for Inferable reflects the types from the input struct of the function.

Unlike the NodeJs SDK, the Golang SDK for Inferable reflects the types from the input struct of the function. It uses the invopop/jsonschema library under the hood to generate JSON schemas from Go types through reflection.

If the input struct defines jsonschema properties using struct tags, the SDK will use those in the generated schema. This allows for fine-grained control over the schema generation.

Here's an example to illustrate this:

import (
    "github.com/inferablehq/inferable/sdk-go/inferable"
    "time"
)

type UserInput struct {
    ID        int       `json:"id" jsonschema:"required"`
    Name      string    `json:"name" jsonschema:"minLength=2,maxLength=50"`
    Email     string    `json:"email" jsonschema:"format=email"`
    BirthDate time.Time `json:"birth_date" jsonschema:"format=date"`
    Tags      []string  `json:"tags" jsonschema:"uniqueItems=true"`
}

func createUser(input UserInput) string {
    // Function implementation
}

service, _ := client.RegisterService("UserService")

err := service.RegisterFunc(inferable.Function{
    Func:        createUser,
    Name:        "CreateUser",
    Description: "Creates a new user",
})

if err != nil {
    // Handle error
}

In this example, the UserInput struct uses jsonschema tags to define additional properties for the schema:

  • The id field is marked as required.
  • The name field has minimum and maximum length constraints.
  • The email field is specified to be in email format.
  • The birth_date field is set to date format.
  • The tags field is defined as an array with unique items.

When this function is registered, the Inferable Go SDK will use these jsonschema tags to generate a more detailed and constrained JSON schema for the input.

The invopop/jsonschema library provides many more options for schema customization, including support for enums, pattern validation, numeric ranges, and more.

Triggering a run

The following code will create an Inferable run with the prompt "Say hello to John" and the sayHello function attached.

You can inspect the progress of the run:

  run, err := client.CreateRun(inferable.CreateRunInput{
    InitialPrompt: "Say hello to John Smith",
    // Optional: Explicitly attach the functions (All functions attached by default)
    AttachedFunctions: []*inferable.FunctionReference{
      inferable.FunctionReference{
        Function: "SayHello",
        Service: "default",
      }
    },
    // Optional: Subscribe an Inferable function to receive notifications when the run status changes
    //OnStatusChange: &inferable.OnStatusChange{
    //  Function: OnStatusChangeFunction
    //}
  })

  fmt.Println("Run started: ", run.ID)
  result, err := run.Poll(nil)
  if err != nil {
    panic(err)
  }
  fmt.Println("Run Result: ", result)

Runs can also be triggered via the API, CLI or playground UI.

Documentation

Support

For support or questions, please create an issue in the repository or join the Discord

Contributing

Contributions to the Inferable Go Client are welcome. Please ensure that your code adheres to the existing style and includes appropriate tests.