glm4v

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-4V 系列模型 SDK

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Choice

type Choice interface {
	NormalChoice | StreamChoice
}

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

type Client

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

GLM-4V Client 客户端

func NewClient

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

新建 GLM-4V 访问客户端

@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[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 ContentFilter

type ContentFilter struct {
	// 安全生效环节,包括
	//  role = assistant 模型推理,
	//  role = user 用户输入,
	//  role = history 历史上下文
	Role  public.ContentFilterRole  `json:"role"`
	Level public.ContentFilterLevel `json:"level"` // 严重程度 level 0-3,level 0表示最严重,3表示轻微
}

返回内容安全的相关信息

type Delta

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

模型增量返回的文本信息

type Message

type Message struct {
	Role public.MessageRole `json:"role"` // 消息的角色信息
	// 消息内容
	//  当消息类型为助理消息时,数据类型为 string
	//  当消息类型为用户消息时,数据类型为 []*UserContent,支持文本、图片、视频,视频和图片类型不能同时输入
	Content interface{} `json:"content"`
}

模型输入的消息类型

func (*Message) Valid

func (r *Message) Valid() error

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

type MessageContentURL

type MessageContentURL struct {
	URL string `json:"url"` // 图片或视频的 URL
}

图片或视频的 URL

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词;
	//   'length' 表示达到 token 长度限制;
	//   'sensitive' 表示内容被安全审核接口拦截(用户应判断并决定是否撤回公开内容);
	//   'network_error' 表示模型推理异常;
	FinishReason public.FinishReason `json:"finish_reason"`
	Message      *MessageResponse    `json:"message"` // 模型返回的文本消息
}

普通调用模型输出内容

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.8
	TopP        float32 `json:"top_p,omitempty"`       // 温度取样的另一种方法,取值范围是:[0.0, 1.0],默认值为 0.6
	MaxTokens   uint32  `json:"max_tokens,omitempty"`  // 模型输出的最大token数,最大输出为4095,默认值为1024
	// 终端用户的唯一 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 使用统计
	ContentFilter []*ContentFilter `json:"content_filter"` // 返回内容安全的相关信息
}

同步响应对象

type StreamChoice

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

流式调用模型输出内容

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,omitempty"` // 文本内容,type 是 text 时补充
	// 图片的 URL,type 是 image_url 时补充
	//   图片 url 或者 base64 编码
	//   图像大小上传限制为每张图像 5M 以下,且像素不超过 6000*6000
	//   支持jpg、png、jpeg格式
	//   说明: GLM-4V-Flash 不支持 base64 编码
	ImageURL *MessageContentURL `json:"image_url,omitempty"`
	// 视频的 URL,type 是 video_url 时补充
	//   仅 glm-4v-plus 支持视频输入
	//   视频理解时,video_url 参数必须在第一个
	//   视频大小仅支持20M以内,视频时长不超过 30s
	//   支持的视频类型 mp4
	VideoURL *MessageContentURL `json:"video_url,omitempty"`
}

用户内容

func (*UserContent) Valid

func (r *UserContent) Valid() error

模型输入的用户消息内容有效性验证

Jump to

Keyboard shortcuts

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