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 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, messages []schema.ChatMessage, ...) (*schema.AIChatMessage, error)
- func (z *ZhiPuAILLM) Generate(ctx context.Context, messageSets [][]schema.ChatMessage, ...) ([]*langchainllm.Generation, error)
- func (z *ZhiPuAILLM) GeneratePrompt(ctx context.Context, promptValues []schema.PromptValue, ...) (langchainllm.LLMResult, 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 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 ¶
func (*ZhiPuAILLM) Call ¶
func (z *ZhiPuAILLM) Call(ctx context.Context, messages []schema.ChatMessage, options ...langchainllm.CallOption) (*schema.AIChatMessage, error)
func (*ZhiPuAILLM) Generate ¶
func (z *ZhiPuAILLM) Generate(ctx context.Context, messageSets [][]schema.ChatMessage, options ...langchainllm.CallOption) ([]*langchainllm.Generation, error)
func (*ZhiPuAILLM) GeneratePrompt ¶
func (z *ZhiPuAILLM) GeneratePrompt(ctx context.Context, promptValues []schema.PromptValue, options ...langchainllm.CallOption) (langchainllm.LLMResult, error)
func (*ZhiPuAILLM) GetNumTokens ¶
func (z *ZhiPuAILLM) GetNumTokens(text string) int