Skip to content

Deeptrain-Community/chatnio-api-go

Repository files navigation

ChatNio Go Library

The official Go library for the Chat Nio API

Features

  • Chat
  • Conversation
  • Quota
  • Subscription and Package

Installation

go get -u github.com/Deeptrain-Community/chatnio-api-go

Usage

  • Authentication
instance := chatnio.NewInstance("sk-...")
// or load from env
instance := chatnio.NewInstanceFromEnv("CHATNIO_TOKEN")

// set custom api endpoint (default: https://api.chatnio.net)
// instance.SetEndpoint("https://example.com/api")
  • Chat
chat, err := instance.NewChat(-1) // id -1: creating new chat
if err != nil {
    panic(err)
}

// using hook
chat.AskStream(&chatnio.ChatRequestForm{
	Message: "hello",
	Model:   "gpt-3.5-turbo",
}, func(resp chatnio.ChatPartialResponse) {
	fmt.Println(resp.End, resp.Quota, resp.Message, resp.Keyword)
})

// or using channel
channel := make(chan chatnio.ChatPartialResponse)
chat.Ask(&chatnio.ChatRequestForm{
    Message: "hello",
    Model:   "gpt-3.5-turbo",
}, channel)
for resp := range channel {
    // do something
}
  • Conversation
// list conversations
if list, err := instance.GetConversationList(); err == nil {
    for _, conv := range list {
        fmt.Println(conv.Id, conv.Name)
    }
}

// get conversation
if conv, err := instance.GetConversation(1); err == nil {
    fmt.Println(conv.Id, conv.Name, conv.Message)
}

// delete conversation
if err := instance.DeleteConversation(1); err == nil {
    fmt.Println("deleted")
}
  • Quota
// get quota
if quota, err := instance.GetQuota(); err == nil {
    fmt.Println(fmt.Sprintf("current quota is %0.2f", quota))
}

// buy quota
if err := instance.BuyQuota(100); err == nil {
    fmt.Println("bought successfully")
}
  • Subscription and Package
// get subscription
if sub, err := instance.GetSubscription(); err == nil {
    fmt.Println(sub.IsSubscribed, sub.Expired)
}

// buy subscription
if err := instance.Subscribe(1, 1); err == nil {
    fmt.Println("bought basic subscription for 1 month successfully")
}

// get package
if pkg, err := instance.GetPackage(); err == nil {
    fmt.Println(pkg.Data.Cert, pkg.Data.Teenager)
}

Test

To run the tests, you need to set the environment variable CHATNIO_TOKEN to your secret key.

export CHATNIO_TOKEN=sk-...

Then run the tests:

go test -v ./...