Skip to content

feat(models): rename published models via strip_free_suffix and aliases - #125

Closed
TiaraBasori wants to merge 7 commits into
jasonxu114514:mainfrom
TiaraBasori:feat/model-aliases
Closed

TiaraBasori wants to merge 7 commits into
jasonxu114514:mainfrom
TiaraBasori:feat/model-aliases

Conversation

@TiaraBasori

Copy link
Copy Markdown
Contributor

动机

模型 id 带着上游的营销后缀,deepseek-v4.1-flash-free 会原样出现在 /v1/models 里。想对外只叫 deepseek-v4.1-flash,目前没有支持的途径——改能力目录或计价元数据都以���游 id 为键,还会连带破坏免费档的请求整形。

改动

两个互相独立的开关,由同一趟命名流程统一应用,因此发现列表与路由不会出现分歧:

  • models.strip_free_suffix(新增,默认 false):把所有以 -free 结尾的模型去掉后缀对外提供,入参两种写法都接受。不需要维护:上游以后新增的模型会在下次刷新时自动改名。
  • models.aliases(新增,默认 {}):手工指定名称,键是公开名、值是上游 id。手动别名优先于自动名称,因此可以「全局去后缀 + 个别模型覆盖」。

改名只改名字:

  • /v1/models 用公开名列出,能力、上下文、上限、计价仍取自真实模型
  • 请求按上游 id 路由
  • chat / responses / Anthropic 的响应(流式与非流式)都回显公开名;SSE 是逐帧重写 model 字段,不是子串替换,因此不会误改 prompt 或工具参数
  • 上游 id 依然可用,加改名不会让已有客户端失效

去后缀后的名称如果与另一个可服务模型重名,该模型保留上游 id,因此 x 和 x-free 仍然是两个模型,不会被静默合并。

实现要点

  • internal/gateway/alias.go 中的 resolveModel / publicNames 是唯一的命名判定入口,请求路径和发现路径共用
  • resolveModel 只在候选 id 真的可路由时才改写成 -free 版本,避免把付费模型错误地映射到免费孪生
  • protocol.RenameStreamModel 与 protocol.RenameResponseModel 分别处理流式与非流式回显;renameStreamReader 抽出 frameRewriter 接口以便复用同一套 SSE 行处理(保留原有 \r\n 结尾与 [DONE] 原样透传)
  • System One 端点同样解析名称,但不改写 payload 结构

配置示例

{
  "models": {
    "strip_free_suffix": true,
    "aliases": { "beam-pro": "beam-free" }
  }
}

效果:/v1/models 返回 deepseek-v4.1-flash、mimo-v2.6-flash、beam-pro 等,beam-free / deepseek-v4.1-flash-free 仍可请求。

验证

本地以真实网关跑过:自动去后缀后 /v1/models 列出 9 个无 -free 的名称;非流式响应 model 回显公开名;流式 13 帧全部为公开名;用原始 -free 名称请求仍正常返回。

Clients saw the upstream's marketing suffix, so `deepseek-v4.1-flash-free`
appeared in /v1/models even when the deployment only serves the free lane.

Two independent knobs, both applied by a single naming pass so discovery and
routing cannot disagree:

- `models.strip_free_suffix` drops a trailing `-free` from every model and
  accepts both spellings on input. It needs no maintenance: a model the
  upstream adds later is renamed on the next refresh.
- `models.aliases` pins a name by hand (public name -> upstream id) and always
  wins over the automatic name, so the two compose.

Names change only: requests are routed by the upstream id, responses and SSE
frames echo the public name back, and the upstream id keeps working. A stripped
name is skipped when it would collide with another servable model, so `x` and
`x-free` stay two entries instead of silently merging.
Brings the free-tier v2 shape work from main so the alias branch stays
reviewable against the current base. Conflicts were mechanical:

- gateway.go: take upstream's shape context and v1ReadTool name, keep
  the alias resolution and the model echo on stream and response paths.
- protocol/stream.go: keep the frameRewriter interface so the model
  renamer and upstream's streamToolRenamer both satisfy the reader.
- protocol/response.go: keep RenameResponseModel alongside the expanded
  renameToolInMap documentation.
…breaker)

Pick up origin/main (19 commits since v1.4.3):

- 2.1.1 client shape: version bump, /inference/openai|anthropic family
  routes, max_completion_tokens 32000, strict:false tools, client key
  order, no HTML escaping, runtime-owned Accept/Content-Length (jasonxu114514#141-jasonxu114514#143)
- anonymous-lane abuse handling: IP-block detection, per-exit block TTL,
  circuit breaker (models.anonymous_backoff), max_anonymous_exits cap,
  deterministic 4xx stops the walk instead of replaying every exit
- placeholder key dropping, probe failure classification (jasonxu114514#111),
  WebUI Cache-Control (jasonxu114514#112), reasoning disable preserved end to end
  (jasonxu114514#150), Italian README/WebUI

Conflict resolutions:
- internal/config/config.go, README.md, README.zh-CN.md: kept both sides —
  the alias fields (aliases/strip_free_suffix) and the new
  anonymous_backoff block sit side by side in ModelsConfig and the README
  tables.

Local adaptation for this deployment's #pool exit (not upstream):
- a #pool transport dials a fresh upstream address on every attempt, so an
  IP-scoped abuse block cannot be parked on the transport: blocked() and
  markBlocked() ignore pool transports and the anonymous walk keeps
  rotating instead of freezing every exit behind one flagged address.
A 429 on the anonymous free lane is the per-IP quota verdict: the /64 the
request left from is spent for now, and every further draw from it wastes
an attempt and the client's budget. Rotating pools already label each
CONNECT with the exit address they drew (X-V6-Exit); the anonymous walk
now captures that label on the transport and, on a 429, fires a
fire-and-forget POST /ban?addr=<exit> at the pool so it stops minting
addresses from that /64. The pool owns the ban lifetime and recheck; the
gateway only reports the verdict and keeps walking to a fresh exit.

Local-only on top of the upstream merge; not for upstream (the /ban
callback protocol belongs to the v6 pool rotator deployment).
The transport-level lastExit snapshot raced: concurrent requests over one
shared #pool transport could ban an address another request was still
using. BunTransport now stores the labeled exit address (X-V6-Exit) in a
per-request context cell while dialing that attempt's tunnel, and the
anonymous walk reads exactly that cell on a 429, so each request bans
only the exit it actually left from. Verified e2e: two concurrent 429s
over one pool transport banned two distinct exits.
# Conflicts:
#	README.zh-CN.md
#	internal/gateway/gateway.go
# Conflicts:
#	internal/gateway/upstream.go
@jasonxu114514

Copy link
Copy Markdown
Owner

已在 main 以 9b44a03 重新實作並驗證,本 PR 由該提交取代,將納入 v1.5.2。包含 models.aliases、strip_free_suffix、名稱碰撞及原始 ID 相容、Chat/Responses/Anthropic/System One 的串流及非串流模型回顯,並實際重寫 System One 本文;保留請求取消語意。感謝提案;關閉舊分支而不直接合併。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants