Official golang client package for on-demand API.
This is the source code for the Go client library for OnDemand API.
The versioning scheme of this library is inspired by SemVer and the format is v{MAJOR}.{MINOR}.{PATCH}. For example, v3.0.0 and v2.5.1 are valid library versions.
- Installation.
- How to use the library ?
- Using the chat API.
- Streaming response from chat API.
- Error Handling.
Install the latest version of the library with the following commands:
go get github.com/dinson/ond-api-client-go
Some examples on how to use the client library are listed below:
package main
import (
"github.com/dinson/ond-api-client-go/ond"
)
func main() {
ondAPIClient := ond.Init("your-ondemand-api-key-here")
}
Chat API supports sync, webhook and streaming response modes. You can choose between sync and webhook modes by passing the appropriate values in the params.QueryParams.ResponseMode
field when calling the chat.Query()
method.
Sync mode
- Return the query response synchronously.
- Use
params.ResponseModeSync
Webhook mode
- Return the query response to your configured webhook.
- Use
params.ResponseModeWebhook
- Configure webhook via the OnDemand dashboard - https://app.on-demand.io/settings/webhooks
package main
import (
"context"
"fmt"
"github.com/dinson/ond-api-client-go/ond"
"github.com/dinson/ond-api-client-go/ond/params"
)
func main() {
ctx := context.Background()
ondAPIClient := ond.Init("your-ondemand-api-key-here")
// create chat session
session, _ := ondAPIClient.Chat.CreateSession(ctx, ¶ms.CreateChatSessionParams{...})
// call query endpoint to receive answer to a question
q, _ := ondAPIClient.Chat.Query(ctx, ¶ms.QueryParams{
SessionID: session.Data.ID,
EndpointID: "predefined-gemini-1.5-pro", // refer https://docs.on-demand.io/docs/fulfillment-models#predefined-models
Query: "How are you today",
ResponseMode: params.ResponseModeSync, // alternatively, use params.ResponseModeWebhook
})
fmt.Println(q.Data.Answer) // answer to your query from the LLM model
}
- You can also consume chat API response in streaming mode via SSE events.
- Use
chat.OpenStream()
to create an SSE connection and then useConsume()
method to start consuming the streaming response. - Refer to the example shown below on how to implement streaming response mode.
package main
import (
"context"
"fmt"
"github.com/dinson/ond-api-client-go/ond"
"github.com/dinson/ond-api-client-go/ond/params"
)
func main() {
ctx := context.Background()
ondAPIClient := ond.Init("your-ondemand-api-key-here")
// create chat session
session, _ := ondAPIClient.Chat.CreateSession(ctx, ¶ms.CreateChatSessionParams{...})
// open connection for SSE stream
stream, _ := ondAPIClient.Chat.OpenStream(ctx, ¶ms.QueryParams{...})
go stream.Consume() // start the `Consume()` in a go routine to prevent blocking
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// responses are made available through the `stream.EventChan` channel
for e := range stream.EventChan {
if e.Done {
// do operations to be conducted after consuming the whole response
break
}
if e.Error != nil {
// handle error
break
}
fmt.Print(e.Data.Answer)
}
}()
wg.Wait()
}
OnDemand API return error status, error code and an optional message when encountering errors.
Every error returned from the API client library will be an object with the following fields:
type ErrResponse struct {
Message string
ErrorCode string
Status int
}
You can use the error object just like you would use the error
package in go.
Example:
func main() {
...
_, err := ondAPIClient.Chat.Query(...)
if err != nil {
fmt.Println(err.Error())
}
}
The err.Error()
method will return a native error package err
.
For detailed documentation on error handling, please visit: https://docs.on-demand.io/reference/errors