sparkclient

package
v0.0.0-...-75382fe Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrContentExclusive = errors.New("only one of Content / MultiContent allowed in message")
View Source
var ErrEmptyResponse = errors.New("empty response")

ErrEmptyResponse is returned when the OpenAI API returns an empty response.

View Source
var ErrorMap = map[SparkErrorCode]string{
	Success:                          "成功",
	ErrorUpgradeToWs:                 "升级为ws出现错误",
	ErrorReadUserMessageFailed:       "通过ws读取用户的消息出错",
	ErrorSendUserMessageFailed:       "通过ws向用户发送消息出错",
	ErrorUserMessageFormatError:      "用户的消息格式有错误",
	ErrorUserDataSchemaError:         "用户数据的schema错误",
	ErrorUserParameterValueError:     "用户参数值有错误",
	ErrorUserConcurrencyError:        "用户并发错误:当前用户已连接,同一用户不能多处同时连接。",
	ErrorUserTrafficLimitError:       "用户流量受限:服务正在处理用户当前的问题,需等待处理完成后再发送新的请求。",
	ErrorServiceCapacityInsufficient: "服务容量不足,联系工作人员",
	ErrorEngineConnectionFailed:      "和引擎建立连接失败",
	ErrorReceiveEngineDataError:      "接收引擎数据的错误",
	ErrorSendDataToEngineError:       "发送数据给引擎的错误",
	ErrorEngineInternalError:         "引擎内部错误",
	ErrorContentReviewFailed:         "输入内容审核不通过,涉嫌违规,请重新调整输入内容",
	ErrorSensitiveOutputError:        "输出内容涉及敏感信息,审核不通过,后续结果无法展示给用户",
	ErrorAppIDBlacklist:              "appid在黑名单中",
	ErrorAppIDAuthorizationError:     "appid授权类的错误。比如:未开通此功能,未开通对应版本,token不足,并发超过授权等等",
	ErrorClearHistoryFailed:          "清除历史失败",
	ErrorSessionContentViolation:     "表示本次会话内容有涉及违规信息的倾向;建议开发者收到此错误码后给用户一个输入涉及违规的提示",
	ErrorServiceBusy:                 "服务忙,请稍后再试",
	ErrorEngineParameterException:    "请求引擎的参数异常 引擎的schema 检查不通过",
	ErrorEngineNetworkException:      "引擎网络异常",
	ErrorTokenLimitExceeded:          "token数量超过上限。对话历史+问题的字数太多,需要精简输入",
	ErrorAuthFunctionLimit:           "授权错误:该appId没有相关功能的授权 或者 业务量超过限制",
	ErrorAuthDailyLimit:              "授权错误:日流控超限。超过当日最大访问量的限制",
	ErrorAuthRateLimit:               "授权错误:秒级流控超限。秒级并发超过授权路数限制",
	ErrorAuthConcurrencyLimit:        "授权错误:并发流控超限。并发路数超过授权路数限制",
}

ErrorMap 将错误码映射到错误信息

Functions

func GetSparkErrorMsg

func GetSparkErrorMsg(errorCode int) string

func GetSparkErrorMsgByCode

func GetSparkErrorMsgByCode(errorCode SparkErrorCode) string

Types

type APIVersion

type APIVersion string
const (
	APIv1 APIVersion = "v1.1"
	APIv2 APIVersion = "v2.1"
	APIv3 APIVersion = "v3.1"
)

type ChatChoice

type ChatChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

ChatChoice is a choice in a chat response.

type ChatMessage

type ChatMessage struct {
	// The role of the author of this message. One of system, user, or assistant.
	Role string
	// The content of the message.
	Content string

	// FunctionCall represents a function call to be made in the message.
	FunctionCall *messages.FunctionCall
}

ChatMessage is a message in a chat request.

func (ChatMessage) GetContent

func (m ChatMessage) GetContent() string

func (ChatMessage) GetType

func (m ChatMessage) GetType() messages.ChatMessageType

func (ChatMessage) MarshalJSON

func (m ChatMessage) MarshalJSON() ([]byte, error)

func (*ChatMessage) UnmarshalJSON

func (m *ChatMessage) UnmarshalJSON(data []byte) error

type ChatRequest

type ChatRequest struct {
	Domain      *string                       `json:"domain"`
	Messages    []messages.ChatMessage        `json:"messages"`
	Temperature *float64                      `json:"temperature,omitempty"`
	TopK        *int64                        `json:"top_p,omitempty"`
	MaxTokens   *int64                        `json:"max_tokens,omitempty"`
	Audit       *string                       `json:"audit,omitempty"`
	Functions   []messages.FunctionDefinition `json:"functions,omitempty"`
}

