sense

package
v0.0.0-...-108afc2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ChatMessageRoleUser      = "user"
	ChatMessageRoleAssistant = "assistant"

	ModelNovaPtcXlV1 = "nova-ptc-xl-v1"
	ModelNovaPtcSV2  = "nova-ptc-s-v2"
	ModelNovaPtcXsV1 = "nova-ptc-xs-v1"
)

Variables

View Source
var (
	ErrChatCompletionInvalidModel       = errors.New("this model is not supported with this method, please use CreateCompletion client method instead") //nolint:lll
	ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream")              //nolint:lll
)

Functions

func GenerateAuthToken

func GenerateAuthToken(accessKey string, secretKey string, expiresIn time.Duration) (string, error)

GenerateAuthToken

Types

type APIError

type APIError struct {
	Err struct {
		Code    int    `json:"code,omitempty"`
		Message string `json:"message"`
		Details []any  `json:"details"`
	} `json:"error"`
}

APIError provides error information returned by the API.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) UnmarshalJSON

func (e *APIError) UnmarshalJSON(data []byte) (err error)

type AuthToken

type AuthToken struct {
	// contains filtered or unexported fields
}

type ChatCompletionMessage

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

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model             string                  `json:"model"`
	Messages          []ChatCompletionMessage `json:"messages"`
	N                 int                     `json:"n,omitempty"` // 生成回复数量,响应参数中的index即为回复序号(在使用某些模型时不支持传入该参数,详情请参考模型清单) https://platform.sensenova.cn/#/doc?path=/model.md
	MaxNewTokens      int                     `json:"max_new_tokens,omitempty"`
	RepetitionPenalty float32                 `json:"repetition_penalty,omitempty"`
	Temperature       float32                 `json:"temperature,omitempty"`
	TopP              float32                 `json:"top_p,omitempty"`
	Stream            bool                    `json:"stream,omitempty"`
	User              string                  `json:"user,omitempty"`
}

ChatCompletionRequest represents a request structure for chat completion API.

type ChatCompletionResponse

type ChatCompletionResponse struct {
	Data  ChatCompletionResponseData  `json:"data"`
	Error ChatCompletionResponseError `json:"error"`
}

ChatCompletionResponse represents a response structure for chat completion API.

type ChatCompletionResponseChoices

type ChatCompletionResponseChoices struct {
	Message      string `json:"message"`
	Delta        string `json:"delta"`
	FinishReason string `json:"finish_reason"`
	Index        int    `json:"index"`
	Role         string `json:"role"`
}

type ChatCompletionResponseData

type ChatCompletionResponseData struct {
	Id      string                          `json:"id"`
	Choices []ChatCompletionResponseChoices `json:"choices"`
	Usage   Usage                           `json:"usage"`

	ErrorCode int    `json:"code"`
	ErrorMsg  string `json:"msg"`
	Success   bool   `json:"success"`
}

type ChatCompletionResponseError

type ChatCompletionResponseError struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message"`
	Details []any  `json:"details"`
}

type ChatCompletionStream

type ChatCompletionStream struct {
	// contains filtered or unexported fields
}

ChatCompletionStream

func (ChatCompletionStream) Close

func (stream ChatCompletionStream) Close()

func (ChatCompletionStream) Recv

func (stream ChatCompletionStream) Recv() (response ChatCompletionResponse, err error)

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(token string) *Client

NewClient creates new Sense AI API client.

func NewClientWithConfig

func NewClientWithConfig(config ClientConfig) *Client

NewClientWithConfig creates new Sense Nova API client for specified config.

func (*Client) CreateChatCompletion

func (c *Client) CreateChatCompletion(
	ctx context.Context,
	request ChatCompletionRequest,
	args ...any,
) (response ChatCompletionResponse, err error)

CreateChatCompletion — API call to Create a completion for the chat message.

func (*Client) CreateChatCompletionStream

func (c *Client) CreateChatCompletionStream(
	ctx context.Context,
	request ChatCompletionRequest,
	args ...any,
) (stream *ChatCompletionStream, err error)

CreateChatCompletionStream — API call to create a chat completion w/ streaming support.

type ClientConfig

type ClientConfig struct {
	BaseURL    string
	HTTPClient *http.Client

	EmptyMessagesLimit uint
	// contains filtered or unexported fields
}

ClientConfig is a configuration of a client.

func DefaultConfig

func DefaultConfig(authToken string) ClientConfig

func (ClientConfig) String

func (ClientConfig) String() string

type ErrorResponse

type ErrorResponse struct {
	Error *APIError `json:"error,omitempty"`
}

type JwtClaims

type JwtClaims struct {
	Iss string
	Exp int64
	Nbf int64
}

type RequestError

type RequestError struct {
	HTTPStatusCode int
	Err            error
}

RequestError provides informations about generic request errors.

func (*RequestError) Error

func (e *RequestError) Error() string

func (*RequestError) Unwrap

func (e *RequestError) Unwrap() error

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Jump to

Keyboard shortcuts

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