重构: 将API配置提取到config模块并优化错误处理
- 将硬编码的API配置移到config模块,支持环境变量配置 - 添加API Key验证和配置检查 - 添加图片上传大小限制(10MB) - 移除不必要的注释,简化代码 - 改进错误处理和日志记录
This commit is contained in:
@@ -13,16 +13,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"jwts/config"
|
||||
"jwts/models"
|
||||
)
|
||||
|
||||
// 配置 - 使用您提供的API配置
|
||||
const (
|
||||
APIURL = "https://api.littlelan.cn/v1/chat/completions"
|
||||
APIKey = "sk-Ll7P7vuiLum3N0pj7XaYfwHhD5UW1rDYlFoQ7F5JmeBfTewZ"
|
||||
Model = "qwen3.5-plus" // 使用qwen3.5-plus模型
|
||||
)
|
||||
|
||||
// APIRequest API请求结构
|
||||
type APIRequest struct {
|
||||
Model string `json:"model"`
|
||||
@@ -68,6 +62,11 @@ func init() {
|
||||
client = &http.Client{
|
||||
Timeout: 120 * time.Second,
|
||||
}
|
||||
|
||||
if config.RecognizerAPIKey == "" {
|
||||
log.Println("警告: RECOGNIZER_API_KEY 未设置,图片识别功能将无法使用")
|
||||
}
|
||||
|
||||
log.Println("recognizer模块已初始化")
|
||||
}
|
||||
|
||||
@@ -133,12 +132,14 @@ func callAPI(imageBase64 string) ([]models.Course, error) {
|
||||
|
||||
// callAPISingle 单次API调用
|
||||
func callAPISingle(imageBase64 string) ([]models.Course, error) {
|
||||
// 构建请求 - 发送图片给AI识别
|
||||
// 禁用qwen3.5的思考模式,避免产生大量不必要的token消耗
|
||||
if config.RecognizerAPIKey == "" {
|
||||
return nil, fmt.Errorf("RECOGNIZER_API_KEY 未配置")
|
||||
}
|
||||
|
||||
falseVal := false
|
||||
zero := 0
|
||||
apiReq := APIRequest{
|
||||
Model: Model,
|
||||
Model: config.RecognizerModel,
|
||||
MaxTokens: 4000,
|
||||
EnableThinking: &falseVal,
|
||||
ThinkingBudget: &zero,
|
||||
@@ -175,14 +176,14 @@ JSON格式要求:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequest("POST", APIURL, bytes.NewBuffer(jsonData))
|
||||
httpReq, err := http.NewRequest("POST", config.RecognizerAPIURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpReq.Header = http.Header{
|
||||
"Content-Type": []string{"application/json"},
|
||||
"Authorization": []string{"Bearer " + APIKey},
|
||||
"Authorization": []string{"Bearer " + config.RecognizerAPIKey},
|
||||
}
|
||||
|
||||
fmt.Println("[识别] 正在调用API进行识别...")
|
||||
|
||||
Reference in New Issue
Block a user