Skip to content

Commit db0f28e

Browse files
authored
Add temp setting
1 parent aedc5f5 commit db0f28e

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

cmd/ask/main.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"os"
1313
"path/filepath"
14+
"strconv"
1415
"strings"
1516

1617
"golang.org/x/crypto/ssh/terminal"
@@ -23,8 +24,9 @@ const AI = "AI 🤖: "
2324
var messageHistory []Message
2425

2526
type chatRequest struct {
26-
Model string `json:"model"`
27-
Messages []Message `json:"messages"`
27+
Model string `json:"model"`
28+
Temperature float32 `json:"temperature"`
29+
Messages []Message `json:"messages"`
2830
}
2931

3032
type Message struct {
@@ -52,9 +54,10 @@ type completionResponse struct {
5254
}
5355

5456
type Settings struct {
55-
APIKey string `json:"api_key"`
56-
Model string `json:"model"`
57-
Role string `json:"role"`
57+
APIKey string `json:"api_key"`
58+
Model string `json:"model"`
59+
Temperature float32 `json:"temperature"`
60+
Role string `json:"role"`
5861
}
5962

6063
func main() {
@@ -175,6 +178,26 @@ func modifySettings(filePath string) Settings {
175178
}
176179
}
177180

181+
if settings.Temperature == 0 {
182+
fmt.Println("Please enter the temperature (Default is 0.7):")
183+
184+
scanner := bufio.NewScanner(os.Stdin)
185+
if scanner.Scan() {
186+
input := scanner.Text()
187+
temp, err := strconv.ParseFloat(input, 32)
188+
189+
if err == nil && temp > 0 && temp <= 1 {
190+
settings.Temperature = float32(temp)
191+
} else {
192+
settings.Temperature = 0.7
193+
}
194+
}
195+
196+
if settings.Temperature == 0 {
197+
settings.Temperature = 0.7
198+
}
199+
}
200+
178201
contents, _ := json.Marshal(settings)
179202
os.WriteFile(filePath, contents, 0600)
180203

@@ -194,8 +217,9 @@ func askAI(apiKey, model, question string) string {
194217
messageHistory = append(messageHistory, Message{Role: "user", Content: question})
195218

196219
requestBody := chatRequest{
197-
Model: model,
198-
Messages: messageHistory,
220+
Model: model,
221+
Temperature: 0.7,
222+
Messages: messageHistory,
199223
}
200224

201225
jsonValue, _ := json.Marshal(requestBody)

0 commit comments

Comments
 (0)