-
Notifications
You must be signed in to change notification settings - Fork 1
/
fit.go
47 lines (43 loc) · 1.37 KB
/
fit.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
package openai
// FitChatContext returns messages that fit into maxTokenCount, skipping those that don't
// fit. This is meant for including knowledge base context into ChatGPT prompts.
func FitChatContext(candidates []Msg, maxTokenCount int, model string) ([]Msg, int) {
const minReasonableLen = 20
used := 0
remaining := maxTokenCount
var result []Msg
for _, msg := range candidates {
n := MsgTokenCount(msg, model)
if remaining >= n {
result = append(result, msg)
remaining -= n
used += n
}
if remaining < minReasonableLen {
break // don't waste time trying to fill a tiny hole
}
}
return result, used
}
func DropChatHistoryIfNeeded(chat []Msg, fixedSuffixLen int, maxTokens int, model string) ([]Msg, int) {
msgTokens := make([]int, len(chat))
usedTokens := chatTokenOverhead
for i, msg := range chat {
c := MsgTokenCount(msg, model) // this is by far the slowest op here; cache result to avoid calling twice
msgTokens[i] = c
usedTokens += c
}
var dropCount int
maxDropCount := len(chat) - fixedSuffixLen
for usedTokens > maxTokens && dropCount < maxDropCount {
i := fixedSuffixLen + dropCount // dropping message at this index
usedTokens -= msgTokens[i]
dropCount++
}
if dropCount > 0 {
copy(chat[fixedSuffixLen:], chat[fixedSuffixLen+dropCount:])
return chat[:len(chat)-dropCount], usedTokens
} else {
return chat, usedTokens
}
}