-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.go
141 lines (110 loc) · 2.58 KB
/
chat.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
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
package oax
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"unicode"
"github.com/itchyny/timefmt-go"
"github.com/pelletier/go-toml"
"github.com/shuntaka9576/oax/openai"
)
type ChatMessage struct {
Role string `toml:"role"`
Content string `toml:"content"`
}
type ChatLogToml struct {
Messages []ChatMessage `toml:"messages"`
}
type ChatLog struct {
ConfigDir string
ChatLogToml ChatLogToml
FilePath *string
}
func (c *ChatLog) AddChatMessage(chatMessage ChatMessage) *ChatLog {
c.ChatLogToml.Messages = append(c.ChatLogToml.Messages, chatMessage)
return c
}
func (c *ChatLog) FilePathForUser() (string, error) {
valuePath := *c.FilePath
replaced, err := replaceHomedirWithTilde(valuePath)
if err != nil {
return "", err
}
return replaced, nil
}
func (c *ChatLog) DeleteFile() error {
err := os.Remove(*c.FilePath)
if err != nil {
return err
}
return nil
}
func (c *ChatLog) LoadLogMessage() error {
data, err := ioutil.ReadFile(*c.FilePath)
if err != nil {
return err
}
var chatLogToml ChatLogToml
err = toml.Unmarshal(data, &chatLogToml)
if err != nil {
return err
}
c.ChatLogToml.Messages = chatLogToml.Messages
for i := range c.ChatLogToml.Messages {
c.ChatLogToml.Messages[i].Content = strings.TrimRightFunc(c.ChatLogToml.Messages[i].Content, unicode.IsSpace)
}
return nil
}
func (c *ChatLog) CreateOpenAIMessages() (messages []openai.Message) {
for _, message := range c.ChatLogToml.Messages {
messages = append(messages, openai.Message{
Content: message.Content,
Role: message.Role,
})
}
return messages
}
func (c *ChatLog) FlushFile() error {
var builder strings.Builder
for _, message := range c.ChatLogToml.Messages {
builder.WriteString(fmt.Sprintf(`[[messages]]
role = "%s"
content = '''
%s
'''
`, message.Role, message.Content))
}
err := ioutil.WriteFile(*c.FilePath, []byte(builder.String()), 0644)
if err != nil {
return err
}
return nil
}
func (c *ChatLog) InitLogFile(title string, fileNameFormat string) {
var useFormat string
if fileNameFormat == "" {
if title == "" {
useFormat = "%Y-%m-%d_%H-%M-%S"
} else {
useFormat = "%Y-%m-%d_%H-%M-%S_${title}"
}
} else {
useFormat = fileNameFormat
}
useFormat = strings.ReplaceAll(useFormat, "${title}", title)
t := time.Now()
filename := timefmt.Format(t, useFormat) + ".toml"
filePath := filepath.Join(c.ConfigDir, filename)
c.FilePath = &filePath
}
func (c *ChatLog) LoadFile(filePath string) error {
filePath, err := replaceTildeWithHomedir(filePath)
if err != nil {
return err
}
c.FilePath = &filePath
return nil
}