#github.com/xulang-org/packages/gateway
Xu 0.16.0 API
导入方式: gateway #github.com/xulang-org/packages/gateway
公开 API: 4 函数, 27 方法, 5 类型, 0 常量, 0 方法集
扩展包 `gateway #github.com/xulang-org/packages/gateway`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
struct ProviderPassthrough
pub struct ProviderPassthrough {
pub anthropic_cache_system bool
pub anthropic_cache_control string
pub anthropic_thinking_budget int
pub anthropic_extra_body_json string
pub gemini_safety_settings_json string
pub gemini_thinking_budget int
pub gemini_extra_body_json string
pub openai_extra_body_json string
}
用途
承载 anthropic_cache_system、anthropic_cache_control、anthropic_thinking_budget、anthropic_extra_body_json、gemini_safety_settings_json 等属性的结构体。
公开字段
anthropic_cache_system bool- 字段(bool)
anthropic_cache_control string- 字段(string)
anthropic_thinking_budget int- 字段(int)
anthropic_extra_body_json string- 字段(string)
gemini_safety_settings_json string- 字段(string)
gemini_thinking_budget int- 字段(int)
gemini_extra_body_json string- 字段(string)
openai_extra_body_json string- 字段(string)
empty_passthrough
pub fn empty_passthrough() ProviderPassthrough
用途
执行 empty passthrough 操作。
参数
无需传入参数。
返回值
ProviderPassthrough — 该类型的值
struct ProviderRoute
pub struct ProviderRoute {
pub name string
pub provider string
pub client openai.Client
pub model string
pub input_cost_per_1m f64
pub output_cost_per_1m f64
pub enabled bool
pub weight int
pub consecutive_failures int
pub circuit_open_until_ms int
pub passthrough ProviderPassthrough
}
用途
承载 name、provider、client、model、input_cost_per_1m 等属性的结构体。
公开字段
name string- 名字
provider string- 字段(string)
client openai.Client- 字段(openai.Client)
model string- 字段(string)
input_cost_per_1m f64- 字段(f64)
output_cost_per_1m f64- 字段(f64)
enabled bool- 字段(bool)
weight int- 字段(int)
consecutive_failures int- 字段(int)
circuit_open_until_ms int- 字段(int)
passthrough ProviderPassthrough- 字段(ProviderPassthrough)
struct SemanticCacheEntry
pub struct SemanticCacheEntry {
pub key_text string
pub embedding []f32
pub response_content string
pub model string
}
用途
承载 key_text、embedding、response_content、model 等属性的结构体。
公开字段
key_text string- 字段(string)
embedding []f32- 字段([]f32)
response_content string- 字段(string)
model string- 字段(string)
struct GatewayStats
pub struct GatewayStats {
pub total_requests int
pub cache_hits int
pub fallbacks int
pub failed_requests int
pub total_prompt_tokens int
pub total_completion_tokens int
pub estimated_cost_usd f64
}
用途
承载 total_requests、cache_hits、fallbacks、failed_requests、total_prompt_tokens 等属性的结构体。
公开字段
total_requests int- 字段(int)
cache_hits int- 字段(int)
fallbacks int- 字段(int)
failed_requests int- 字段(int)
total_prompt_tokens int- 字段(int)
total_completion_tokens int- 字段(int)
estimated_cost_usd f64- 字段(f64)
struct Gateway
用途
Gateway 上下文或状态结构体。
公开字段
此类型没有公开字段。
new_gateway
pub fn new_gateway() Gateway
用途
构造 gateway。
参数
无需传入参数。
返回值
Gateway — 该类型的值
($&Gateway) set_redis_pool
pub fn (g $&Gateway) set_redis_pool(pool redis.Pool, prefix string)
用途
$&Gateway 上的操作:设置 redis pool。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
pool redis.Pool- pool(redis.Pool)
prefix string- 前缀(string)
返回值
不返回值。
($&Gateway) set_redis_store
pub fn (g $&Gateway) set_redis_store(addr string, prefix string)
用途
$&Gateway 上的操作:设置 redis store。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
addr string- 地址(string)
prefix string- 前缀(string)
返回值
不返回值。
($&Gateway) set_redis_store_auth
pub fn (g $&Gateway) set_redis_store_auth(addr string, password string, prefix string)
用途
$&Gateway 上的操作:设置 redis store auth。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
addr string- 地址(string)
password string- password(string)
prefix string- 前缀(string)
返回值
不返回值。
($&Gateway) set_redis_url
pub fn (g $&Gateway) set_redis_url(raw_url string, prefix string)
用途
$&Gateway 上的操作:设置 redis url。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
raw_url string- raw_url(string)
prefix string- 前缀(string)
返回值
不返回值。
($&Gateway) set_cache_ttl
pub fn (g $&Gateway) set_cache_ttl(seconds int)
用途
$&Gateway 上的操作:设置 cache ttl。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
seconds int- seconds(int)
返回值
不返回值。
(&Gateway) is_redis_enabled
pub fn (g &Gateway) is_redis_enabled() bool
用途
&Gateway 上的操作:判断是否 redis enabled。
参数
g &Gateway- 本次调用使用的
Gateway 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
($&Gateway) close_redis
pub fn (g $&Gateway) close_redis()
用途
$&Gateway 上的操作:执行 close redis 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Gateway) add_route
pub fn (g $&Gateway) add_route(name string, client openai.Client, model string, input_cost_per_1m f64, output_cost_per_1m f64)
用途
$&Gateway 上的操作:执行 add route 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string- 名字(string)
client openai.Client- client(openai.Client)
model string- model(string)
input_cost_per_1m f64- input_cost_per_1m(f64)
output_cost_per_1m f64- output_cost_per_1m(f64)
返回值
不返回值。
($&Gateway) add_weighted_route
pub fn (g $&Gateway) add_weighted_route(name string, client openai.Client, model string, input_cost_per_1m f64, output_cost_per_1m f64, weight int)
用途
$&Gateway 上的操作:执行 add weighted route 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string- 名字(string)
client openai.Client- client(openai.Client)
model string- model(string)
input_cost_per_1m f64- input_cost_per_1m(f64)
output_cost_per_1m f64- output_cost_per_1m(f64)
weight int- weight(int)
返回值
不返回值。
($&Gateway) add_anthropic_route
pub fn (g $&Gateway) add_anthropic_route(name string, client anthropic.Client, model string, input_cost_per_1m f64, output_cost_per_1m f64, weight int)
用途
$&Gateway 上的操作:执行 add anthropic route 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string- 名字(string)
client anthropic.Client- client(anthropic.Client)
model string- model(string)
input_cost_per_1m f64- input_cost_per_1m(f64)
output_cost_per_1m f64- output_cost_per_1m(f64)
weight int- weight(int)
返回值
不返回值。
($&Gateway) add_gemini_route
pub fn (g $&Gateway) add_gemini_route(name string, client gemini.Client, model string, input_cost_per_1m f64, output_cost_per_1m f64, weight int)
用途
$&Gateway 上的操作:执行 add gemini route 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string- 名字(string)
client gemini.Client- client(gemini.Client)
model string- model(string)
input_cost_per_1m f64- input_cost_per_1m(f64)
output_cost_per_1m f64- output_cost_per_1m(f64)
weight int- weight(int)
返回值
不返回值。
($&Gateway) set_route_passthrough
pub fn (g $&Gateway) set_route_passthrough(name string, pt ProviderPassthrough) bool
用途
$&Gateway 上的操作:设置 route passthrough。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
name string- 名字(string)
pt ProviderPassthrough- pt(ProviderPassthrough)
返回值
bool — 是否成立
($&Gateway) set_circuit_breaker
pub fn (g $&Gateway) set_circuit_breaker(fail_threshold int, cooldown_ms int)
用途
$&Gateway 上的操作:设置 circuit breaker。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
fail_threshold int- fail_threshold(int)
cooldown_ms int- cooldown_ms(int)
返回值
不返回值。
($&Gateway) reset_circuits
pub fn (g $&Gateway) reset_circuits()
用途
$&Gateway 上的操作:执行 reset circuits 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
($&Gateway) set_rate_limit
pub fn (g $&Gateway) set_rate_limit(requests_per_min int)
用途
$&Gateway 上的操作:设置 rate limit。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
requests_per_min int- requests_per_min(int)
返回值
不返回值。
($&Gateway) enable_semantic_cache
pub fn (g $&Gateway) enable_semantic_cache(threshold f64, max_entries int)
用途
$&Gateway 上的操作:执行 enable semantic cache 操作。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
threshold f64- threshold(f64)
max_entries int- max_entries(int)
返回值
不返回值。
(&Gateway) estimate_cost_usd
pub fn (g &Gateway) estimate_cost_usd(route_idx int, prompt_tokens int, completion_tokens int) f64
用途
&Gateway 上的操作:执行 estimate cost usd 操作。
参数
g &Gateway- 本次调用使用的
Gateway 实例;以只读借用传入,调用方仍持有实例。
route_idx int- route_idx(int)
prompt_tokens int- prompt_tokens(int)
completion_tokens int- completion_tokens(int)
返回值
f64 — 该类型的值
openai_req_to_anthropic_with
pub fn openai_req_to_anthropic_with(req &openai.ChatRequest, model string, pt &ProviderPassthrough) anthropic.MessagesRequest
用途
执行 openai req to anthropic with 操作。
参数
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
model string- model(string)
pt &ProviderPassthrough- pt(只读借用,调用方仍持有原值;类型 &ProviderPassthrough)
返回值
anthropic.MessagesRequest — 该类型的值
openai_req_to_gemini_with
pub fn openai_req_to_gemini_with(req &openai.ChatRequest, model string, pt &ProviderPassthrough) gemini.GenerateRequest
用途
执行 openai req to gemini with 操作。
参数
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
model string- model(string)
pt &ProviderPassthrough- pt(只读借用,调用方仍持有原值;类型 &ProviderPassthrough)
返回值
gemini.GenerateRequest — 该类型的值
($&Gateway) chat_with_passthrough
pub fn (g $&Gateway) chat_with_passthrough(req &openai.ChatRequest, pt &ProviderPassthrough) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat with passthrough 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
pt &ProviderPassthrough- pt(只读借用,调用方仍持有原值;类型 &ProviderPassthrough)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_with_fallback
pub fn (g $&Gateway) chat_with_fallback(req &openai.ChatRequest) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat with fallback 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_weighted
pub fn (g $&Gateway) chat_weighted(req &openai.ChatRequest) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat weighted 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_stream_with_fallback
pub fn (g $&Gateway) chat_stream_with_fallback(req &openai.ChatRequest, on_delta fn(openai.StreamDelta) bool) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat stream with fallback 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
on_delta fn(openai.StreamDelta) bool- on_delta(函数值;类型 fn(openai.StreamDelta) bool)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_stream_chan_with_fallback
pub fn (g $&Gateway) chat_stream_chan_with_fallback(req &openai.ChatRequest, ch chan openai.StreamDelta) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat stream chan with fallback 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
ch chan openai.StreamDelta- 字符码(通道;类型 chan openai.StreamDelta)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_stream_weighted
pub fn (g $&Gateway) chat_stream_weighted(req &openai.ChatRequest, on_delta fn(openai.StreamDelta) bool) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat stream weighted 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
on_delta fn(openai.StreamDelta) bool- on_delta(函数值;类型 fn(openai.StreamDelta) bool)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_stream_chan_weighted
pub fn (g $&Gateway) chat_stream_chan_weighted(req &openai.ChatRequest, ch chan openai.StreamDelta) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat stream chan weighted 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
ch chan openai.StreamDelta- 字符码(通道;类型 chan openai.StreamDelta)
返回值
openai.ChatResponse — 该类型的值
($&Gateway) chat_cached
pub fn (g $&Gateway) chat_cached(req &openai.ChatRequest, query_emb &[]f32) openai.ChatResponse
用途
$&Gateway 上的操作:执行 chat cached 操作。失败时抛出错误,调用方需处理或继续传播。
参数
g $&Gateway- 本次调用使用的
Gateway 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
req &openai.ChatRequest- req(只读借用,调用方仍持有原值;类型 &openai.ChatRequest)
query_emb &[]f32- query_emb(只读借用,调用方仍持有原值;类型 &[]f32)
返回值
openai.ChatResponse — 该类型的值
(&Gateway) get_stats
pub fn (g &Gateway) get_stats() GatewayStats
用途
&Gateway 上的操作:读取 stats。
参数
g &Gateway- 本次调用使用的
Gateway 实例;以只读借用传入,调用方仍持有实例。
返回值
GatewayStats — 该类型的值
(&Gateway) format_metrics_prometheus
pub fn (g &Gateway) format_metrics_prometheus() string
用途
&Gateway 上的操作:格式化为 metrics prometheus。
参数
g &Gateway- 本次调用使用的
Gateway 实例;以只读借用传入,调用方仍持有实例。
返回值
string — 结果字符串