-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmessages.go
76 lines (63 loc) · 1.42 KB
/
messages.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
package agency
import "encoding/json"
type Message interface {
Role() Role
Content() []byte
Kind() Kind
}
type Kind string
const (
TextKind Kind = "text"
ImageKind Kind = "image"
VoiceKind Kind = "voice"
EmbeddingKind Kind = "embedding"
)
type Role string
const (
UserRole Role = "user"
SystemRole Role = "system"
AssistantRole Role = "assistant"
ToolRole Role = "tool"
)
type BaseMessage struct {
content []byte
role Role
kind Kind
}
func (bm BaseMessage) Role() Role {
return bm.role
}
func (bm BaseMessage) Kind() Kind {
return bm.kind
}
func (bm BaseMessage) Content() []byte {
return bm.content
}
// NewMessage creates new `Message` with the specified `Role` and `Kind`
func NewMessage(role Role, kind Kind, content []byte) BaseMessage {
return BaseMessage{
content: content,
role: role,
kind: kind,
}
}
// NewTextMessage creates new `Message` with Text kind and the specified `Role`
func NewTextMessage(role Role, content string) BaseMessage {
return BaseMessage{
content: []byte(content),
role: role,
kind: TextKind,
}
}
// NewJsonMessage marshals content and creates new `Message` with text kind and the specified `Role`
func NewJsonMessage(role Role, content any) (BaseMessage, error) {
data, err := json.Marshal(content)
if err != nil {
return BaseMessage{}, err
}
return BaseMessage{
content: data,
role: role,
kind: TextKind,
}, nil
}