moonshot

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 12 Imported by: 1

README

Go Moonshot

Go Report Card Go Version tag Go Reference codecov FOSSA Status License

简体中文 | English

A Go SDK for Kimi which created by MoonshotAI.

🚀 Installation

go get github.com/northes/go-moonshot@v0.4.1

You can find the docs at go docs.

🤘 Feature

  • Easy to use and simple API, chain operation.
  • Full API support.
  • Predefined enumeration.

📄 Supported API

API Done
Chat Completion
Chat Completion(stream)
List Models
List Files
Upload File
Delete File
Get File Info
Get File Contents
Estimate Token Count
User Balance

🥪 Usage

Initialize client

  1. Get a MoonshotAI API Key: https://platform.moonshot.cn.
  2. Set up key using a configuration file or environment variable.

⚠ Note: Your API key is sensitive information. Do not share it with anyone.

With Only Key
key, ok := os.LookupEnv("MOONSHOT_KEY")
if !ok {
    return errors.New("missing environment variable: moonshot_key")
}

cli, err := moonshot.NewClient(key)
if err != nil {
    return err
}
With Config
key, ok := os.LookupEnv("MOONSHOT_KEY")
if !ok {
    return errors.New("missing environment variable: moonshot_key")
}

cli, err := moonshot.NewClientWithConfig(
    moonshot.NewConfig(
        moonshot.WithAPIKey("xxxx"),
    ),
)

API

List Models
resp, err := cli.Models().List(context.Background())
if err != nil {
    return err
}
Chat Completions
// Use builder to build a request more conveniently
builder := moonshot.NewChatCompletionsBuilder()
builder.AppendPrompt("你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。").
	AppendUser("你好,我叫李雷,1+1等于多少?").
	WithTemperature(0.3)

resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
if err != nil {
    return err
}
// {"id":"cmpl-eb8e8474fbae4e42bea9f6bbf38d56ed","object":"chat.completion","created":2647921,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"role":"assistant","content":"你好,李雷!1+1等于2。这是一个基本的数学加法运算。如果你有任何其他问题或需要帮助,请随时告诉我。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":87,"completion_tokens":31,"total_tokens":118}}

// do something...

// append context
for _, choice := range resp.Choices {
    builder.AppendMessage(choice.Message)
}

builder.AppendUser("在这个基础上再加3等于多少")

resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
if err != nil {
    return err
}
// {"id":"cmpl-a7b938eaddc04fbf85fe578a980040ac","object":"chat.completion","created":5455796,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"role":"assistant","content":"在这个基础上,即1+1=2的结果上再加3,等于5。所以,2+3=5。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":131,"completion_tokens":26,"total_tokens":157}}
Chat completions with stream
// use struct
resp, err := cli.Chat().CompletionsStream(context.Background(), &moonshot.ChatCompletionsRequest{
    Model: moonshot.ModelMoonshotV18K,
    Messages: []*moonshot.ChatCompletionsMessage{
        {
            Role:    moonshot.RoleUser,
            Content: "你好,我叫李雷,1+1等于多少?",
        },
    },
    Temperature: 0.3,
    Stream:      true,
})
if err != nil {
    return err
}

for receive := range resp.Receive() {
    msg, err := receive.GetMessage()
    if err != nil {
        if errors.Is(err, io.EOF) {
            break
        }
        break
    }
    switch msg.Role {
        case moonshot.RoleSystem,moonshot.RoleUser,moonshot.RoleAssistant:
        // do something...
        default:
        // do something...
    }
}

🤝 Missing a Feature?

Feel free to open a new issue, or contact me.

📘 License

This is open-sourced library licensed under the MIT license.

FOSSA Status

Documentation

Index

Constants

View Source
const DefaultHost = "https://api.moonshot.cn"

Variables

This section is empty.

Functions

func ResponseToError added in v0.4.1

func ResponseToError(resp *httpx.Response) error

ResponseToError bind and return error from response

Types

type ChatCompletionsFinishReason

type ChatCompletionsFinishReason string
const (
	FinishReasonStop   ChatCompletionsFinishReason = "stop"
	FinishReasonLength ChatCompletionsFinishReason = "length"
)

func (ChatCompletionsFinishReason) String added in v0.4.0

type ChatCompletionsMessage

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

type ChatCompletionsMessageRole

