baidu

package
v0.0.0-...-2a14085 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: Apache-2.0 Imports: 11 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_FUNCATION 函数
	CHAT_MESSAGE_ROLE_FUNCATION string = "function"
)
View Source
const (
	// MODEL_FOR_35_8K
	MODEL_FOR_35_8K = "completions"

	// MODEL_FOR_35_8K_0205
	MODEL_FOR_35_8K_0205 = "ernie-3.5-8k-0205"

	// MODEL_FOR_35_8K_1222
	MODEL_FOR_35_8K_1222 = "ernie-3.5-8k-1222"

	// MODEL_FOR_35_4k_0205
	MODEL_FOR_35_4k_0205 = "ernie-3.5-4k-0205"

	// MODEL_FOR_35_8k_PREEMPTIBLE
	MODEL_FOR_35_8k_PREEMPTIBLE = "completions_preemptible"

	// MODEL_FOR_35_8K
	MODEL_FOR_35_8K_PREVIEW = "ernie-3.5-8k-preview"

	// MODEL_FOR_40_8K
	MODEL_FOR_40_8K = "completions_pro"

	// MODEL_FOR_40_8K_PREEMPTIBLE
	MODEL_FOR_40_8K_PREEMPTIBLE = "completions_pro_preemptible"

	// MODEL_FOR_40_8K_PREVIEW
	MODEL_FOR_40_8K_PREVIEW = "ernie-4.0-8k-preview"
)

Variables

This section is empty.

Functions

func GetAccessToken

func GetAccessToken() (string, int64)

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

func NewClient

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

NewClient 初始化百度请求客户端

func RefreshAccessToken

func RefreshAccessToken() error

RefreshAccessToken 刷新access token

func SetDebug

func SetDebug(debug bool)

SetDebug debug开关

func SetMaxEmptyMessageCount

func SetMaxEmptyMessageCount(count int)

SetMaxEmptyMessageCount 最大空消息数量

Types

type ChatRequest

type ChatRequest struct {
	// 聊天上下文信息
	Messages []MessageInfo `json:"messages"`
	// 随机性
	Temperature float32 `json:"temperature,omitempty"`
	// 多样性
	TopP float32 `json:"top_p,omitempty"`
	// 惩罚值
	PenaltyScore float32 `json:"penalty_score,omitempty"`

	// 流式输出[不用填]
	Stream bool `json:"stream,omitempty"`

	// 人设
	System string `json:"system,omitempty"`
	// 停止生成标识
	Stop []string `json:"stop,omitempty"`

	// 是否强制关闭实时搜索功能
	DisableSearch bool `json:"disableSearch,omitempty"`
	// 是否开启上角标返回
	EnableCitation bool `json:"enable_citation,omitempty"`
	// 是否开启搜索溯源信息
	EnableTrace bool `json:"enable_trace,omitempty"`

	// 指定最大输出token数
	MaxOutputTokens int64 `json:"max_output_tokens,omitempty"`
	// 表示最终用户唯一标识
	UserId string `json:"user_id"`
}

type ChatResponse

type ChatResponse struct {
	// 本轮对话ID
	Id string `json:"id"`
	// 回包类型 chat.completion:多轮对话返回
	Object string `json:"object"`
	// 时间戳
	Created int64 `json:"created"`
	// 表示当前子句的序号。只有在流式接口模式下会返回该字段
	SentenceID int64 `json:"sentence_id"`
	// 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段
	IsEnd bool `json:"is_end"`
	// 当前生成的结果是否被截断
	IsTruncated bool `json:"is_truncated"`
	/**
	输出内容标识,说明:
	· normal:输出内容完全由大模型生成,未触发截断、替换
	· stop:输出结果命中入参stop中指定的字段后被截断
	· length:达到了最大的token数,根据EB返回结果is_truncated来截断
	· content_filter:输出内容被截断、兜底、替换为**等
	· function_call:调用了funtion call功能
	*/
	FinishReason string
	// 搜索结果
	SearchInfo *SearchInfo
	// 生成的结果
	Result string `json:"result"`
	// 表示用户输入是否存在安全风险,是否关闭当前会话,清理历史会话信息
	NeedClearHistory bool `json:"need_clear_history"`
	/**
	说明:
	· 0:正常返回
	· 其他:非正常
	*/
	Flag int `json:"flag"`
	// 当need_clear_history为true时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round=-1
	BanRound int `json:"ban_round"`
	// 消耗说明
	Usage *Usage `json:"usage"`
}

func Chat

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

Chat 对话接口

type Client

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

Client 百度API请求客户端

func (*Client) Chat

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

Chat 对话方法

func (*Client) ChatStream

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

ChatStream 流式对话方法

type MessageInfo

type MessageInfo struct {
	/**
	角色
	user: 表示用户
	assistant: 表示对话助手
	function: 表示函数
	*/
	Role string `json:"role"`
	// 对话内容
	Content string `json:"content"`
}

MessageInfo 对话信息

type PluginUsage

type PluginUsage struct {
	// plugin名称,chatFile:chat file插件消耗的tokens
	Name string `json:"name"`
	// 解析文档tokens
	ParseTokens int64 `json:"parse_tokens"`
	// 摘要文档tokens
	AbstractTokens int64 `json:"abstract_tokens"`
	// 检索文档tokens
	SearchTokens int64 `json:"search_tokens"`
	// 总tokens
	TotalTokens int64 `json:"total_tokens"`
}

PluginUsage 插件消耗说明

type SearchInfo

type SearchInfo struct {
	// 序号
	Index int64 `json:"index"`
	// 搜索结果URL
	Url string `json:"url"`
	// 搜索结果标题
	Title string `json:"title"`
}

SearchInfo 搜索结果信息

type StreamReader

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

func ChatStream

func ChatStream(model string, 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 Usage

type Usage struct {
	// 问题tokens数
	PromptTokens int64 `json:"prompt_tokens"`
	// 回答tokens数
	CompletionTokens int64 `json:"completion_tokens"`
	// tokens总数
	TotalTokens int64 `json:"total_tokens"`
}

Usage token 消耗说明

Jump to

Keyboard shortcuts

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