XU XUv0.16.0

#github.com/xulang-org/packages/gemini

导入方式: gemini #github.com/xulang-org/packages/gemini

公开 API: 11 函数, 8 方法, 9 类型, 2 常量, 0 方法集

扩展包 `gemini #github.com/xulang-org/packages/gemini`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

const DEFAULT_TIMEOUT_NS

XuAPI
pub const DEFAULT_TIMEOUT_NS = 60000000000

用途

常量 DEFAULT_TIMEOUT_NS,定义了相关的协议或算法配置参数。

const DEFAULT_BASE_URL

XuAPI
pub const DEFAULT_BASE_URL = "https:

用途

常量 DEFAULT_BASE_URL,定义了相关的协议或算法配置参数。

struct Client

XuAPI
pub struct Client {
    pub base_url string
    pub api_key string
    pub timeout_ns int
    pub max_retries int
    pub extra_headers []string
}

用途

Gemini 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

base_url string
字段(string)
api_key string
字段(string)
timeout_ns int
超时(纳秒)
max_retries int
字段(int)
extra_headers []string
字段([]string)

struct Part

XuAPI
pub struct Part {
    pub kind string
    pub text string
    pub mime_type string
    pub data string
    pub name string
    pub args_json string
    pub response_json string
    pub thought bool
}

用途

承载 kind、text、mime_type、data、name 等属性的结构体。

公开字段

kind string
类型标签
text string
文本
mime_type string
字段(string)
data string
数据
name string
名字
args_json string
字段(string)
response_json string
字段(string)
thought bool
字段(bool)

struct Content

XuAPI
pub struct Content {
    pub role string
    pub parts []Part
}

用途

承载 role、parts 等属性的结构体。

公开字段

role string
字段(string)
parts []Part
各段

struct FunctionDecl

XuAPI
pub struct FunctionDecl {
    pub name string
    pub description string
    pub parameters_json string
}

用途

承载 name、description、parameters_json 等属性的结构体。

公开字段

name string
名字
description string
字段(string)
parameters_json string
字段(string)

struct GenerateRequest

XuAPI
pub struct GenerateRequest {
    pub model string
    pub system_instruction string
    pub contents []Content
    pub tools []FunctionDecl
    pub temperature f64
    pub has_temperature bool
    pub top_p f64
    pub has_top_p bool
    pub max_output_tokens int
    pub stop_sequences []string
    pub response_mime_type string
    pub response_schema_json string
    pub thinking_budget int
    pub safety_settings_json string
    pub extra_body_json string
}

用途

承载 model、system_instruction、contents、tools、temperature 等属性的结构体。

公开字段

model string
字段(string)
system_instruction string
字段(string)
contents []Content
字段([]Content)
tools []FunctionDecl
字段([]FunctionDecl)
temperature f64
字段(f64)
has_temperature bool
字段(bool)
top_p f64
字段(f64)
has_top_p bool
字段(bool)
max_output_tokens int
字段(int)
stop_sequences []string
字段([]string)
response_mime_type string
字段(string)
response_schema_json string
字段(string)
thinking_budget int
字段(int)
safety_settings_json string
字段(string)
extra_body_json string
字段(string)

struct FunctionCall

XuAPI
pub struct FunctionCall {
    pub name string
    pub args_json string
}

用途

承载 name、args_json 等属性的结构体。

公开字段

name string
名字
args_json string
字段(string)

struct Usage

XuAPI
pub struct Usage {
    pub prompt_token_count int
    pub candidates_token_count int
    pub total_token_count int
    pub thoughts_token_count int
}

用途

承载 prompt_token_count、candidates_token_count、total_token_count、thoughts_token_count 等属性的结构体。

公开字段

prompt_token_count int
字段(int)
candidates_token_count int
字段(int)
total_token_count int
字段(int)
thoughts_token_count int
字段(int)

struct GenerateResponse

XuAPI
pub struct GenerateResponse {
    pub text string
    pub thoughts string
    pub function_calls []FunctionCall
    pub finish_reason string
    pub usage Usage
}

用途

Gemini 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

text string
文本
thoughts string
字段(string)
function_calls []FunctionCall
字段([]FunctionCall)
finish_reason string
字段(string)
usage Usage
用法说明

struct StreamChunk

XuAPI
pub struct StreamChunk {
    pub text_delta string
    pub thought_delta string
    pub function_calls []FunctionCall
    pub finish_reason string
    pub usage Usage
    pub done bool
}

用途

承载 text_delta、thought_delta、function_calls、finish_reason、usage 等属性的结构体。

公开字段

text_delta string
字段(string)
thought_delta string
字段(string)
function_calls []FunctionCall
字段([]FunctionCall)
finish_reason string
字段(string)
usage Usage
用法说明
done bool
字段(bool)

new_client

XuAPI
pub fn new_client(api_key string) Client

用途

构造 client。

参数

api_key string
api_key(string)

返回值

Client — 该类型的值

new_client_with_base

XuAPI
pub fn new_client_with_base(base_url string, api_key string) Client