ChatRequest is a request to complete a chat completion..

type ChatResponse

type ChatResponse struct {
	//Choices []*ChatChoice `json:"choices,omitempty"`
	Role         string                 `json:"role"`
	Content      string                 `json:"content,omitempty"`
	FunctionCall *messages.FunctionCall `json:"function_call"`
	Usage        struct {
		CompletionTokens float64 `json:"completion_tokens,omitempty"`
		PromptTokens     float64 `json:"prompt_tokens,omitempty"`
		TotalTokens      float64 `json:"total_tokens,omitempty"`
	} `json:"usage,omitempty"`
}

ChatResponse is a response to a chat request.

func (*ChatResponse) GetContent

func (c *ChatResponse) GetContent() string

func (*ChatResponse) GetType

func (c *ChatResponse) GetType() messages.ChatMessageType

func (*ChatResponse) UpdateContent

func (c *ChatResponse) UpdateContent(msg string)

type ChatUsage

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

ChatUsage is the usage of a chat completion request.

type Client

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

Client is a client for the OpenAI API.

func New

func New(domain, apiKey, apiSecret, appId string, baseURL string, organization string,
	apiVersion string, embeddingsModel string,
	opts ...Option,
) (*Client, error)

New returns a new SparkAI client.

func (*Client) CreateChat

func (c *Client) CreateChat(ctx context.Context, r *ChatRequest) (messages.ChatMessage, error)

CreateChat creates chat request.

func (*Client) CreateChatWithCallBack

func (c *Client) CreateChatWithCallBack(ctx context.Context, r *ChatRequest, stream_cb func(msg messages.ChatMessage) error) (messages.ChatMessage, error)

CreateChat creates chat request.

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(ctx context.Context, r *CompletionRequest) (messages.ChatMessage, error)

CreateCompletion creates a completion.

func (*Client) HmacWithShaTobase64

func (c *Client) HmacWithShaTobase64(algorithm, data, key string) string

type Completion

type Completion struct {
	Text string `json:"text"`
}

Completion is a completion.

type CompletionRequest

type CompletionRequest struct {
	Prompt      string                        `json:"prompt"`
	Temperature float64                       `json:"temperature,omitempty"`
	MaxTokens   int64                         `json:"max_tokens,omitempty"`
	N           int                           `json:"n,omitempty"`
	TopK        int64                         `json:"top_k,omitempty"`
	Functions   []messages.FunctionDefinition `json:"functions"`
}

CompletionRequest is a request to complete a completion.

type CompletionResponse

type CompletionResponse struct {
	ID      string  `json:"id,omitempty"`
	Created float64 `json:"created,omitempty"`
	Choices []struct {
		FinishReason string      `json:"finish_reason,omitempty"`
		Index        float64     `json:"index,omitempty"`
		Logprobs     interface{} `json:"logprobs,omitempty"`
		Text         string      `json:"text,omitempty"`
	} `json:"choices,omitempty"`
	Model  string `json:"model,omitempty"`
	Object string `json:"object,omitempty"`
	Usage  struct {
		CompletionTokens float64 `json:"completion_tokens,omitempty"`
		PromptTokens     float64 `json:"prompt_tokens,omitempty"`
		TotalTokens      float64 `json:"total_tokens,omitempty"`
	} `json:"usage,omitempty"`
	FunctionCall *messages.FunctionCall `json:"function_call"`
}

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer performs a HTTP request.

type EmbeddingRequest

type EmbeddingRequest struct {
	Model string   `json:"model"`
	Input []string `json:"input"`
}

EmbeddingRequest is a request to create an embedding.

type FunctionCall

type FunctionCall struct {
	// Name is the name of the function to call.
	Name string `json:"name"`
	// Arguments is the set of arguments to pass to the function.
	Arguments string `json:"arguments"`
}

FunctionCall is a call to a function.

type FunctionCallBehavior

type FunctionCallBehavior string

FunctionCallBehavior is the behavior to use when calling functions.

const (
	// FunctionCallBehaviorUnspecified is the empty string.
	FunctionCallBehaviorUnspecified FunctionCallBehavior = ""
	// FunctionCallBehaviorNone will not call any functions.
	FunctionCallBehaviorNone FunctionCallBehavior = "none"
	// FunctionCallBehaviorAuto will call functions automatically.
	FunctionCallBehaviorAuto FunctionCallBehavior = "auto"
)

type FunctionDefinition

