Merge branch 'develop' of gitee.com:hezhaohui123/gen2d into feat/async-generate-pipeline

Signed-off-by: 何朝晖 <hezhaohui0807@163.com>
This commit is contained in:
2026-05-25 06:38:14 +00:00
committed by Gitee
29 changed files with 896 additions and 244 deletions
+10 -3
View File
@@ -7,11 +7,11 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"gen2d/internal/config"
"gen2d/internal/logger"
"github.com/cloudwego/eino/compose"
)
@@ -130,8 +130,10 @@ type chatResponse struct {
// callLLMRefine 调用 LLM 生成规范化提示词。未配置 API key 时回退到模板生成。
func callLLMRefine(ctx context.Context, metaPrompt string) (PromptAgentOutput, error) {
l := logger.FromCtx(ctx)
if llmCfg.APIKey == "" {
log.Println("[prompt_agent] LLM API key not configured, using template fallback")
l.Warn("LLM API key not configured, using template fallback")
return fallbackRefine(metaPrompt), nil
}
@@ -140,12 +142,14 @@ func callLLMRefine(ctx context.Context, metaPrompt string) (PromptAgentOutput, e
{Role: "user", Content: metaPrompt},
}
l.Info("calling LLM API", "model", llmCfg.Model)
result, err := chatCompletion(ctx, messages)
if err != nil {
log.Printf("[prompt_agent] LLM API call failed: %v, using template fallback", err)
l.Error("LLM API call failed, using template fallback", "error", err)
return fallbackRefine(metaPrompt), nil
}
l.Info("LLM API succeeded", "response_length", len(result))
return PromptAgentOutput{
Prompt: result,
RawText: result,
@@ -175,6 +179,8 @@ func chatCompletion(ctx context.Context, messages []chatMessage) (string, error)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+llmCfg.APIKey)
l := logger.FromCtx(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("send request: %w", err)
@@ -183,6 +189,7 @@ func chatCompletion(ctx context.Context, messages []chatMessage) (string, error)
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
l.Error("LLM API error", "status", resp.StatusCode, "body", string(b))
return "", fmt.Errorf("llm api error %d: %s", resp.StatusCode, string(b))
}