glm4alltools

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: 12 Imported by: 0

Documentation

Overview

智谱 GLM-4-AllTools 系列模型 SDK

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

GLM-4-AllTools Client 客户端

func NewClient

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

新建 GLM-4-AllTools 访问客户端

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

func (*Client) StreamCall

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

发送流式请求

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

type CodeInterpreterResponse

type CodeInterpreterResponse struct {
	Input  string                           `json:"input"`   // 生成的代码片段,需要输入给代码沙盒
	Output []*CodeInterpreterResponseOutput `json:"outputs"` // 模型生成的代码输出
}

代码解析工具返回的信息

type CodeInterpreterResponseOutput

type CodeInterpreterResponseOutput struct {
	Type string `json:"type"` // logs 表示代码执行日志,file 表示代码生成文件
	Logs string `json:"logs"` // 代码执行的日志结果
	File string `json:"file"` // 代码生成的文件地址
}

代码解析工具返回的信息(输出部分)

type CodeInterpreterTool

type CodeInterpreterTool struct {
	// 指定代码沙盒环境
	//   默认为 auto,即自动调用沙盒环境执行代码;
	//   设置 sandbox = none,不启用沙盒环境后。代码生成后返回状态 status = requires_action,需要用户提交代码执行结果;
	Sandbox public.ToolStatus `json:"sandbox,omitempty"`
	FileIDs []string          `json:"file_ids,omitempty"` // 工具可访问的文件 id 列表
}

代码解析工具

func (*CodeInterpreterTool) Valid

func (r *CodeInterpreterTool) Valid() error

代码解析工具有效性验证

type Delta

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

模型增量返回的文本信息

type DrawingTool

type DrawingTool struct{}

绘图工具

func (*DrawingTool) Valid

func (r *DrawingTool) Valid() error

绘图工具有效性验证

type DrawingToolResponse

type DrawingToolResponse struct {
	Input  string                       `json:"input"`   //生成图片的输入
	Output []*DrawingToolResponseOutput `json:"outputs"` // 生成图片的结果
}

绘图工具返回的信息

type DrawingToolResponseOutput

type DrawingToolResponseOutput struct {
	Image string `json:"image"` // 代码生成的图片地址
}

绘图工具返回的信息(输出部分)

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 只能是字符串类型;
	//   当消息类型为 '用户消息' 时,Content 只能是 []*UserContent 类型;
	Content interface{} `json:"content"`
}

模型输入的消息类型

func (*Message) Valid

func (r *Message) Valid() error

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

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
	Stream      bool         `json:"stream"`                // 仅支持流式 SSE,需要传入 True
	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
	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 struct {
	ID      string `json:"id"`      // 智谱 AI 开放平台生成的任务序号,调用请求结果接口时请使用此序号
	Created uint64 `json:"created"` // 请求创建时间,为 Unix 时间戳,单位为秒
	// 返回状态。
	//   completed 表示任务全部推理完成
	//   requires_action 表示需要用户提交工具执行的结果
	//   failed 表示任务推理过程或者工具使用遇到错误
	Status  public.ResponseStatus `json:"status"`
	Choices []*StreamChoice       `json:"choices"` // 当前对话的模型输出内容
	Usage   *Usage                `json:"usage"`   // 模型调用结束时返回的 token 使用统计
}

同步响应对象

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"`         // 函数工具
	CodeInterpreter *CodeInterpreterTool `json:"code_interpreter,omitempty"` // 代码解析工具
	DrawingTool     *DrawingTool         `json:"drawing_tool,omitempty"`     // 绘图工具
	WebBrowser      *WebBrowserTool      `json:"web_browser,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"`         // 函数工具
	CodeInterpreter *CodeInterpreterResponse `json:"code_interpreter,omitempty"` // 代码解析工具
	DrawingTool     *DrawingToolResponse     `json:"drawing_tool,omitempty"`     // 绘图工具
	WebBrowser      *WebBrowserResponse      `json:"web_browser,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 UserContent

type UserContent struct {
	Type public.MessageContentType `json:"type"`
	Text string                    `json:"text"`
}

用户输入消息类型

func (*UserContent) Valid

func (r *UserContent) Valid() error

用户消息有效性验证

type WebBrowserResponse

type WebBrowserResponse struct {
	Input   string                      `json:"input"`   // 联网搜索的输入
	Outputs []*WebBrowserResponseOutput `json:"outputs"` // 搜索结果
}

网页浏览器返回的信息

type WebBrowserResponseOutput

type WebBrowserResponseOutput struct {
	Title   string `json:"title"`   // 搜索结果的标题
	Link    string `json:"link"`    // 搜索结果的网页链接
	Content string `json:"content"` // 搜索结果网页引用的文本内容
}

网页浏览器返回的信息(输出部分)

type WebBrowserTool

type WebBrowserTool struct {
	// 默认 browser = auto 自动对搜索结果的链接和输入的网址进行网页浏览;
	//   如果仅需搜索的摘要信息,不使用浏览工具可以关闭,browser = none;
	Browser public.ToolStatus `json:"browser,omitempty"`
}

网页浏览器工具

func (*WebBrowserTool) Valid

func (r *WebBrowserTool) Valid() error

网页浏览器工具有效性验证

Jump to

Keyboard shortcuts

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