Skip to content

Commit 224f4ef

Browse files
authored
Merge pull request #169 from kalafus/MacPaw.parameter_update
API: Parameter Update
2 parents 1f6d1c0 + ba5cd2b commit 224f4ef

32 files changed

+1507
-555
lines changed

Demo/DemoChat/Sources/ChatStore.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public final class ChatStore: ObservableObject {
8585
return
8686
}
8787

88-
let weatherFunction = ChatFunctionDeclaration(
88+
let weatherFunction = ChatQuery.ChatCompletionToolParam(function: .init(
8989
name: "getWeatherData",
9090
description: "Get the current weather in a given location",
9191
parameters: .init(
@@ -95,38 +95,38 @@ public final class ChatStore: ObservableObject {
9595
],
9696
required: ["location"]
9797
)
98-
)
98+
))
9999

100100
let functions = [weatherFunction]
101101

102102
let chatsStream: AsyncThrowingStream<ChatStreamResult, Error> = openAIClient.chatsStream(
103103
query: ChatQuery(
104-
model: model,
105104
messages: conversation.messages.map { message in
106-
Chat(role: message.role, content: message.content)
107-
},
108-
functions: functions
105+
ChatQuery.ChatCompletionMessageParam(role: message.role, content: message.content)!
106+
}, model: model,
107+
tools: functions
109108
)
110109
)
111110

