-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
llm.go
360 lines (294 loc) · 8.98 KB
/
llm.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"log"
"os"
"strings"
"sync"
"time"
"github.com/dstotijn/go-notion"
"github.com/sashabaranov/go-openai"
"github.com/zhuochun/notion-toolset/transformer"
"golang.org/x/time/rate"
)
type LangModelConfig struct {
DatabaseID string `yaml:"databaseID"`
DatabaseQuery string `yaml:"databaseQuery"`
LookbackDays int `yaml:"lookbackDays"` // additional date info
// Read from a chain file instead of database, overwrite database configs above
// chain file is supported in flashback
ChainFile string `yaml:"chainFile"`
// LLM config prompt message
Prompt string `yaml:"prompt"`
Model string `yaml:"model"` // optional, default to GPT3-Turbo
Temperature *float32 `yaml:"temperature"` // optional
// LLM response format
// - format follow https://pkg.go.dev/github.com/dstotijn/go-notion#ParagraphBlock
// - when using JSON mode, always instruct the model to produce JSON via some message in the conversation
RespJSON bool `yaml:"respJSON"` // optional, default to false
RespTextBlock string `yaml:"respTextBlock"` // mandatory for JSON model, else optional and default convert to paragraphs
// Tuning https://developers.notion.com/reference/request-limits
TaskSpeed float64 `yaml:"taskSpeed"` // optional
// skip processing a pages if chars is <min or >max thresholds
PageMinChars int `yaml:"pageMinChars"`
PageMaxChars int `yaml:"pageMaxChars"`
}
type LangModel struct {
DebugMode bool
ExecOne string
Client *notion.Client
OpenaiClient *openai.Client
LangModelConfig
queryLimiter *rate.Limiter
taskPool chan notion.Page
queryPool chan *transformer.BlockFuture
}
func (m *LangModel) Validate() error {
if m.Prompt == "" {
return errors.Join(ErrConfigRequired, fmt.Errorf("set Prompt"))
}
// init OpenAI client
openaiToken := os.Getenv("DOT_OPENAI_KEY")
if openaiToken == "" {
return fmt.Errorf("missing token in env.DOT_OPENAI_KEY")
}
m.OpenaiClient = openai.NewClient(openaiToken)
// set default exportspeed
if m.TaskSpeed < 1 {
m.TaskSpeed = 2.8
} else if m.TaskSpeed > 3 {
m.TaskSpeed = 3
}
return nil
}
func (m *LangModel) Run() error {
m.queryLimiter = rate.NewLimiter(rate.Limit(m.TaskSpeed), int(m.TaskSpeed))
// workers to process LLM prompt per page
taskWg := new(sync.WaitGroup)
m.taskPool = m.StartLLMTasker(taskWg, int(m.TaskSpeed))
// workers to query content of notion blocks
queryWg := new(sync.WaitGroup)
m.queryPool = m.StartQuerier(queryWg, int(m.TaskSpeed))
// query database pages, queue each pages for export
pagesChan, errChan := m.ScanPages()
pageNum := 0
for pages := range pagesChan {
for _, page := range pages {
pageNum += 1
m.taskPool <- page
if m.DebugMode && pageNum%500 == 0 {
log.Printf("Scanned pages: %v so far", pageNum)
}
}
}
log.Printf("Scanned pages: %v", pageNum)
close(m.taskPool) // TODO do not support sub-page now, same as export cmd
taskWg.Wait()
close(m.queryPool)
queryWg.Wait()
select {
case err := <-errChan:
return err
default:
return nil
}
}
func (m *LangModel) ScanPages() (chan []notion.Page, chan error) {
if m.ExecOne != "" { // exec one page ID
return m.scanDirectPages([]string{m.ExecOne})
}
if m.ChainFile != "" { // exec IDs found in a file
content, err := os.ReadFile(m.ChainFile)
if err != nil {
log.Printf("Open file errored, file: %v, err: %v", m.ChainFile, err)
return m.scanDirectPages([]string{})
}
normalizedContent := strings.Replace(string(content), "\r\n", "\n", -1)
pageIDs := strings.Split(normalizedContent, "\n")
return m.scanDirectPages(pageIDs)
}
return m.scanDatabasePages()
}
func (m *LangModel) scanDirectPages(pageIDs []string) (chan []notion.Page, chan error) {
pagesChan := make(chan []notion.Page, len(pageIDs))
errChan := make(chan error, 1)
for _, pageID := range pageIDs {
if pageID == "" {
continue
}
pageID = transformer.SimpleID(pageID)
if page, err := m.Client.FindPageByID(context.Background(), pageID); err == nil {
pagesChan <- []notion.Page{page}
} else {
errChan <- err
}
}
close(pagesChan)
return pagesChan, errChan
}
func (m *LangModel) scanDatabasePages() (chan []notion.Page, chan error) {
q := NewDatabaseQuery(m.Client, m.DatabaseID)
date := "" // default
if m.LookbackDays > 0 {
date = time.Now().AddDate(0, 0, -m.LookbackDays).Format(layoutDate)
}
if err := q.SetQuery(m.DatabaseQuery, QueryBuilder{Date: date}); err != nil {
log.Panicf("Invalid query: %v, err: %v", m.DatabaseQuery, err)
}
if m.DebugMode {
log.Printf("DatabaseQuery Filter: %+v", q.Query.Filter)
log.Printf("DatabaseQuery Sorter: %+v", q.Query.Sorts)
}
return q.Go(context.Background(), 1, m.queryLimiter)
}
func (m *LangModel) StartQuerier(wg *sync.WaitGroup, size int) chan *transformer.BlockFuture {
taskPool := make(chan *transformer.BlockFuture, size)
for i := 0; i < size; i++ {
wg.Add(1)
go func() {
for task := range taskPool {
blocks, err := m.QueryBlocks(task.BlockID) // TODO add retry?
task.Write(blocks, err)
}
wg.Done()
}()
}
return taskPool
}
func (m *LangModel) QueryBlocks(blockID string) ([]notion.Block, error) {
m.queryLimiter.Wait(context.Background())
blocks := []notion.Block{}
cursor := ""
for {
query := ¬ion.PaginationQuery{StartCursor: cursor}
resp, err := m.Client.FindBlockChildrenByID(context.TODO(), blockID, query)
if err != nil {
return blocks, err
}
blocks = append(blocks, resp.Results...)
if resp.HasMore {
cursor = *resp.NextCursor
} else {
break
}
}
return blocks, nil
}
func (m *LangModel) StartLLMTasker(wg *sync.WaitGroup, size int) chan notion.Page {
taskPool := make(chan notion.Page, size)
for i := 0; i < size; i++ {
wg.Add(1)
go func() {
for page := range taskPool {
if err := m.runLLMPage(page); err != nil {
log.Printf("Failed to run LLM: %v", err)
}
}
wg.Done()
}()
}
return taskPool
}
func (m *LangModel) runLLMPage(page notion.Page) error {
blocks, err := m.QueryBlocks(page.ID)
if err != nil {
return fmt.Errorf("query block id: %v, err: %v", page.ID, err)
}
markdown := transformer.MarkdownConfig{
NoAlias: true,
NoFrontMatters: true,
NoMetadata: true,
TitleToH1: true,
PlainText: true,
}
t := transformer.New(markdown, &page, blocks, m.queryPool, nil)
content := t.Transform()
if len(content) < m.PageMinChars {
log.Printf("Skip content by MinChars=%v, id: %v, len: %v", m.PageMinChars, page.ID, len(content))
return nil
} else if m.PageMaxChars > 0 && len(content) > m.PageMaxChars {
log.Printf("Skip content by MaxChars=%v, id: %v, len: %v", m.PageMaxChars, page.ID, len(content))
return nil
}
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: m.Prompt,
},
{
Role: openai.ChatMessageRoleUser,
Content: content,
},
},
}
if m.Model != "" { // Use user config model
req.Model = m.Model
}
if m.Temperature != nil { // Use user config temperature
req.Temperature = *m.Temperature
}
if m.RespJSON { // Experimental
req.ResponseFormat = &openai.ChatCompletionResponseFormat{
Type: openai.ChatCompletionResponseFormatTypeJSONObject,
}
}
resp, err := m.OpenaiClient.CreateChatCompletion(context.Background(), req)
if err != nil {
return fmt.Errorf("openai chat err: %v", err)
}
block := notion.BlockChildrenResponse{}
if m.RespJSON {
block, err = m.WriteJSON(page, resp.Choices[0].Message.Content)
} else {
block, err = m.WriteBlock(page, resp.Choices[0].Message.Content)
}
if err != nil {
return err
}
if len(block.Results) > 0 {
log.Printf("Append block child %v", block.Results[0].ID())
}
return nil
}
func (m *LangModel) WriteBlock(page notion.Page, content string) (notion.BlockChildrenResponse, error) {
w := NewAppendBlock(m.Client, page.ID)
paragraphs := strings.Split(content, "\n")
for _, p := range paragraphs {
if p == "" { // skip empty lines
continue
}
p = strings.TrimPrefix(p, "- ") // TODO better handle response text
if m.RespTextBlock != "" {
if err := w.AddParagraph("LLM", m.RespTextBlock, BlockBuilder{
Date: time.Now().Format(layoutDate),
Content: template.HTMLEscaper(p),
}); err != nil {
return notion.BlockChildrenResponse{}, err
}
} else {
w.Blocks = append(w.Blocks, notion.ParagraphBlock{
RichText: []notion.RichText{
{Text: ¬ion.Text{Content: p}},
},
})
}
}
return w.Do(context.TODO())
}
func (m *LangModel) WriteJSON(page notion.Page, content string) (notion.BlockChildrenResponse, error) {
w := NewAppendBlock(m.Client, page.ID)
contentJSON := map[string]interface{}{}
if err := json.Unmarshal([]byte(content), &contentJSON); err != nil {
return notion.BlockChildrenResponse{}, fmt.Errorf("unmarshal content: %w", err)
}
if err := w.AddBlocks("llmJSON", m.RespTextBlock, contentJSON); err != nil {
return notion.BlockChildrenResponse{}, err
}
return w.Do(context.TODO())
}