- graph.go: 构建 Graph 拓扑 START→STT→History→ChatModel→Splitter→TTS→Done→END - 创建 eino-ext ChatModel 对接 DashScope OpenAI 兼容接口 - 统一使用值类型(PipelineInput/PipelineOutput) - Callback 在运行时通过 Stream option 传入 - adapter.go: EinoOrchestrator 实现 orchestrator.Orchestrator 接口 - 解码 base64 音频/图片,注入 context 值 - 调用 Graph.Stream() 触发惰性执行并消费输出 - 追加用户/助手消息到历史 - main.go: 移除旧 llmService + orchestrator.New() 替换为 eino.NewPipelineGraph() + eino.NewEinoOrchestrator() - 各节点统一使用值类型,State 传递请求元数据 Co-Authored-By: Claude <noreply@anthropic.com>
119 lines
3.3 KiB
Go
119 lines
3.3 KiB
Go
package eino
|
||
|
||
import (
|
||
"context"
|
||
"encoding/base64"
|
||
|
||
"github.com/cloudwego/eino/compose"
|
||
"github.com/cloudwego/eino/schema"
|
||
|
||
"github.com/hhs/camtalk/internal/ai/llm"
|
||
"github.com/hhs/camtalk/internal/logger"
|
||
"github.com/hhs/camtalk/internal/models"
|
||
)
|
||
|
||
// NewHistoryLambda 创建历史组装 Lambda 节点。
|
||
// 输入: *STTOutput → 输出: []*schema.Message
|
||
//
|
||
// 从 PipelineState 读取请求元数据(SessionID、Scenario、ImageData 等),
|
||
// 构建系统提示词,组装历史消息和当前用户输入(含多模态图片)。
|
||
func NewHistoryLambda(historyFetcher func(ctx context.Context, sessionID string, limit int) ([]models.Message, error), maxHistory int) *compose.Lambda {
|
||
return compose.InvokableLambda(func(ctx context.Context, sttOut STTOutput) ([]*schema.Message, error) {
|
||
log := logger.Log
|
||
|
||
// 从 State 读取请求元数据
|
||
state := stateFromCtx(ctx)
|
||
if state == nil {
|
||
return []*schema.Message{}, nil
|
||
}
|
||
|
||
state.mu.Lock()
|
||
sessionID := state.SessionID
|
||
requestID := state.RequestID
|
||
imageData := state.ImageData
|
||
scenario := state.Scenario
|
||
detailLevel := state.DetailLevel
|
||
language := sttOut.Language
|
||
state.mu.Unlock()
|
||
|
||
// 构建系统提示词
|
||
scenarioPrompt := llm.GetScenarioPrompt(scenario, language)
|
||
systemPrompt := llm.BuildSystemPrompt(language, detailLevel, scenarioPrompt)
|
||
|
||
// 构建 system message(含图片)
|
||
systemMsg := &schema.Message{
|
||
Role: schema.System,
|
||
Content: systemPrompt,
|
||
}
|
||
|
||
// 如果有图片,添加到 system message 的多模态内容中
|
||
if len(imageData) > 0 {
|
||
base64Str := base64.StdEncoding.EncodeToString(imageData)
|
||
mimeType := detectImageMimeType(imageData)
|
||
systemMsg.UserInputMultiContent = []schema.MessageInputPart{
|
||
{
|
||
Type: schema.ChatMessagePartTypeImageURL,
|
||
Image: &schema.MessageInputImage{
|
||
MessagePartCommon: schema.MessagePartCommon{
|
||
Base64Data: &base64Str,
|
||
MIMEType: mimeType,
|
||
},
|
||
Detail: schema.ImageURLDetailAuto,
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
messages := []*schema.Message{systemMsg}
|
||
|
||
// 获取并追加历史消息
|
||
if historyFetcher != nil && sessionID != "" {
|
||
history, err := historyFetcher(ctx, sessionID, maxHistory)
|
||
if err != nil {
|
||
log.Warnw("获取历史消息失败,继续处理", "error", err, "request_id", requestID)
|
||
} else {
|
||
for _, msg := range history {
|
||
messages = append(messages, &schema.Message{
|
||
Role: schema.RoleType(msg.Role),
|
||
Content: msg.Content,
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
// 追加当前用户输入
|
||
messages = append(messages, &schema.Message{
|
||
Role: schema.User,
|
||
Content: sttOut.Text,
|
||
})
|
||
|
||
log.Infow("历史组装完成",
|
||
"request_id", requestID,
|
||
"message_count", len(messages),
|
||
"has_image", len(imageData) > 0,
|
||
"scenario", scenario)
|
||
|
||
return messages, nil
|
||
})
|
||
}
|
||
|
||
// detectImageMimeType 简单检测图片 MIME 类型。
|
||
func detectImageMimeType(data []byte) string {
|
||
if len(data) < 4 {
|
||
return "image/jpeg"
|
||
}
|
||
if data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF {
|
||
return "image/jpeg"
|
||
}
|
||
if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 {
|
||
return "image/png"
|
||
}
|
||
if data[0] == 0x47 && data[1] == 0x49 && data[2] == 0x46 {
|
||
return "image/gif"
|
||
}
|
||
if data[0] == 0x52 && data[1] == 0x49 && data[2] == 0x46 && data[3] == 0x46 {
|
||
return "image/webp"
|
||
}
|
||
return "image/jpeg"
|
||
}
|