用途

构造 client with base。

参数

base_url string
base_url(string)
api_key string
api_key(string)

返回值

Client — 该类型的值

($&Client) set_max_retries

XuAPI
pub fn (c $&Client) set_max_retries(n int)

用途

$&Client 上的操作:设置 max retries。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
n int
数量、长度或上限(int)

返回值

不返回值。

($&Client) add_header

XuAPI
pub fn (c $&Client) add_header(name string, value string)

用途

$&Client 上的操作:增加头。

参数

c $&Client
本次调用使用的 Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string
名字(string)
value string
value(string)

返回值

不返回值。

(&Client) clone_client

XuAPI
pub fn (c &Client) clone_client() Client

用途

&Client 上的操作:拷贝客户端。

参数

c &Client
本次调用使用的 Client 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

text_part

XuAPI
pub fn text_part(text string) Part

用途

执行 text part 操作。

参数

text string
文本(string)

返回值

Part — 该类型的值

inline_data_part

XuAPI
pub fn inline_data_part(mime_type string, data_base64 string) Part

用途

执行 inline data part 操作。

参数

mime_type string
mime_type(string)
data_base64 string
data_base64(string)

返回值

Part — 该类型的值

function_call_part

XuAPI
pub fn function_call_part(name string, args_json string) Part

用途

执行 function call part 操作。

参数

name string
名字(string)
args_json string
args_json(string)

返回值

Part — 该类型的值

function_response_part

XuAPI
pub fn function_response_part(name string, response_json string) Part

用途

执行 function response part 操作。

参数

name string
名字(string)
response_json string
response_json(string)

返回值

Part — 该类型的值

content

XuAPI
pub fn content(role string, parts []Part) Content

用途

执行 content 处理。

参数

role string
role(string)
parts []Part
各段([]Part)

返回值

Content — 该类型的值

user_text

XuAPI
pub fn user_text(text string) Content

用途

用户文本。

参数

text string
文本(string)

返回值

Content — 该类型的值

model_text

XuAPI
pub fn model_text(text string) Content

用途

执行 model text 操作。

参数

text string
文本(string)

返回值

Content — 该类型的值

function_decl

XuAPI
pub fn function_decl(name string, description string, parameters_json string) FunctionDecl

用途

执行 function decl 操作。

参数

name string
名字(string)
description string
description(string)
parameters_json string
parameters_json(string)

返回值

FunctionDecl — 该类型的值

generate_request

XuAPI
pub fn generate_request(model string, contents []Content) GenerateRequest

用途

生成请求。

参数

model string
model(string)
contents []Content
contents([]Content)

返回值

GenerateRequest — 该类型的值

(&Client) generate

XuAPI
pub fn (c &Client) generate(req &GenerateRequest) GenerateResponse

用途

&Client 上的操作:生成。失败时抛出错误,调用方需处理或继续传播。

参数

c &Client
本次调用使用的 Client 实例;以只读借用传入,调用方仍持有实例。
req &GenerateRequest
req(只读借用,调用方仍持有原值;类型 &GenerateRequest)

返回值

GenerateResponse — 该类型的值

(&Client) embed

XuAPI
pub fn (c &Client) embed(model string, text string) []f64

用途

在 &Client 上执行 embed 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c &Client
本次调用使用的 Client 实例;以只读借用传入,调用方仍持有实例。
model string
model(string)
text string
文本(string)

返回值

[]f64 — 切片结果

(&Client) batch_embed

XuAPI
pub fn (c &Client) batch_embed(model string, texts &[]string) [][]f64

用途

&Client 上的操作:执行 batch embed 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c &Client
本次调用使用的 Client 实例;以只读借用传入,调用方仍持有实例。
model string
model(string)
texts &[]string
texts(只读借用,调用方仍持有原值;类型 &[]string)

返回值

[][]f64 — 切片结果

(&Client) generate_stream

XuAPI
pub fn (c &Client) generate_stream(req &GenerateRequest, on_chunk fn(StreamChunk) bool) GenerateResponse

用途

&Client 上的操作:生成流。失败时抛出错误,调用方需处理或继续传播。

参数

c &Client
本次调用使用的 Client 实例;以只读借用传入,调用方仍持有实例。
req &GenerateRequest
req(只读借用,调用方仍持有原值;类型 &GenerateRequest)
on_chunk fn(StreamChunk) bool
on_chunk(函数值;类型 fn(StreamChunk) bool)

返回值

GenerateResponse — 该类型的值

(&Client) generate_stream_chan

XuAPI
pub fn (c &Client) generate_stream_chan(req &GenerateRequest, ch chan StreamChunk) GenerateResponse

用途

&Client 上的操作:执行 generate stream chan 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c &Client
本次调用使用的 Client 实例;以只读借用传入,调用方仍持有实例。
req &GenerateRequest
req(只读借用,调用方仍持有原值;类型 &GenerateRequest)
ch chan StreamChunk
字符码(通道;类型 chan StreamChunk)

返回值

GenerateResponse — 该类型的值