glm4

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: MulanPSL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

智谱 GLM-4 系列模型 SDK

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncResultResponse

type AsyncResultResponse struct {
	ID         string                 `json:"id"`          // 智谱 AI 开放平台生成的任务序号,调用请求结果接口时请使用此序号
	RequestID  string                 `json:"request_id"`  // 请求发起时客户端提交的任务号或平台生成的任务号
	Model      public.Model           `json:"model"`       // 模型名称
	TaskStatus public.AsyncTaskStatus `json:"task_status"` // 请求的处理状态:PROCESSING(处理中),SUCCESS(成功),FAIL(失败)。此状态必须查询才能确定结果
	Choices    []*NormalChoice        `json:"choices"`     // 当前对话的模型输出内容
	Usage      *Usage                 `json:"usage"`       // 模型调用结束时返回的 token 使用统计
}

异步请求结果查询响应对象

type AsyncSendResponse

type AsyncSendResponse struct {
	ID         string                 `json:"id"`          // 智普 AI 开放平台生成的任务序号,调用请求结果接口时请使用此序号
	Model      public.Model           `json:"model"`       // 模型名称
	RequestID  string                 `json:"request_id"`  // 请求发起时客户端提交的任务号或平台生成的任务号
	TaskStatus public.AsyncTaskStatus `json:"task_status"` // 请求的处理状态:PROCESSING(处理中),SUCCESS(成功),FAIL(失败)。此状态必须查询才能确定结果
}

异步请求响应对象

type Choice

type Choice interface {
	NormalChoice | StreamChoice
}

模型输出内容类型(普通调用 / 流式调用)

type Client

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

GLM-4 Client 客户端

func NewClient

func NewClient(timeOutSeconds int, key string, tokenExpireSeconds int) (*Client, error)

新建 GLM-4 访问客户端

@param key API Key
@param tokenExpireSeconds Token 有效秒数,当值小于等于 0 时,表示使用 API Key 鉴权,而非 Token 鉴权
@param timeOutSeconds 超时秒数
@return Client 客户端实例
@return error 错误信息

func (*Client) AsyncCall

func (r *Client) AsyncCall(reqBody *Request) (*AsyncSendResponse, error)

发送异步请求

异步请求不支持流式响应, Stream 标识位无效
@param reqBody 请求体
@return *AsyncSendResponse 返回请求结果
@return error 错误信息

func (*Client) AsyncResultCall

func (r *Client) AsyncResultCall(id string) (*AsyncResultResponse, error)

获取异步请求结果

异步请求不支持流式响应, Stream 标识位无效
@param id 请求 ID
@return *AsyncResultResponse 返回请求结果
@return error 错误信息

func (*Client) StreamCall

func (r *Client) StreamCall(reqBody *Request) ([]*Response[StreamChoice], error)

发送流式请求

SSE(Server-Sent Events) 协议实现
@param reqBody 请求体
@return []*Response[StreamChoice] 返回数据
@return error 错误信息

func (*Client) SyncCall

func (r *Client) SyncCall(reqBody *Request) (*Response[NormalChoice], error)

发送同步请求

RestFul 协议实现
@param reqBody 请求体
@return *Response[NormalChoice] 返回数据
@return error 错误信息

type Delta

type Delta struct {
	Role      public.MessageRole `json:"role"`       // 消息的角色信息
	Content   string             `json:"content"`    // 消息内容
	ToolCalls []*ToolCall        `json:"tool_calls"` // 模型生成的应调用的函数名称和参数
}

模型增量返回的文本信息

type FunctionParamProp

type FunctionParamProp struct {
	Description string                       `json:"description"` // 属性描述
	Type        public.FunctionParamPropType `json:"type"`        // 属性类型
}

函数工具参数属性对象

func (*FunctionParamProp) Valid

func (r *FunctionParamProp) Valid() error

函数工具参数属性有效性验证

type FunctionTool

type FunctionTool struct {
	Name        string               `json:"name"`        // 函数名称,只能包含 a-z、A-Z、0-9、下划线和连字符。最大长度限制为 64
	Description string               `json:"description"` // 用于描述函数的能力
	Parameters  []*FunctionToolParam `json:"parameters"`  // 参数字段
}

函数工具

