|
| 1 | +// |
| 2 | +// ChatStore.swift |
| 3 | +// DemoChat |
| 4 | +// |
| 5 | +// Created by Sihao Lu on 3/25/23. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Combine |
| 10 | +import OpenAI |
| 11 | + |
| 12 | +public final class AssistantStore: ObservableObject { |
| 13 | + public var openAIClient: OpenAIProtocol |
| 14 | + let idProvider: () -> String |
| 15 | + @Published var selectedAssistantId: String? |
| 16 | + |
| 17 | + @Published var availableAssistants: [Assistant] = [] |
| 18 | + |
| 19 | + public init( |
| 20 | + openAIClient: OpenAIProtocol, |
| 21 | + idProvider: @escaping () -> String |
| 22 | + ) { |
| 23 | + self.openAIClient = openAIClient |
| 24 | + self.idProvider = idProvider |
| 25 | + } |
| 26 | + |
| 27 | + // MARK: Models |
| 28 | + |
| 29 | + @MainActor |
| 30 | + func createAssistant(name: String, description: String, instructions: String, codeInterpreter: Bool, fileSearch: Bool, functions: [FunctionDeclaration], fileIds: [String]? = nil) async -> String? { |
| 31 | + do { |
| 32 | + let toolResources: ToolResources? = if let fileIds { |
| 33 | + ToolResources(fileSearch: nil, codeInterpreter: .init(fileIds: fileIds)) |
| 34 | + } else { |
| 35 | + nil |
| 36 | + } |
| 37 | + |
| 38 | + let tools = createToolsArray(codeInterpreter: codeInterpreter, fileSearch: fileSearch, functions: functions) |
| 39 | + let query = AssistantsQuery(model: Model.gpt4_o_mini, name: name, description: description, instructions: instructions, tools:tools, toolResources: toolResources) |
| 40 | + let response = try await openAIClient.assistantCreate(query: query) |
| 41 | + |
| 42 | + // Refresh assistants with one just created (or modified) |
| 43 | + let _ = await getAssistants() |
| 44 | + |
| 45 | + // Returns assistantId |
| 46 | + return response.id |
| 47 | + |
| 48 | + } catch { |
| 49 | + // TODO: Better error handling |
| 50 | + print(error.localizedDescription) |
| 51 | + } |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + @MainActor |
| 56 | + func modifyAssistant(asstId: String, name: String, description: String, instructions: String, codeInterpreter: Bool, fileSearch: Bool, functions: [FunctionDeclaration], fileIds: [String]? = nil) async -> String? { |
| 57 | + do { |
| 58 | + let toolResources: ToolResources? = if let fileIds { |
| 59 | + ToolResources(fileSearch: nil, codeInterpreter: .init(fileIds: fileIds)) |
| 60 | + } else { |
| 61 | + nil |
| 62 | + } |
| 63 | + |
| 64 | + let tools = createToolsArray(codeInterpreter: codeInterpreter, fileSearch: fileSearch, functions: functions) |
| 65 | + let query = AssistantsQuery(model: Model.gpt4_o_mini, name: name, description: description, instructions: instructions, tools:tools, toolResources: toolResources) |
| 66 | + let response = try await openAIClient.assistantModify(query: query, assistantId: asstId) |
| 67 | + |
| 68 | + // Returns assistantId |
| 69 | + return response.id |
| 70 | + |
| 71 | + } catch { |
| 72 | + // TODO: Better error handling |
| 73 | + print(error.localizedDescription) |
| 74 | + } |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + @MainActor |
| 79 | + func getAssistants(limit: Int = 20, after: String? = nil) async -> [Assistant] { |
| 80 | + do { |
| 81 | + let response = try await openAIClient.assistants(after: after) |
| 82 | + |
| 83 | + var assistants = [Assistant]() |
| 84 | + for result in response.data ?? [] { |
| 85 | + let tools = result.tools ?? [] |
| 86 | + let codeInterpreter = tools.contains { $0 == .codeInterpreter } |
| 87 | + let fileSearch = tools.contains { $0 == .fileSearch } |
| 88 | + let functions = tools.compactMap { |
| 89 | + switch $0 { |
| 90 | + case let .function(declaration): |
| 91 | + return declaration |
| 92 | + default: |
| 93 | + return nil |
| 94 | + } |
| 95 | + } |
| 96 | + let fileIds = result.toolResources.codeInterpreter?.fileIds ?? [] |
| 97 | + |
| 98 | + assistants.append(Assistant(id: result.id, name: result.name ?? "", description: result.description, instructions: result.instructions, codeInterpreter: codeInterpreter, fileSearch: fileSearch, fileIds: fileIds, functions: functions)) |
| 99 | + } |
| 100 | + if after == nil { |
| 101 | + availableAssistants = assistants |
| 102 | + } |
| 103 | + else { |
| 104 | + availableAssistants = availableAssistants + assistants |
| 105 | + } |
| 106 | + return assistants |
| 107 | + |
| 108 | + } catch { |
| 109 | + // TODO: Better error handling |
| 110 | + print(error.localizedDescription) |
| 111 | + } |
| 112 | + return [] |
| 113 | + } |
| 114 | + |
| 115 | + func selectAssistant(_ assistantId: String?) { |
| 116 | + selectedAssistantId = assistantId |
| 117 | + } |
| 118 | + |
| 119 | + @MainActor |
| 120 | + func uploadFile(url: URL) async -> FilesResult? { |
| 121 | + do { |
| 122 | + |
| 123 | + let mimeType = url.mimeType() |
| 124 | + |
| 125 | + let fileData = try Data(contentsOf: url) |
| 126 | + |
| 127 | + let result = try await openAIClient.files(query: FilesQuery(purpose: "assistants", file: fileData, fileName: url.lastPathComponent, contentType: mimeType)) |
| 128 | + return result |
| 129 | + } |
| 130 | + catch { |
| 131 | + print("error = \(error)") |
| 132 | + return nil |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + func createToolsArray(codeInterpreter: Bool, fileSearch: Bool, functions: [FunctionDeclaration]) -> [Tool] { |
| 137 | + var tools = [Tool]() |
| 138 | + if codeInterpreter { |
| 139 | + tools.append(.codeInterpreter) |
| 140 | + } |
| 141 | + if fileSearch { |
| 142 | + tools.append(.fileSearch) |
| 143 | + } |
| 144 | + return tools + functions.map { .function($0) } |
| 145 | + } |
| 146 | +} |
0 commit comments