zhipu

package
v0.0.0-...-92dc6b9 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CHAT_MESSAGE_ROLE_USER 用户
	CHAT_MESSAGE_ROLE_USER string = "user"

	// CHAT_MESSAGE_ROLE_ASSISTANT 对话助手
	CHAT_MESSAGE_ROLE_ASSISTANT string = "assistant"

	// CHAT_MESSAGE_ROLE_SYSTEM 对话背景
	CHAT_MESSAGE_ROLE_SYSTEM string = "system"

	// CHAT_MESSAGE_ROLE_TOOL 工具调用
	CHAT_MESSAGE_ROLE_TOOL string = "tools"
)
View Source
const (
	// MODEL_VERSION_4
	MODEL_VERSION_4 = "glm-4"

	// MODEL_VERSION_3
	MODEL_VERSION_3 = "glm-3-turbo"
)
View Source
const (
	// TOOLS_TYPE_FOR_RETRIEVAL 知识库类型
	TOOLS_TYPE_FOR_RETRIEVAL = "retrieval"

	// TOOLS_TYPE_FOR_WEB_SEARCH 联网搜索
	TOOLS_TYPE_FOR_WEB_SEARCH = "web_search"
)

Variables

This section is empty.

Functions

func GetAuthToken

func GetAuthToken() (string, int64)

GetAuthToken 返回auth token信息,比如在相同业务系统还需要用到这个Token

func NewClient

func NewClient(apiKey string, debug ...bool) error

NewClient 初始化请求客户端

func RefreshAuthToken

func RefreshAuthToken() error

RefreshAuthToken 刷新access token

func SetAuthTokenExp

func SetAuthTokenExp(expSeconds int64)

SetAuthTokenExp 设置auth token过期时间(单位为秒)

func SetDebug

func SetDebug(debug bool)

SetDebug debug开关

func SetMaxEmptyMessageCount

func SetMaxEmptyMessageCount(count int)

SetMaxEmptyMessageCount 最大空消息数量

Types

type ChatRequest

type ChatRequest struct {
	// 所要调用的模型编码
	Model string `json:"model"`
	// 对话消息
	Messages []MessageInfo `json:"messages"`
	// 由用户端传参,需保证唯一性;用于区分每次请求的唯一标识,用户端不传时平台会默认生成。
	RequestId string `json:"request_id,omitempty"`
	// do_sample 为 true 时启用采样策略,do_sample 为 false 时采样策略 temperature、top_p 将不生效。默认值为 true。
	DoSample bool `json:"do_sample,omitempty"`
	// 是否使用流式输出
	Stream bool `json:"stream,omitempty"`
	// 随机性控制
	Temperature float32 `json:"temperature,omitempty"`
	// 核取样
	TopP float32 `json:"top_p,omitempty"`
	// 模型输出最大 tokens,最大输出为8192,默认值为1024
	MaxTokens int64 `json:"max_tokens,omitempty"`
	// 模型在遇到stop所制定的字符时将停止生成,目前仅支持单个停止词,格式为["stop_word1"]
	Stop []string `json:"stop,omitempty"`
	// 可供模型调用的工具列表,tools字段会计算 tokens ,同样受到tokens长度的限制
	Tools []ToolsInfo `json:"tools,omitempty"`
	// 终端用户的唯一ID,协助平台对终端用户的违规行为、生成违法及不良信息或其他滥用行为进行干预。ID长度要求:最少6个字符,最多128个字符。
	UserId string `json:"user_id,omitempty"`
}

ChatRequest

type ChatResponse

type ChatResponse struct {
	// 任务ID
	Id string `json:"id"`
	// 请求创建时间,是以秒为单位的 Unix 时间戳
	Created int64 `json:"created"`
	// 模型名称
	Model string `json:"model"`
	// 请求ID
	RequestId string `json:"request_id"`
	// 当前对话的模型输出内容
	Choices []Choices `json:"choices"`
	// token消耗信息
	Usage Usage `json:"usage"`
}

