1、添加中英文版本
2、修复已知BUG 3、完善功能 4、添加minimax视频渠道
This commit is contained in:
@@ -159,6 +159,27 @@ func (c *OpenAIClient) sendChatRequest(req *ChatCompletionRequest) (*ChatComplet
|
||||
|
||||
fmt.Printf("OpenAI: Successfully parsed response, choices count: %d\n", len(chatResp.Choices))
|
||||
|
||||
if len(chatResp.Choices) == 0 {
|
||||
fmt.Printf("OpenAI: No choices in response\n")
|
||||
return nil, fmt.Errorf("no choices in response")
|
||||
}
|
||||
|
||||
// 检查 finish_reason,处理内容过滤的情况
|
||||
if len(chatResp.Choices) > 0 {
|
||||
finishReason := chatResp.Choices[0].FinishReason
|
||||
content := chatResp.Choices[0].Message.Content
|
||||
|
||||
fmt.Printf("OpenAI: finish_reason=%s, content_length=%d\n", finishReason, len(content))
|
||||
|
||||
if finishReason == "content_filter" {
|
||||
return nil, fmt.Errorf("AI内容被安全过滤器拦截,可能因为:\n1. 请求内容触发了安全策略\n2. 生成的内容包含敏感信息\n3. 建议:调整输入内容或联系API提供商调整过滤策略")
|
||||
}
|
||||
|
||||
if content == "" && finishReason != "stop" {
|
||||
return nil, fmt.Errorf("AI返回内容为空 (finish_reason: %s),可能的原因:\n1. 内容被过滤\n2. Token限制\n3. API异常", finishReason)
|
||||
}
|
||||
}
|
||||
|
||||
return &chatResp, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,10 @@ type Config struct {
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Version string `mapstructure:"version"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
Name string `mapstructure:"name"`
|
||||
Version string `mapstructure:"version"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
Language string `mapstructure:"language"` // zh 或 en
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
|
||||
@@ -21,16 +21,44 @@ func SafeParseAIJSON(aiResponse string, v interface{}) error {
|
||||
|
||||
// 1. 移除可能的Markdown代码块标记
|
||||
cleaned := strings.TrimSpace(aiResponse)
|
||||
// 移除开头的 ```json 或 ```
|
||||
cleaned = regexp.MustCompile("(?m)^```json\\s*").ReplaceAllString(cleaned, "")
|
||||
cleaned = regexp.MustCompile("(?m)^```\\s*").ReplaceAllString(cleaned, "")
|
||||
// 移除结尾的 ```
|
||||
cleaned = regexp.MustCompile("(?m)```\\s*$").ReplaceAllString(cleaned, "")
|
||||
cleaned = strings.TrimSpace(cleaned)
|
||||
|
||||
// 2. 提取JSON对象 (查找第一个 { 到最后一个 })
|
||||
jsonRegex := regexp.MustCompile(`(?s)\{.*\}`)
|
||||
jsonMatch := jsonRegex.FindString(cleaned)
|
||||
// 2. 提取JSON (支持对象 {} 和数组 [])
|
||||
var jsonMatch string
|
||||
|
||||
// 优先尝试提取完整的JSON(对象或数组)
|
||||
// 先尝试对象格式
|
||||
if strings.HasPrefix(cleaned, "{") {
|
||||
jsonRegex := regexp.MustCompile(`(?s)\{.*\}`)
|
||||
jsonMatch = jsonRegex.FindString(cleaned)
|
||||
}
|
||||
|
||||
// 如果没找到对象,尝试数组格式
|
||||
if jsonMatch == "" && strings.HasPrefix(cleaned, "[") {
|
||||
jsonRegex := regexp.MustCompile(`(?s)\[.*\]`)
|
||||
jsonMatch = jsonRegex.FindString(cleaned)
|
||||
}
|
||||
|
||||
// 如果还是没找到,尝试从中间提取
|
||||
if jsonMatch == "" {
|
||||
// 尝试对象
|
||||
objRegex := regexp.MustCompile(`(?s)\{.*\}`)
|
||||
jsonMatch = objRegex.FindString(cleaned)
|
||||
|
||||
// 如果对象没找到,尝试数组
|
||||
if jsonMatch == "" {
|
||||
arrRegex := regexp.MustCompile(`(?s)\[.*\]`)
|
||||
jsonMatch = arrRegex.FindString(cleaned)
|
||||
}
|
||||
}
|
||||
|
||||
if jsonMatch == "" {
|
||||
return fmt.Errorf("响应中未找到有效的JSON对象,原始响应: %s", truncateString(aiResponse, 200))
|
||||
return fmt.Errorf("响应中未找到有效的JSON对象或数组,原始响应: %s", truncateString(aiResponse, 200))
|
||||
}
|
||||
|
||||
// 3. 尝试解析JSON
|
||||
@@ -47,7 +75,17 @@ func SafeParseAIJSON(aiResponse string, v interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 提供详细的错误上下文
|
||||
// 5. 检测是否是响应被截断导致的问题
|
||||
if isTruncated(jsonMatch) {
|
||||
return fmt.Errorf(
|
||||
"AI响应可能被截断,导致JSON不完整。\n请尝试:\n1. 增加maxTokens参数\n2. 简化输入内容\n3. 使用更强大的模型\n\n原始错误: %s\n响应长度: %d\n响应末尾: %s",
|
||||
err.Error(),
|
||||
len(jsonMatch),
|
||||
truncateString(jsonMatch[maxInt(0, len(jsonMatch)-200):], 200),
|
||||
)
|
||||
}
|
||||
|
||||
// 6. 提供详细的错误上下文
|
||||
if jsonErr, ok := err.(*json.SyntaxError); ok {
|
||||
errorPos := int(jsonErr.Offset)
|
||||
start := maxInt(0, errorPos-100)
|
||||
@@ -130,6 +168,38 @@ func ValidateJSON(jsonStr string) error {
|
||||
return json.Unmarshal([]byte(jsonStr), &js)
|
||||
}
|
||||
|
||||
// isTruncated 检测JSON字符串是否可能被截断
|
||||
func isTruncated(jsonStr string) bool {
|
||||
trimmed := strings.TrimSpace(jsonStr)
|
||||
if len(trimmed) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查是否以不完整的字符串结尾(引号未闭合)
|
||||
lastChar := trimmed[len(trimmed)-1]
|
||||
if lastChar != '}' && lastChar != ']' {
|
||||
return true
|
||||
}
|
||||
|
||||
// 检查括号是否匹配
|
||||
openBraces := strings.Count(trimmed, "{")
|
||||
closeBraces := strings.Count(trimmed, "}")
|
||||
openBrackets := strings.Count(trimmed, "[")
|
||||
closeBrackets := strings.Count(trimmed, "]")
|
||||
|
||||
if openBraces != closeBraces || openBrackets != closeBrackets {
|
||||
return true
|
||||
}
|
||||
|
||||
// 检查引号是否匹配(简化检查,不考虑转义)
|
||||
quoteCount := strings.Count(trimmed, `"`)
|
||||
if quoteCount%2 != 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
func truncateString(s string, maxLen int) string {
|
||||
if len(s) <= maxLen {
|
||||
|
||||
@@ -9,6 +9,36 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// MiniMax Hailuo 支持的模型
|
||||
const (
|
||||
// ModelHailuo23 全新视频生成模型,肢体动作、面部表情、物理表现与指令遵循再度突破
|
||||
// 支持:文生视频、图生视频
|
||||
// 时长:768P(6s/10s), 1080P(6s)
|
||||
ModelHailuo23 = "MiniMax-Hailuo-2.3"
|
||||
|
||||
// ModelHailuo23Fast 全新图生视频模型,物理表现与指令遵循具佳,更快更优惠
|
||||
// 支持:图生视频
|
||||
// 时长:768P(6s/10s), 1080P(6s)
|
||||
ModelHailuo23Fast = "MiniMax-Hailuo-2.3-Fast"
|
||||
|
||||
// ModelHailuo02 新一代视频生成模型,1080p 原生,SOTA 指令遵循,极致物理表现
|
||||
// 支持:文生视频、图生视频、首尾帧模式
|
||||
// 时长:768P(6s/10s), 1080P(6s)
|
||||
ModelHailuo02 = "MiniMax-Hailuo-02"
|
||||
)
|
||||
|
||||
// MiniMax Hailuo 支持的分辨率
|
||||
const (
|
||||
Resolution768P = "768P"
|
||||
Resolution1080P = "1080P"
|
||||
)
|
||||
|
||||
// MiniMax Hailuo 支持的时长(秒)
|
||||
const (
|
||||
Duration6s = 6
|
||||
Duration10s = 10
|
||||
)
|
||||
|
||||
// MinimaxClient Minimax视频生成客户端
|
||||
type MinimaxClient struct {
|
||||
BaseURL string
|
||||
@@ -32,21 +62,42 @@ type MinimaxRequest struct {
|
||||
Resolution string `json:"resolution,omitempty"`
|
||||
}
|
||||
|
||||
type MinimaxResponse struct {
|
||||
// MinimaxCreateResponse 创建任务的响应
|
||||
type MinimaxCreateResponse struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
BaseResp struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
StatusMsg string `json:"status_msg"`
|
||||
} `json:"base_resp"`
|
||||
Video struct {
|
||||
URL string `json:"url"`
|
||||
Duration int `json:"duration"`
|
||||
} `json:"video"`
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
// MinimaxQueryResponse 查询任务状态的响应
|
||||
type MinimaxQueryResponse struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"` // Processing, Success, Failed
|
||||
FileID string `json:"file_id"`
|
||||
VideoWidth int `json:"video_width"`
|
||||
VideoHeight int `json:"video_height"`
|
||||
BaseResp struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
StatusMsg string `json:"status_msg"`
|
||||
} `json:"base_resp"`
|
||||
}
|
||||
|
||||
// MinimaxFileResponse 获取文件信息的响应
|
||||
type MinimaxFileResponse struct {
|
||||
File struct {
|
||||
FileID string `json:"file_id"`
|
||||
Bytes int `json:"bytes"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Filename string `json:"filename"`
|
||||
Purpose string `json:"purpose"`
|
||||
DownloadURL string `json:"download_url"`
|
||||
} `json:"file"`
|
||||
BaseResp struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
StatusMsg string `json:"status_msg"`
|
||||
} `json:"base_resp"`
|
||||
}
|
||||
|
||||
func NewMinimaxClient(baseURL, apiKey, model string) *MinimaxClient {
|
||||
@@ -61,6 +112,7 @@ func NewMinimaxClient(baseURL, apiKey, model string) *MinimaxClient {
|
||||
}
|
||||
|
||||
// GenerateVideo 生成视频(支持首尾帧和主体参考)
|
||||
// 步骤1:创建任务,返回 task_id
|
||||
func (c *MinimaxClient) GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error) {
|
||||
options := &VideoOptions{
|
||||
Duration: 6,
|
||||
@@ -87,19 +139,26 @@ func (c *MinimaxClient) GenerateVideo(imageURL, prompt string, opts ...VideoOpti
|
||||
reqBody.Resolution = options.Resolution
|
||||
}
|
||||
|
||||
// 如果有首帧图片(从imageURL或FirstFrameURL)
|
||||
// 支持首帧图片
|
||||
if options.FirstFrameURL != "" {
|
||||
reqBody.FirstFrameImage = options.FirstFrameURL
|
||||
} else if imageURL != "" {
|
||||
reqBody.FirstFrameImage = imageURL
|
||||
}
|
||||
|
||||
// 支持尾帧图片
|
||||
if options.LastFrameURL != "" {
|
||||
reqBody.LastFrameImage = options.LastFrameURL
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
endpoint := c.BaseURL + "/v1/video_generation"
|
||||
// 步骤1:创建任务,POST 请求
|
||||
// 注意:BaseURL 应该已包含 /v1,例如 https://api.minimaxi.com/v1
|
||||
endpoint := c.BaseURL + "/video_generation"
|
||||
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
@@ -123,32 +182,31 @@ func (c *MinimaxClient) GenerateVideo(imageURL, prompt string, opts ...VideoOpti
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var result MinimaxResponse
|
||||
var result MinimaxCreateResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.Error.Message != "" {
|
||||
return nil, fmt.Errorf("minimax error: %s", result.Error.Message)
|
||||
if result.BaseResp.StatusCode != 0 {
|
||||
return nil, fmt.Errorf("minimax error: %s", result.BaseResp.StatusMsg)
|
||||
}
|
||||
|
||||
// 第一步只返回 task_id,状态为 Processing
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.TaskID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
Duration: result.Video.Duration,
|
||||
}
|
||||
|
||||
if result.Video.URL != "" {
|
||||
videoResult.VideoURL = result.Video.URL
|
||||
videoResult.Completed = true
|
||||
Status: "Processing",
|
||||
Completed: false,
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
// GetTaskStatus 查询任务状态
|
||||
// 步骤2:查询任务状态,如果成功则进入步骤3获取文件下载地址
|
||||
func (c *MinimaxClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
endpoint := c.BaseURL + "/v1/video_generation/" + taskID
|
||||
// 步骤2:查询任务状态
|
||||
// 注意:BaseURL 应该已包含 /v1
|
||||
endpoint := fmt.Sprintf("%s/query/video_generation?task_id=%s", c.BaseURL, taskID)
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
@@ -167,26 +225,77 @@ func (c *MinimaxClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
var result MinimaxResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var queryResult MinimaxQueryResponse
|
||||
if err := json.Unmarshal(body, &queryResult); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if queryResult.BaseResp.StatusCode != 0 {
|
||||
return nil, fmt.Errorf("minimax error: %s", queryResult.BaseResp.StatusMsg)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.TaskID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
Duration: result.Video.Duration,
|
||||
TaskID: queryResult.TaskID,
|
||||
Status: queryResult.Status,
|
||||
Width: queryResult.VideoWidth,
|
||||
Height: queryResult.VideoHeight,
|
||||
Completed: false,
|
||||
}
|
||||
|
||||
if result.Error.Message != "" {
|
||||
videoResult.Error = result.Error.Message
|
||||
}
|
||||
|
||||
if result.Video.URL != "" {
|
||||
videoResult.VideoURL = result.Video.URL
|
||||
// 如果状态是 Success 且有 file_id,则获取文件下载地址
|
||||
if queryResult.Status == "Success" && queryResult.FileID != "" {
|
||||
downloadURL, err := c.getFileDownloadURL(queryResult.FileID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get download URL: %w", err)
|
||||
}
|
||||
videoResult.VideoURL = downloadURL
|
||||
videoResult.Completed = true
|
||||
} else if queryResult.Status == "Failed" {
|
||||
videoResult.Error = "Video generation failed"
|
||||
videoResult.Completed = true
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
// getFileDownloadURL 步骤3:根据 file_id 获取文件下载地址
|
||||
func (c *MinimaxClient) getFileDownloadURL(fileID string) (string, error) {
|
||||
// 注意:BaseURL 应该已包含 /v1
|
||||
endpoint := fmt.Sprintf("%s/files/retrieve?file_id=%s", c.BaseURL, fileID)
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var fileResult MinimaxFileResponse
|
||||
if err := json.Unmarshal(body, &fileResult); err != nil {
|
||||
return "", fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if fileResult.BaseResp.StatusCode != 0 {
|
||||
return "", fmt.Errorf("minimax error: %s", fileResult.BaseResp.StatusMsg)
|
||||
}
|
||||
|
||||
return fileResult.File.DownloadURL, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user