moonshot

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 14 Imported by: 3

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.

[!WARNING]
This project is still actively developing, and the API may change with the release of the version. Please pay attention when you upgrade the version.

🚀 Installation

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

You can find the docs at go docs.

🤘 Feature

  • Easy to use and simple API, chain operation.
  • Full API and builtin functions support.
  • Predefined enumeration.
  • Messages builder.

📄 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
Tool Use
Context Cache

🗜 Builtin Functions

  • $web_search

🥪 Usage

For more examples, you can view the test file of the corresponding interface.

Initialize client

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

[!TIP] Your API key is sensitive information. Do not share it with anyone.

With Only Key
key, ok := os.LookupEnv("MOONSHOT_KEY")
// do something...

cli, err := moonshot.NewClient(key)
// do something...
With Config
key, ok := os.LookupEnv("MOONSHOT_KEY")
// do something...

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

API

List Models
resp, err := cli.Models().List(context.Background())
// do something...
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()) 
// {"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())
// {"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}}

// do something...
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,
})
// do something...

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...
    }
}
Web search tool
builder := moonshot.NewChatCompletionsBuilder()
builder.SetModel(moonshot.ModelMoonshotV1128K)
builder.AddUserContent("请搜索 Moonshot AI Context Caching 技术,并告诉我它是什么。")
builder.SetTool(&moonshot.ChatCompletionsTool{
	Type: moonshot.ChatCompletionsToolTypeBuiltinFunction,
	Function: &moonshot.ChatCompletionsToolFunction{
		Name: moonshot.BuiltinFunctionWebSearch,
	},
})

resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
// do something...

if len(resp.Choices) != 0 {
	choice := resp.Choices[0]
	if choice.FinishReason == moonshot.FinishReasonToolCalls {
		for _, tool := range choice.Message.ToolCalls {
			if tool.Function.Name == moonshot.BuiltinFunctionWebSearch {
				// web search
				arguments := new(moonshot.ChatCompletionsToolBuiltinFunctionWebSearchArguments)
				if err = json.Unmarshal([]byte(tool.Function.Arguments), arguments); err != nil {
					continue
				}
				// do something...

				builder.AddMessageFromChoices(resp.Choices)
				builder.AddToolContent(tool.Function.Arguments, tool.Function.Name, tool.ID)
			}
		}
	}
}

resp, err = cli.Chat().Completions(ctx, builder.ToRequest())
// do something...

🤝 Missing a Feature?

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

🥳 Contributors

contributors

Made with contrib.rocks.

📘 License

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

FOSSA Status

Documentation

Index

Constants

View Source
const (
	ResetTTLNever     = -1 // ResetTTLNever is the value for never reset ttl
	ResetTTLImmediate = 0  // ResetTTLImmediate is the value for immediate reset ttl
)
View Source
const (
	BuiltinFunctionWebSearch string = "$web_search"
)
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"
	FinishReasonToolCalls ChatCompletionsFinishReason = "tool_calls"
)

func (ChatCompletionsFinishReason) String added in v0.4.0

type ChatCompletionsMessage

type ChatCompletionsMessage struct {
	Role    ChatCompletionsMessageRole `json:"role"`
	Content string                     `json:"content"`
	Partial bool                       `json:"partial,omitempty"`
	Name    string                     `json:"name,omitempty"`
	// returns only in use tool response
	ToolCalls []*ChatCompletionsResponseToolCalls `json:"tool_calls,omitempty"`
	// use tool request need it
	ToolCallID string `json:"tool_call_id,omitempty"`
}

type ChatCompletionsMessageRole

type ChatCompletionsMessageRole string
const (
	RoleSystem       ChatCompletionsMessageRole = "system"
	RoleUser         ChatCompletionsMessageRole = "user"
	RoleAssistant    ChatCompletionsMessageRole = "assistant"
	RoleTool         ChatCompletionsMessageRole = "tool"
	RoleContextCache ChatCompletionsMessageRole = "cache"
)

func (ChatCompletionsMessageRole) String added in v0.4.0

type ChatCompletionsModelFamily added in v0.5.0

type ChatCompletionsModelFamily string
const (
	ModelFamilyMoonshotV1 ChatCompletionsModelFamily = "moonshot-v1"
)

func (ChatCompletionsModelFamily) String added in v0.5.0

type ChatCompletionsModelID

type ChatCompletionsModelID string
const (
	ModelMoonshotV1Auto ChatCompletionsModelID = "moonshot-v1-auto"
	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 ChatCompletionsParametersType added in v0.5.0

type ChatCompletionsParametersType string
const (
	ChatCompletionsParametersTypeObject ChatCompletionsParametersType = "object"
)

func (ChatCompletionsParametersType) String added in v0.5.0

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"`
	ResponseFormat   *ChatCompletionsRequestResponseFormat `json:"response_format"`
	Stop             []string                              `json:"stop"`
	Stream           bool                                  `json:"stream"`
	// When you use a tool, you need to define it
	Tools []*ChatCompletionsTool `json:"tools,omitempty"`
}

