From e2b435d13ae5cec00033044454c63b4eea738695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Heidekr=C3=BCger?= Date: Fri, 4 Oct 2024 13:48:38 -0700 Subject: [PATCH] Swiftlint --- ...MFunctionParameterWrapper+ArrayTypes.swift | 36 ++--------------- ...FunctionParameterWrapper+CustomTypes.swift | 18 +-------- .../LLMFunctionParameterWrapper+Enum.swift | 18 +-------- ...nctionParameterWrapper+OptionalTypes.swift | 36 ++--------------- ...ctionParameterWrapper+PrimitiveTypes.swift | 2 +- .../LLMOpenAIParameterTests+CustomTypes.swift | 10 ++--- .../LLMOpenAIParameterTests+Enum.swift | 40 +++++++++---------- 7 files changed, 38 insertions(+), 122 deletions(-) diff --git a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+ArrayTypes.swift b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+ArrayTypes.swift index 8039cc8..176362c 100644 --- a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+ArrayTypes.swift +++ b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+ArrayTypes.swift @@ -46,14 +46,7 @@ extension _LLMFunctionParameterWrapper where T: AnyArray, T.Element: BinaryInteg "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("LLMFunctionParameterWrapper+ArrayTypes") self.init(description: "") @@ -94,14 +87,7 @@ extension _LLMFunctionParameterWrapper where T: AnyArray, T.Element: BinaryFloat "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LMMFunctionParameter+ArrayTypes") self.init(description: "") @@ -136,14 +122,7 @@ extension _LLMFunctionParameterWrapper where T: AnyArray, T.Element == Bool { "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionParameterWrapper+ArrayTypes") self.init(description: "") @@ -184,14 +163,7 @@ extension _LLMFunctionParameterWrapper where T: AnyArray, T.Element: StringProto "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionParameterWrapper+ArrayTypes") self.init(description: "") diff --git a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+CustomTypes.swift b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+CustomTypes.swift index 25b245f..a32898f 100644 --- a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+CustomTypes.swift +++ b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+CustomTypes.swift @@ -43,14 +43,7 @@ extension _LLMFunctionParameterWrapper where T: AnyArray, T.Element: LLMFunction "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("Couldn't create FunctionParameterWrapper+CustomType \(error)") self.init(description: "") @@ -91,14 +84,7 @@ extension _LLMFunctionParameterWrapper where T: AnyOptional, T.Wrapped: AnyArray "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("Couldn't create LLMFunctionParameterWrapper+CustomTypes") self.init(description: "") diff --git a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+Enum.swift b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+Enum.swift index 0f9d73e..ae51fbf 100644 --- a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+Enum.swift +++ b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+Enum.swift @@ -97,14 +97,7 @@ extension _LLMFunctionParameterWrapper where T: AnyArray, T.Element: LLMFunction "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger .error( @@ -147,14 +140,7 @@ extension _LLMFunctionParameterWrapper where T: AnyOptional, "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger .error( diff --git a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+OptionalTypes.swift b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+OptionalTypes.swift index cde0c2d..cd3f390 100644 --- a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+OptionalTypes.swift +++ b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+OptionalTypes.swift @@ -163,14 +163,7 @@ extension _LLMFunctionParameterWrapper where T: AnyOptional, T.Wrapped: AnyArray "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionPropertyWrapper+OptionalType") self.init(description: "") @@ -212,14 +205,7 @@ extension _LLMFunctionParameterWrapper where T: AnyOptional, T.Wrapped: AnyArray "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionParameterWrapper+OptionalTypes") self.init(description: "") @@ -254,14 +240,7 @@ extension _LLMFunctionParameterWrapper where T: AnyOptional, T.Wrapped: AnyArray "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionParameterWrapper+OptionalTypes.swift") self.init(description: "") @@ -303,14 +282,7 @@ extension _LLMFunctionParameterWrapper where T: AnyOptional, T.Wrapped: AnyArray "minItems": minItems as Any?, "maxItems": maxItems as Any?, "uniqueItems": uniqueItems as Any? - ].compactMapValues { $0 } - // .filter { _, value in if let dict = value as? [String: Any] { - // dict.count > 1 - // } else { - // true - // } - // })) - )) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionParameterWrapper+OptionalType") self.init(description: "") diff --git a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+PrimitiveTypes.swift b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+PrimitiveTypes.swift index 87a9975..50ef43a 100644 --- a/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+PrimitiveTypes.swift +++ b/Sources/SpeziLLMOpenAI/FunctionCalling/LLMFunctionParameterWrapper+PrimitiveTypes.swift @@ -32,7 +32,7 @@ extension _LLMFunctionParameterWrapper where T: BinaryInteger { "multipleOf": multipleOf as Any?, "minimum": minimum.map { Double($0) } as Any?, "maximum": maximum.map { Double($0) } as Any? - ].compactMapValues { $0 })) + ].compactMapValues { $0 })) } catch { logger.error("SpeziLLMOpenAI - initialization error - LLMFunctionParameter+PrimitveTypes") self.init(description: "") diff --git a/Tests/SpeziLLMTests/LLMOpenAIParameterTests+CustomTypes.swift b/Tests/SpeziLLMTests/LLMOpenAIParameterTests+CustomTypes.swift index 758db0c..c055f79 100644 --- a/Tests/SpeziLLMTests/LLMOpenAIParameterTests+CustomTypes.swift +++ b/Tests/SpeziLLMTests/LLMOpenAIParameterTests+CustomTypes.swift @@ -14,7 +14,7 @@ final class LLMOpenAIParameterCustomTypesTests: XCTestCase { struct CustomType: LLMFunctionParameterArrayElement, Encodable, Equatable { static var itemSchema: LLMFunctionParameterItemSchema = { do { - return try .init(unvalidatedValue: [ + return try .init(unvalidatedValue: [ "type": "object", "properties": [ "propertyA": [ @@ -27,19 +27,19 @@ final class LLMOpenAIParameterCustomTypesTests: XCTestCase { ] ] ]) - } - catch { + } catch { print("unable to initialse schema in LLMOpenAIParameterCustomTypesTets") return .init() } }() + var propertyA: String var propertyB: Int } - + struct Parameters: Encodable { static let shared = Self() - + let customArrayParameter = [ CustomType(propertyA: "testA", propertyB: 123), CustomType(propertyA: "testB", propertyB: 456) diff --git a/Tests/SpeziLLMTests/LLMOpenAIParameterTests+Enum.swift b/Tests/SpeziLLMTests/LLMOpenAIParameterTests+Enum.swift index 4343dde..31aaa09 100644 --- a/Tests/SpeziLLMTests/LLMOpenAIParameterTests+Enum.swift +++ b/Tests/SpeziLLMTests/LLMOpenAIParameterTests+Enum.swift @@ -81,35 +81,35 @@ final class LLMOpenAIParameterEnumTests: XCTestCase { // Validate parameter schema let schemaEnum = try XCTUnwrap(llmFunction.schemaValueCollectors["enumParameter"]) var schema = schemaEnum.schema.value - XCTAssertEqual(schema["type"] as! String, "string") - XCTAssertEqual(schema["description"] as! String, "Enum Parameter") - XCTAssertEqual(schema["const"] as! String, "optionA") - XCTAssertEqual(schema["enum"] as! [String], CustomEnumType.allCases.map { $0.rawValue }) + XCTAssertEqual(schema["type"] as? String, "string") + XCTAssertEqual(schema["description"] as? String, "Enum Parameter") + XCTAssertEqual(schema["const"] as? String, "optionA") + XCTAssertEqual(schema["enum"] as? [String], CustomEnumType.allCases.map { $0.rawValue }) let schemaOptionalEnum = try XCTUnwrap(llmFunction.schemaValueCollectors["optionalEnumParameter"]) schema = schemaOptionalEnum.schema.value - XCTAssertEqual(schema["type"] as! String, "string") - XCTAssertEqual(schema["description"] as! String, "Optional Enum Parameter") - XCTAssertEqual(schema["enum"] as! [String], CustomEnumType.allCases.map { $0.rawValue }) + XCTAssertEqual(schema["type"] as? String, "string") + XCTAssertEqual(schema["description"] as? String, "Optional Enum Parameter") + XCTAssertEqual(schema["enum"] as? [String], CustomEnumType.allCases.map { $0.rawValue }) let schemaArrayEnum = try XCTUnwrap(llmFunction.schemaValueCollectors["arrayEnumParameter"]) schema = schemaArrayEnum.schema.value - var items = schema["items"] as! [String: Any] - XCTAssertEqual(schema["type"] as! String, "array") - XCTAssertEqual(schema["description"] as! String, "Array Enum Parameter") - XCTAssertEqual(schema["minItems"] as! Int, 1) - XCTAssertEqual(schema["maxItems"] as! Int, 5) - XCTAssertFalse(schema["uniqueItems"] as! Bool) - XCTAssertEqual(items["type"] as! String, "string") - XCTAssertEqual(items["enum"] as! [String], CustomEnumType.allCases.map { $0.rawValue }) + var items = schema["items"] as? [String: Any] + XCTAssertEqual(schema["type"] as? String, "array") + XCTAssertEqual(schema["description"] as? String, "Array Enum Parameter") + XCTAssertEqual(schema["minItems"] as? Int, 1) + XCTAssertEqual(schema["maxItems"] as? Int, 5) + XCTAssertFalse(schema["uniqueItems"] as? Bool) + XCTAssertEqual(items["type"] as? String, "string") + XCTAssertEqual(items["enum"] as? [String], CustomEnumType.allCases.map { $0.rawValue }) let schemaOptionalArrayEnum = try XCTUnwrap(llmFunction.schemaValueCollectors["optionalArrayEnumParameter"]) schema = schemaOptionalArrayEnum.schema.value - items = schema["items"] as! [String: Any] - XCTAssertEqual(schema["type"] as! String, "array") - XCTAssertEqual(schema["description"] as! String, "Optional Array Enum Parameter") - XCTAssertEqual(items["type"] as! String, "string") - XCTAssertEqual(items["enum"] as! [String], CustomEnumType.allCases.map { $0.rawValue }) + items = schema["items"] as? [String: Any] + XCTAssertEqual(schema["type"] as? String, "array") + XCTAssertEqual(schema["description"] as? String, "Optional Array Enum Parameter") + XCTAssertEqual(items["type"] as? String, "string") + XCTAssertEqual(items["enum"] as? [String], CustomEnumType.allCases.map { $0.rawValue }) // Validate parameter injection let parameterData = try XCTUnwrap(