From 078cfe62785fd4f7af945be9134314a53502eae5 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sat, 13 Jun 2026 15:42:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9A=E4=B9=89=20LLM=20Service=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=88ChatStream=20+=20Request/Chunk/Token?= =?UTF-8?q?Usage=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/ai/llm/llm.go | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 backend/internal/ai/llm/llm.go diff --git a/backend/internal/ai/llm/llm.go b/backend/internal/ai/llm/llm.go new file mode 100644 index 0000000..13f3990 --- /dev/null +++ b/backend/internal/ai/llm/llm.go @@ -0,0 +1,37 @@ +package llm + +import ( + "context" + + "github.com/hhs/camtalk/internal/models" +) + +// Service 多模态大模型服务契约。 +type Service interface { + // ChatStream 流式推理,返回增量文本的 channel。 + // 调用方必须消费 channel 直到 Done=true,否则需 cancel ctx 以释放连接。 + ChatStream(ctx context.Context, req Request) (<-chan Chunk, error) +} + +// Request 推理请求。 +type Request struct { + Image []byte // JPEG 图片(已从 Base64 解码) + Text string // 用户语音识别后的文本 + History []models.Message // 最近 N 轮对话历史 + Language string // 语言,如 "zh-CN" +} + +// Chunk 流式推理的一个增量片段。 +type Chunk struct { + Delta string // 增量文本 + Done bool // 是否结束 + TokensUsed *TokenUsage // 仅 Done=true 时有值 + Model string // 实际使用的模型名 +} + +// TokenUsage 用量统计。 +type TokenUsage struct { + Prompt int + Completion int + Total int +}