-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
169 lines (143 loc) · 3.92 KB
/
provider.go
File metadata and controls
169 lines (143 loc) · 3.92 KB
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
package llm
import (
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/Back-to-code/go-llm/cache"
"github.com/Back-to-code/go-llm/log"
)
var DefaultCacheDuration = time.Hour * 24
type ResponseFormat string
var (
// ResponseFormatJsonSchema ResponseFormat = "json_schema" // (Unsupported)
ResponseFormatJsonObject ResponseFormat = "json_object"
)
type Thinking uint8
const (
NoThinking Thinking = iota
MinimalThinking
LowThinking
MediumThinking
HighThinking
)
type Options struct {
// Generically implemented
Cache time.Duration // If <= 0, nothing will be cached
NoRetry bool
// Implemented by each providers
Timeout time.Duration // The request timeout
MaxTokens int
Ctx context.Context
ResponseFormat ResponseFormat
Tools []Tool
Thinking Thinking
}
func (o Options) prepare(isStream bool, provider Provider) (Options, error) {
if isStream && !provider.SupportsStreaming() {
return o, fmt.Errorf("provider %T does not support streaming", provider)
}
if o.ResponseFormat != "" && !provider.SupportsStructuredOutput() {
return o, fmt.Errorf("provider %T does not support structured output", provider)
}
if len(o.Tools) > 0 && !provider.SupportsTools() {
return o, fmt.Errorf("provider %T does not support tools", provider)
}
if o.Timeout <= 0 {
o.Timeout = time.Second * 30
}
for idx, tool := range o.Tools {
if tool.Resolver == nil {
return o, fmt.Errorf("tool %s (#%d) is missing a resolver", tool.Function.Name, idx+1)
}
if tool.Type == "" {
tool.Type = "function"
o.Tools[idx] = tool
}
}
return o, nil
}
type Provider interface {
Prompt(model string, messages []Message, options Options) (Response, error)
Stream(model string, messages []Message, options Options) (chan string, error)
SupportsStructuredOutput() bool
SupportsStreaming() bool
SupportsTools() bool
}
type Prompter interface {
Prompt(messages []Message, options Options) (Response, error)
PromptSingle(message string, options Options) (Response, error)
Stream(messages []Message, options Options) (chan string, error)
ModelName() string
}
type Model struct {
Name string
Provider Provider
}
func (m *Model) ModelName() string {
if m.Name == "" {
return "<none>"
}
return m.Name
}
func (m *Model) Prompt(messages []Message, options Options) (Response, error) {
var err error
options, err = options.prepare(false, m.Provider)
if err != nil {
return Response{}, err
}
retries := 5
if options.NoRetry {
retries = 1
}
var cacheKey string
if options.Cache > 0 {
cacheKeyHashContents, err := json.Marshal(messages)
if err == nil {
cacheKeyHash := sha1.New()
cacheKeyHash.Write(cacheKeyHashContents)
cacheKey = m.Name + ":" + hex.EncodeToString(cacheKeyHash.Sum(nil))
cachedResponse, err := cache.Get(cacheKey)
if err == nil && cachedResponse != "" {
return Response{
Value: cachedResponse,
Conversation: messages,
}, nil
}
}
}
var resp Response
for i := 0; i < retries; i++ {
if options.Ctx != nil && options.Ctx.Err() != nil {
return Response{}, options.Ctx.Err()
}
start := time.Now()
resp, err = m.Provider.Prompt(m.Name, messages, options)
if err != nil {
if time.Since(start) < time.Second {
time.Sleep(time.Millisecond * 100 * (time.Duration(i) + 1))
}
continue
}
if cacheKey != "" {
cache.Set(cacheKey, resp.Value, options.Cache)
}
return resp, err
}
return resp, err
}
// PromptSingle is a wrapper around prompt but only prompt 1 user message
func (m *Model) PromptSingle(message string, options Options) (Response, error) {
return m.Prompt([]Message{User(message)}, options)
}
func (m *Model) Stream(messages []Message, options Options) (chan string, error) {
var err error
options, err = options.prepare(true, m.Provider)
if err != nil {
return nil, err
}
log.Info("Sending prompt to " + m.Name)
return m.Provider.Stream(m.Name, messages, options)
}