type ChatCompletionsRequestResponseFormat added in v0.5.1

type ChatCompletionsRequestResponseFormat struct {
	Type ChatCompletionsResponseFormatType `json:"type"`
}

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"`
	// returns only in non-stream mode
	Usage *ChatCompletionsResponseUsage `json:"usage,omitempty"`
}

func (*ChatCompletionsResponse) GetMessage added in v0.4.2

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"`

	// returns only in stream mode
	Usage *ChatCompletionsResponseUsage `json:"usage,omitempty"`
}

type ChatCompletionsResponseFormatType added in v0.5.1

type ChatCompletionsResponseFormatType string
const (
	ChatCompletionsResponseFormatJSONObject ChatCompletionsResponseFormatType = "json_object"
	ChatCompletionsResponseFormatText       ChatCompletionsResponseFormatType = "text"
)

func (ChatCompletionsResponseFormatType) String added in v0.5.1

type ChatCompletionsResponseToolCalls added in v0.5.0

type ChatCompletionsResponseToolCalls struct {
	Index    int64                                     `json:"index"`
	ID       string                                    `json:"id"`
	Type     string                                    `json:"type"`
	Function *ChatCompletionsResponseToolCallsFunction `json:"function"`
}

type ChatCompletionsResponseToolCallsFunction added in v0.5.0

type ChatCompletionsResponseToolCallsFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

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 ChatCompletionsTool added in v0.5.0

type ChatCompletionsTool struct {
	Type     ChatCompletionsToolType      `json:"type"`
	Function *ChatCompletionsToolFunction `json:"function"`
}

type ChatCompletionsToolBuiltinFunctionWebSearchArguments added in v0.5.2

type ChatCompletionsToolBuiltinFunctionWebSearchArguments struct {
	SearchResult struct {
		SearchId string `json:"search_id"`
	} `json:"search_result"`
	Usage struct {
		TotalTokens int `json:"total_tokens"`
	} `json:"usage"`
}

type ChatCompletionsToolFunction added in v0.5.0

type ChatCompletionsToolFunction struct {
	Name        string                                 `json:"name"`
	Description string                                 `json:"description,omitempty"`
	Parameters  *ChatCompletionsToolFunctionParameters `json:"parameters,omitempty"`
}

type ChatCompletionsToolFunctionParameters added in v0.5.0

type ChatCompletionsToolFunctionParameters struct {
	Type       ChatCompletionsParametersType                     `json:"type"`
	Properties map[string]*ChatCompletionsToolFunctionProperties `json:"properties"`
	Required   []string                                          `json:"required,omitempty"`
}

type ChatCompletionsToolFunctionProperties added in v0.5.0

type ChatCompletionsToolFunctionProperties struct {
	Type        string   `json:"type"`
	Description string   `json:"description,omitempty"`
	Enum        []string `json:"enum,omitempty"`
}

type ChatCompletionsToolType added in v0.5.0

type ChatCompletionsToolType string
const (
	ChatCompletionsToolTypeFunction        ChatCompletionsToolType = "function"
	ChatCompletionsToolTypeBuiltinFunction ChatCompletionsToolType = "builtin_function"
)

func (ChatCompletionsToolType) String added in v0.5.0

func (c ChatCompletionsToolType) String() string

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) ContextCache added in v0.5.0

func (c *Client) ContextCache() IContextCache

ContextCache returns a new context cache 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 ContextCache added in v0.5.0

type ContextCache struct {
	Id          string                     `json:"id"`          // 缓存的唯一标识
	Status      ContextCacheStatus         `json:"status"`      // 缓存的状态
	Object      string                     `json:"object"`      // 缓存的类型
	CreatedAt   int64                      `json:"created_at"`  // 缓存的创建时间
	ExpiredAt   int64                      `json:"expired_at"`  // 缓存的过期时间
	Tokens      int                        `json:"tokens"`      // 缓存的 Token 数量
	Model       ChatCompletionsModelFamily `json:"model"`       // 缓存的模型组名称
	Messages    []ChatCompletionsMessage   `json:"messages"`    // 缓存的消息内容
	Tools       []ChatCompletionsTool      `json:"tools"`       // 缓存使用的工具
	Name        string                     `json:"name"`        // 缓存的名称
	Description string                     `json:"description"` // 缓存的描述信息
	Metadata    map[string]string          `json:"metadata"`    // 缓存的元信息
}

ContextCache is the cache of the context

type ContextCacheContent added in v0.5.0

type ContextCacheContent struct {
	CacheId  string `json:"cache_id"`
	Tag      string `json:"tag"`
	ResetTTL int64  `json:"reset_ttl"`
	DryRun   bool   `json:"dry_run"`
}

