diff --git a/.gitignore b/.gitignore index 6390b73..186cd20 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ hr_receiver.iml main.go.bak config.yaml +*.exe +*.pdf +*.md +*.csv diff --git a/config.sample.yaml b/config.sample.yaml index ea3dcf2..32837dd 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -4,3 +4,7 @@ database: user: postgres password: root name: training_db +ai: + base_url: https://api.lkeap.cloud.tencent.com/v1 + api_key: "" + model: deepseek-v3.2 diff --git a/config/config.go b/config/config.go index 3f6fb33..5ac0680 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "gorm.io/driver/postgres" ) import ( @@ -9,6 +10,7 @@ import ( ) var DB *gorm.DB +var App AppConfig type AppConfig struct { DB struct { @@ -18,6 +20,11 @@ type AppConfig struct { Password string `yaml:"password"` Name string `yaml:"name"` } `yaml:"database"` + AI struct { + BaseURL string `yaml:"base_url"` + APIKey string `yaml:"api_key"` + Model string `yaml:"model"` + } `yaml:"ai"` } func InitConfig() { @@ -27,14 +34,17 @@ func InitConfig() { if err := viper.ReadInConfig(); err != nil { panic("Failed to read config: " + err.Error()) } + if err := viper.Unmarshal(&App); err != nil { + panic("Failed to parse config: " + err.Error()) + } } func ConnectDB() { - dsn := "host=" + viper.GetString("database.host") + - " user=" + viper.GetString("database.user") + - " password=" + viper.GetString("database.password") + - " dbname=" + viper.GetString("database.name") + - " port=" + viper.GetString("database.port") + + dsn := "host=" + App.DB.Host + + " user=" + App.DB.User + + " password=" + App.DB.Password + + " dbname=" + App.DB.Name + + " port=" + App.DB.Port + " sslmode=disable" var err error @@ -43,3 +53,16 @@ func ConnectDB() { panic("Failed to connect database: " + err.Error()) } } + +func GetAIConfig() (baseURL, apiKey, model string, err error) { + if App.AI.BaseURL == "" { + return "", "", "", fmt.Errorf("missing config: ai.base_url") + } + if App.AI.APIKey == "" { + return "", "", "", fmt.Errorf("missing config: ai.api_key") + } + if App.AI.Model == "" { + return "", "", "", fmt.Errorf("missing config: ai.model") + } + return App.AI.BaseURL, App.AI.APIKey, App.AI.Model, nil +} diff --git a/controllers/ai.go b/controllers/ai.go index c158dcb..c242584 100644 --- a/controllers/ai.go +++ b/controllers/ai.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/sashabaranov/go-openai" + "hr_receiver/config" "hr_receiver/util" "io" "io/ioutil" @@ -16,13 +17,6 @@ import ( "os" ) -// 配置文件 (与 main.go 保持一致) -const ( - BaseURL = "https://api.lkeap.cloud.tencent.com/v1" - APIKey = "sk-Y4zjnwulSuSlf60mrzwCxq2ipktHSs4jZHgWeQOArWuWJEOd" // 请替换为实际的 API Key - Model = "deepseek-v3" -) - // readDocxContent 读取 .docx 文件并将其转换为结构化文本 // 修改为先保存临时文件再读取 func readDocxContent(fileHeader *multipart.FileHeader) (string, error) { @@ -143,14 +137,19 @@ func buildAnalysisPrompt(teachingPlanContent, heartRateContent string) string { // callAIForAnalysis 调用大模型进行分析 func callAIForAnalysis(prompt string) (string, error) { - config := openai.DefaultConfig(APIKey) - config.BaseURL = BaseURL - client := openai.NewClientWithConfig(config) + baseURL, apiKey, model, err := config.GetAIConfig() + if err != nil { + return "", err + } + + clientConfig := openai.DefaultConfig(apiKey) + clientConfig.BaseURL = baseURL + client := openai.NewClientWithConfig(clientConfig) resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ - Model: Model, + Model: model, Messages: []openai.ChatCompletionMessage{ { Role: openai.ChatMessageRoleUser,