Skip to content

Commit

Permalink
Fix error when locating config.yaml
Browse files Browse the repository at this point in the history
Implements a fallback mechanism for configuration loading and improves error handling in `azure/init.go`.

- Adds a check for the existence of `config.yaml` before attempting to read it. If the file does not exist, the application now falls back to loading configuration from environment variables instead of terminating with an error.
- Updates the error message when `config.yaml` cannot be located to suggest checking the file's existence or using environment variables for configuration.
- Ensures that if both the configuration file is missing and required environment variables are not set, a clear error is returned indicating the absence of both configuration sources.


---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/stulzq/azure-openai-proxy?shareId=906b60bd-c6a9-40b1-b401-639f8a386023).
  • Loading branch information
PeterDaveHello committed May 12, 2024
1 parent e85eed3 commit 065c1b1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions azure/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stulzq/azure-openai-proxy/util"
"log"
"net/url"
"os"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -101,6 +102,22 @@ func InitFromConfigFile() error {
configFile = filepath.Join(util.GetWorkdir(), configFile)
}

if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Printf("config file %s does not exist, falling back to environment variables", configFile)
apiVersion := viper.GetString(constant.ENV_AZURE_OPENAI_API_VER)
endpoint := viper.GetString(constant.ENV_AZURE_OPENAI_ENDPOINT)
openaiModelMapper := viper.GetString(constant.ENV_AZURE_OPENAI_MODEL_MAPPER)
if endpoint != "" && openaiModelMapper != "" {
if apiVersion == "" {
apiVersion = "2023-07-01-preview"
}
InitFromEnvironmentVariables(apiVersion, endpoint, openaiModelMapper)
return nil
} else {
return fmt.Errorf("config file %s does not exist and no environment variables set", configFile)
}
}

viper.SetConfigType("yaml")
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
Expand Down

0 comments on commit 065c1b1

Please sign in to comment.