type ChatCompletionsMessageRole string
const (
	RoleSystem    ChatCompletionsMessageRole = "system"
	RoleUser      ChatCompletionsMessageRole = "user"
	RoleAssistant ChatCompletionsMessageRole = "assistant"
)

func (ChatCompletionsMessageRole) String added in v0.4.0

type ChatCompletionsModelID

type ChatCompletionsModelID string
const (
	ModelMoonshotV18K   ChatCompletionsModelID = "moonshot-v1-8k"
	ModelMoonshotV132K  ChatCompletionsModelID = "moonshot-v1-32k"
	ModelMoonshotV1128K ChatCompletionsModelID = "moonshot-v1-128k"
)

func (ChatCompletionsModelID) String added in v0.4.0

func (c ChatCompletionsModelID) String() string

type ChatCompletionsRequest

type ChatCompletionsRequest struct {
	Messages         []*ChatCompletionsMessage `json:"messages"`
	Model            ChatCompletionsModelID    `json:"model"`
	MaxTokens        int                       `json:"max_tokens"`
	Temperature      float64                   `json:"temperature"`
	TopP             float64                   `json:"top_p"`
	N                int                       `json:"n"`
	PresencePenalty  float64                   `json:"presence_penalty"`
	FrequencyPenalty float64                   `json:"frequency_penalty"`
	Stop             []string                  `json:"stop"`
	Stream           bool                      `json:"stream"`
}

type ChatCompletionsResponse

type ChatCompletionsResponse struct {
	ID      string                            `json:"id"`
	Object  string                            `json:"object"`
	Created int                               `json:"created"`
	Model   string                            `json:"model"`
	Choices []*ChatCompletionsResponseChoices `json:"choices"`
	Usage   *ChatCompletionsResponseUsage     `json:"usage"`
}

type ChatCompletionsResponseChoices

type ChatCompletionsResponseChoices struct {
	Index int `json:"index"`

	// return with no stream
	Message *ChatCompletionsMessage `json:"message,omitempty"`
	// return With stream
	Delta *ChatCompletionsMessage `json:"delta,omitempty"`

	FinishReason ChatCompletionsFinishReason `json:"finish_reason"`
}

type ChatCompletionsResponseUsage

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

type ChatCompletionsStreamResponse

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

func (*ChatCompletionsStreamResponse) Receive added in v0.2.0

Receive returns a channel to receive messages from the stream

type ChatCompletionsStreamResponseReceive

type ChatCompletionsStreamResponseReceive struct {
	ChatCompletionsResponse
	// contains filtered or unexported fields
}

func (*ChatCompletionsStreamResponseReceive) GetMessage

GetMessage returns the message from the stream

type Client

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

func NewClient

func NewClient(key string) (*Client, error)

NewClient creates a new client

func NewClientWithConfig

func NewClientWithConfig(cfg *Config) (*Client, error)

NewClientWithConfig creates a new client with a custom configuration

func (*Client) Chat

func (c *Client) Chat() IChat

Chat returns a new chat controller

func (*Client) Files

func (c *Client) Files() IFiles

Files returns a new files controller

func (*Client) HTTPClient

func (c *Client) HTTPClient() *httpx.Client

HTTPClient returns a new http client

func (*Client) Models

func (c *Client) Models() IModels

Models returns a new models controller

func (*Client) Tokenizers

func (c *Client) Tokenizers() ITokenizers

Tokenizers returns a new tokenizers controller

func (*Client) Users added in v0.4.0

func (c *Client) Users() IUsers

Users returns a new users controller

type CommonAPIResponse

type CommonAPIResponse struct {
	Error *CommonAPIResponseError `json:"error,omitempty"`
}

type CommonAPIResponseError

type CommonAPIResponseError struct {
	Message string `json:"message,omitempty"`
	Type    string `json:"type,omitempty"`
}

type CommonResponse

type CommonResponse struct {
	Code    int    `json:"code,omitempty"`
	Error   string `json:"error,omitempty"`
	Message string `json:"message,omitempty"`
	Method  string `json:"method,omitempty"`
	Scode   string `json:"scode,omitempty"`
	Status  bool   `json:"status,omitempty"`
	UA      string `json:"ua,omitempty"`
	URL     string `json:"url,omitempty"`
}

type Config

type Config struct {
	Host   string
	APIKey string
	Debug  bool
}

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig creates a new config

type FileContentResponse

