Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New content format #122

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 108 additions & 3 deletions Sources/OpenAI/Public/Models/ChatQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import Foundation



// See more https://platform.openai.com/docs/guides/text-generation/json-mode
public struct ResponseFormat: Codable, Equatable {
ingvarus-bc marked this conversation as resolved.
Show resolved Hide resolved
public static let jsonObject = ResponseFormat(type: .jsonObject)
Expand All @@ -20,10 +22,89 @@ public struct ResponseFormat: Codable, Equatable {
}
}

public struct ChatContent: Codable, Equatable {
let type: ChatContentType
let value: String

public enum ChatContentType: String, Codable {
case text
case imageUrl = "image_url"
}

public struct ImageUrl: Codable, Equatable {
let url: String

enum CodingKeys: CodingKey {
case url
}
}


enum CodingKeys: CodingKey {
case type
case value
}

public static func text(_ text: String) -> Self {
Self.init(text)
}

public static func imageUrl(_ url: String) -> Self {
Self.init(type: .imageUrl, value: url)
}

public init(type: ChatContentType, value: String) {
self.type = type
self.value = value
}

public init(_ text: String) {
self.type = .text
self.value = text
}

// we need to perform a custom encoding since the `value` key is variable based on the `type`
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: ChatContent.CodingKeys.self)
var dynamicContainer = encoder.container(keyedBy: DynamicKey.self)

try container.encode(type, forKey: .type)

switch self.type {
case .text:
try dynamicContainer.encode(value, forKey: .init(stringValue: "text"))
break
case .imageUrl:
var nested = dynamicContainer.nestedContainer(keyedBy: ImageUrl.CodingKeys.self, forKey: .init(stringValue: "image_url"))
try nested.encode(value, forKey: .url)
break
}
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decode(ChatContentType.self, forKey: .type)

let dynamicContainer = try decoder.container(keyedBy: DynamicKey.self)

switch self.type {
case .text:
self.value = try dynamicContainer.decode(String.self, forKey: .init(stringValue: "text"))
break
case .imageUrl:
let nested = try dynamicContainer.nestedContainer(keyedBy: ImageUrl.CodingKeys.self, forKey: .init(stringValue: "image_url"))
self.value = try nested.decode(String.self, forKey: .url)
break
}
}
}


public struct Chat: Codable, Equatable {
public let role: Role
/// The contents of the message. `content` is required for all messages except assistant messages with function calls.
public let content: String?
public let content: StringOrCodable<[ChatContent]>?

/// The name of the author of this message. `name` is required if role is `function`, and it should be the name of the function whose response is in the `content`. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
public let name: String?
public let functionCall: ChatFunctionCall?
Expand All @@ -42,9 +123,33 @@ public struct Chat: Codable, Equatable {
case functionCall = "function_call"
}

public init(role: Role, content: String? = nil, name: String? = nil, functionCall: ChatFunctionCall? = nil) {
public init(role: Role, content stringContent: String? = nil, name: String? = nil, functionCall: ChatFunctionCall? = nil) {
let stringOrCodable: StringOrCodable<[ChatContent]>?;

if let string = stringContent {
stringOrCodable = .string(string)
} else {
stringOrCodable = nil
}

self.init(role: role, contents: stringOrCodable, name: name, functionCall: functionCall)
}

public init(role: Role, content arr: [ChatContent]? = nil ,name: String? = nil, functionCall: ChatFunctionCall? = nil) {
let stringOrCodable: StringOrCodable<[ChatContent]>?

if let arr = arr {
stringOrCodable = .object(arr)
} else {
stringOrCodable = nil
}

self.init(role: role, contents: stringOrCodable, name: name, functionCall: functionCall)
}

public init(role: Role, contents: StringOrCodable<[ChatContent]>? = nil, name: String? = nil, functionCall: ChatFunctionCall? = nil) {
self.role = role
self.content = content
self.content = contents
self.name = name
self.functionCall = functionCall
}
Expand Down
118 changes: 118 additions & 0 deletions Sources/OpenAI/Public/Utilities/CodableUtilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// CodableUtilities.swift
//
//
// Created by Federico Vitale on 09/11/23.
//

import Foundation


/// Allows having dynamic keys in codables.
struct DynamicKey: CodingKey {
var stringValue: String
var intValue: Int?

init(stringValue: String) {
self.stringValue = stringValue
}

init?(intValue: Int) {
self.intValue = intValue
self.stringValue = "\(intValue)"
}
}


/// Allows to encode/decode ``Chat`` or ``Codable`` (T)
/// ```swift
/// struct Person: Codable, Equatable {
/// let name: StringOrCodable<FullName>
///
/// struct FullName {
/// firstName: String
/// lastName: String
/// }
/// }
///
/// let person = Person(name: .string("John Doe"))
/// let fullNamePerson = Person(name: .object(.init(firstName: "John", lastName: "Doe")))
/// ```
public enum StringOrCodable<T: Codable>: Equatable, Codable where T: Equatable {
rawnly marked this conversation as resolved.
Show resolved Hide resolved
case string(String)
case object(T)

enum CodingKeys: CodingKey {
case string
case object
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

switch self {
case .string(let string):
try container.encode(string)
case .object(let object):
try container.encode(object)
}
}


public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

if let string = try? container.decode(String.self) {
self = .string(string)
} else if let object = try? container.decode(T.self) {
self = .object(object)
} else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Invalid data encountered when decoding StringOrCodable<T>"
)
)
}
}
}

