docs(comments): 注释中文化 · internal/runtime/multimodal(21 文件,纯替换)
- 只翻注释/文档字符串,代码零变化:go/scanner 词法流比对 21 文件 token+字面量全等,增删行数 1:1(467/467) - 刻意保留非散文英文:JSON Schema description、工具 Description() 返回值、adapter prompt、API 路径与命令示例、环境变量名 - 逐行确认代码前缀未变,未触碰其他模块
This commit is contained in:
1 parent
b9e042d07d
commit
b0fbb8bc0d
21 files changed
+467
-467
No files matched your search
@@ -13,28 +13,28 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
||||
)
|
||||
|
||||
// visionMaxTokens caps the vision/OCR response length.
|
||||
// visionMaxTokens 限制 vision/OCR 响应的长度。
|
||||
const visionMaxTokens = 512
|
||||
|
||||
// VisionAdapter understands an image and returns a description of it. It is the
|
||||
// backend behind the vision tool; real providers implement it without exposing
|
||||
// the image to the main-model context.
|
||||
// VisionAdapter 理解一张图像并返回对它的描述。它是
|
||||
// 视觉工具背后的后端;真实提供方实现它,且不把图像
|
||||
// 暴露给主模型上下文。
|
||||
type VisionAdapter interface {
|
||||
// Describe returns a description of img. prompt is an optional instruction
|
||||
// overriding the default "describe this image" ask.
|
||||
// Describe 返回 img 的描述。prompt 是可选的指令,
|
||||
// 用于覆盖默认的「描述这张图」提问。
|
||||
Describe(ctx context.Context, img ImageRef, prompt string) (string, error)
|
||||
}
|
||||
|
||||
// OCRAdapter extracts text from an image. It is the backend behind the OCR tool.
|
||||
// OCRAdapter 从图像中抽取文字。它是 OCR 工具背后的后端。
|
||||
type OCRAdapter interface {
|
||||
// Extract returns the text found in img.
|
||||
// Extract 返回 img 中发现的文字。
|
||||
Extract(ctx context.Context, img ImageRef) (string, error)
|
||||
}
|
||||
|
||||
// OpenAICompatibleAdapter implements both VisionAdapter and OCRAdapter by
|
||||
// calling an OpenAI-compatible /chat/completions endpoint with the image
|
||||
// embedded as a content part. It is the ready default implementation; real
|
||||
// cloud vision/OCR providers expose the same protocol.
|
||||
// OpenAICompatibleAdapter 通过调用 OpenAI 兼容的 /chat/completions
|
||||
// endpoint(图像作为内容部件内嵌)同时实现 VisionAdapter 与 OCRAdapter。
|
||||
// 它是开箱可用的默认实现;真实的
|
||||
// 云端 vision/OCR 提供方暴露相同协议。
|
||||
type OpenAICompatibleAdapter struct {
|
||||
cfg config.LLMConfig
|
||||
model string
|
||||
@@ -42,10 +42,10 @@ type OpenAICompatibleAdapter struct {
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// OpenAIOption customizes an OpenAICompatibleAdapter.
|
||||
// OpenAIOption 用于定制 OpenAICompatibleAdapter。
|
||||
type OpenAIOption func(*OpenAICompatibleAdapter)
|
||||
|
||||
// WithModel overrides the adapter model (defaults to cfg.Model).
|
||||
// WithModel 覆盖适配器的模型(默认取 cfg.Model)。
|
||||
func WithModel(m string) OpenAIOption {
|
||||
return func(a *OpenAICompatibleAdapter) {
|
||||
if m != "" {
|
||||
@@ -54,7 +54,7 @@ func WithModel(m string) OpenAIOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets the *http.Client used for the request; nil keeps default.
|
||||
// WithHTTPClient 设置请求所用的 *http.Client;nil 表示保持默认。
|
||||
func WithHTTPClient(c *http.Client) OpenAIOption {
|
||||
return func(a *OpenAICompatibleAdapter) {
|
||||
if c != nil {
|
||||
@@ -63,7 +63,7 @@ func WithHTTPClient(c *http.Client) OpenAIOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger sets the logger used on failures; nil keeps a discard logger.
|
||||
// WithLogger 设置失败时使用的日志器;nil 表示保持丢弃日志器。
|
||||
func WithLogger(l *slog.Logger) OpenAIOption {
|
||||
return func(a *OpenAICompatibleAdapter) {
|
||||
if l != nil {
|
||||
@@ -72,8 +72,8 @@ func WithLogger(l *slog.Logger) OpenAIOption {
|
||||
}
|
||||
}
|
||||
|
||||
// NewOpenAICompatible builds an adapter from cfg, using cfg.Model unless
|
||||
// overridden.
|
||||
// NewOpenAICompatible 用 cfg 构造一个适配器,未覆盖时使用
|
||||
// cfg.Model。
|
||||
func NewOpenAICompatible(cfg config.LLMConfig, opts ...OpenAIOption) *OpenAICompatibleAdapter {
|
||||
a := &OpenAICompatibleAdapter{
|
||||
cfg: cfg,
|
||||
@@ -87,7 +87,7 @@ func NewOpenAICompatible(cfg config.LLMConfig, opts ...OpenAIOption) *OpenAIComp
|
||||
return a
|
||||
}
|
||||
|
||||
// Describe returns an image description from the underlying chat endpoint.
|
||||
// Describe 从底层 chat endpoint 返回图像描述。
|
||||
func (a *OpenAICompatibleAdapter) Describe(ctx context.Context, img ImageRef, prompt string) (string, error) {
|
||||
if prompt == "" {
|
||||
prompt = "Describe this image in detail."
|
||||
@@ -95,12 +95,12 @@ func (a *OpenAICompatibleAdapter) Describe(ctx context.Context, img ImageRef, pr
|
||||
return a.ask(ctx, img, prompt)
|
||||
}
|
||||
|
||||
// Extract returns text found in the image via the same endpoint.
|
||||
// Extract 经同一 endpoint 返回图像中发现的文字。
|
||||
func (a *OpenAICompatibleAdapter) Extract(ctx context.Context, img ImageRef) (string, error) {
|
||||
return a.ask(ctx, img, "Extract all text visible in this image. Return only the text.")
|
||||
}
|
||||
|
||||
// ask resolves the image, builds a chat request and returns the assistant text.
|
||||
// ask 解析图像、构造 chat 请求并返回助手文本。
|
||||
func (a *OpenAICompatibleAdapter) ask(ctx context.Context, img ImageRef, prompt string) (string, error) {
|
||||
if strings.TrimSpace(a.cfg.BaseURL) == "" {
|
||||
return "", ErrNotConfigured
|
||||
@@ -139,8 +139,8 @@ func (a *OpenAICompatibleAdapter) ask(ctx context.Context, img ImageRef, prompt
|
||||
return a.decode(resp.Body)
|
||||
}
|
||||
|
||||
// imageURL converts an ImageRef to the image_url value the wire expects: an
|
||||
// http(s) URL passes through, anything else becomes a base64 data: URI.
|
||||
// imageURL 把 ImageRef 转换为线上格式期望的 image_url 取值:http(s)
|
||||
// URL 原样透传,其余一律转为 base64 data: URI。
|
||||
func (a *OpenAICompatibleAdapter) imageURL(ctx context.Context, img ImageRef) (string, error) {
|
||||
if img.Source == SourceURL {
|
||||
return img.Data, nil
|
||||
@@ -152,7 +152,7 @@ func (a *OpenAICompatibleAdapter) imageURL(ctx context.Context, img ImageRef) (s
|
||||
return DataURI(mime, data), nil
|
||||
}
|
||||
|
||||
// openAIVisionRequest is the JSON body sent to /chat/completions.
|
||||
// openAIVisionRequest 是发往 /chat/completions 的 JSON 请求体。
|
||||
type openAIVisionRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []openAIMessage `json:"messages"`
|
||||
@@ -186,7 +186,7 @@ type openAIResponseMessage struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// request builds the wire body for an image+prompt.
|
||||
// request 构造图像 + prompt 的线上请求体。
|
||||
func (a *OpenAICompatibleAdapter) request(imageURL, prompt string) openAIVisionRequest {
|
||||
return openAIVisionRequest{
|
||||
Model: a.model,
|
||||
@@ -200,7 +200,7 @@ func (a *OpenAICompatibleAdapter) request(imageURL, prompt string) openAIVisionR
|
||||
}
|
||||
}
|
||||
|
||||
// decode extracts the first choice's text from a response body.
|
||||
// decode 从响应体中取出第一个 choice 的文本。
|
||||
func (a *OpenAICompatibleAdapter) decode(r io.Reader) (string, error) {
|
||||
var wire openAIVisionResponse
|
||||
if err := json.NewDecoder(r).Decode(&wire); err != nil {
|
||||
@@ -212,6 +212,6 @@ func (a *OpenAICompatibleAdapter) decode(r io.Reader) (string, error) {
|
||||
return wire.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
// interface assertion: one concrete adapter serves both directions.
|
||||
// 接口断言:同一个具体适配器同时服务两个方向。
|
||||
var _ VisionAdapter = (*OpenAICompatibleAdapter)(nil)
|
||||
var _ OCRAdapter = (*OpenAICompatibleAdapter)(nil)
|
||||
Reference in new issue
Block a user