Skip to content

Commit 649a8f2

Browse files
authored
Message Content - added image url response type (#103)
1 parent c1be624 commit 649a8f2

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

Sources/OpenAI/Public/ResponseModels/Messages/MessageContent.swift

+34-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import Foundation
1010
public enum MessageContent: Codable {
1111

1212
case imageFile(ImageFile)
13+
case imageUrl(ImageURL)
1314
case text(Text)
1415

1516
enum CodingKeys: String, CodingKey {
1617
case type
1718
case imageFile = "image_file"
19+
case imageUrl = "image_url"
1820
case text
1921
}
2022

@@ -28,6 +30,9 @@ public enum MessageContent: Codable {
2830
case .imageFile(let imageFile):
2931
try container.encode("image_file", forKey: .type)
3032
try container.encode(imageFile, forKey: .imageFile)
33+
case .imageUrl(let imageUrl):
34+
try container.encode("image_url", forKey: .type)
35+
try container.encode(imageUrl, forKey: .imageUrl)
3136
case .text(let text):
3237
try container.encode("text", forKey: .type)
3338
try container.encode(text, forKey: .text)
@@ -40,8 +45,11 @@ public enum MessageContent: Codable {
4045

4146
switch type {
4247
case "image_file":
43-
let imageFile = try ImageFile(from: decoder)
48+
let imageFile = try ImageFile(from: decoder)
4449
self = .imageFile(imageFile)
50+
case "image_url":
51+
let imageUrl = try ImageURL(from: decoder)
52+
self = .imageUrl(imageUrl)
4553
case "text":
4654
let text = try Text(from: decoder)
4755
self = .text(text)
@@ -76,6 +84,31 @@ public struct ImageFile: Codable {
7684
}
7785
}
7886

87+
// MARK: Image URl
88+
89+
public struct ImageURL: Codable {
90+
/// Always image_url.
91+
public let type: String
92+
93+
/// References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message.
94+
public let imageUrl: ImageUrlContent
95+
96+
public struct ImageUrlContent: Codable {
97+
98+
/// The [File](https://platform.openai.com/docs/api-reference/files) URL of the image in the message content.
99+
public let url: String
100+
101+
enum CodingKeys: String, CodingKey {
102+
case url
103+
}
104+
}
105+
106+
enum CodingKeys: String, CodingKey {
107+
case imageUrl = "image_url"
108+
case type
109+
}
110+
}
111+
79112
// MARK: Text
80113

81114
public struct Text: Codable {

0 commit comments

Comments
 (0)