Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Created November 24, 2023 16:53
Show Gist options
  • Save nathanborror/35ff13e925e27ca96518f51f1821ece0 to your computer and use it in GitHub Desktop.
Save nathanborror/35ff13e925e27ca96518f51f1821ece0 to your computer and use it in GitHub Desktop.
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment