git-subtree-dir: server git-subtree-mainline:c4e617ada7git-subtree-split:e64421295f
57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
// CandidateData 预筛候选服装(供 LLM 选择组合)
|
|
type CandidateData struct {
|
|
SetId int64 `json:"set_id"` // 所属预筛组合编号
|
|
ItemId int64 `json:"item_id"` // 衣橱条目 id
|
|
Category string `json:"category"`
|
|
Name string `json:"name"`
|
|
Color string `json:"color"`
|
|
Season string `json:"season"`
|
|
Style string `json:"style"`
|
|
}
|
|
|
|
// PlanOutfits 规则预筛候选 → LLM 润色规划(1 次调用)
|
|
func PlanOutfits(ctx context.Context, cfg *ModelConfig, sysPrompt, userInput string, candidates []CandidateData) (*PlanOutput, error) {
|
|
candJSON, err := json.Marshal(candidates)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("候选序列化失败: %w", err)
|
|
}
|
|
msg := userInput + "\n候选服装(JSON,set_id 表示第几套预选,请在同一套内选择):" + string(candJSON)
|
|
return callPlan(ctx, cfg, sysPrompt, msg)
|
|
}
|
|
|
|
// CreateRecommendPlan 兜底创作(全低分时调用,1 次调用)
|
|
func CreateRecommendPlan(ctx context.Context, cfg *ModelConfig, sysPrompt, userInput string) (*PlanOutput, error) {
|
|
return callPlan(ctx, cfg, sysPrompt, userInput)
|
|
}
|
|
|
|
func callPlan(ctx context.Context, cfg *ModelConfig, sysPrompt, userInput string) (*PlanOutput, error) {
|
|
req := &ChatRequest{
|
|
Messages: []*ChatMessage{
|
|
{Role: RoleSystem, Content: sysPrompt},
|
|
{Role: RoleUser, Content: userInput},
|
|
},
|
|
MaxTokens: cfg.MaxTokens,
|
|
Temperature: cfg.Temperature,
|
|
}
|
|
resp, err := CallChatModel(ctx, cfg, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out, err := ParsePlanOutput(resp.Content)
|
|
if err != nil {
|
|
g.Log().Warningf(ctx, "LLM 方案输出解析失败: %v\n原始输出: %s", err, resp.Content)
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|