-
Notifications
You must be signed in to change notification settings - Fork 58
/
token.go
153 lines (127 loc) · 2.97 KB
/
token.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
package openai
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkoukk/tiktoken-go"
)
type TokenCounter struct {
}
func NewTokenCounter() *TokenCounter {
// tiktoken.SetBpeLoader(tiktoken_loader.NewOfflineLoader())
return &TokenCounter{}
}
func (tc *TokenCounter) Count(model string, input string) (int, error) {
encoder, err := tiktoken.GetEncoding("r50k_base")
if err != nil {
return 0, err
}
if strings.HasPrefix(model, "text-search") || strings.HasPrefix(model, "text-similarity") {
token := encoder.Encode(input, nil, nil)
return len(token), nil
}
encoder, err = tiktoken.EncodingForModel(model)
if err != nil {
return 0, err
}
token := encoder.Encode(input, nil, nil)
return len(token), nil
}
type functionCallProp interface {
GetDescription() string
GetType() string
GetEnum() []string
GetItems() (functionCallProp, error)
GetRequired() []string
GetProperties() (map[string]functionCallProp, error)
}
func formatObjectProperties(p functionCallProp, indent int) string {
if p == nil {
return ""
}
lines := []string{}
properties, err := p.GetProperties()
if err != nil {
return ""
}
for name, param := range properties {
description := param.GetDescription()
if len(description) != 0 {
lines = append(lines, fmt.Sprintf(`// %s`, description))
}
fieldRequired := false
for _, requiredField := range param.GetRequired() {
if requiredField == name {
fieldRequired = true
}
}
if fieldRequired {
lines = append(lines, fmt.Sprintf(`%s: %s,`, name, formatType(param, indent)))
}
if !fieldRequired {
lines = append(lines, fmt.Sprintf(`%s?: %s,`, name, formatType(param, indent)))
}
}
formatedLines := []string{}
for _, line := range lines {
formatedLines = append(formatedLines, newSpaces(indent)+line)
}
return strings.Join(formatedLines, "\n")
}
func formatType(p functionCallProp, indent int) string {
if p == nil {
return "any"
}
switch p.GetType() {
case "string":
enums := p.GetEnum()
if len(enums) != 0 {
return fmt.Sprintf(`%s[]`, strings.Join(enums, " | "))
}
return "string"
case "number":
enums := p.GetEnum()
if len(enums) != 0 {
return fmt.Sprintf(`%s[]`, strings.Join(enums, " | "))
}
return "number"
case "integer":
enums := p.GetEnum()
if len(enums) != 0 {
return fmt.Sprintf(`%s[]`, strings.Join(enums, " | "))
}
return "integer"
case "boolean":
return "boolean"
case "null":
return "null"
case "object":
return strings.Join([]string{
"{",
formatObjectProperties(p, indent+2),
"}",
}, "\n")
case "array":
items, err := p.GetItems()
if err != nil {
return fmt.Sprintf(`%s[]`, formatType(items, indent))
}
return "any[]"
}
return "any"
}
func unmarshalInterface(source interface{}, dst interface{}) error {
data, err := json.Marshal(source)
if err != nil {
return err
}
err = json.Unmarshal(data, dst)
return err
}
func newSpaces(indent int) string {
result := ""
for i := 0; i < indent; i++ {
result += " "
}
return result
}