-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcredential.go
More file actions
49 lines (46 loc) · 1.3 KB
/
Copy pathcredential.go
File metadata and controls
49 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"log"
"os"
"path/filepath"
)
type Credentials struct {
OpenAIAPIKey string `yaml:"openai_api_key"`
}
func ReadOpenAIAPIKey() (string, error) {
// first try to read from env
key, found := os.LookupEnv("OPENAI_API_KEY")
if found {
return key, nil
}
// then try to read from ~/.aichat/credentials.yml
homedir, err := os.UserHomeDir()
if err != nil {
return "", err
}
path := filepath.Join(homedir, ".aichat", "credentials.yml")
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return "", fmt.Errorf("OpenAI API credentials not found\nTo use aichat, you need to set up your API key using one of these methods:\n\n" +
"1. Create a credentials file at: %s with the following content:\n" +
" ```yaml\n" +
" openai_api_key: YOUR_API_KEY\n" +
" ```\n\n" +
"2. Or set the OPENAI_API_KEY environment variable:\n" +
" export OPENAI_API_KEY=your_api_key\n\n" +
"See README.md for more information", path)
}
return "", err
}
// check permission and warn if its too open
if info.Mode()&0077 != 0 {
log.Printf("WARN: credentials file %s has too open permission\n", path)
}
credentials := &Credentials{}
if err := ReadYamlFromFile(path, credentials); err != nil {
return "", err
}
return credentials.OpenAIAPIKey, nil
}