ContextCacheContent is the content for the context cache

func NewContextCacheContentWithId added in v0.5.0

func NewContextCacheContentWithId(cacheId string) *ContextCacheContent

func NewContextCacheContentWithTag added in v0.5.0

func NewContextCacheContentWithTag(tag string) *ContextCacheContent

func (*ContextCacheContent) Content added in v0.5.0

func (c *ContextCacheContent) Content() string

func (*ContextCacheContent) WithDryRun added in v0.5.0

func (c *ContextCacheContent) WithDryRun(dryRun bool) *ContextCacheContent

WithDryRun set the dry run for the context cache

func (*ContextCacheContent) WithResetTTL added in v0.5.0

func (c *ContextCacheContent) WithResetTTL(resetTTL int64) *ContextCacheContent

WithResetTTL set the reset ttl for the context cache

type ContextCacheCreateRequest added in v0.5.0

type ContextCacheCreateRequest struct {
	Model       ChatCompletionsModelFamily `json:"model"`                 // 模型组(model family)名称
	Messages    []ChatCompletionsMessage   `json:"messages"`              // 消息内容
	Tools       []ChatCompletionsTool      `json:"tools,omitempty"`       // 使用的工具
	Name        string                     `json:"name,omitempty"`        // 缓存名称
	Description string                     `json:"description,omitempty"` // 缓存描述信息
	Metadata    map[string]string          `json:"metadata,omitempty"`    // 缓存的元信息
	ExpiredAt   int64                      `json:"expired_at"`            // 缓存的过期时间
	TTL         int64                      `json:"ttl,omitempty"`         // 缓存的有效期
}

ContextCacheCreateRequest is the request for creating a context cache

type ContextCacheCreateResponse added in v0.5.0

type ContextCacheCreateResponse ContextCache

ContextCacheCreateResponse is the response for creating a context cache

type ContextCacheCreateTagRequest added in v0.5.0

type ContextCacheCreateTagRequest struct {
	Tag     string `json:"tag"`      // 缓存的标签
	CacheId string `json:"cache_id"` // 缓存的唯一标识
}

ContextCacheCreateTagRequest is the request for creating a context cache tag

type ContextCacheCreateTagResponse added in v0.5.0

type ContextCacheCreateTagResponse ContextCacheTag

ContextCacheCreateTagResponse is the response for creating a context cache tag

type ContextCacheDeleteRequest added in v0.5.0

type ContextCacheDeleteRequest struct {
	Id string `json:"id"` // 缓存的唯一标识
}

ContextCacheDeleteRequest is the request for deleting a context cache

type ContextCacheDeleteResponse added in v0.5.0

type ContextCacheDeleteResponse struct {
	Deleted bool   `json:"deleted"` // 缓存是否被删除
	Id      string `json:"id"`      // 被删除的缓存的唯一标识
	Object  string `json:"object"`  // 返回的数据类型
}

ContextCacheDeleteResponse is the response for deleting a context cache

type ContextCacheDeleteTagRequest added in v0.5.0

type ContextCacheDeleteTagRequest struct {
	Tag string `json:"_"` // 缓存的标签
}

ContextCacheDeleteTagRequest is the request for deleting a context cache tag

type ContextCacheDeleteTagResponse added in v0.5.0

type ContextCacheDeleteTagResponse struct {
	Deleted bool   `json:"deleted"` // 缓存是否被删除
	Object  string `json:"object"`  // 返回的数据类型
	Tag     string `json:"tag"`     // 被删除的缓存的标签
}

ContextCacheDeleteTagResponse is the response for deleting a context cache tag

type ContextCacheGetRequest added in v0.5.0

type ContextCacheGetRequest struct {
	Id string `json:"_"` // 缓存的唯一标识
}

ContextCacheGetRequest is the request for getting a context cache

type ContextCacheGetResponse added in v0.5.0

type ContextCacheGetResponse ContextCache

ContextCacheGetResponse is the response for getting a context cache

type ContextCacheGetTagContentRequest added in v0.5.0

type ContextCacheGetTagContentRequest struct {
	Tag string `json:"_"` // 缓存的标签
}

ContextCacheGetTagContentRequest is the request for getting a context cache tag content

type ContextCacheGetTagContentResponse added in v0.5.0

type ContextCacheGetTagContentResponse ContextCache

ContextCacheGetTagContentResponse is the response for getting a context cache tag content

type ContextCacheGetTagRequest added in v0.5.0

type ContextCacheGetTagRequest struct {
	Tag string `json:"_"` // 缓存的标签
}

ContextCacheGetTagRequest is the request for getting a context cache tag

type ContextCacheGetTagResponse added in v0.5.0

type ContextCacheGetTagResponse ContextCacheTag

