minimax

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package minimax 实现 MiniMax 官方 API 的 ModelProvider.

MiniMax 提供三种 API 接入方式:

  1. Native API - /v1/text/chatcompletion_v2(官方路径,本实现默认)
  2. OpenAI 兼容 - /v1/chat/completions
  3. Anthropic 兼容 - /anthropic/v1/messages(MiniMax 对外推广,但实测效果不稳定)

默认使用 Native 模式,原因:

  • Native 是 MiniMax 的第一方 API,语义最完整
  • 实测 Anthropic 兼容端点在编程类 Agent 场景下表现较差 (可能是 Anthropic 特有的 beta headers / system prompt 格式未完全兼容)
  • OpenAI 兼容和 Native 的 SSE 格式相同,切换成本低

Native API 的 SSE 格式与 OpenAI 完全相同(data: {...}, data: [DONE]), 因此我们复用 internal/wire/openai_compat.go,只需修改端点路径.

历史包袱(LEGACY): flysafe 早期通过 BaseURL="https://api.minimaxi.com/anthropic" 使用 MiniMax,走的是 Anthropic 兼容端点. 迁移路径:flysafe 改用 minimax.New(Config{APIKey: "..."}) 即可, 无需关心底层使用哪种格式.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// APIKey 是 MiniMax API 密钥(从 platform.minimax.io 获取).
	APIKey string

	// Region 控制使用国际节点还是中国节点(默认 RegionChina).
	// MiniMax 主要面向中国市场,默认使用国内节点(api.minimaxi.com).
	// 如需使用国际节点(api.minimax.io),显式设置 RegionGlobal.
	Region Region

	// Mode 控制使用哪种 API 格式(默认 ModeNative).
	Mode Mode

	// ThinkingBudget 启用思考模式(仅 M2.1+ 支持).
	// 0 = 禁用.推荐值:5000-16000.
	// ModeNative/ModeOpenAI 时通过 reasoning_split 参数传递.
	// ModeAnthropic 时通过 thinking.budget_tokens 传递.
	ThinkingBudget int

	// HTTPClient 注入自定义 HTTP 客户端.
	// nil = 使用默认 http.Client (带 Timeout 字段配置的 ResponseHeaderTimeout).
	// 非 nil 时 consumer 完全接管超时责任, 下面的 Timeout 字段被忽略.
	//
	// 精妙之处(CLEVER): minimax 的 HTTPClient 会被双模式 (ModeAnthropic / ModeNative|OpenAI)
	// 两个独立客户端同时使用 - 消费者注入的 client 会被两边共享.这是 MiniMax 特有的,
	// 因为同一个 Provider 实例只会选一种 Mode, 不会同时用两个 client.
	HTTPClient *http.Client

	// Timeout 限制"从请求发出到收到响应首字节"的时间.
	//
	// 通过 http.Transport.ResponseHeaderTimeout 实现, **不影响** SSE 流式响应的后续
	// body 读取 - MiniMax 长思考模式 (M2.7 Thinking) 可以正常读完.
	//
	// 精妙之处(CLEVER): 不要误用 http.Client.Timeout - 那会把 SSE 流砍死.
	// MiniMax 的双模式 (ModeAnthropic / ModeNative|OpenAI) 都从这一个字段读超时,
	// 两个底层 client 独立应用, 语义保持等价.
	//
	// 0 = 使用 defaultTimeout (60s, 适合 MiniMax 云端).
	// 仅当 HTTPClient 为 nil 时生效; 提供自定义 HTTPClient 时此字段被忽略.
	Timeout time.Duration

	// ModelOverrides 覆盖静态模型表.
	ModelOverrides []flyto.ModelInfo
}

Config 是 MiniMax provider 的配置.

func (Config) GoString

func (c Config) GoString() string

GoString 实现 fmt.GoStringer,防止 %#v 打印时泄露 APIKey. 升华改进(ELEVATED): 参见 anthropic.Config.GoString 的说明,同理.

type Mode

type Mode string

Mode 是 MiniMax API 接入模式.

const (
	// ModeNative 使用 MiniMax 官方 API(/v1/text/chatcompletion_v2).
	// 推荐:语义最完整,SSE 格式与 OpenAI 相同,思考内容通过 delta.reasoning 字段传递.
	ModeNative Mode = "native"

	// ModeOpenAI 使用 MiniMax 的 OpenAI 兼容端点(/v1/chat/completions).
	// 与 ModeNative 几乎等价,端点路径不同.
	ModeOpenAI Mode = "openai"

	// ModeAnthropic 使用 MiniMax 的 Anthropic 兼容端点(/anthropic/v1/messages).
	// 注意:实测在编程类 Agent 场景下效果可能不如 Native.
	// 仅在明确需要 Anthropic 格式特性(如 cache_control 细粒度控制)时使用.
	ModeAnthropic Mode = "anthropic"
)

type Provider

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

Provider 是 MiniMax ModelProvider 实现.

func New

func New(cfg Config) *Provider

New 创建 MiniMax ModelProvider.