type FileContentResponse struct {
	Content  string `json:"content"`
	FileType string `json:"file_type"`
	Filename string `json:"filename"`
	Title    string `json:"title"`
	Type     string `json:"type"`
}

type FilesBatchDeleteRequest added in v0.3.0

type FilesBatchDeleteRequest struct {
	FileIDList []string `json:"file_ids"`
}

type FilesBatchDeleteResponse added in v0.3.0

type FilesBatchDeleteResponse struct {
	RespList  []*FilesDeleteResponse `json:"resp_list"`
	ErrorList []error                `json:"error_list"`
}

type FilesDeleteResponse

type FilesDeleteResponse struct {
	Deleted bool   `json:"deleted"`
	ID      string `json:"id"`
	Object  string `json:"object"`
}

type FilesInfoResponse

type FilesInfoResponse struct {
	ID            string `json:"id"`
	Object        string `json:"object"`
	Bytes         int    `json:"bytes"`
	CreatedAt     int    `json:"created_at"`
	Filename      string `json:"filename"`
	Purpose       string `json:"purpose"`
	Status        string `json:"status"`
	StatusDetails string `json:"status_details"`
}

type FilesListRequest

type FilesListRequest struct {
}

type FilesListResponse

type FilesListResponse struct {
	Object string                   `json:"object"`
	Data   []*FilesListResponseData `json:"data"`
}

type FilesListResponseData

type FilesListResponseData struct {
	ID           string       `json:"id"`
	Object       string       `json:"object"`
	Bytes        int          `json:"bytes"`
	CreatedAt    int          `json:"created_at"`
	Filename     string       `json:"filename"`
	Purpose      FilesPurpose `json:"purpose"`
	Status       string       `json:"status"`
	StatusDetail string       `json:"status_detail"`
}

type FilesPurpose

type FilesPurpose string
const (
	FilePurposeExtract FilesPurpose = "file-extract"
)

func (FilesPurpose) String

func (f FilesPurpose) String() string

type FilesUploadBytesRequest

type FilesUploadBytesRequest struct {
	Name    string
	Bytes   []byte
	Purpose FilesPurpose
}

type FilesUploadBytesResponse

type FilesUploadBytesResponse struct {
	ID            string `json:"id"`
	Object        string `json:"object"`
	Bytes         int    `json:"bytes"`
	CreatedAt     int    `json:"created_at"`
	Filename      string `json:"filename"`
	Purpose       string `json:"purpose"`
	Status        string `json:"status"`
	StatusDetails string `json:"status_details"`
}

type FilesUploadRequest

type FilesUploadRequest struct {
	Name    string
	Path    string
	Purpose FilesPurpose
}

type FilesUploadResponse

type FilesUploadResponse struct {
	ID            string `json:"id"`
	Object        string `json:"object"`
	Bytes         int    `json:"bytes"`
	CreatedAt     int    `json:"created_at"`
	Filename      string `json:"filename"`
	Purpose       string `json:"purpose"`
	Status        string `json:"status"`
	StatusDetails string `json:"status_details"`
}

type IChat added in v0.3.0

type IChat interface {
	Completions(ctx context.Context, req *ChatCompletionsRequest) (*ChatCompletionsResponse, error)
	CompletionsStream(ctx context.Context, req *ChatCompletionsRequest) (*ChatCompletionsStreamResponse, error)
}

type IChatCompletionsBuilder added in v0.4.1

type IChatCompletionsBuilder interface {
	AddUserContent(content string) IChatCompletionsBuilder
	AddSystemContent(content string) IChatCompletionsBuilder
	AddAssistantContent(content string) IChatCompletionsBuilder
	AddPrompt(prompt string) IChatCompletionsBuilder
	AddMessage(message *ChatCompletionsMessage) IChatCompletionsBuilder

	SetModel(model ChatCompletionsModelID) IChatCompletionsBuilder
	SetTemperature(temperature float64) IChatCompletionsBuilder
	SetStream(enable bool) IChatCompletionsBuilder
	SetMaxTokens(num int) IChatCompletionsBuilder
	SetTopP(num float64) IChatCompletionsBuilder
	SetN(num int) IChatCompletionsBuilder
	SetPresencePenalty(num float64) IChatCompletionsBuilder
	SetFrequencyPenalty(num float64) IChatCompletionsBuilder
	SetStop(stop []string) IChatCompletionsBuilder

	ToRequest() *ChatCompletionsRequest
}

