-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.go
99 lines (80 loc) · 1.67 KB
/
ai.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
package ai
import (
"context"
"encoding"
"errors"
"strings"
)
var ErrAIClosed = errors.New("AI client is nil or already closed")
type AI interface {
LLMs() LLMs
Model(context.Context) (string, error)
Limiter
SetModel(string)
Model
Chatbot
ChatSession() ChatSession
Close() error
}
type Function struct {
Name string
Description string
Parameters Schema
}
type Schema struct {
Type string `json:"type"`
Properties map[string]any `json:"properties"`
Enum []string `json:"enum,omitempty"`
Required []string `json:"required"`
}
var _ encoding.TextUnmarshaler = new(FunctionCallingMode)
type FunctionCallingMode int
func (m *FunctionCallingMode) UnmarshalText(text []byte) error {
switch strings.ToLower(string(text)) {
case "auto":
*m = FunctionCallingAuto
case "any":
*m = FunctionCallingAny
case "none":
*m = FunctionCallingNone
default:
*m = 0
}
return nil
}
const (
FunctionCallingAuto FunctionCallingMode = iota + 1
FunctionCallingAny
FunctionCallingNone
)
type Model interface {
SetFunctionCall([]Function, FunctionCallingMode)
SetCount(x int64)
SetMaxTokens(x int64)
SetTemperature(x float64)
SetTopP(x float64)
SetJSONResponse(b bool)
}
type Chatbot interface {
Chat(context.Context, ...Part) (ChatResponse, error)
ChatStream(context.Context, ...Part) (ChatStream, error)
}
type ChatSession interface {
Chatbot
History() []Content
}
type ChatStream interface {
Next() (ChatResponse, error)
Close() error
}
type TokenCount struct {
Prompt int64
Result int64
Total int64
}
type ChatResponse interface {
Raw() any
Results() []string
FunctionCalls() []FunctionCall
TokenCount() TokenCount
}