-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Vector store * improving examples * Fixing vision
- Loading branch information
1 parent
4d90fcb
commit 3be54cb
Showing
12 changed files
with
524 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Examples/SwiftOpenAIExample/SwiftOpenAIExample/Files/AttachmentView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// AttachmentView.swift | ||
// SwiftOpenAIExample | ||
// | ||
// Created by James Rochabrun on 5/29/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct AttachmentView: View { | ||
|
||
let fileName: String | ||
@Binding var actionTrigger: Bool | ||
let isLoading: Bool | ||
|
||
var body: some View { | ||
HStack(spacing: Sizes.spacingExtraSmall) { | ||
HStack { | ||
if isLoading == true { | ||
ProgressView() | ||
.frame(width: 10, height: 10) | ||
.padding(.horizontal, Sizes.spacingExtraSmall) | ||
} else { | ||
Image(systemName: "doc") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.frame(width: 10) | ||
.foregroundColor(.secondary) | ||
} | ||
Text(fileName) | ||
.font(.caption2) | ||
} | ||
Button { | ||
actionTrigger = true | ||
|
||
} label: { | ||
Image(systemName: "xmark.circle.fill") | ||
} | ||
.disabled(isLoading) | ||
} | ||
.padding(.leading, Sizes.spacingMedium) | ||
.background( | ||
RoundedRectangle(cornerRadius: 8) | ||
.stroke(.gray.opacity(0.5), lineWidth: 0.5) | ||
) | ||
} | ||
} | ||
|
||
#Preview { | ||
AttachmentView(fileName: "Mydocument.pdf", actionTrigger: .constant(true), isLoading: true) | ||
} |
118 changes: 118 additions & 0 deletions
118
Examples/SwiftOpenAIExample/SwiftOpenAIExample/Files/FileAttachmentView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// FileAttachmentView.swift | ||
// SwiftOpenAIExample | ||
// | ||
// Created by James Rochabrun on 5/29/24. | ||
// | ||
|
||
import SwiftUI | ||
import SwiftOpenAI | ||
|
||
struct FileAttachmentView: View { | ||
|
||
init( | ||
service: OpenAIService, | ||
action: FilePickerAction, | ||
fileUploadedCompletion: @escaping (_ file: FileObject) -> Void, | ||
fileDeletedCompletion: @escaping (_ parameters: FilePickerAction, _ id: String) -> Void) | ||
{ | ||
self.fileProvider = FilesPickerProvider(service: service) | ||
self.action = action | ||
self.fileUploadedCompletion = fileUploadedCompletion | ||
self.fileDeletedCompletion = fileDeletedCompletion | ||
} | ||
|
||
func newUploadedFileView( | ||
parameters: FileParameters) | ||
-> some View | ||
{ | ||
AttachmentView(fileName: fileObject?.filename ?? parameters.fileName, actionTrigger: $deleted, isLoading: fileObject == nil || deleted) | ||
.disabled(fileObject == nil) | ||
.opacity(fileObject == nil ? 0.3 : 1) | ||
.onFirstAppear { | ||
Task { | ||
fileObject = try await fileProvider.uploadFile(parameters: parameters) | ||
} | ||
} | ||
.onChange(of: fileObject) { oldValue, newValue in | ||
if oldValue != newValue, let newValue { | ||
fileUploadedCompletion(newValue) | ||
} | ||
} | ||
} | ||
|
||
func previousUploadedFileView( | ||
id: String) | ||
-> some View | ||
{ | ||
AttachmentView(fileName: fileObject?.filename ?? "Document", actionTrigger: $deleted, isLoading: fileObject == nil || deleted) | ||
.onFirstAppear { | ||
Task { | ||
fileObject = try await fileProvider.retrieveFileWith(id: id) | ||
} | ||
} | ||
} | ||
|
||
var body: some View { | ||
Group { | ||
switch action { | ||
case .request(let parameters): | ||
newUploadedFileView(parameters: parameters) | ||
case .retrieveAndDisplay(let id): | ||
previousUploadedFileView(id: id) | ||
} | ||
} | ||
.onChange(of: deleted) { oldValue, newValue in | ||
if oldValue != newValue, newValue { | ||
Task { | ||
if let fileObject { | ||
fileDeleteStatus = try await fileProvider.deleteFileWith(id: fileObject.id) | ||
} | ||
} | ||
} | ||
} | ||
.onChange(of: fileDeleteStatus) { oldValue, newValue in | ||
if oldValue != newValue, let newValue, newValue.deleted { | ||
fileDeletedCompletion(action, newValue.id) | ||
} | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private let fileProvider: FilesPickerProvider | ||
private let fileUploadedCompletion: (_ file: FileObject) -> Void | ||
private let fileDeletedCompletion: (_ action: FilePickerAction, _ id: String) -> Void | ||
private let action: FilePickerAction | ||
@State private var fileObject: FileObject? | ||
@State private var fileDeleteStatus: DeletionStatus? | ||
@State private var deleted: Bool = false | ||
} | ||
|
||
|
||
private struct OnFirstAppear: ViewModifier { | ||
let perform: () -> Void | ||
|
||
@State private var firstTime = true | ||
|
||
func body(content: Content) -> some View { | ||
content.onAppear { | ||
if firstTime { | ||
firstTime = false | ||
perform() | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
func onFirstAppear(perform: @escaping () -> Void) -> some View { | ||
modifier(OnFirstAppear(perform: perform)) | ||
} | ||
} | ||
|
||
extension DeletionStatus: Equatable { | ||
public static func == (lhs: DeletionStatus, rhs: DeletionStatus) -> Bool { | ||
lhs.id == rhs.id | ||
} | ||
} |
Oops, something went wrong.