-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (89 loc) · 2.24 KB
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
openai "github.com/sashabaranov/go-openai"
)
func main() {
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
rawMessageCap := os.Getenv("MESSAGE_CAP")
geppetoDebug := os.Getenv("GEPPETO_DEBUG")
debug := strings.Contains(geppetoDebug, "yes")
if rawMessageCap == "" {
rawMessageCap = "50"
}
messageCap, err := strconv.Atoi(rawMessageCap)
if err != nil {
log.Fatal("Failed to parse message cap:", err)
}
req := openai.ChatCompletionRequest{
Stream: true,
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "you are a helpful chatbot",
},
},
}
infifo := os.Args[1]
outfifo := os.Args[2]
inputFile, err := os.OpenFile(infifo, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
log.Fatal("Failed to open input FIFO:", err)
}
defer inputFile.Close()
outputFile, err := os.OpenFile(outfifo, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
log.Fatal("Failed to open output FIFO:", err)
}
defer outputFile.Close()
s := bufio.NewScanner(inputFile)
for s.Scan() {
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: s.Text(),
})
stream, err := client.CreateChatCompletionStream(context.Background(), req)
if err != nil {
msg := fmt.Sprintf("ChatCompletion error: %v", err)
if debug {
outputFile.WriteString(msg)
outputFile.WriteString("\n")
}
fmt.Printf("%s\n", msg)
continue
}
msg := openai.ChatCompletionMessage{Role: openai.ChatMessageRoleAssistant}
BUFFERING:
for {
resp, err := stream.Recv()
if err != nil {
if err == io.EOF {
outputFile.WriteString("\n")
break BUFFERING
}
msg := fmt.Sprintf("ChatCompletion error: %v", err)
if debug {
outputFile.WriteString(msg)
outputFile.WriteString("\n")
}
fmt.Printf("%s\n", msg)
}
outputFile.WriteString(fmt.Sprintf("%s", resp.Choices[0].Delta.Content))
msg.Content = msg.Content + resp.Choices[0].Delta.Content
}
req.Messages = append(req.Messages, msg)
ran := len(req.Messages) - messageCap
if ran < 0 {
ran = 0
}
req.Messages = req.Messages[ran:]
}
}