/// Same as ``StringOrCodable`` but accepts 2 codable generics instead of String as first generic argument
public enum AnyOf<T: Codable, U: Codable>: Equatable, Codable where T: Equatable, U: Equatable {
case objectA(T)
case objectB(U)

enum CodingKeys: CodingKey {
case objectA
case objectB
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

switch self {
case .objectB(let value):
try container.encode(value)
case .objectA(let value):
try container.encode(value)
}
}


public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

if let valueT = try? container.decode(T.self) {
self = .objectA(valueT)
} else if let valueU = try? container.decode(U.self) {
self = .objectB(valueU)
} else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Invalid data encountered when decoding StringOrCodable<T>"
)
)
}
}
}
84 changes: 84 additions & 0 deletions Tests/OpenAITests/CodableUtilsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// File.swift
//
//
// Created by Federico Vitale on 09/11/23.
//

import XCTest
@testable import OpenAI

fileprivate class TestUtils {
static func decode<T: Decodable & Equatable>(
_ jsonString: String,
_ expectedValue: T
) throws {
let data = jsonString.data(using: .utf8)!
let decoded = try JSONDecoder().decode(T.self, from: data)

XCTAssertEqual(decoded, expectedValue)
}

static func encode<T: Encodable & Equatable>(
_ value: T,
_ expectedJson: String
) throws {
let source = try jsonDataAsNSDictionary(JSONEncoder().encode(value))
let expected = try jsonDataAsNSDictionary(expectedJson.data(using: .utf8)!)

XCTAssertEqual(source, expected)
}

static func jsonDataAsNSDictionary(_ data: Data) throws -> NSDictionary {
return NSDictionary(dictionary: try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any])
}
}


class CodableUtilsTests: XCTestCase {
func testStringOrCodable_String() throws {
struct Person: Codable, Equatable {
let name: StringOrCodable<FullName>

struct FullName: Codable, Equatable {
let firstName: String
let lastName: String
}
}

let jsonString = """
{
"name": "test"
}
"""

let value = Person(name: .string("test"))

try TestUtils.encode(value, jsonString)
try TestUtils.decode(jsonString, value)
}

func testStringOrCodable_Object() throws {
struct Person: Codable, Equatable {
let name: StringOrCodable<FullName>

struct FullName: Codable, Equatable {
let firstName: String
let lastName: String
}
}

let jsonString = """
{
"name": { "firstName": "first", "lastName": "last" }
}
"""

let value = Person(name: .object(.init(firstName: "first", lastName: "last")))

try TestUtils.encode(value, jsonString)
try TestUtils.decode(jsonString, value)
}
}


23 changes: 12 additions & 11 deletions Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ class OpenAITests: XCTestCase {
}

func testChats() async throws {
let query = ChatQuery(model: .gpt4, messages: [
.init(role: .system, content: "You are Librarian-GPT. You know everything about the books."),
.init(role: .user, content: "Who wrote Harry Potter?")
])
let chatResult = ChatResult(id: "id-12312", object: "foo", created: 100, model: .gpt3_5Turbo, choices: [
.init(index: 0, message: .init(role: .system, content: "bar"), finishReason: "baz"),
.init(index: 0, message: .init(role: .user, content: "bar1"), finishReason: "baz1"),
.init(index: 0, message: .init(role: .assistant, content: "bar2"), finishReason: "baz2")
let query = ChatQuery(model: .gpt4, messages: [
.init(role: .system, content: "You are Librarian-GPT. You know everything about the books."),
.init(role: .user, content: "Who wrote Harry Potter?")
])
let chatResult = ChatResult(id: "id-12312", object: "foo", created: 100, model: .gpt3_5Turbo, choices: [
.init(index: 0, message: .init(role: .system, content: "bar"), finishReason: "baz"),
.init(index: 0, message: .init(role: .user, content: "bar1"), finishReason: "baz1"),
.init(index: 0, message: .init(role: .assistant, content: "bar2"), finishReason: "baz2")
], usage: .init(promptTokens: 100, completionTokens: 200, totalTokens: 300))
try self.stub(result: chatResult)
try self.stub(result: chatResult)

let result = try await openAI.chats(query: query)

let result = try await openAI.chats(query: query)
XCTAssertEqual(result, chatResult)
XCTAssertEqual(result, chatResult)
}

func testChatsFunction() async throws {
Expand Down
Loading