Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Created November 24, 2023 16:53
Show Gist options
  • Select an option

  • Save nathanborror/35ff13e925e27ca96518f51f1821ece0 to your computer and use it in GitHub Desktop.

Select an option

Save nathanborror/35ff13e925e27ca96518f51f1821ece0 to your computer and use it in GitHub Desktop.

Revisions

  1. nathanborror created this gist Nov 24, 2023.
    49 changes: 49 additions & 0 deletions Message.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    struct Message: Codable, Identifiable {
    var id: String
    var role: Role
    var content: String?
    var toolCalls: [ToolCall]?
    var finishReason: FinishReason?
    var created: Date
    var modified: Date

    enum Role: String, Codable {
    case system, assistant, user, tool
    }

    enum FinishReason: Codable {
    case stop, length, toolCalls, contentFilter, cancelled
    }

    struct Content: Codable {
    var type: ContentType
    var text: String?
    var image_url: ImageURL?

    struct ImageURL: Codable {
    var url: String
    var detail: String?
    }

    enum ContentType: String, Codable {
    case text, image_url
    }
    }

    init(id: String = UUID().uuidString, role: Role, content: String? = nil, toolCalls: [ToolCall]? = nil, finishReason: FinishReason? = nil) {
    self.id = id
    self.role = role
    self.content = content
    self.toolCalls = toolCalls
    self.finishReason = finishReason
    self.created = .now
    self.modified = .now
    }

    init(id: String = UUID().uuidString, role: Role, content: [Content], toolCalls: [ToolCall]? = nil, finishReason: FinishReason? = nil) throws {
    self.init(id: id, role: role, toolCalls: toolCalls, finishReason: finishReason)

    let data = try JSONEncoder().encode(content)
    self.content = String(data: data, encoding: .utf8)
    }
    }