func (*FunctionTool) Valid

func (r *FunctionTool) Valid() error

函数工具有效性验证

type FunctionToolParam

type FunctionToolParam struct {
	Type       public.FunctionParamType      `json:"type"`       // 参数类型
	Properties map[string]*FunctionParamProp `json:"properties"` // 参数属性列表
	Required   []string                      `json:"required"`   // 必填属性
}

函数工具参数对象

func (*FunctionToolParam) Valid

func (r *FunctionToolParam) Valid() error

函数工具参数有效性验证

type Message

type Message struct {
	Role public.MessageRole `json:"role"` // 消息的角色信息
	// 消息内容
	//   'Content' 与 'ToolCalls' 二选一
	//   当 'ToolCalls' 被设置后 'Content' 必须为空
	Content string `json:"content"`
	// 工具调用的记录
	//   仅当消息类型为 '工具消息' 时须设置,其它情况须设置为空
	ToolCallID string `json:"tool_call_id"`
	// 模型产生的工具调用消息
	//   'Content' 与 'ToolCalls' 二选一
	//   当 'Content' 被设置后 'ToolCalls' 必须为空
	ToolCalls []*ToolCall `json:"tool_calls"`
}

模型输入的消息类型

func (*Message) Valid

func (r *Message) Valid() error

模型输入的消息类型有效性验证

type MessageResponse

type MessageResponse struct {
	Role    public.MessageRole `json:"role"`    // 消息的角色信息
	Content string             `json:"content"` // 消息内容
}

模型返回的文本消息

type NormalChoice

type NormalChoice struct {
	Index uint32 `json:"index"` // 结果索引
	// 模型推理终止的原因。
	//   'stop' 表示自然结束或触发stop词;
	//   'tool_calls' 表示模型命中函数;
	//   'length' 表示达到 token 长度限制;
	//   'sensitive' 表示内容被安全审核接口拦截(用户应判断并决定是否撤回公开内容);
	//   'network_error' 表示模型推理异常;
	FinishReason public.FinishReason `json:"finish_reason"`
	Message      *MessageResponse    `json:"message"`    // 模型返回的文本消息
	ToolCalls    []*ToolCall         `json:"tool_calls"` // 模型生成的应调用的函数名称和参数
}

普通调用模型输出内容

type Request

type Request struct {
	Model     public.Model `json:"model"`                // 要调用的模型编码
	Messages  []*Message   `json:"messages"`             // 调用语言模型时,当前对话消息列表作为模型的提示输入
	RequestID string       `json:"request_id,omitempty"` // 由用户端传递,需要唯一;用于区分每次请求的唯一标识符。如果用户端未提供,平台将默认生成
	DoSample  bool         `json:"do_sample,omitempty"`  // 当 do_sample 为 true 时,启用采样策略;当 do_sample 为 false 时,温度和 top_p 等采样策略参数将不生效。默认值为 true
	// 是否启用流式响应
	//   默认值为 false,表示模型在生成所有内容后一次性返回所有内容
	//   如果设置为 true,模型将通过标准 Event Stream 逐块返回生成的内容。当 Event Stream 结束时,将返回一个 data: [DONE] 消息
	Stream         bool            `json:"stream,omitempty"`
	Temperature    float32         `json:"temperature,omitempty"`     // 采样温度,控制输出的随机性,必须为正数取值范围是:[0.0, 1.0],默认值为 0.95
	TopP           float32         `json:"top_p,omitempty"`           // 温度取样的另一种方法,取值范围是:[0.0, 1.0],默认值为 0.7
	MaxTokens      uint32          `json:"max_tokens,omitempty"`      // 模型输出的最大token数,最大输出为4095,默认值为1024
	ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // 指定模型输出格式,默认为 text
	Stop           []string        `json:"stop,omitempty"`            // 模型遇到 stop 指定的字符时会停止生成。目前仅支持单个 stop 词,格式为 ["stop_word1"]
	Tools          []*Tool         `json:"tools,omitempty"`           // 模型可以调用的工具
	// 终端用户的唯一 ID
	//   帮助平台对终端用户的非法活动、生成非法不当信息或其他滥用行为进行干预;
	//   ID 长度要求:至少 6 个字符,最多 128 个字符
	UserID string `json:"user_id,omitempty"`
}

