Skip to content

Commit

Permalink
Merge branch 'MacPaw' into MacPaw.codable
Browse files Browse the repository at this point in the history
  • Loading branch information
James J Kalafus committed Feb 16, 2024
2 parents c82f4ec + 224f4ef commit 2758d1a
Show file tree
Hide file tree
Showing 35 changed files with 1,610 additions and 602 deletions.
32 changes: 16 additions & 16 deletions Demo/DemoChat/Sources/ChatStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public final class ChatStore: ObservableObject {
return
}

let weatherFunction = ChatFunctionDeclaration(
let weatherFunction = ChatQuery.ChatCompletionToolParam(function: .init(
name: "getWeatherData",
description: "Get the current weather in a given location",
parameters: .init(
Expand All @@ -95,38 +95,38 @@ public final class ChatStore: ObservableObject {
],
required: ["location"]
)
)
))

let functions = [weatherFunction]

let chatsStream: AsyncThrowingStream<ChatStreamResult, Error> = openAIClient.chatsStream(
query: ChatQuery(
model: model,
messages: conversation.messages.map { message in
Chat(role: message.role, content: message.content)
},
functions: functions
ChatQuery.ChatCompletionMessageParam(role: message.role, content: message.content)!
}, model: model,
tools: functions
)
)

var functionCallName = ""
var functionCallArguments = ""
var functionCalls = [(name: String, argument: String?)]()
for try await partialChatResult in chatsStream {
for choice in partialChatResult.choices {
let existingMessages = conversations[conversationIndex].messages
// Function calls are also streamed, so we need to accumulate.
if let functionCallDelta = choice.delta.functionCall {
if let nameDelta = functionCallDelta.name {
functionCallName += nameDelta
}
if let argumentsDelta = functionCallDelta.arguments {
functionCallArguments += argumentsDelta
choice.delta.toolCalls?.forEach { toolCallDelta in
if let functionCallDelta = toolCallDelta.function {
if let nameDelta = functionCallDelta.name {
functionCalls.append((nameDelta, functionCallDelta.arguments))
}
}
}
var messageText = choice.delta.content ?? ""
if let finishReason = choice.finishReason,
finishReason == "function_call" {
messageText += "Function call: name=\(functionCallName) arguments=\(functionCallArguments)"
finishReason == .toolCalls
{
functionCalls.forEach { (name: String, argument: String?) in
messageText += "Function call: name=\(name) arguments=\(argument ?? "")\n"
}
}
let message = Message(
id: partialChatResult.id,
Expand Down
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/ImageStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import OpenAI
public final class ImageStore: ObservableObject {
public var openAIClient: OpenAIProtocol

@Published var images: [ImagesResult.URLResult] = []
@Published var images: [ImagesResult.Image] = []

public init(
openAIClient: OpenAIProtocol
Expand Down
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/MiscStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class MiscStore: ObservableObject {
do {
let response = try await openAIClient.moderations(
query: ModerationsQuery(
input: message.content,
input: .init(message.content),
model: .textModerationLatest
)
)
Expand Down
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import OpenAI

struct Message {
var id: String
var role: Chat.Role
var role: ChatQuery.ChatCompletionMessageParam.Role
var content: String
var createdAt: Date
}
Expand Down
7 changes: 4 additions & 3 deletions Demo/DemoChat/Sources/SpeechStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ public final class SpeechStore: ObservableObject {

@MainActor
func createSpeech(_ query: AudioSpeechQuery) async {
guard let input = query.input, !input.isEmpty else { return }
let input = query.input
guard !input.isEmpty else { return }
do {
let response = try await openAIClient.audioCreateSpeech(query: query)
guard let data = response.audioData else { return }
let data = response.audio
let player = try? AVAudioPlayer(data: data)
let audioObject = AudioObject(prompt: input,
audioPlayer: player,
originResponse: response,
format: query.responseFormat.rawValue)
format: query.responseFormat?.rawValue ?? AudioSpeechQuery.AudioSpeechResponseFormat.mp3.rawValue)
audioObjects.append(audioObject)
} catch {
print(error.localizedDescription)
Expand Down
4 changes: 2 additions & 2 deletions Demo/DemoChat/Sources/UI/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ struct ChatBubble: View {
.foregroundColor(userForegroundColor)
.background(userBackgroundColor)
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
case .function:
case .tool:
Text(message.content)
.font(.footnote.monospaced())
.padding(.horizontal, 16)
Expand All @@ -223,7 +223,7 @@ struct DetailView_Previews: PreviewProvider {
Message(id: "1", role: .assistant, content: "Hello, how can I help you today?", createdAt: Date(timeIntervalSinceReferenceDate: 0)),
Message(id: "2", role: .user, content: "I need help with my subscription.", createdAt: Date(timeIntervalSinceReferenceDate: 100)),
Message(id: "3", role: .assistant, content: "Sure, what seems to be the problem with your subscription?", createdAt: Date(timeIntervalSinceReferenceDate: 200)),
Message(id: "4", role: .function, content:
Message(id: "4", role: .tool, content:
"""
get_current_weather({
"location": "Glasgow, Scotland",
Expand Down
10 changes: 5 additions & 5 deletions Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public struct ImageCreationView: View {

@State private var prompt: String = ""
@State private var n: Int = 1
@State private var size: String
private var sizes = ["256x256", "512x512", "1024x1024"]
@State private var size = ImagesQuery.Size.allCases.first!

private var sizes = ImagesQuery.Size.allCases

public init(store: ImageStore) {
self.store = store
Expand All @@ -37,7 +37,7 @@ public struct ImageCreationView: View {
HStack {
Picker("Size", selection: $size) {
ForEach(sizes, id: \.self) {
Text($0)
Text($0.rawValue)
}
}
}
Expand All @@ -56,7 +56,7 @@ public struct ImageCreationView: View {
}
if !$store.images.isEmpty {
Section("Images") {
ForEach($store.images, id: \.self) { image in
ForEach($store.images, id: \.url) { image in
let urlString = image.wrappedValue.url ?? ""
if let imageURL = URL(string: urlString), UIApplication.shared.canOpenURL(imageURL) {
LinkPreview(previewURL: imageURL)
Expand Down
2 changes: 1 addition & 1 deletion Demo/DemoChat/Sources/UI/Misc/ListModelsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct ListModelsView: View {

public var body: some View {
NavigationStack {
List($store.availableModels) { row in
List($store.availableModels.wrappedValue, id: \.id) { row in
Text(row.id)
}
.listStyle(.insetGrouped)
Expand Down
4 changes: 2 additions & 2 deletions Demo/DemoChat/Sources/UI/TextToSpeechView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public struct TextToSpeechView: View {
}
if !$store.audioObjects.wrappedValue.isEmpty {
Section("Click to play, swipe to save:") {
ForEach(store.audioObjects) { object in
ForEach(store.audioObjects, id: \.id) { object in
HStack {
Text(object.prompt.capitalized)
Spacer()
Expand All @@ -122,7 +122,7 @@ public struct TextToSpeechView: View {
}
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button {
presentUserDirectoryDocumentPicker(for: object.originResponse.audioData, filename: "GeneratedAudio.\(object.format)")
presentUserDirectoryDocumentPicker(for: object.originResponse.audio, filename: "GeneratedAudio.\(object.format)")
} label: {
Image(systemName: "square.and.arrow.down")
}
Expand Down
Loading

0 comments on commit 2758d1a

Please sign in to comment.