From 0b7363757606a9101d33b63470cf1cd6560e0d39 Mon Sep 17 00:00:00 2001 From: Vatsal Manot Date: Thu, 12 Dec 2024 01:00:04 +0530 Subject: [PATCH] Update package --- Package.swift | 15 ++++++-- ...=> ModelContent+LargeLanguageModels.swift} | 35 ++++++++++++------- Sources/GoogleGenerativeAI/module.swift | 5 +++ Tests/GoogleAITests/ChatTests.swift | 1 + Tests/GoogleAITests/CodeExecutionTests.swift | 1 + .../GenerateContentRequestTests.swift | 1 + .../GenerateContentResponseTests.swift | 1 + .../GoogleAITests/GenerationConfigTests.swift | 2 +- .../GoogleAITests/GenerativeModelTests.swift | 1 + Tests/GoogleAITests/GoogleAITests.swift | 2 +- Tests/GoogleAITests/JSONValueTests.swift | 2 +- Tests/GoogleAITests/ModelContentTests.swift | 2 +- .../PartsRepresentableTests.swift | 2 +- .../PreternaturalAI_Tests.swift} | 22 ++++++++---- 14 files changed, 65 insertions(+), 27 deletions(-) rename Sources/GoogleAI/Preternatural/{Gemini+AbstractLLM.swift => ModelContent+LargeLanguageModels.swift} (53%) create mode 100644 Sources/GoogleGenerativeAI/module.swift rename Tests/GoogleAITests/{GoogleLLMRequestHandlingTests.swift => Preternatural/PreternaturalAI_Tests.swift} (90%) diff --git a/Package.swift b/Package.swift index c965951..5fa925d 100644 --- a/Package.swift +++ b/Package.swift @@ -27,7 +27,8 @@ let package = Package( .library( name: "Gemini", targets: [ - "Gemini" + "Gemini", + "GoogleGenerativeAI", ] ), ], @@ -40,12 +41,20 @@ let package = Package( dependencies: [ "AI" ], - path: "Sources" + path: "Sources/GoogleAI" + ), + .target( + name: "GoogleGenerativeAI", + dependencies: [ + "Gemini" + ], + path: "Sources/GoogleGenerativeAI" ), .testTarget( name: "GeminiTests", dependencies: [ - "Gemini" + "Gemini", + "GoogleGenerativeAI", ], path: "Tests", resources: [ diff --git a/Sources/GoogleAI/Preternatural/Gemini+AbstractLLM.swift b/Sources/GoogleAI/Preternatural/ModelContent+LargeLanguageModels.swift similarity index 53% rename from Sources/GoogleAI/Preternatural/Gemini+AbstractLLM.swift rename to Sources/GoogleAI/Preternatural/ModelContent+LargeLanguageModels.swift index f7308f3..9325224 100644 --- a/Sources/GoogleAI/Preternatural/Gemini+AbstractLLM.swift +++ b/Sources/GoogleAI/Preternatural/ModelContent+LargeLanguageModels.swift @@ -3,6 +3,7 @@ // @_spi(Internal) import LargeLanguageModels +import FoundationX import Swallow import SwiftUIX @@ -17,18 +18,28 @@ extension ModelContent { for component in messageContent.components { switch component.payload { - case .string(let string): - parts.append(.text(string)) - case .image(let image): - if case .url(let url) = image { - let (data, response) = try await URLSession.shared.data(from: url) - let mimeType = response.mimeType ?? "image/png" - parts.append(.data(mimetype: mimeType, data)) - } - case .functionCall(_): - TODO.unimplemented - case .resultOfFunctionCall(_): - TODO.unimplemented + case .string(let string): + parts.append(.text(string)) + case .image(let image): + switch image { + case .url(let url): do { + let (data, response) = try await URLSession.shared.data(from: url) + let mimeType: String = response.mimeType ?? "image/png" + + parts.append(.data(mimetype: mimeType, data)) + } + case .image(let image): + let data: Data = try image.jpegData.unwrap() + let mimeType: String = _MediaAssetFileType.jpeg.mimeType + + parts.append(.data(mimetype: mimeType, data)) + case .base64DataURL: + TODO.unimplemented + } + case .functionCall(_): + TODO.unimplemented + case .resultOfFunctionCall(_): + TODO.unimplemented } } diff --git a/Sources/GoogleGenerativeAI/module.swift b/Sources/GoogleGenerativeAI/module.swift new file mode 100644 index 0000000..52579a1 --- /dev/null +++ b/Sources/GoogleGenerativeAI/module.swift @@ -0,0 +1,5 @@ +// +// Copyright (c) Preternatural AI, Inc. +// + +@_exported import Gemini diff --git a/Tests/GoogleAITests/ChatTests.swift b/Tests/GoogleAITests/ChatTests.swift index 37b2dc8..043d1c8 100644 --- a/Tests/GoogleAITests/ChatTests.swift +++ b/Tests/GoogleAITests/ChatTests.swift @@ -14,6 +14,7 @@ import Foundation @testable import Gemini +@testable import GoogleGenerativeAI import XCTest @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, *) diff --git a/Tests/GoogleAITests/CodeExecutionTests.swift b/Tests/GoogleAITests/CodeExecutionTests.swift index 2818fe6..3733309 100644 --- a/Tests/GoogleAITests/CodeExecutionTests.swift +++ b/Tests/GoogleAITests/CodeExecutionTests.swift @@ -14,6 +14,7 @@ import XCTest +@testable import Gemini @testable import GoogleGenerativeAI @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) diff --git a/Tests/GoogleAITests/GenerateContentRequestTests.swift b/Tests/GoogleAITests/GenerateContentRequestTests.swift index dbc838c..adb920d 100644 --- a/Tests/GoogleAITests/GenerateContentRequestTests.swift +++ b/Tests/GoogleAITests/GenerateContentRequestTests.swift @@ -16,6 +16,7 @@ import Foundation import XCTest @testable import Gemini +@testable import GoogleGenerativeAI @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) final class GenerateContentRequestTests: XCTestCase { diff --git a/Tests/GoogleAITests/GenerateContentResponseTests.swift b/Tests/GoogleAITests/GenerateContentResponseTests.swift index 46ee8b7..a273334 100644 --- a/Tests/GoogleAITests/GenerateContentResponseTests.swift +++ b/Tests/GoogleAITests/GenerateContentResponseTests.swift @@ -15,6 +15,7 @@ import Foundation import XCTest +@testable import Gemini @testable import GoogleGenerativeAI @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) diff --git a/Tests/GoogleAITests/GenerationConfigTests.swift b/Tests/GoogleAITests/GenerationConfigTests.swift index bf310a0..06dada7 100644 --- a/Tests/GoogleAITests/GenerationConfigTests.swift +++ b/Tests/GoogleAITests/GenerationConfigTests.swift @@ -13,7 +13,7 @@ // limitations under the License. import Foundation -import Gemini +import GoogleGenerativeAI import XCTest @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) diff --git a/Tests/GoogleAITests/GenerativeModelTests.swift b/Tests/GoogleAITests/GenerativeModelTests.swift index 1f86af3..0676025 100644 --- a/Tests/GoogleAITests/GenerativeModelTests.swift +++ b/Tests/GoogleAITests/GenerativeModelTests.swift @@ -13,6 +13,7 @@ // limitations under the License. @testable import Gemini +@testable import GoogleGenerativeAI import XCTest @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, *) diff --git a/Tests/GoogleAITests/GoogleAITests.swift b/Tests/GoogleAITests/GoogleAITests.swift index ddb3db9..151e35d 100644 --- a/Tests/GoogleAITests/GoogleAITests.swift +++ b/Tests/GoogleAITests/GoogleAITests.swift @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import Gemini +import GoogleGenerativeAI import XCTest #if canImport(AppKit) import AppKit // For NSImage extensions. diff --git a/Tests/GoogleAITests/JSONValueTests.swift b/Tests/GoogleAITests/JSONValueTests.swift index a12a2a9..19c871e 100644 --- a/Tests/GoogleAITests/JSONValueTests.swift +++ b/Tests/GoogleAITests/JSONValueTests.swift @@ -13,7 +13,7 @@ // limitations under the License. import XCTest -@testable import Gemini +@testable import GoogleGenerativeAI final class JSONValueTests: XCTestCase { let decoder = JSONDecoder() diff --git a/Tests/GoogleAITests/ModelContentTests.swift b/Tests/GoogleAITests/ModelContentTests.swift index 602037a..ef5d1c9 100644 --- a/Tests/GoogleAITests/ModelContentTests.swift +++ b/Tests/GoogleAITests/ModelContentTests.swift @@ -13,7 +13,7 @@ // limitations under the License. import Foundation -import Gemini +import GoogleGenerativeAI import XCTest @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) diff --git a/Tests/GoogleAITests/PartsRepresentableTests.swift b/Tests/GoogleAITests/PartsRepresentableTests.swift index a35c428..da2f35f 100644 --- a/Tests/GoogleAITests/PartsRepresentableTests.swift +++ b/Tests/GoogleAITests/PartsRepresentableTests.swift @@ -14,7 +14,7 @@ import CoreGraphics import CoreImage -import Gemini +import GoogleGenerativeAI import XCTest #if canImport(UIKit) import UIKit diff --git a/Tests/GoogleAITests/GoogleLLMRequestHandlingTests.swift b/Tests/GoogleAITests/Preternatural/PreternaturalAI_Tests.swift similarity index 90% rename from Tests/GoogleAITests/GoogleLLMRequestHandlingTests.swift rename to Tests/GoogleAITests/Preternatural/PreternaturalAI_Tests.swift index 0e1ac66..9754cc2 100644 --- a/Tests/GoogleAITests/GoogleLLMRequestHandlingTests.swift +++ b/Tests/GoogleAITests/Preternatural/PreternaturalAI_Tests.swift @@ -14,11 +14,11 @@ import AI import Gemini -import XCTest import SwiftUIX +import XCTest -final class GoogleLLMRequestHandlingTestCase: XCTestCase { - let client: any LLMRequestHandling = Gemini.Client(apiKey: "AIzaSyB-HamvuejgTbWm6gRy1Bb_PnCclMWUloc") +final class PreternaturalAI_Tests: XCTestCase { + let client: any LLMRequestHandling = Gemini.Client(apiKey: "API_KEY") func testAvailableModels() { let models = client._availableModels @@ -122,9 +122,17 @@ final class GoogleLLMRequestHandlingTestCase: XCTestCase { Remember to ALWAYS respond in accordance to the testing rules. """ - let parameters: AbstractLLM.ChatCompletionParameters = .init() + let parameters = AbstractLLM.ChatCompletionParameters() + let imageConfiguration = AppKitOrUIKitImage.SymbolConfiguration( + pointSize: 64 + ) + let image = AppKitOrUIKitImage( + _SwiftUIX_systemName: "arrow.up", + withConfiguration: imageConfiguration.applying(.init(hierarchicalColor: .blue)) + )! + + image.backgroundColor = AppKitOrUIKitColor.white - let image = AppKitOrUIKitImage(systemSymbolName: "arrow.right", accessibilityDescription: nil)! let imageLiteral = try PromptLiteral(image: image) let messagesWithSystemPrompt: [AbstractLLM.ChatMessage] = [ @@ -148,7 +156,7 @@ final class GoogleLLMRequestHandlingTestCase: XCTestCase { let completion1: String = try await client.complete( messagesWithSystemPrompt, parameters: parameters, - model: Gemini.Model.gemini_1_5_flash, + model: Gemini.Model.gemini_2_0_flash_exp, as: .string ) @@ -157,7 +165,7 @@ final class GoogleLLMRequestHandlingTestCase: XCTestCase { let completion2: String = try await client.complete( messagesWithOutPrompt, parameters: parameters, - model: Gemini.Model.gemini_1_5_flash, + model: Gemini.Model.gemini_2_0_flash_exp, as: .string )