func NewChatCompletionsBuilder added in v0.4.1

func NewChatCompletionsBuilder(req ...ChatCompletionsRequest) IChatCompletionsBuilder

NewChatCompletionsBuilder creates a new chat completions builder, or with the given request

type IFiles added in v0.3.0

type IFiles interface {
	Upload(ctx context.Context, req *FilesUploadRequest) (resp *FilesUploadResponse, err error)
	UploadBytes(ctx context.Context, req *FilesUploadBytesRequest) (resp *FilesUploadBytesResponse, err error)
	List(ctx context.Context) (res *FilesListResponse, err error)
	Delete(ctx context.Context, fileID string) (resp *FilesDeleteResponse, err error)
	BatchDelete(ctx context.Context, req *FilesBatchDeleteRequest) (resp *FilesBatchDeleteResponse, err error)
	Info(ctx context.Context, fileID string) (resp *FilesInfoResponse, err error)
	Content(ctx context.Context, fileID string) (resp *FileContentResponse, err error)
}

type IModels added in v0.3.0

type IModels interface {
	List(ctx context.Context) (*ListModelsResponse, error)
}

type ITokenizers added in v0.3.0

type ITokenizers interface {
	EstimateTokenCount(ctx context.Context, req *TokenizersEstimateTokenCountRequest) (resp *TokenizersEstimateTokenCountResponse, err error)
}

type IUsers added in v0.4.0

type IUsers interface {
	Balance(ctx context.Context) (*UsersBalanceResponse, error)
}

type ListModelResponseData

type ListModelResponseData struct {
	Created    int                                 `json:"created"`
	ID         string                              `json:"id"`
	Object     string                              `json:"object"`
	OwnedBy    string                              `json:"owned_by"`
	Permission []*ListModelsResponseDataPermission `json:"permission"`
	Root       string                              `json:"root"`
	Parent     string                              `json:"parent"`
}

type ListModelsRequest

type ListModelsRequest struct {
}

type ListModelsResponse

type ListModelsResponse struct {
	CommonResponse
	Object string                   `json:"object"`
	Data   []*ListModelResponseData `json:"data"`
}

type ListModelsResponseDataPermission

type ListModelsResponseDataPermission struct {
	Created            int    `json:"created"`
	ID                 string `json:"id"`
	Object             string `json:"object"`
	AllowCreateEngine  bool   `json:"allow_create_engine"`
	AllowSampling      bool   `json:"allow_sampling"`
	AllowLogprobs      bool   `json:"allow_logprobs"`
	AllowSearchIndices bool   `json:"allow_search_indices"`
	AllowView          bool   `json:"allow_view"`
	AllowFineTuning    bool   `json:"allow_fine_tuning"`
	Organization       string `json:"organization"`
	Group              string `json:"group"`
	IsBlocking         bool   `json:"is_blocking"`
}

type Option

type Option func(*Config)

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey sets the API key

func WithHost

func WithHost(host string) Option

WithHost sets the host

type TokenizersEstimateTokenCountRequest

type TokenizersEstimateTokenCountRequest struct {
	Model    ChatCompletionsModelID    `json:"model"`
	Messages []*ChatCompletionsMessage `json:"messages"`
}

type TokenizersEstimateTokenCountResponse

type TokenizersEstimateTokenCountResponse struct {
	CommonResponse
	Data *TokenizersEstimateTokenCountResponseData `json:"data"`
}

type TokenizersEstimateTokenCountResponseData

type TokenizersEstimateTokenCountResponseData struct {
	TotalTokens int `json:"total_tokens"`
}

type UsersBalanceResponse added in v0.4.0

type UsersBalanceResponse struct {
	Code   int                       `json:"code"`
	Data   *UsersBalanceResponseData `json:"data"`
	Scode  string                    `json:"scode"`
	Status bool                      `json:"status"`
}

type UsersBalanceResponseData added in v0.4.0

type UsersBalanceResponseData struct {
	// AvailableBalance including cash balance and voucher balance. When it is less than or equal to 0, the user cannot call the completions API
	AvailableBalance float64 `json:"available_balance"`
	// VoucherBalance will not be negative
	VoucherBalance float64 `json:"voucher_balance"`
	// CashBalance may be negative, which means that the user owes the cost. When it is negative, the AvailableBalance can be the amount of VoucherBalance
	CashBalance float64 `json:"cash_balance"`
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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