type FunctionDefinition struct {
	// Name is the name of the function.
	Name string `json:"name"`
	// Description is a description of the function.
	Description string `json:"description"`
	// Parameters is a list of parameters for the function.
	Parameters any `json:"parameters"`
}

FunctionDefinition is a definition of a function that can be called by the model.

type Option

type Option func(*Client) error

Option is an option for the Spark client.

type SparkErrorCode

type SparkErrorCode int

SparkErrorCode 定义错误码的类型

const (
	Success                          SparkErrorCode = 0     // 成功
	ErrorUpgradeToWs                 SparkErrorCode = 10000 // 升级为ws出现错误
	ErrorReadUserMessageFailed       SparkErrorCode = 10001 // 通过ws读取用户的消息出错
	ErrorSendUserMessageFailed       SparkErrorCode = 10002 // 通过ws向用户发送消息出错
	ErrorUserMessageFormatError      SparkErrorCode = 10003 // 用户的消息格式有错误
	ErrorUserDataSchemaError         SparkErrorCode = 10004 // 用户数据的schema错误
	ErrorUserParameterValueError     SparkErrorCode = 10005 // 用户参数值有错误
	ErrorUserConcurrencyError        SparkErrorCode = 10006 // 用户并发错误:当前用户已连接,同一用户不能多处同时连接。
	ErrorUserTrafficLimitError       SparkErrorCode = 10007 // 用户流量受限:服务正在处理用户当前的问题,需等待处理完成后再发送新的请求。
	ErrorServiceCapacityInsufficient SparkErrorCode = 10008 // 服务容量不足,联系工作人员
	ErrorEngineConnectionFailed      SparkErrorCode = 10009 // 和引擎建立连接失败
	ErrorReceiveEngineDataError      SparkErrorCode = 10010 // 接收引擎数据的错误
	ErrorSendDataToEngineError       SparkErrorCode = 10011 // 发送数据给引擎的错误
	ErrorEngineInternalError         SparkErrorCode = 10012 // 引擎内部错误
	ErrorContentReviewFailed         SparkErrorCode = 10013 // 输入内容审核不通过,涉嫌违规,请重新调整输入内容
	ErrorSensitiveOutputError        SparkErrorCode = 10014 // 输出内容涉及敏感信息,审核不通过,后续结果无法展示给用户
	ErrorAppIDBlacklist              SparkErrorCode = 10015 // appid在黑名单中
	ErrorAppIDAuthorizationError     SparkErrorCode = 10016 // appid授权类的错误。比如:未开通此功能,未开通对应版本,token不足,并发超过授权等等
	ErrorClearHistoryFailed          SparkErrorCode = 10017 // 清除历史失败
	ErrorSessionContentViolation     SparkErrorCode = 10019 // 表示本次会话内容有涉及违规信息的倾向;建议开发者收到此错误码后给用户一个输入涉及违规的提示
	ErrorServiceBusy                 SparkErrorCode = 10110 // 服务忙,请稍后再试
	ErrorEngineParameterException    SparkErrorCode = 10163 // 请求引擎的参数异常 引擎的schema 检查不通过
	ErrorEngineNetworkException      SparkErrorCode = 10222 // 引擎网络异常
	ErrorTokenLimitExceeded          SparkErrorCode = 10907 // token数量超过上限。对话历史+问题的字数太多,需要精简输入
	ErrorAuthFunctionLimit           SparkErrorCode = 11200 // 授权错误:该appId没有相关功能的授权 或者 业务量超过限制
	ErrorAuthDailyLimit              SparkErrorCode = 11201 // 授权错误:日流控超限。超过当日最大访问量的限制
	ErrorAuthRateLimit               SparkErrorCode = 11202 // 授权错误:秒级流控超限。秒级并发超过授权路数限制
	ErrorAuthConcurrencyLimit        SparkErrorCode = 11203 // 授权错误:并发流控超限。并发路数超过授权路数限制
)

预定义的错误码及其对应的错误信息

type StreamedChatResponsePayload

type StreamedChatResponsePayload struct {
	ID      string  `json:"id,omitempty"`
	Created float64 `json:"created,omitempty"`
	Model   string  `json:"model,omitempty"`
	Object  string  `json:"object,omitempty"`
	Choices []struct {
		Index float64 `json:"index,omitempty"`
		Delta struct {
			Role         string        `json:"role,omitempty"`
			Content      string        `json:"content,omitempty"`
			FunctionCall *FunctionCall `json:"function_call,omitempty"`
		} `json:"delta,omitempty"`
		FinishReason string `json:"finish_reason,omitempty"`
	} `json:"choices,omitempty"`
}

StreamedChatResponsePayload is a chunk from the stream.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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