refactor: ai config.
This commit is contained in:
@@ -2,3 +2,7 @@
|
||||
hr_receiver.iml
|
||||
main.go.bak
|
||||
config.yaml
|
||||
*.exe
|
||||
*.pdf
|
||||
*.md
|
||||
*.csv
|
||||
|
||||
@@ -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
|
||||
|
||||
+28
-5
@@ -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
|
||||
}
|
||||
|
||||
+10
-11
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user