forked from linexjlin/inputGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
105 lines (87 loc) · 2.1 KB
/
config.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"github.com/hanyuancheung/gpt-go"
)
func isDebug() bool {
return os.Getenv("DEBUG") != ""
}
func getGPTHotkeys() []string {
hotkeys := os.Getenv("GPT_HOTKEYS")
if hotkeys == "" {
if runtime.GOOS == "windows" {
hotkeys = "space+shift"
} else if runtime.GOOS == "darwin" {
hotkeys = "space+shift"
} else {
hotkeys = "space+shift"
}
}
return strings.Split(hotkeys, "+")
}
func getOpenAIkey() string {
return os.Getenv("OPENAI_API_KEY")
}
func getOpenAIBaseUrl() string {
if os.Getenv("OPENAI_API_BASE_URL") != "" {
return os.Getenv("OPENAI_API_BASE_URL")
}
return "https://api.openai.com/v1"
}
func getMaxContext() int {
maxContext := os.Getenv("MAX_CONTEXT")
maxContextInt, err := strconv.Atoi(maxContext)
if err != nil {
fmt.Println("MAX_CONTEXT不是一个有效的数字")
return 0
} else {
return maxContextInt
}
}
type ModePrompt struct {
Name string `json:"name"`
Model string `json:"model"`
HeadMessages []gpt.ChatCompletionRequestMessage `json:"headMessages"`
MaxContext int `json:"maxContext"`
}
func loadModePrompt(filepath string) (ModePrompt, error) {
var prompt ModePrompt
// Read the file contents
data, err := os.ReadFile(filepath)
if err != nil {
return prompt, err
}
// Unmarshal the JSON data into the ModePrompt struct
err = json.Unmarshal(data, &prompt)
if err != nil {
return prompt, err
}
return prompt, nil
}
func parseModePrompt(content string) (ModePrompt, error) {
var prompt ModePrompt
// Unmarshal the JSON data into the ModePrompt struct
err := json.Unmarshal([]byte(content), &prompt)
if err != nil {
return prompt, err
}
return prompt, nil
}
func saveModePrompt(prompt ModePrompt, filepath string) error {
// Marshal the ModePrompt struct into JSON data
data, err := json.Marshal(prompt)
if err != nil {
return err
}
// Write the JSON data to the file
err = os.WriteFile(filepath, data, 0644)
if err != nil {
return err
}
return nil
}