112-
var functionCallName = ""
113-
var functionCallArguments = ""
111+
var functionCalls = [(name: String, argument: String?)]()
114112
for try await partialChatResult in chatsStream {
115113
for choice in partialChatResult.choices {
116114
let existingMessages = conversations[conversationIndex].messages
117115
// Function calls are also streamed, so we need to accumulate.
118-
if let functionCallDelta = choice.delta.functionCall {
119-
if let nameDelta = functionCallDelta.name {
120-
functionCallName += nameDelta
121-
}
122-
if let argumentsDelta = functionCallDelta.arguments {
123-
functionCallArguments += argumentsDelta
116+
choice.delta.toolCalls?.forEach { toolCallDelta in
117+
if let functionCallDelta = toolCallDelta.function {
118+
if let nameDelta = functionCallDelta.name {
119+
functionCalls.append((nameDelta, functionCallDelta.arguments))
120+
}
124121
}
125122
}
126123
var messageText = choice.delta.content ?? ""
127124
if let finishReason = choice.finishReason,
128-
finishReason == "function_call" {
129-
messageText += "Function call: name=\(functionCallName) arguments=\(functionCallArguments)"
125+
finishReason == .toolCalls
126+
{
127+
functionCalls.forEach { (name: String, argument: String?) in
128+
messageText += "Function call: name=\(name) arguments=\(argument ?? "")\n"
129+
}
130130
}
131131
let message = Message(
132132
id: partialChatResult.id,

Demo/DemoChat/Sources/ImageStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import OpenAI
1111
public final class ImageStore: ObservableObject {
1212
public var openAIClient: OpenAIProtocol
1313

14-
@Published var images: [ImagesResult.URLResult] = []
14+
@Published var images: [ImagesResult.Image] = []
1515

1616
public init(
1717
openAIClient: OpenAIProtocol

Demo/DemoChat/Sources/MiscStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public final class MiscStore: ObservableObject {
5151
do {
5252
let response = try await openAIClient.moderations(
5353
query: ModerationsQuery(
54-
input: message.content,
54+
input: .init(message.content),
5555
model: .textModerationLatest
5656
)
5757
)

Demo/DemoChat/Sources/Models/Message.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import OpenAI
1010

1111
struct Message {
1212
var id: String
13-
var role: Chat.Role
13+
var role: ChatQuery.ChatCompletionMessageParam.Role
1414
var content: String
1515
var createdAt: Date
1616
}

Demo/DemoChat/Sources/SpeechStore.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ public final class SpeechStore: ObservableObject {
3030

3131
@MainActor
3232
func createSpeech(_ query: AudioSpeechQuery) async {
33-
guard let input = query.input, !input.isEmpty else { return }
33+
let input = query.input
34+
guard !input.isEmpty else { return }
3435
do {
3536
let response = try await openAIClient.audioCreateSpeech(query: query)
36-
guard let data = response.audioData else { return }
37+
let data = response.audio
3738
let player = try? AVAudioPlayer(data: data)
3839
let audioObject = AudioObject(prompt: input,
3940
audioPlayer: player,
4041
originResponse: response,
41-
format: query.responseFormat.rawValue)
42+
format: query.responseFormat?.rawValue ?? AudioSpeechQuery.AudioSpeechResponseFormat.mp3.rawValue)
4243
audioObjects.append(audioObject)
4344
} catch {
4445
print(error.localizedDescription)

Demo/DemoChat/Sources/UI/DetailView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct ChatBubble: View {
199199
.foregroundColor(userForegroundColor)
200200
.background(userBackgroundColor)
201201
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
202-
case .function:
202+
case .tool:
203203
Text(message.content)
204204
.font(.footnote.monospaced())
205205
.padding(.horizontal, 16)
@@ -223,7 +223,7 @@ struct DetailView_Previews: PreviewProvider {
223223
Message(id: "1", role: .assistant, content: "Hello, how can I help you today?", createdAt: Date(timeIntervalSinceReferenceDate: 0)),
224224
Message(id: "2", role: .user, content: "I need help with my subscription.", createdAt: Date(timeIntervalSinceReferenceDate: 100)),
225225
Message(id: "3", role: .assistant, content: "Sure, what seems to be the problem with your subscription?", createdAt: Date(timeIntervalSinceReferenceDate: 200)),
226-
Message(id: "4", role: .function, content:
226+
Message(id: "4", role: .tool, content:
227227
"""
228228
get_current_weather({
229229
"location": "Glasgow, Scotland",

Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public struct ImageCreationView: View {
1313

1414
@State private var prompt: String = ""
1515
@State private var n: Int = 1
16-
@State private var size: String
17-
18-
private var sizes = ["256x256", "512x512", "1024x1024"]
16+
@State private var size = ImagesQuery.Size.allCases.first!
17+
18+
private var sizes = ImagesQuery.Size.allCases
1919

2020
public init(store: ImageStore) {
2121
self.store = store
@@ -37,7 +37,7 @@ public struct ImageCreationView: View {
3737
HStack {
3838
Picker("Size", selection: $size) {
3939
ForEach(sizes, id: \.self) {
40-
Text($0)
40+
Text($0.rawValue)
4141
}
4242
}
4343
}
@@ -56,7 +56,7 @@ public struct ImageCreationView: View {
5656
}
5757
if !$store.images.isEmpty {
5858
Section("Images") {
59-
ForEach($store.images, id: \.self) { image in
59+
ForEach($store.images, id: \.url) { image in
6060
let urlString = image.wrappedValue.url ?? ""
6161
if let imageURL = URL(string: urlString), UIApplication.shared.canOpenURL(imageURL) {
6262
LinkPreview(previewURL: imageURL)

Demo/DemoChat/Sources/UI/Misc/ListModelsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct ListModelsView: View {
1212

1313
public var body: some View {
1414
NavigationStack {
15-
List($store.availableModels) { row in
15+
List($store.availableModels.wrappedValue, id: \.id) { row in
1616
Text(row.id)
1717
}
1818
.listStyle(.insetGrouped)

Demo/DemoChat/Sources/UI/TextToSpeechView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public struct TextToSpeechView: View {
101101
}
102102
if !$store.audioObjects.wrappedValue.isEmpty {
103103
Section("Click to play, swipe to save:") {
104-
ForEach(store.audioObjects) { object in
104+
ForEach(store.audioObjects, id: \.id) { object in
105105
HStack {
106106
Text(object.prompt.capitalized)
107107
Spacer()
@@ -122,7 +122,7 @@ public struct TextToSpeechView: View {
122122
}
123123
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
124124
Button {
125-
presentUserDirectoryDocumentPicker(for: object.originResponse.audioData, filename: "GeneratedAudio.\(object.format)")
125+
presentUserDirectoryDocumentPicker(for: object.originResponse.audio, filename: "GeneratedAudio.\(object.format)")
126126
} label: {
127127
Image(systemName: "square.and.arrow.down")
128128
}

Sources/OpenAI/OpenAI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension OpenAI {
182182
return completion(.failure(OpenAIError.emptyData))
183183
}
184184

185-
completion(.success(AudioSpeechResult(audioData: data)))
185+
completion(.success(AudioSpeechResult(audio: data)))
186186
}
187187
task.resume()
188188
} catch {

0 commit comments

Comments
 (0)