Skip to content

Commit 0dccd99

Browse files
committed
Merge branch 'feature/update-openai-api-to-latest-format' into develop
2 parents 37111d2 + 21d5d68 commit 0dccd99

26 files changed

Lines changed: 1215 additions & 595 deletions

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension WebChatContextCollector {
3232
static func detectLinks(from messages: [ChatMessage]) -> [String] {
3333
return messages.lazy
3434
.compactMap {
35-
$0.content ?? $0.functionCall?.arguments
35+
$0.content ?? $0.toolCalls?.map(\.function.arguments).joined(separator: " ") ?? ""
3636
}
3737
.map(detectLinks(from:))
3838
.flatMap { $0 }

Core/Sources/ChatGPTChatTab/Chat.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public struct DisplayedChatMessage: Equatable {
99
public enum Role: Equatable {
1010
case user
1111
case assistant
12-
case function
12+
case tool
1313
case ignored
1414
}
1515

1616
public struct Reference: Equatable {
1717
public typealias Kind = ChatMessage.Reference.Kind
18-
18+
1919
public var title: String
2020
public var subtitle: String
2121
public var uri: String
@@ -135,7 +135,7 @@ struct Chat: ReducerProtocol {
135135
await send(.focusOnTextField)
136136
await send(.refresh)
137137
}
138-
138+
139139
case .refresh:
140140
return .run { send in
141141
await send(.chatMenu(.refresh))
@@ -298,8 +298,9 @@ struct Chat: ReducerProtocol {
298298
}.cancellable(id: CancelID.observeDefaultScopesChange(id), cancelInFlight: true)
299299

300300
case .historyChanged:
301-
state.history = service.chatHistory.map { message in
302-
.init(
301+
state.history = service.chatHistory.flatMap { message in
302+
var all = [DisplayedChatMessage]()
303+
all.append(.init(
303304
id: message.id,
304305
role: {
305306
switch message.role {
@@ -312,7 +313,6 @@ struct Chat: ReducerProtocol {
312313
return .assistant
313314
}
314315
return .ignored
315-
case .function: return .function
316316
}
317317
}(),
318318
text: message.summary ?? message.content ?? "",
@@ -325,7 +325,18 @@ struct Chat: ReducerProtocol {
325325
kind: $0.kind
326326
)
327327
}
328-
)
328+
))
329+
330+
for call in message.toolCalls ?? [] {
331+
all.append(.init(
332+
id: message.id + call.id,
333+
role: .tool,
334+
text: call.response.summary ?? call.response.content,
335+
references: []
336+
))
337+
}
338+
339+
return all
329340
}
330341

331342
state.title = {
@@ -401,7 +412,7 @@ struct ChatMenu: ReducerProtocol {
401412
return .run {
402413
await $0(.refresh)
403414
}
404-
415+
405416
case .refresh:
406417
state.temperatureOverride = service.configuration.overriding.temperature
407418
state.chatModelIdOverride = service.configuration.overriding.modelId

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ struct ChatHistory: View {
258258
trailing: -8
259259
))
260260
.padding(.vertical, 4)
261-
case .function:
261+
case .tool:
262262
FunctionMessage(id: message.id, text: text)
263263
case .ignored:
264264
EmptyView()
@@ -453,7 +453,7 @@ struct ChatPanel_Preview: PreviewProvider {
453453
),
454454
.init(
455455
id: "6",
456-
role: .function,
456+
role: .tool,
457457
text: """
458458
Searching for something...
459459
- abc

Core/Sources/ChatService/ChatService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ public final class ChatService: ObservableObject {
124124
await chatGPTService.stopReceivingMessage()
125125
isReceivingMessage = false
126126

127-
// if it's stopped before the function finishes, remove the function call.
127+
// if it's stopped before the tool calls finish, remove the message.
128128
await memory.mutateHistory { history in
129-
if history.last?.role == .assistant, history.last?.functionCall != nil {
129+
if history.last?.role == .assistant, history.last?.toolCalls != nil {
130130
history.removeLast()
131131
}
132132
}

Core/Sources/ChatService/ContextAwareAutoManagedChatGPTMemory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final class ContextAwareAutoManagedChatGPTMemory: ChatGPTMemory {
3939

4040
public func generatePrompt() async -> ChatGPTPrompt {
4141
let content = (await memory.history)
42-
.last(where: { $0.role == .user || $0.role == .function })?.content
42+
.last(where: { $0.role == .user })?.content
4343
try? await contextController.collectContextInformation(
4444
systemPrompt: """
4545
\(chatService?.systemPrompt ?? "")

Core/Sources/HostApp/AccountSettings/ChatModelManagement/ChatModelEdit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ extension ChatModelEdit.State {
170170
maxTokens: model.info.maxTokens,
171171
supportsFunctionCalling: model.info.supportsFunctionCalling,
172172
modelName: model.info.modelName,
173-
ollamaKeepAlive: model.info.ollamaKeepAlive,
173+
ollamaKeepAlive: model.info.ollamaInfo.keepAlive,
174174
apiKeySelection: .init(
175175
apiKeyName: model.info.apiKeyName,
176176
apiKeyManagement: .init(availableAPIKeyNames: [model.info.apiKeyName])
@@ -198,7 +198,7 @@ extension ChatModel {
198198
return state.supportsFunctionCalling
199199
}(),
200200
modelName: state.modelName.trimmingCharacters(in: .whitespacesAndNewlines),
201-
ollamaKeepAlive: state.ollamaKeepAlive
201+
ollamaInfo: .init(keepAlive: state.ollamaKeepAlive)
202202
)
203203
)
204204
}

Core/Sources/HostApp/AccountSettings/EmbeddingModelManagement/EmbeddingModelEdit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ extension EmbeddingModelEdit.State {
155155
format: model.format,
156156
maxTokens: model.info.maxTokens,
157157
modelName: model.info.modelName,
158-
ollamaKeepAlive: model.info.ollamaKeepAlive,
158+
ollamaKeepAlive: model.info.ollamaInfo.keepAlive,
159159
apiKeySelection: .init(
160160
apiKeyName: model.info.apiKeyName,
161161
apiKeyManagement: .init(availableAPIKeyNames: [model.info.apiKeyName])
@@ -177,7 +177,7 @@ extension EmbeddingModel {
177177
isFullURL: state.isFullURL,
178178
maxTokens: state.maxTokens,
179179
modelName: state.modelName.trimmingCharacters(in: .whitespacesAndNewlines),
180-
ollamaKeepAlive: state.ollamaKeepAlive
180+
ollamaInfo: .init(keepAlive: state.ollamaKeepAlive)
181181
)
182182
)
183183
}

Pro

Submodule Pro updated from fbb89b8 to ede561a

Tool/Sources/AIModel/ChatModel.swift

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ public struct ChatModel: Codable, Equatable, Identifiable {
2525
}
2626

2727
public struct Info: Codable, Equatable {
28+
public struct OllamaInfo: Codable, Equatable {
29+
@FallbackDecoding<EmptyString>
30+
public var keepAlive: String
31+
32+
public init(keepAlive: String = "") {
33+
self.keepAlive = keepAlive
34+
}
35+
}
36+
37+
public struct OpenAIInfo: Codable, Equatable {
38+
@FallbackDecoding<EmptyString>
39+
public var organizationID: String
40+
41+
public init(organizationID: String = "") {
42+
self.organizationID = organizationID
43+
}
44+
}
45+
2846
@FallbackDecoding<EmptyString>
2947
public var apiKeyName: String
3048
@FallbackDecoding<EmptyString>
@@ -35,35 +53,32 @@ public struct ChatModel: Codable, Equatable, Identifiable {
3553
public var maxTokens: Int
3654
@FallbackDecoding<EmptyBool>
3755
public var supportsFunctionCalling: Bool
38-
@FallbackDecoding<EmptyBool>
39-
public var supportsOpenAIAPI2023_11: Bool
4056
@FallbackDecoding<EmptyString>
4157
public var modelName: String
42-
public var azureOpenAIDeploymentName: String {
43-
get { modelName }
44-
set { modelName = newValue }
45-
}
46-
@FallbackDecoding<EmptyString>
47-
public var ollamaKeepAlive: String
58+
59+
@FallbackDecoding<EmptyChatModelOpenAIInfo>
60+
public var openAIInfo: OpenAIInfo
61+
@FallbackDecoding<EmptyChatModelOllamaInfo>
62+
public var ollamaInfo: OllamaInfo
4863

4964
public init(
5065
apiKeyName: String = "",
5166
baseURL: String = "",
5267
isFullURL: Bool = false,
5368
maxTokens: Int = 4000,
5469
supportsFunctionCalling: Bool = true,
55-
supportsOpenAIAPI2023_11: Bool = false,
5670
modelName: String = "",
57-
ollamaKeepAlive: String = ""
71+
openAIInfo: OpenAIInfo = OpenAIInfo(),
72+
ollamaInfo: OllamaInfo = OllamaInfo()
5873
) {
5974
self.apiKeyName = apiKeyName
6075
self.baseURL = baseURL
6176
self.isFullURL = isFullURL
6277
self.maxTokens = maxTokens
6378
self.supportsFunctionCalling = supportsFunctionCalling
64-
self.supportsOpenAIAPI2023_11 = supportsOpenAIAPI2023_11
6579
self.modelName = modelName
66-
self.ollamaKeepAlive = ollamaKeepAlive
80+
self.openAIInfo = openAIInfo
81+
self.ollamaInfo = ollamaInfo
6782
}
6883
}
6984

@@ -80,7 +95,7 @@ public struct ChatModel: Codable, Equatable, Identifiable {
8095
return "\(baseURL)/v1/chat/completions"
8196
case .azureOpenAI:
8297
let baseURL = info.baseURL
83-
let deployment = info.azureOpenAIDeploymentName
98+
let deployment = info.modelName
8499
let version = "2024-02-15-preview"
85100
if baseURL.isEmpty { return "" }
86101
return "\(baseURL)/openai/deployments/\(deployment)/chat/completions?api-version=\(version)"
@@ -104,3 +119,11 @@ public struct EmptyChatModelFormat: FallbackValueProvider {
104119
public static var defaultValue: ChatModel.Format { .openAI }
105120
}
106121

122+
public struct EmptyChatModelOllamaInfo: FallbackValueProvider {
123+
public static var defaultValue: ChatModel.Info.OllamaInfo { .init() }
124+
}
125+
126+
public struct EmptyChatModelOpenAIInfo: FallbackValueProvider {
127+
public static var defaultValue: ChatModel.Info.OpenAIInfo { .init() }
128+
}
129+

Tool/Sources/AIModel/EmbeddingModel.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Foundation
21
import CodableWrappers
2+
import Foundation
33

44
public struct EmbeddingModel: Codable, Equatable, Identifiable {
55
public var id: String
@@ -24,6 +24,9 @@ public struct EmbeddingModel: Codable, Equatable, Identifiable {
2424
}
2525

2626
public struct Info: Codable, Equatable {
27+
public typealias OllamaInfo = ChatModel.Info.OllamaInfo
28+
public typealias OpenAIInfo = ChatModel.Info.OpenAIInfo
29+
2730
@FallbackDecoding<EmptyString>
2831
public var apiKeyName: String
2932
@FallbackDecoding<EmptyString>
@@ -36,12 +39,11 @@ public struct EmbeddingModel: Codable, Equatable, Identifiable {
3639
public var dimensions: Int
3740
@FallbackDecoding<EmptyString>
3841
public var modelName: String
39-
public var azureOpenAIDeploymentName: String {
40-
get { modelName }
41-
set { modelName = newValue }
42-
}
43-
@FallbackDecoding<EmptyString>
44-
public var ollamaKeepAlive: String
42+
43+
@FallbackDecoding<EmptyChatModelOpenAIInfo>
44+
public var openAIInfo: OpenAIInfo
45+
@FallbackDecoding<EmptyChatModelOllamaInfo>
46+
public var ollamaInfo: OllamaInfo
4547

4648
public init(
4749
apiKeyName: String = "",
@@ -50,18 +52,20 @@ public struct EmbeddingModel: Codable, Equatable, Identifiable {
5052
maxTokens: Int = 8192,
5153
dimensions: Int = 1536,
5254
modelName: String = "",
53-
ollamaKeepAlive: String = ""
55+
openAIInfo: OpenAIInfo = OpenAIInfo(),
56+
ollamaInfo: OllamaInfo = OllamaInfo()
5457
) {
5558
self.apiKeyName = apiKeyName
5659
self.baseURL = baseURL
5760
self.isFullURL = isFullURL
5861
self.maxTokens = maxTokens
5962
self.dimensions = dimensions
6063
self.modelName = modelName
61-
self.ollamaKeepAlive = ollamaKeepAlive
64+
self.openAIInfo = openAIInfo
65+
self.ollamaInfo = ollamaInfo
6266
}
6367
}
64-
68+
6569
public var endpoint: String {
6670
switch format {
6771
case .openAI:
@@ -75,7 +79,7 @@ public struct EmbeddingModel: Codable, Equatable, Identifiable {
7579
return "\(baseURL)/v1/embeddings"
7680
case .azureOpenAI:
7781
let baseURL = info.baseURL
78-
let deployment = info.azureOpenAIDeploymentName
82+
let deployment = info.modelName
7983
let version = "2024-02-15-preview"
8084
if baseURL.isEmpty { return "" }
8185
return "\(baseURL)/openai/deployments/\(deployment)/embeddings?api-version=\(version)"
@@ -87,11 +91,11 @@ public struct EmbeddingModel: Codable, Equatable, Identifiable {
8791
}
8892
}
8993

90-
9194
public struct EmptyEmbeddingModelInfo: FallbackValueProvider {
9295
public static var defaultValue: EmbeddingModel.Info { .init() }
9396
}
9497

9598
public struct EmptyEmbeddingModelFormat: FallbackValueProvider {
9699
public static var defaultValue: EmbeddingModel.Format { .openAI }
97100
}
101+

0 commit comments

Comments
 (0)