请求对象

func (*Request) Valid

func (r *Request) Valid() error

请求对象有效性验证

type Response

type Response[T Choice] struct {
	ID        string               `json:"id"`         // 智谱 AI 开放平台生成的任务序号,调用请求结果接口时请使用此序号
	Created   uint64               `json:"created"`    // 请求创建时间,为 Unix 时间戳,单位为秒
	Model     public.Model         `json:"model"`      // 模型名称
	Choices   []*T                 `json:"choices"`    // 当前对话的模型输出内容
	Usage     *Usage               `json:"usage"`      // 模型调用结束时返回的 token 使用统计
	WebSearch []*WebSearchResponse `json:"web_search"` // 返回与网页搜索相关的信息
}

同步响应对象

type ResponseFormat

type ResponseFormat struct {
	Type public.ResponseFormatType `json:"type"` // 模型输出格式
}

模型输出格式

func (*ResponseFormat) Valid

func (r *ResponseFormat) Valid() error

模型输出格式有效性验证

type RetrievalTool

type RetrievalTool struct {
	KnowledgeID    string `json:"knowledge_id"`              // 知识库 ID
	PromptTemplate string `json:"prompt_template,omitempty"` // 知识库模板
}

知识库检索工具

func (*RetrievalTool) Valid

func (r *RetrievalTool) Valid() error

知识库检索工具有效性验证

type StreamChoice

type StreamChoice struct {
	Index uint32 `json:"index"` // 结果索引
	// 模型推理终止的原因。
	//   'stop' 表示自然结束或触发stop词;
	//   'tool_calls' 表示模型命中函数;
	//   'length' 表示达到 token 长度限制;
	//   'sensitive' 表示内容被安全审核接口拦截(用户应判断并决定是否撤回公开内容);
	//   'network_error' 表示模型推理异常;
	FinishReason public.FinishReason `json:"finish_reason"`
	Delta        *Delta              `json:"delta"` // 模型增量返回的文本信息
}

流式调用模型输出内容

type Tool

type Tool struct {
	// 工具类型
	//   类型目前支持 '函数工具' '知识库检索工具' '网页检索工具' 三种类型;
	//   类型只能设置一种,且需要与对应的工具调用匹配,不匹配的工具调用必须设置为空;
	Type      public.ToolType `json:"type"`
	Function  *FunctionTool   `json:"function,omitempty"`   // 函数工具调用
	Retrieval *RetrievalTool  `json:"retrieval,omitempty"`  // 知识库检索工具调用
	WebSearch *WebSearchTool  `json:"web_search,omitempty"` // 网页检索工具调用
}

模型工具

func (*Tool) Valid

func (r *Tool) Valid() error

模型工具有效性验证

type ToolCall

type ToolCall struct {
	ID       string          `json:"id"`                 // 工具 ID
	Type     public.ToolType `json:"type"`               // 工具类型
	Function *ToolCallFunc   `json:"function,omitempty"` // 模型生成的函数调用
}

模型产生的工具调用

type ToolCallFunc

type ToolCallFunc struct {
	Name      string `json:"name"`      // 模型生成的函数名称
	Arguments string `json:"arguments"` // 模型生成的函数调用参数的 JSON 格式
}

模型生成的函数对象

type Usage

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

模型调用结束时返回的 token 使用统计

type WebSearchResponse

type WebSearchResponse struct {
	Icon    string `json:"icon"`    // 来源网站的图标
	Title   string `json:"title"`   // 搜索结果的标题
	Link    string `json:"link"`    // 搜索结果的网页链接
	Media   string `json:"media"`   // 搜索结果网页的媒体来源名称
	Content string `json:"content"` // 搜索结果网页引用的文本内容
}

返回与网页搜索相关的信息

type WebSearchTool

type WebSearchTool struct {
	Enable       bool   `json:"enable,omitempty"`        // 网络搜索功能,默认为 false
	SearchQuery  string `json:"search_query,omitempty"`  // 强制自定义搜索键内容
	SearchResult bool   `json:"search_result,omitempty"` // 获取网页搜索来源的详细信息,默认为 true
}

网页检索工具

Jump to

Keyboard shortcuts

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