Skip to content

Commit

Permalink
fix: Update SDK paths in documentation and codebase (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeesha authored Oct 25, 2024
1 parent bde396a commit fc87915
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 100 deletions.
13 changes: 6 additions & 7 deletions sdk-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

# Go SDK for Inferable

[![Go Reference](https://pkg.go.dev/badge/github.com/inferablehq/inferable-go.svg)](https://pkg.go.dev/github.com/inferablehq/inferable-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/inferablehq/inferable/sdk-go.svg)](https://pkg.go.dev/github.com/inferablehq/inferable/sdk-go)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Documentation](https://img.shields.io/badge/docs-inferable.ai-brightgreen)](https://docs.inferable.ai/)
[![Go Report Card](https://goreportcard.com/badge/github.com/inferablehq/inferable-go)](https://goreportcard.com/report/github.com/inferablehq/inferable-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/inferablehq/inferable/sdk-go)](https://goreportcard.com/report/github.com/inferablehq/inferable/sdk-go)

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.

Expand All @@ -16,7 +16,7 @@ Inferable Go Client is a Go package that provides a client for interacting with
To install the Inferable Go Client, use the following command:

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

## Usage
Expand All @@ -26,7 +26,7 @@ go get github.com/inferablehq/inferable-go
To create a new Inferable client, use the `New` function:

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

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

Expand Down Expand Up @@ -69,7 +69,7 @@ Here's an example to illustrate this:

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

Expand Down Expand Up @@ -154,11 +154,10 @@ The following code will create an [Inferable run](https://docs.inferable.ai/page

> Runs can also be triggered via the [API](https://docs.inferable.ai/pages/invoking-a-run-api), [CLI](https://www.npmjs.com/package/@inferable/cli) or [playground UI](https://app.inferable.ai/).

## Contributing

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

## Support

For support or questions, please [create an issue in the repository](https://github.com/inferablehq/inferable-go/issues).
For support or questions, please [create an issue in the repository](https://github.com/inferablehq/inferable/sdk-go/issues).
2 changes: 1 addition & 1 deletion sdk-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/inferablehq/inferable-go
module github.com/inferablehq/inferable/sdk-go

go 1.22

Expand Down
156 changes: 76 additions & 80 deletions sdk-go/inferable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http"
"reflect"

"github.com/inferablehq/inferable-go/internal/client"
"github.com/inferablehq/inferable-go/internal/util"
"github.com/inferablehq/inferable/sdk-go/internal/client"
"github.com/inferablehq/inferable/sdk-go/internal/util"
)

// Version of the inferable package
Expand All @@ -28,51 +28,51 @@ type Inferable struct {
apiSecret string
functionRegistry functionRegistry
machineID string
clusterID string
clusterID string
Default *service
}

type InferableOptions struct {
APIEndpoint string
APISecret string
MachineID string
ClusterID string
ClusterID string
}

// Input struct passed to a Run's result handler
type RunResultHandlerInput struct {
Status string `json:"status"`
RunId string `json:"runId"`
Result interface{} `json:"result"`
Summary string `json:"summary"`
Metadata interface{} `json:"metadata"`
Status string `json:"status"`
RunId string `json:"runId"`
Result interface{} `json:"result"`
Summary string `json:"summary"`
Metadata interface{} `json:"metadata"`
}

type RunResult struct {
Handler *FunctionHandle
Schema interface{}
Handler *FunctionHandle
Schema interface{}
}

type RunTemplate struct {
ID string
Input map[string]interface{}
ID string
Input map[string]interface{}
}

type Run struct {
Functions []*FunctionHandle
Message string
Result *RunResult
Metadata map[string]string
Template *RunTemplate
Functions []*FunctionHandle
Message string
Result *RunResult
Metadata map[string]string
Template *RunTemplate
}

type runHandle struct {
ID string
ID string
}

type templateHandle struct {
ID string
Run func(input *Run) (*runHandle, error)
ID string
Run func(input *Run) (*runHandle, error)
}

func New(options InferableOptions) (*Inferable, error) {
Expand All @@ -89,15 +89,14 @@ func New(options InferableOptions) (*Inferable, error) {

machineID := options.MachineID
if machineID == "" {
machineID = util.GenerateMachineID(8)
machineID = util.GenerateMachineID(8)
}


inferable := &Inferable{
client: client,
apiEndpoint: options.APIEndpoint,
apiSecret: options.APISecret,
clusterID: options.ClusterID,
clusterID: options.ClusterID,
functionRegistry: functionRegistry{services: make(map[string]*service)},
machineID: machineID,
}
Expand Down Expand Up @@ -125,43 +124,40 @@ func (i *Inferable) RegisterService(serviceName string) (*service, error) {
}

func (i *Inferable) CreateRun(input *Run) (*runHandle, error) {
if i.clusterID == "" {
return nil, fmt.Errorf("cluster ID must be provided to manage runs")
}

var attachedFunctions []string
for _, fn := range input.Functions {
attachedFunctions = append(attachedFunctions, fmt.Sprintf("%s_%s", fn.Service, fn.Function))
}
if i.clusterID == "" {
return nil, fmt.Errorf("cluster ID must be provided to manage runs")
}

var attachedFunctions []string
for _, fn := range input.Functions {
attachedFunctions = append(attachedFunctions, fmt.Sprintf("%s_%s", fn.Service, fn.Function))
}

payload := client.CreateRunInput{
Message: input.Message,
AttachedFunctions: attachedFunctions,
Metadata: input.Metadata,
}

if (input.Template != nil) {
payload.Template = &client.CreateRunTemplateInput{
Input: input.Template.Input,
ID: input.Template.ID,
}
}

if (input.Result != nil) {
payload.Result = &client.CreateRunResultInput{
}
if (input.Result.Handler != nil) {
payload.Result.Handler = &client.CreateRunResultHandlerInput{
Service: input.Result.Handler.Service,
Function: input.Result.Handler.Function,
}
}
if (input.Result.Schema != nil) {
payload.Result.Schema = input.Result.Schema
}
}
Message: input.Message,
AttachedFunctions: attachedFunctions,
Metadata: input.Metadata,
}

if input.Template != nil {
payload.Template = &client.CreateRunTemplateInput{
Input: input.Template.Input,
ID: input.Template.ID,
}
}

if input.Result != nil {
payload.Result = &client.CreateRunResultInput{}
if input.Result.Handler != nil {
payload.Result.Handler = &client.CreateRunResultHandlerInput{
Service: input.Result.Handler.Service,
Function: input.Result.Handler.Function,
}
}
if input.Result.Schema != nil {
payload.Result.Schema = input.Result.Schema
}
}

// Marshal the payload to JSON
jsonPayload, err := json.Marshal(payload)
Expand Down Expand Up @@ -200,13 +196,13 @@ func (i *Inferable) CreateRun(input *Run) (*runHandle, error) {
return nil, fmt.Errorf("failed to parse run response: %v", err)
}

return &runHandle{ID: response.ID}, nil
return &runHandle{ID: response.ID}, nil
}

func (i *Inferable) GetTemplate(id string) (*templateHandle, error) {
if i.clusterID == "" {
return nil, fmt.Errorf("cluster ID must be provided to manage runs")
}
if i.clusterID == "" {
return nil, fmt.Errorf("cluster ID must be provided to manage runs")
}

// Prepare headers
headers := map[string]string{
Expand Down Expand Up @@ -238,26 +234,26 @@ func (i *Inferable) GetTemplate(id string) (*templateHandle, error) {
return nil, fmt.Errorf("failed to parse template response: %v", err)
}

return &templateHandle{
ID: response.ID,
Run: func(input *Run) (*runHandle, error) {
// CLone the input
inputCopy := *input

// Set the template ID
if inputCopy.Template == nil {
inputCopy.Template = &RunTemplate{
ID: response.ID,
}
} else {
inputCopy.Template.ID = response.ID
}
return &templateHandle{
ID: response.ID,
Run: func(input *Run) (*runHandle, error) {
// CLone the input
inputCopy := *input

// Set the template ID
if inputCopy.Template == nil {
inputCopy.Template = &RunTemplate{
ID: response.ID,
}
} else {
inputCopy.Template.ID = response.ID
}

fmt.Println(inputCopy)
fmt.Println(inputCopy)

return i.CreateRun(&inputCopy)
},
}, nil
return i.CreateRun(&inputCopy)
},
}, nil
}

func (i *Inferable) callFunc(serviceName, funcName string, args ...interface{}) ([]reflect.Value, error) {
Expand Down Expand Up @@ -323,7 +319,7 @@ func (i *Inferable) fetchData(options client.FetchDataOptions) ([]byte, http.Hea
options.Headers["Content-Type"] = "application/json"
}

data, headers, err, status:= i.client.FetchData(options)
data, headers, err, status := i.client.FetchData(options)
return []byte(data), headers, err, status
}

Expand Down
2 changes: 1 addition & 1 deletion sdk-go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package inferable
import (
"testing"

"github.com/inferablehq/inferable-go/internal/util"
"github.com/inferablehq/inferable/sdk-go/internal/util"
)

type EchoInput struct {
Expand Down
20 changes: 10 additions & 10 deletions sdk-go/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/invopop/jsonschema"

"github.com/inferablehq/inferable-go/internal/client"
"github.com/inferablehq/inferable/sdk-go/internal/client"
)

const (
Expand Down Expand Up @@ -55,14 +55,14 @@ type callResult struct {
}

type FunctionHandle struct {
Service string
Function string
Service string
Function string
}

func (s *service) RegisterFunc(fn Function) (*FunctionHandle, error) {
if s.isPolling() {
return nil, fmt.Errorf("functions must be registered before starting the service.")
}
if s.isPolling() {
return nil, fmt.Errorf("functions must be registered before starting the service.")
}

if _, exists := s.Functions[fn.Name]; exists {
return nil, fmt.Errorf("function with name '%s' already registered for service '%s'", fn.Name, s.Name)
Expand Down Expand Up @@ -249,9 +249,9 @@ func (s *service) poll() error {

result, respHeaders, err, status := s.inferable.fetchData(options)

if status == 410 {
s.registerMachine()
}
if status == 410 {
s.registerMachine()
}

if err != nil {
return fmt.Errorf("failed to poll calls: %v", err)
Expand Down Expand Up @@ -292,7 +292,7 @@ func (s *service) handleMessage(msg callMessage) error {
fn, ok := s.Functions[msg.Function]
if !ok {
log.Printf("Received call for unknown function: %s", msg.Function)
return nil
return nil
}

// Create a new instance of the function's input type
Expand Down
2 changes: 1 addition & 1 deletion sdk-go/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/inferablehq/inferable-go/internal/util"
"github.com/inferablehq/inferable/sdk-go/internal/util"
)

func TestRegisterFunc(t *testing.T) {
Expand Down

0 comments on commit fc87915

Please sign in to comment.