/
models.go
58 lines (46 loc) · 1.41 KB
/
models.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
package models
import (
"context"
"errors"
"github.com/baalimago/clai/internal/tools"
)
type Querier interface {
Query(ctx context.Context) error
}
type ChatQuerier interface {
Querier
TextQuery(context.Context, Chat) (Chat, error)
}
type StreamCompleter interface {
// Setup the stream completer, do things like init http.Client/websocket etc
// Will be called asynchronously. Should return error if setup fails
Setup() error
// StreamCompletions and return a channel which sends CompletionsEvents.
// The CompletionEvents should either be a string or an error. If there is
// a catastrophic error, return the error and close the channel.
StreamCompletions(context.Context, Chat) (chan CompletionEvent, error)
}
// A ToolBox can register tools which later on will be added to the chat completion queries
type ToolBox interface {
// RegisterTool registers a tool to the ToolBox
RegisterTool(tools.AiTool)
}
type CompletionEvent any
type NoopEvent struct{}
type Chat struct {
ID string `json:"id"`
Messages []Message `json:"messages"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
// SystemMessage returns the first encountered Message with role 'system'
func (c *Chat) SystemMessage() (Message, error) {
for _, msg := range c.Messages {
if msg.Role == "system" {
return msg, nil
}
}
return Message{}, errors.New("failed to find any system message")
}