Compare commits
No commits in common. "f9b4da20f6f9cb795dea7c6d0e4108ee22f3c412" and "3a298a6d3ac544e2d4e209f59246a9ab077f132b" have entirely different histories.
f9b4da20f6
...
3a298a6d3a
@ -66,6 +66,7 @@ func (g *AiGenerator) GenerateTitleAndContentFromImage(imageURL, titleRequire, c
|
|||||||
return title, content, nil
|
return title, content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 带图片的聊天(使用兼容模式接口)
|
||||||
func (g *AiGenerator) chatWithImage(imageURL, prompt string) (string, error) {
|
func (g *AiGenerator) chatWithImage(imageURL, prompt string) (string, error) {
|
||||||
reqBody := ChatCompletionRequest{
|
reqBody := ChatCompletionRequest{
|
||||||
Model: "qwen-vl-plus",
|
Model: "qwen-vl-plus",
|
||||||
@ -157,114 +158,3 @@ func parseTitleAndContent(response string) (string, string) {
|
|||||||
|
|
||||||
return strings.TrimSpace(title), strings.TrimSpace(content)
|
return strings.TrimSpace(title), strings.TrimSpace(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 文本生成文本(聊天模式)
|
|
||||||
func (g *AiGenerator) GenerateTitleAndContentFromText(titleRequire, contentRequire string) (string, string, error) {
|
|
||||||
// 构建提示词
|
|
||||||
prompt := fmt.Sprintf(`请根据以下要求生成内容:
|
|
||||||
|
|
||||||
生成要求:
|
|
||||||
1. 标题要求:%s
|
|
||||||
2. 内容要求:%s
|
|
||||||
|
|
||||||
请严格按照以下格式返回,不要有任何额外文字:
|
|
||||||
标题:{生成的标题}
|
|
||||||
内容:{生成的内容}`,
|
|
||||||
titleRequire,
|
|
||||||
contentRequire,
|
|
||||||
)
|
|
||||||
|
|
||||||
// 发送聊天请求
|
|
||||||
response, err := g.chatWithText(prompt)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析响应
|
|
||||||
title, content := parseTitleAndContent(response)
|
|
||||||
return title, content, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 文本聊天(纯文本生成)
|
|
||||||
func (g *AiGenerator) chatWithText(prompt string) (string, error) {
|
|
||||||
reqBody := ChatCompletionRequest{
|
|
||||||
Model: "qwen-max", // 使用文本模型
|
|
||||||
Messages: []ChatMessage{
|
|
||||||
{
|
|
||||||
Role: "user",
|
|
||||||
Content: []Content{
|
|
||||||
{
|
|
||||||
Type: "text",
|
|
||||||
Text: prompt,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MaxTokens: 2000,
|
|
||||||
Temperature: 0.7,
|
|
||||||
}
|
|
||||||
|
|
||||||
url := g.cfg.BaseURL + "/compatible-mode/v1/chat/completions"
|
|
||||||
jsonData, err := json.Marshal(reqBody)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("JSON序列化失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("创建请求失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("Authorization", "Bearer "+g.cfg.APIKey)
|
|
||||||
|
|
||||||
resp, err := g.client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("API请求失败: %v", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("读取响应失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return "", fmt.Errorf("API错误: %d, 响应: %s", resp.StatusCode, string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
var result ChatCompletionResponse
|
|
||||||
if err := json.Unmarshal(body, &result); err != nil {
|
|
||||||
return "", fmt.Errorf("JSON解析失败: %v, 响应: %s", err, string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Error.Message != "" {
|
|
||||||
return "", fmt.Errorf("API返回错误: %s", result.Error.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(result.Choices) == 0 {
|
|
||||||
return "", errors.New("AI未生成有效响应")
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.TrimSpace(result.Choices[0].Message.Content), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *AiGenerator) GenerateImageFromText(prompt, size string, n int) (string, error) {
|
|
||||||
// 构建图片生成提示词
|
|
||||||
imagePrompt := fmt.Sprintf(`请根据以下描述生成图片:
|
|
||||||
|
|
||||||
图片描述:%s
|
|
||||||
生成数量:%d张
|
|
||||||
图片尺寸:%s
|
|
||||||
|
|
||||||
请直接生成图片,不要返回任何文字描述。`,
|
|
||||||
prompt, n, size)
|
|
||||||
|
|
||||||
// 使用文生图API
|
|
||||||
result, err := g.TextToImage(imagePrompt, size, n)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.Output.TaskID, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@ -215,25 +215,18 @@ func (p *BatchProcessor) generateTitleAndContent(req *excelData) (string, string
|
|||||||
}
|
}
|
||||||
return title, content, nil
|
return title, content, nil
|
||||||
} else {
|
} else {
|
||||||
title, content, err := NewAiGenerator().GenerateTitleAndContentFromText(
|
// 无图片:使用文生文
|
||||||
req.PhotoUrl,
|
title, err := p.generateTitle(req)
|
||||||
req.TitleRequire,
|
if err != nil {
|
||||||
)
|
return "", "", fmt.Errorf("生成标题失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := p.generateContent(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("生成内容失败: %v", err)
|
return "", "", fmt.Errorf("生成内容失败: %v", err)
|
||||||
}
|
}
|
||||||
return title, content, nil
|
|
||||||
//// 无图片:使用文生文
|
|
||||||
//title, err := p.generateTitle(req)
|
|
||||||
//if err != nil {
|
|
||||||
// return "", "", fmt.Errorf("生成标题失败: %v", err)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//content, err := p.generateContent(req)
|
|
||||||
//if err != nil {
|
|
||||||
// return "", "", fmt.Errorf("生成内容失败: %v", err)
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
return title, content, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,30 +274,6 @@ func (p *BatchProcessor) generateContent(req *excelData) (string, error) {
|
|||||||
return req.Content, nil
|
return req.Content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (p *BatchProcessor) generateImage(req *excelData) (string, error) {
|
|
||||||
// prompt := fmt.Sprintf("请根据以下要求生成内容:%s", req.PhotoRequire)
|
|
||||||
// if req.Title != "" {
|
|
||||||
// prompt += fmt.Sprintf("\n标题:%s", req.Title) // 关联标题
|
|
||||||
// }
|
|
||||||
// if req.Content != "" {
|
|
||||||
// prompt += fmt.Sprintf("\n内容:%s", req.Content) // 关联内容
|
|
||||||
// }
|
|
||||||
// if req.Desc != "" {
|
|
||||||
// prompt += fmt.Sprintf("\n艺人简介:%s", req.Desc)
|
|
||||||
// }
|
|
||||||
// prompt += "\n请基于标题和内容生成相关内容"
|
|
||||||
//
|
|
||||||
// result, err := NewAiGenerator().TextToImage(
|
|
||||||
// prompt,
|
|
||||||
// "1024*1024",
|
|
||||||
// req.PhotoNum,
|
|
||||||
// )
|
|
||||||
// if err != nil {
|
|
||||||
// return "", err
|
|
||||||
// }
|
|
||||||
// return result.Output.TaskID, nil
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (p *BatchProcessor) generateImage(req *excelData) (string, error) {
|
func (p *BatchProcessor) generateImage(req *excelData) (string, error) {
|
||||||
prompt := fmt.Sprintf("请根据以下要求生成内容:%s", req.PhotoRequire)
|
prompt := fmt.Sprintf("请根据以下要求生成内容:%s", req.PhotoRequire)
|
||||||
if req.Title != "" {
|
if req.Title != "" {
|
||||||
@ -318,7 +287,7 @@ func (p *BatchProcessor) generateImage(req *excelData) (string, error) {
|
|||||||
}
|
}
|
||||||
prompt += "\n请基于标题和内容生成相关内容"
|
prompt += "\n请基于标题和内容生成相关内容"
|
||||||
|
|
||||||
result, err := NewAiGenerator().GenerateImageFromText(
|
result, err := NewAiGenerator().TextToImage(
|
||||||
prompt,
|
prompt,
|
||||||
"1024*1024",
|
"1024*1024",
|
||||||
req.PhotoNum,
|
req.PhotoNum,
|
||||||
@ -326,7 +295,7 @@ func (p *BatchProcessor) generateImage(req *excelData) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return result, nil
|
return result.Output.TaskID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BatchProcessor) GetTaskStatistics() (completed, pending, total int, completedTasks, failedTasks []*ImageTask) {
|
func (p *BatchProcessor) GetTaskStatistics() (completed, pending, total int, completedTasks, failedTasks []*ImageTask) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user