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