chatnio

package module
v0.0.0-...-1e93ed8 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 4 Imported by: 0

README

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 ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chat

type Chat struct {
	Id    int
	Uri   string
	Token string
	Conn  *utils.WebSocket
}

func (*Chat) Ask

func (c *Chat) Ask(form *ChatRequestForm, channel chan ChatPartialResponse)

func (*Chat) AskStream

func (c *Chat) AskStream(form *ChatRequestForm, callback func(ChatPartialResponse))

func (*Chat) Close

func (c *Chat) Close() error

func (*Chat) DeferClose

func (c *Chat) DeferClose()

func (*Chat) Send

func (c *Chat) Send(v interface{}) bool

func (*Chat) SendAuthRequest

func (c *Chat) SendAuthRequest() bool

type ChatAuthForm

type ChatAuthForm struct {
	Id    int    `json:"id"`
	Token string `json:"token"`
}

type ChatPartialResponse

type ChatPartialResponse struct {
	Message string  `json:"message"`
	Keyword string  `json:"keyword"`
	Quota   float32 `json:"quota"`
	End     bool    `json:"end"`
}

type ChatRequestForm

type ChatRequestForm struct {
	Message string `json:"message"`
	Model   string `json:"model"`
	Web     bool   `json:"web"`
}

type Conversation

type Conversation struct {
	Id      int       `json:"id"`
	Name    string    `json:"name"`
	Message []Message `json:"message"`
}

type ConversationDelete

type ConversationDelete struct {
	Status  bool   `json:"status"`
	Message string `json:"message"`
}

type ConversationList

type ConversationList struct {
	Status  bool           `json:"status"`
	Message string         `json:"message"`
	Data    []Conversation `json:"data"`
}

type ConversationLoad

type ConversationLoad struct {
	Status  bool         `json:"status"`
	Message string       `json:"message"`
	Data    Conversation `json:"data"`
}

type Instance

type Instance struct {
	ApiKey   string
	Endpoint string
}

func NewInstance

func NewInstance(key string) *Instance

NewInstance creates a new instance of the chatnio client

func NewInstanceFromEnv

func NewInstanceFromEnv(env string) *Instance

NewInstanceFromEnv creates a new instance of the chatnio client from the environment

func (*Instance) BuyQuota

func (i *Instance) BuyQuota(quota int) error

func (*Instance) DeleteConversation

func (i *Instance) DeleteConversation(id int) error

func (*Instance) GetApiKey

func (i *Instance) GetApiKey() string

func (*Instance) GetChatApiKey

func (i *Instance) GetChatApiKey() string

func (*Instance) GetChatEndpoint

func (i *Instance) GetChatEndpoint() (host string)

func (*Instance) GetConversation

func (i *Instance) GetConversation(id int) (*Conversation, error)

func (*Instance) GetConversationList

func (i *Instance) GetConversationList() ([]Conversation, error)

func (*Instance) GetEndpoint

func (i *Instance) GetEndpoint() string

func (*Instance) GetHeaders

func (i *Instance) GetHeaders() utils.Headers

func (*Instance) GetPackage

func (i *Instance) GetPackage() (*Package, error)

func (*Instance) GetQuota

func (i *Instance) GetQuota() (float32, error)

func (*Instance) GetSubscription

func (i *Instance) GetSubscription() (*Subscription, error)

func (*Instance) IsAuthenticated

func (i *Instance) IsAuthenticated() bool

func (*Instance) Mix

func (i *Instance) Mix(path string) string

func (*Instance) NewChat

func (i *Instance) NewChat(id int) (*Chat, error)

func (*Instance) SetApiKey

func (i *Instance) SetApiKey(apiKey string)

func (*Instance) SetEndpoint

func (i *Instance) SetEndpoint(endpoint string)

func (*Instance) Subscribe

func (i *Instance) Subscribe(level int, month int) error

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type Package

type Package struct {
	Status bool `json:"status"`
	Data   struct {
		Cert     bool `json:"cert"`
		Teenager bool `json:"teenager"`
	} `json:"data"`
}

type Quota

type Quota struct {
	Status bool    `json:"status"`
	Quota  float32 `json:"quota"`
}

type QuotaBuy

type QuotaBuy struct {
	Status bool   `json:"status"`
	Error  string `json:"error"`
}

type Subscribe

type Subscribe struct {
	Status bool   `json:"status"`
	Error  string `json:"error"`
}

type Subscription

type Subscription struct {
	Status       bool  `json:"status"`
	IsSubscribed bool  `json:"is_subscribed"`
	Expired      int64 `json:"expired"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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