feat: load .env file from current working directory upward recursively
This commit is contained in:
parent
b5f3e7ff96
commit
3801ffb744
|
@ -1 +1 @@
|
|||
v5.0.0-beta-2503192247
|
||||
v5.0.0-beta-2503201423
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue