Documentation ¶
Overview ¶
NOTE: Reference zhipuai's python sdk: utils/jwt_token.py
Index ¶
- Constants
- Variables
- func BuildAPIURL(model string, method Method) string
- func GenerateToken(apikey string, expSeconds int64) (string, error)
- func Stream(apiURL, token string, params ModelParams, timeout time.Duration, ...) error
- func ValidateModelParams(params ModelParams) error
- type Choice
- type Data
- type EmbeddingData
- type EmbeddingResponse
- type EmbeddingText
- type Method
- type ModelParams
- type Option
- type Prompt
- type Response
- type Role
- type SSEClient
- type Usage
- type ZhiPuAI
- func (z *ZhiPuAI) AsyncInvoke(params ModelParams) (*Response, error)
- func (z *ZhiPuAI) Call(data []byte) (llms.Response, error)
- func (z *ZhiPuAI) CreateEmbedding(ctx context.Context, inputTexts []string) ([][]float32, error)
- func (z *ZhiPuAI) Embedding(text EmbeddingText) (*EmbeddingResponse, error)
- func (z *ZhiPuAI) Get(params ModelParams) (*Response, error)
- func (z *ZhiPuAI) Invoke(params ModelParams) (*Response, error)
- func (z *ZhiPuAI) SSEInvoke(params ModelParams, handler func(*sse.Event)) error
- func (z ZhiPuAI) Type() llms.LLMType
- func (z *ZhiPuAI) Validate(ctx context.Context, options ...langchainllms.CallOption) (llms.Response, error)
- type ZhiPuAILLM
- func (z *ZhiPuAILLM) Call(ctx context.Context, prompt string, options ...langchainllm.CallOption) (string, error)
- func (z *ZhiPuAILLM) GenerateContent(ctx context.Context, messages []langchainllm.MessageContent, ...) (*langchainllm.ContentResponse, error)
- func (z *ZhiPuAILLM) GetNumTokens(text string) int
Constants ¶
const ( ZhipuaiModelAPIURL = "https://open.bigmodel.cn/api/paas/v3/model-api" ZhipuaiModelDefaultTimeout = 300 * time.Second RetryLimit = 3 )
const ( APITokenTTLSeconds = 3 * 60 // FIXME: impl TLL Cache CacheTTLSeconds = (APITokenTTLSeconds - 30) )
const ( CodeConcurrencyHigh = 1302 // 您当前使用该 API 的并发数过高,请降低并发,或联系客服增加限额 CodefrequencyHigh = 1303 // 您当前使用该 API 的频率过高,请降低频率,或联系客服增加限额 CodeTimesHigh = 1305 // 当前 API 请求过多,请稍后重试 )
Variables ¶
var ( ErrEmptyResponse = errors.New("no response") ErrEmptyPrompt = errors.New("empty prompt") )
Functions ¶
func BuildAPIURL ¶
func Stream ¶
func Stream(apiURL, token string, params ModelParams, timeout time.Duration, handler func(*sse.Event)) error
func ValidateModelParams ¶
func ValidateModelParams(params ModelParams) error
Types ¶
type EmbeddingData ¶
type EmbeddingResponse ¶
type EmbeddingResponse struct { Code int `json:"code"` Data *EmbeddingData `json:"data"` Msg string `json:"msg"` Success bool `json:"success"` }
func EmbeddingPost ¶
func EmbeddingPost(apiURL, token string, text EmbeddingText, timeout time.Duration) (*EmbeddingResponse, error)
EmbeddingPost posts embedding request to model FIXME: should this be made an interface?
func (*EmbeddingResponse) Bytes ¶
func (embeddingResp *EmbeddingResponse) Bytes() []byte
func (*EmbeddingResponse) String ¶
func (embeddingResp *EmbeddingResponse) String() string
func (*EmbeddingResponse) Type ¶
func (embeddingResp *EmbeddingResponse) Type() llms.LLMType
func (*EmbeddingResponse) Unmarshall ¶
func (embeddingResp *EmbeddingResponse) Unmarshall(bytes []byte) error
type EmbeddingText ¶
type ModelParams ¶
type ModelParams struct { // Method used for this prompt call Method Method `json:"method,omitempty"` // Model used for this prompt call Model string `json:"model,omitempty"` // Temperature is float in zhipuai Temperature float32 `json:"temperature,omitempty"` // TopP is float in zhipuai TopP float32 `json:"top_p,omitempty"` // Contents Prompt []Prompt `json:"prompt"` // TaskID is used for getting result of AsyncInvoke TaskID string `json:"task_id,omitempty"` // Incremental is only Used for SSE Invoke Incremental bool `json:"incremental,omitempty"` }
+kubebuilder:object:generate=true ZhiPuAIParams defines the params of ZhiPuAI Prompt Call
func DefaultModelParams ¶
func DefaultModelParams() ModelParams
func MergeParams ¶
func MergeParams(a, b ModelParams) ModelParams
MergeZhiPuAI merges b to a with this rule - if a.x is empty and b.x is not, then a.x = b.x
func (*ModelParams) DeepCopy ¶
func (in *ModelParams) DeepCopy() *ModelParams
DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ModelParams.
func (*ModelParams) DeepCopyInto ¶
func (in *ModelParams) DeepCopyInto(out *ModelParams)
DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (*ModelParams) Marshal ¶
func (params *ModelParams) Marshal() []byte
func (*ModelParams) Unmarshal ¶
func (params *ModelParams) Unmarshal(bytes []byte) error
type Option ¶ added in v0.2.0
type Option func(*options)
func WithCallback ¶ added in v0.2.1
func WithRetryTimes ¶ added in v0.2.0
type Prompt ¶
+kubebuilder:object:generate=true Prompt defines the content of ZhiPuAI Prompt Call
func (*Prompt) DeepCopy ¶
DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Prompt.
func (*Prompt) DeepCopyInto ¶
DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
type Response ¶
type Response struct { Code int `json:"code"` Data *Data `json:"data"` Msg string `json:"msg"` Success bool `json:"success"` }
type SSEClient ¶
type SSEClient struct { LastEventID atomic.Value // []byte EncodingBase64 bool // contains filtered or unexported fields }
func NewSSEClient ¶
func NewSSEClient() *SSEClient
type ZhiPuAI ¶
type ZhiPuAI struct {
// contains filtered or unexported fields
}
func NewZhiPuAI ¶
func (*ZhiPuAI) AsyncInvoke ¶
func (z *ZhiPuAI) AsyncInvoke(params ModelParams) (*Response, error)
AsyncInvoke only returns a task id which can be used to get result of task later
func (*ZhiPuAI) CreateEmbedding ¶
CreateEmbedding do batch embedding To compatible with langchaingo/llms
func (*ZhiPuAI) Embedding ¶
func (z *ZhiPuAI) Embedding(text EmbeddingText) (*EmbeddingResponse, error)
func (*ZhiPuAI) Get ¶
func (z *ZhiPuAI) Get(params ModelParams) (*Response, error)
Get result of task async-invoke
func (*ZhiPuAI) Invoke ¶
func (z *ZhiPuAI) Invoke(params ModelParams) (*Response, error)
Invoke calls zhipuai and returns result immediately
func (*ZhiPuAI) SSEInvoke ¶
func (z *ZhiPuAI) SSEInvoke(params ModelParams, handler func(*sse.Event)) error
func (*ZhiPuAI) Validate ¶
func (z *ZhiPuAI) Validate(ctx context.Context, options ...langchainllms.CallOption) (llms.Response, error)
Validate zhipuai service against CallOption
type ZhiPuAILLM ¶
type ZhiPuAILLM struct {
// contains filtered or unexported fields
}
func NewZhiPuAILLM ¶ added in v0.2.0
func NewZhiPuAILLM(apiKey string, opts ...Option) *ZhiPuAILLM
func (*ZhiPuAILLM) Call ¶
func (z *ZhiPuAILLM) Call(ctx context.Context, prompt string, options ...langchainllm.CallOption) (string, error)
func (*ZhiPuAILLM) GenerateContent ¶ added in v0.2.1
func (z *ZhiPuAILLM) GenerateContent(ctx context.Context, messages []langchainllm.MessageContent, options ...langchainllm.CallOption) (*langchainllm.ContentResponse, error)
func (*ZhiPuAILLM) GetNumTokens ¶
func (z *ZhiPuAILLM) GetNumTokens(text string) int