-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistory.go
More file actions
87 lines (69 loc) · 2.11 KB
/
history.go
File metadata and controls
87 lines (69 loc) · 2.11 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
package llmberjack
import (
"sync"
)
// History manages the conversation context by storing a sequence of messages.
// It is generic in type `T`, where `T` represents the specific message format
// required by a particular LLM provider (e.g., OpenAI's Message or AIStudio's ChatMessage).
// This allows the adapter to maintain conversation state across multiple requests.
type History[T any] struct {
mtx sync.Mutex
history map[*ThreadId][]T
}
// Save appends a new message to the conversation history.
// The `message` parameter should be of the generic type `T`, matching the
// content representation expected by the LLM provider.
func (h *History[T]) Save(threadId *ThreadId, message T) {
h.mtx.Lock()
defer h.mtx.Unlock()
if h.history == nil {
h.history = make(map[*ThreadId][]T)
}
if _, ok := h.history[threadId]; !ok {
h.history[threadId] = make([]T, 0)
}
h.history[threadId] = append(h.history[threadId], message)
}
// Load retrieves the entire conversation history as a slice of messages.
// This history can then be included in subsequent requests to the LLM
// to maintain conversational context.
func (h *History[T]) Load(threadId *ThreadId) []T {
h.mtx.Lock()
defer h.mtx.Unlock()
if h.history == nil {
return []T{}
}
return h.history[threadId]
}
// Clear empties the entire conversation history, effectively starting a
// new conversation. This also clears any system instructions that were
// part of the history.
func (h *History[T]) Clear(threadId *ThreadId) {
h.mtx.Lock()
defer h.mtx.Unlock()
if h.history != nil {
h.history[threadId] = make([]T, 0)
}
}
func (h *History[T]) Close(threadId *ThreadId) {
h.mtx.Lock()
defer h.mtx.Unlock()
if h.history != nil {
delete(h.history, threadId)
}
}
func (h *History[T]) Copy(threadId *ThreadId) *ThreadId {
if threadId == nil {
return nil
}
h.mtx.Lock()
defer h.mtx.Unlock()
if _, ok := h.history[threadId]; ok {
return nil
}
newThreadId := &ThreadId{provider: threadId.provider}
newHistory := make([]T, len(h.history[threadId]))
copy(newHistory, h.history[threadId])
h.history[newThreadId] = newHistory
return newThreadId
}