-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathChatGPTMemory.swift
More file actions
140 lines (131 loc) · 4.85 KB
/
ChatGPTMemory.swift
File metadata and controls
140 lines (131 loc) · 4.85 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
import Foundation
public struct ChatGPTPrompt: Equatable {
public var history: [ChatMessage]
public var references: [ChatMessage.Reference]
public var remainingTokenCount: Int?
public init(
history: [ChatMessage],
references: [ChatMessage.Reference] = [],
remainingTokenCount: Int? = nil
) {
self.history = history
self.references = references
self.remainingTokenCount = remainingTokenCount
}
}
public protocol ChatGPTMemory {
/// The message history.
var history: [ChatMessage] { get async }
/// Update the message history.
func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async
/// Generate prompt that would be send through the API.
///
/// A memory should make sure that the history in the prompt
/// doesn't exceed the maximum token count.
///
/// The history can be different from the actual history.
func generatePrompt() async -> ChatGPTPrompt
}
public extension ChatGPTMemory {
/// Append a message to the history.
func appendMessage(_ message: ChatMessage) async {
await mutateHistory { history in
if let index = history.firstIndex(where: { $0.id == message.id }) {
history[index] = message
} else {
history.append(message)
}
}
}
/// Update a message in the history.
func updateMessage(id: String, _ update: (inout ChatMessage) -> Void) async {
await mutateHistory { history in
if let index = history.firstIndex(where: { $0.id == id }) {
update(&history[index])
}
}
}
/// Remove a message from the history.
func removeMessage(_ id: String) async {
await mutateHistory {
$0.removeAll { $0.id == id }
}
}
/// Stream a message to the history.
func streamMessage(
id: String,
role: ChatMessage.Role? = nil,
content: String? = nil,
name: String? = nil,
toolCallId: String? = nil,
toolCalls: [ChatMessage.ToolCall]? = nil,
summary: String? = nil,
references: [ChatMessage.Reference]? = nil
) async {
await mutateHistory { history in
if let index = history.firstIndex(where: { $0.id == id }) {
if let content {
if history[index].content == nil {
history[index].content = content
} else {
history[index].content?.append(content)
}
}
if let role {
history[index].role = role
}
if let toolCalls {
if history[index].toolCalls == nil {
history[index].toolCalls = toolCalls
} else {
for toolCall in toolCalls {
if let index = history[index].toolCalls?
.firstIndex(where: { $0.id == toolCall.id })
{
if !toolCall.id.isEmpty {
history[index].toolCalls?[index].id = toolCall.id
}
if !toolCall.type.isEmpty {
history[index].toolCalls?[index].type = toolCall.type
}
history[index].toolCalls?[index].function.name
.append(toolCall.function.name)
history[index].toolCalls?[index].function.arguments
.append(toolCall.function.arguments)
} else {
history[index].toolCalls?.append(toolCall)
}
}
}
}
if let summary {
history[index].summary = summary
}
if let references {
history[index].references.append(contentsOf: references)
}
if let name {
history[index].name = name
}
if let toolCallId {
history[index].toolCallId = toolCallId
}
} else {
history.append(.init(
id: id,
role: role ?? .system,
content: content,
name: name,
toolCallId: toolCallId,
toolCalls: toolCalls,
summary: summary,
references: references ?? []
))
}
}
}
/// Clear the history.
func clearHistory() async {
await mutateHistory { $0.removeAll() }
}
}