forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.go
112 lines (94 loc) · 2.75 KB
/
count.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
package openai
import (
"encoding/json"
openai "github.com/gptscript-ai/chat-completion-client"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/pkoukk/tiktoken-go"
tiktoken_loader "github.com/pkoukk/tiktoken-go-loader"
)
func init() {
tiktoken.SetBpeLoader(tiktoken_loader.NewOfflineLoader())
}
const DefaultMaxTokens = 128_000
func decreaseTenPercent(maxTokens int) int {
maxTokens = getBudget(maxTokens)
return int(float64(maxTokens) * 0.9)
}
func getBudget(maxTokens int) int {
if maxTokens <= 0 {
return DefaultMaxTokens
}
return maxTokens
}
func dropMessagesOverCount(maxTokens, toolTokenCount int, msgs []openai.ChatCompletionMessage) (result []openai.ChatCompletionMessage, err error) {
var (
lastSystem int
withinBudget int
budget = getBudget(maxTokens) - toolTokenCount
)
for i, msg := range msgs {
if msg.Role == openai.ChatMessageRoleSystem {
count, err := countMessage(msg)
if err != nil {
return nil, err
}
budget -= count
lastSystem = i
result = append(result, msg)
} else {
break
}
}
for i := len(msgs) - 1; i > lastSystem; i-- {
withinBudget = i
count, err := countMessage(msgs[i])
if err != nil {
return nil, err
}
budget -= count
if budget <= 0 {
break
}
}
// OpenAI gets upset if there is a tool message without a tool call preceding it.
// Check the oldest message within budget, and if it is a tool message, just drop it.
// We do this in a loop because it is possible for multiple tool messages to be in a row,
// due to parallel tool calls.
for withinBudget < len(msgs) && msgs[withinBudget].Role == openai.ChatMessageRoleTool {
withinBudget++
}
if withinBudget == len(msgs)-1 {
// We are going to drop all non system messages, which seems useless, so just return them
// all and let it fail
return msgs, nil
}
return append(result, msgs[withinBudget:]...), nil
}
func countMessage(msg openai.ChatCompletionMessage) (int, error) {
encoding, err := tiktoken.GetEncoding("o200k_base")
if err != nil {
return 0, err
}
count := len(encoding.Encode(msg.Role, nil, nil))
count += len(encoding.Encode(msg.Content, nil, nil))
for _, content := range msg.MultiContent {
count += len(encoding.Encode(content.Text, nil, nil))
}
for _, tool := range msg.ToolCalls {
count += len(encoding.Encode(tool.Function.Name, nil, nil))
count += len(encoding.Encode(tool.Function.Arguments, nil, nil))
}
count += len(encoding.Encode(msg.ToolCallID, nil, nil))
return count, nil
}
func countTools(tools []types.ChatCompletionTool) (int, error) {
encoding, err := tiktoken.GetEncoding("o200k_base")
if err != nil {
return 0, err
}
toolJSON, err := json.Marshal(tools)
if err != nil {
return 0, err
}
return len(encoding.Encode(string(toolJSON), nil, nil)), nil
}