ChatResponse

func Chat

func Chat(chatRequest *ChatRequest) (*ChatResponse, error)

Chat 对话接口

type Choices

type Choices struct {
	// 结果下标
	Index int `json:"index"`
	/*
		模型推理终止的原因。
		stop代表推理自然结束或触发停止词。
		tool_calls 代表模型命中函数。
		length代表到达 tokens 长度上限。
		sensitive 代表模型推理内容被安全审核接口拦截。请注意,针对此类内容,请用户自行判断并决定是否撤回已公开的内容。
		network_error 代表模型推理异常。
	*/
	FinishReason string `json:"finish_reason"`
	// 模型返回的文本信息
	Message MessageInfo `json:"message"`
	// 流式输出是使用这个字段
	Delta MessageInfo `json:"delta"`
}

type Client

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

Client API请求客户端

func (*Client) Chat

func (c *Client) Chat(chatRequest *ChatRequest) (*ChatResponse, error)

Chat 对话方法

func (*Client) ChatStream

func (c *Client) ChatStream(chatRequest *ChatRequest) (*StreamReader, error)

ChatStream 流式对话方法

type MessageInfo

type MessageInfo struct {
	/*
		角色 取值为[system, user, assistant, tool]
		system: 用于设置对话背景
		user: 表示是用户的问题
		assistant: 表示AI的回复
		tool: 工具调用回复
	*/
	Role string `json:"role"`
	// 用户和AI的对话内容
	// 所有content的累计tokens需控制8192以内
	Content string `json:"content"`
}

MessageInfo 对话消息结构体

type RetrievalInfo

type RetrievalInfo struct {
	// 知识库ID
	KnowledgeId string `json:"knowledge_id"`
	// 请求模型时的知识库模板
	PromptTemplate string `json:"prompt_template,omitempty"`
}

RetrievalInfo

type StreamReader

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

func ChatStream

func ChatStream(chatRequest *ChatRequest) (*StreamReader, error)

ChatStream 流式对话接口

func (*StreamReader) Close

func (streamReader *StreamReader) Close()

Close

func (*StreamReader) IsFinish

func (streamReader *StreamReader) IsFinish() bool

IsFinish

func (*StreamReader) IsMaxEmptyLimit

func (streamReader *StreamReader) IsMaxEmptyLimit() bool

IsFinish

func (*StreamReader) Reader

func (streamReader *StreamReader) Reader() *bufio.Reader

Reader

func (*StreamReader) Receive

func (streamReader *StreamReader) Receive() ([]byte, error)

Receive

func (*StreamReader) ReceiveFormat

func (streamReader *StreamReader) ReceiveFormat() (*ChatResponse, error)

ReceiveFormat

func (*StreamReader) Response

func (streamReader *StreamReader) Response() *ghttp.Response

Response

type ToolsInfo

type ToolsInfo struct {
	/*
		工具类型
		retrieval: 知识库
		web_search: 联网搜索
	*/
	Type string `json:"type"`
	// 知识库使用
	Retrieval RetrievalInfo `json:"retrieval"`
}

ToolsInfo

type Usage

type Usage struct {
	// 用户输入的 tokens 数量
	PromptTokens int64 `json:"prompt_tokens"`
	// 模型输入的 tokens 数量
	CompletionTokens int64 `json:"completion_tokens"`
	// 总 tokens 数量
	TotalTokens int64 `json:"total_tokens"`
}

Usage

type WebSearch

type WebSearch struct {
	// 是否启用搜索,默认弃用
	Enable bool `json:"enable,omitempty"`
	// 强制搜索自定义关键内容,此时模型会根据自定义搜索关键内容返回的结果作为背景知识来回答用户发起的对话。
	SearchQuery string `json:"search_query"`
}

WebSearch

Jump to

Keyboard shortcuts

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