Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
294 lines (275 loc) · 8.83 KB
/
Copy pathmain.go
File metadata and controls
294 lines (275 loc) · 8.83 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
"time"
openai "github.com/sashabaranov/go-openai"
"github.com/spf13/cobra"
)
const defaultModel = openai.GPT4o
const helpText = `Commands:
/system <prompt> set the system prompt and start a new conversation
/reset clear the conversation history
/save <file> save the conversation as JSON (use --force to overwrite)
/load <file> load a saved JSON conversation
/image <file> attach an image (png, jpg, gif, webp) to your next question
/draw <prompt> generate an image and save it as draw-<time>.png
/help show this help
quit, exit leave
Press Ctrl+C while an answer is streaming to interrupt it.`
// lookupAPIKey returns the API key from API_KEY or OPENAI_API_KEY.
func lookupAPIKey() string {
if key := os.Getenv("API_KEY"); key != "" {
return key
}
return os.Getenv("OPENAI_API_KEY")
}
func envOr(name, fallback string) string {
if v := os.Getenv(name); v != "" {
return v
}
return fallback
}
// ask runs one request. Ctrl+C cancels the request instead of the program.
func ask(ctx context.Context, chat *Chat, timeout time.Duration, question string) error {
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
defer stop()
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
if err := chat.Ask(ctx, os.Stdout, question); err != nil {
if errors.Is(ctx.Err(), context.Canceled) {
fmt.Fprintln(os.Stderr, "[interrupted]")
} else {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return err
}
}
if usage := chat.Usage(); usage != nil {
fmt.Fprintf(os.Stderr, "Tokens: %d prompt, %d completion, %d total",
usage.PromptTokens, usage.CompletionTokens, usage.TotalTokens)
if cost, ok := estimateCost(chat.model, usage); ok {
fmt.Fprintf(os.Stderr, " (estimated cost: $%.6f)", cost)
}
fmt.Fprintln(os.Stderr)
}
return nil
}
func run(ctx context.Context, in io.Reader, chat *Chat, timeout time.Duration) {
scanner := bufio.NewScanner(in)
scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
for {
fmt.Print("Input your question (type `quit` to exit, `/help` for commands): ")
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
switch {
case input == "quit" || input == "exit":
return
case input == "/help":
fmt.Println(helpText)
case input == "/reset":
chat.Reset()
fmt.Println("Conversation cleared.")
case input == "/save" || strings.HasPrefix(input, "/save "):
fields := strings.Fields(input)
force := false
var path string
for _, field := range fields[1:] {
if field == "--force" {
force = true
} else if path == "" {
path = field
} else {
path = ""
break
}
}
if path == "" {
fmt.Fprintln(os.Stderr, "Usage: /save [--force] <file>")
} else if err := chat.Save(path, force); err != nil {
fmt.Fprintf(os.Stderr, "Error saving conversation: %v\n", err)
} else {
fmt.Printf("Conversation saved to %s.\n", path)
}
case input == "/load" || strings.HasPrefix(input, "/load "):
fields := strings.Fields(input)
if len(fields) != 2 {
fmt.Fprintln(os.Stderr, "Usage: /load <file>")
} else if err := chat.Load(fields[1]); err != nil {
fmt.Fprintf(os.Stderr, "Error loading conversation: %v\n", err)
} else {
fmt.Printf("Conversation loaded from %s.\n", fields[1])
}
case input == "/image" || strings.HasPrefix(input, "/image "):
path := strings.TrimSpace(strings.TrimPrefix(input, "/image"))
if path == "" {
fmt.Fprintln(os.Stderr, "Usage: /image <file>")
} else if err := chat.AttachImage(path); err != nil {
fmt.Fprintf(os.Stderr, "Error attaching image: %v\n", err)
} else {
fmt.Printf("Image attached (%d pending); it is sent with your next question.\n", chat.PendingImages())
}
case input == "/draw" || strings.HasPrefix(input, "/draw "):
prompt := strings.TrimSpace(strings.TrimPrefix(input, "/draw"))
if prompt == "" {
fmt.Fprintln(os.Stderr, "Usage: /draw <prompt>")
} else {
drawCtx, stop := signal.NotifyContext(ctx, os.Interrupt)
if timeout > 0 {
var cancel context.CancelFunc
drawCtx, cancel = context.WithTimeout(drawCtx, timeout)
defer cancel()
}
path, err := chat.Draw(drawCtx, ".", prompt)
stop()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
} else {
fmt.Printf("Image saved to %s\n", path)
}
}
case input == "/system" || strings.HasPrefix(input, "/system "):
chat.SetSystem(strings.TrimSpace(strings.TrimPrefix(input, "/system")))
fmt.Println("System prompt updated, conversation cleared.")
default:
if question := validateQuestion(input); question != "" {
_ = ask(ctx, chat, timeout, question)
}
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
}
}
func main() {
var (
model string
system string
prompt string
baseURL string
timeout time.Duration
effort string
schema string
)
rootCmd := &cobra.Command{
Use: "chatgpt",
Short: "Chat with ChatGPT in console.",
RunE: func(cmd *cobra.Command, args []string) error {
apiKey := lookupAPIKey()
if apiKey == "" {
return errors.New("missing API key: set API_KEY or OPENAI_API_KEY")
}
cfg := openai.DefaultConfig(apiKey)
if baseURL != "" {
cfg.BaseURL = baseURL
}
chat := NewChat(openai.NewClientWithConfig(cfg), model, system)
chat.ReasoningEffort = effort
chat.ImageModel = os.Getenv("OPENAI_IMAGE_MODEL")
if schema != "" {
raw := []byte(schema)
if strings.HasPrefix(schema, "@") {
var err error
if raw, err = os.ReadFile(schema[1:]); err != nil {
return fmt.Errorf("read json schema: %w", err)
}
}
if !json.Valid(raw) {
return errors.New("--json-schema is not valid JSON")
}
chat.JSONSchema = json.RawMessage(raw)
}
if prompt != "" || !isTerminal(os.Stdin) {
var input []byte
if !isTerminal(os.Stdin) {
var err error
input, err = readLimited(os.Stdin, maxStdinSize)
if err != nil {
return fmt.Errorf("read stdin: %w", err)
}
}
question := prompt
if piped := strings.TrimSpace(string(input)); piped != "" {
if question != "" {
question += "\n\n"
}
question += piped
}
question = strings.TrimSpace(question)
if question == "" {
return errors.New("provide a prompt with -p or pipe input to stdin")
}
if err := ask(cmd.Context(), chat, timeout, question); err != nil {
return err
}
return nil
}
run(cmd.Context(), os.Stdin, chat, timeout)
return nil
},
}
rootCmd.Flags().StringVarP(&model, "model", "m", envOr("OPENAI_MODEL", defaultModel), "model to use (env OPENAI_MODEL)")
rootCmd.Flags().StringVarP(&system, "system", "s", "", "system prompt")
rootCmd.Flags().StringVarP(&prompt, "prompt", "p", "", "send one prompt and exit")
rootCmd.Flags().StringVar(&baseURL, "base-url", envOr("OPENAI_BASE_URL", ""), "OpenAI-compatible API base URL (env OPENAI_BASE_URL)")
rootCmd.Flags().StringVar(&effort, "reasoning-effort", envOr("OPENAI_REASONING_EFFORT", ""), "reasoning effort for o-series models: low, medium, high (env OPENAI_REASONING_EFFORT)")
rootCmd.Flags().StringVar(&schema, "json-schema", "", "force JSON output matching this JSON schema (inline JSON or @file)")
rootCmd.Flags().DurationVar(&timeout, "timeout", 5*time.Minute, "maximum time for one answer (0 for no limit)")
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
os.Exit(1)
}
}
// maxStdinSize bounds piped input so unbounded streams cannot exhaust memory.
const maxStdinSize = 16 << 20
// readLimited reads all of r, failing if it holds more than limit bytes.
func readLimited(r io.Reader, limit int64) ([]byte, error) {
data, err := io.ReadAll(io.LimitReader(r, limit+1))
if err != nil {
return nil, err
}
if int64(len(data)) > limit {
return nil, fmt.Errorf("input exceeds %d MiB limit", limit>>20)
}
return data, nil
}
func isTerminal(file *os.File) bool {
info, err := file.Stat()
return err == nil && info.Mode()&os.ModeCharDevice != 0
}
func estimateCost(model string, usage *openai.Usage) (float64, bool) {
type rates struct{ input, output float64 }
prices := map[string]rates{
"gpt-4o": {2.50, 10.00},
"gpt-4o-mini": {0.15, 0.60},
"gpt-4.1": {2.00, 8.00},
"gpt-4.1-mini": {0.40, 1.60},
"gpt-4.1-nano": {0.10, 0.40},
}
rate, ok := prices[model]
if !ok || usage == nil {
return 0, false
}
return (float64(usage.PromptTokens)*rate.input +
float64(usage.CompletionTokens)*rate.output) / 1_000_000, true
}
func validateQuestion(question string) string {
quest := strings.Trim(question, " ")
keywords := []string{"", "loop", "break", "continue", "cls", "block"}
for _, x := range keywords {
if quest == x {
return ""
}
}
return quest
}