feat: load .env file from current working directory upward recursively

This commit is contained in:
lilong.129 2025-03-20 14:16:32 +08:00
parent b5f3e7ff96
commit 3801ffb744
4 changed files with 45 additions and 14 deletions

View File

@ -1 +1 @@
v5.0.0-beta-2503192247
v5.0.0-beta-2503201423

View File

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
@ -31,6 +32,46 @@ const (
EnvOpenAIInitConfigJSON = "OPENAI_INIT_CONFIG_JSON"
)
func init() {
err := loadEnv()
if err != nil {
log.Warn().Err(err).Msg("load .env file failed")
}
}
// loadEnv loads environment variables from .env file
// it will search for .env file from current working directory upward recursively
func loadEnv() error {
// get current working directory
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %v", err)
}
// locate .env file from current working directory upward recursively
envPath := cwd
for {
envFile := filepath.Join(envPath, ".env")
if _, err := os.Stat(envFile); err == nil {
// found .env file
// override existing env variables
err = godotenv.Overload(envFile)
if err != nil {
return fmt.Errorf("failed to load env file: %v", err)
}
log.Info().Str("path", envFile).Msg("load env success")
return nil
}
// reached root directory
parent := filepath.Dir(envPath)
if parent == envPath {
return fmt.Errorf("no .env file found from current directory to root")
}
envPath = parent
}
}
func checkEnvLLM() error {
openaiBaseURL := os.Getenv("OPENAI_BASE_URL")
if openaiBaseURL == "" {
@ -50,17 +91,6 @@ func checkEnvLLM() error {
return nil
}
// loadEnv loads environment variables from a file
func loadEnv(envPath string) error {
err := godotenv.Load(envPath)
if err != nil {
return err
}
log.Info().Str("path", envPath).Msg("load env success")
return nil
}
func GetEnvConfig(key string) string {
return os.Getenv(key)
}

View File

@ -13,6 +13,7 @@ import (
"strings"
"github.com/cloudwego/eino-ext/components/model/openai"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@ -44,7 +45,7 @@ func NewPlanner(ctx context.Context) (*Planner, error) {
type Planner struct {
ctx context.Context
model *openai.ChatModel
model model.ChatModel
parser *ActionParser
}

View File

@ -25,7 +25,7 @@ func TestVLMPlanning(t *testing.T) {
5. 得分机制: 每成功连接并消除一对图案玩家会获得相应的分数完成游戏后根据剩余时间和消除效率计算总分
6. 关卡设计: 游戏可能包含多个关卡随着关卡的推进图案的复杂度和数量会增加`
userInstruction += "\n\n请基于以上游戏规则给出消除所有图案的行动序列"
userInstruction += "\n\n请基于以上游戏规则请依次点击两个可消除的相同图案"
planner, err := NewPlanner(context.Background())
require.NoError(t, err)