func (*Provider) ExtractVision

func (p *Provider) ExtractVision(ctx context.Context, req *VisionRequest) (*VisionResponse, error)

ExtractVision implements flyto.VisionProvider. Single HTTP POST, blocks until the full response body is read. Errors propagate from transport failures, non-2xx HTTP status, non-zero base_resp.status_code (MiniMax error envelope), or empty content (caller cannot parse "" so we fail loudly).

ExtractVision 实现 flyto.VisionProvider. 单次 HTTP POST, 阻塞至 读完响应 body. 错误来源: 传输失败 / 非 2xx HTTP / 非零 base_resp.status_code (MiniMax 错误外壳) / 空 content (调用方解析 "" 没意义, 这里 fail-loud).

func (*Provider) Models

func (p *Provider) Models(_ context.Context) ([]flyto.ModelInfo, error)

Models 返回 MiniMax 可用模型列表(静态表).

func (*Provider) Name

func (p *Provider) Name() string

Name 返回 provider 标识.

func (*Provider) Stream

func (p *Provider) Stream(ctx context.Context, req *flyto.Request) (<-chan flyto.Event, error)

Stream 向 MiniMax API 发起流式请求.

升华改进(ELEVATED): data-driven-capabilities RFC PR2.6 - Stream dispatcher 层统一处理 max_tools 检查 + want×can 检测.两条 stream path(streamOpenAI / streamAnthropic)原本 各自做一份检查,提到 dispatcher 层 DRY + 行为一致.能力决策是模型级属性,和序列化格式无关.

type Region

type Region string

Region 是 MiniMax API 地区.

const (
	// RegionGlobal 使用国际节点(api.minimax.io).
	RegionGlobal Region = "global"

	// RegionChina 使用中国节点(api.minimaxi.com).
	RegionChina Region = "china"
)

type VisionClient

type VisionClient interface {
	ExtractVision(ctx context.Context, req *VisionRequest) (*VisionResponse, error)
}

VisionClient is the local capability interface for MiniMax vision extraction. *Provider satisfies it (see ExtractVision below). Consumers depend on this interface (not *Provider) to allow test injection without an httptest server. Lives in the minimax package by design (not flyto.*) -- see VisionRequest doc for the full rationale.

VisionClient 是 MiniMax 视觉抽取的本地 capability 接口. *Provider 满足该接口 (见下方 ExtractVision). 消费者依赖此接口 (非 *Provider) 以便测试时注入而不必起 httptest server. 故意放 minimax 包不放 flyto.* -- 完整理由见 VisionRequest 注释.

type VisionRequest

type VisionRequest struct {
	Prompt    string // instructions for the model
	Image     []byte // raw image bytes
	MediaType string // e.g. "image/png", "image/jpeg"; empty defaults to "image/png"
	MaxTokens int    // reserved; vlm endpoint currently has no token cap
}

VisionRequest is a single-shot image extraction request to MiniMax vlm. Image is raw bytes (NOT base64); ExtractVision encodes it as a data URI for the wire. MaxTokens is reserved for future endpoint support -- /v1/coding_plan/vlm currently has no max_tokens knob, the field is accepted but ignored.

Why this lives in the minimax package (not in flyto): vlm is a MiniMax-specific endpoint (FAQ-confirmed: M2 chat completions and the Anthropic-compat /v1/messages reject mixed text-image input; the only vision path is /v1/coding_plan/vlm). Other mainstream LLM providers (Claude / GPT-4o / Gemini) carry images inside messages as `BlockImage` content blocks via the streaming Provider interface. Putting a vision contract on flyto.* would pollute the cross-provider core for one outlier; per rule-of-two it stays here until a second provider needs the same vendor-specific shape.

VisionRequest 是发到 MiniMax vlm 的 single-shot 图像抽取请求. Image 原始字节 (非 base64); ExtractVision 编码为 data URI 上线. MaxTokens 为未来端点扩展预留 -- /v1/coding_plan/vlm 当前无 max_tokens 旋钮, 字段被接收但忽略.

为什么放在 minimax 包而不是 flyto: vlm 是 MiniMax 特有端点 (FAQ 明确: M2 chat completions 与 Anthropic-compat /v1/messages 拒 mixed text-image 输入; 唯一 vision 路径就是 /v1/coding_plan/vlm). 其他主流 LLM (Claude / GPT-4o / Gemini) 通过 streaming Provider 接口的 BlockImage content 块送图. 把 vision 契约放 flyto.* 会让 跨 provider 核心被一个 outlier 污染; 按 rule of two 留在 minimax, 等第二个 provider 需要同款形态再 promote.

type VisionResponse

type VisionResponse struct {
	Content string
}

VisionResponse carries the model's extracted content. Content is the raw string the model returned; the consumer parses (typically JSON when prompt asked for structured output).

VisionResponse 携带模型抽取内容. Content 是模型返的原始字符串; 消费者解析 (prompt 要求结构化输出时通常 JSON).

Jump to

Keyboard shortcuts

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