ContextCacheGetTagResponse is the response for getting a context cache tag

type ContextCacheListRequest added in v0.5.0

type ContextCacheListRequest struct {
	Limit    int               `json:"limit,omitempty"`    // 当前请求单页返回的缓存数量
	Order    ContextCacheOrder `json:"order,omitempty"`    // 当前请求时查询缓存的排序规则
	After    string            `json:"after,omitempty"`    // 当前请求时,应该从哪一个缓存开始进行查找
	Before   string            `json:"before,omitempty"`   // 当前请求时,应该查询到哪一个缓存为止
	Metadata map[string]string `json:"metadata,omitempty"` // 用于筛选缓存的 metadata 信息
}

ContextCacheListRequest is the request for listing context caches

type ContextCacheListResponse added in v0.5.0

type ContextCacheListResponse struct {
	Object string         `json:"object"` // 返回的数据类型
	Data   []ContextCache `json:"data"`   // 返回的缓存列表
}

ContextCacheListResponse is the response for listing context caches

type ContextCacheListTagRequest added in v0.5.0

type ContextCacheListTagRequest struct {
	Limit  int               `json:"limit,omitempty"`  // 当前请求单页返回的缓存数量
	Order  ContextCacheOrder `json:"order,omitempty"`  // 当前请求时查询缓存的排序规则
	After  string            `json:"after,omitempty"`  // 当前请求时,应该从哪一个缓存开始进行查找
	Before string            `json:"before,omitempty"` // 当前请求时,应该查询到哪一个缓存为止
}

ContextCacheListTagRequest is the request for listing context cache tags

type ContextCacheListTagResponse added in v0.5.0

type ContextCacheListTagResponse struct {
	Object string            `json:"object"` // 返回的数据类型
	Data   []ContextCacheTag `json:"data"`   // 返回的缓存标签列表
}

ContextCacheListTagResponse is the response for listing context cache tags

type ContextCacheOrder added in v0.5.0

type ContextCacheOrder string
const (
	ContextCacheOrderAsc  ContextCacheOrder = "asc"  // 升序
	ContextCacheOrderDesc ContextCacheOrder = "desc" // 降序
)

func (ContextCacheOrder) String added in v0.5.0

func (c ContextCacheOrder) String() string

type ContextCacheStatus added in v0.5.0

type ContextCacheStatus string
const (
	ContextCacheStatusPending  ContextCacheStatus = "pending"  // 当缓存被初次创建时,其初始状态为 pending
	ContextCacheStatusReady    ContextCacheStatus = "ready"    // 如果参数合法,缓存创建成功,其状态变更为 ready
	ContextCacheStatusError    ContextCacheStatus = "error"    // 如果参数不合法,或因其他原因缓存创建失败,其状态变更为 error
	ContextCacheStatusInactive ContextCacheStatus = "inactive" // 对于已过期的缓存,其状态变更为 inactive
)

func (ContextCacheStatus) String added in v0.5.0

func (c ContextCacheStatus) String() string

type ContextCacheTag added in v0.5.0

type ContextCacheTag struct {
	Tag       string `json:"tag"`        // 缓存的标签
	CacheId   string `json:"cache_id"`   // 缓存的唯一标识
	Object    string `json:"object"`     // 缓存的类型
	OwnedBy   string `json:"owned_by"`   // 缓存的拥有者
	CreatedAt int    `json:"created_at"` // 缓存的创建时间
}

ContextCacheTag is the tag of the context cache

type ContextCacheUpdateRequest added in v0.5.0

type ContextCacheUpdateRequest struct {
	Id        string            `json:"_"`                  // 缓存的唯一标识
	Metadata  map[string]string `json:"metadata,omitempty"` // 缓存的元信息
	ExpiredAt int64             `json:"expired_at"`         // 缓存的过期时间
	TTL       int64             `json:"ttl,omitempty"`      // 缓存的有效期
}

ContextCacheUpdateRequest is the request for updating a context cache

type ContextCacheUpdateResponse added in v0.5.0

type ContextCacheUpdateResponse ContextCache

ContextCacheUpdateResponse is the response for updating a context cache

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, partialMode ...bool) IChatCompletionsBuilder
	AddToolContent(content, name, toolCallID string) IChatCompletionsBuilder
	AddPrompt(prompt string) IChatCompletionsBuilder
	AddMessage(message *ChatCompletionsMessage) IChatCompletionsBuilder
	AddMessageFromChoices(choices []*ChatCompletionsResponseChoices) 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
	SetTool(tool *ChatCompletionsTool) IChatCompletionsBuilder
	SetTools(tools []*ChatCompletionsTool) IChatCompletionsBuilder
	SetContextCacheContent(content *ContextCacheContent) IChatCompletionsBuilder
	SetResponseFormat(format ChatCompletionsResponseFormatType) 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