Skip to content

Commit 0977263

Browse files
authored
Merge pull request #4 from andgordio/structured-output-ii
October updates
2 parents 1495de3 + 279ca37 commit 0977263

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,22 +405,22 @@ struct MovieInfo: StructuredOutput {
405405
.init(
406406
title: "Earth",
407407
director: "Alexander Dovzhenko",
408-
release: Calendar.current.date(year: 1930, month: 4, day: 8)!,
408+
release: Calendar.current.date(from: DateComponents(year: 1930, month: 4, day: 1))!,
409409
genres: [.drama],
410-
cast: [ "Stepan Shkurat", "Semyon Svashenko", "Yuliya Solntseva" ]
410+
cast: ["Stepan Shkurat", "Semyon Svashenko", "Yuliya Solntseva"]
411411
)
412412
}()
413413
}
414414

415-
enum MovieGenre: String, StructuredOutputEnum {
415+
enum MovieGenre: String, Codable, StructuredOutputEnum {
416416
case action, drama, comedy, scifi
417417

418418
var caseNames: [String] { Self.allCases.map { $0.rawValue } }
419419
}
420420

421421
let query = ChatQuery(
422-
messages: [.system(.init(content: message))],
423-
model: .gpt4_o_2024_08_06,
422+
messages: [.system(.init(content: "Best Picture winner at the 2011 Oscars"))],
423+
model: .gpt4_o,
424424
responseFormat: .jsonSchema(name: "movie-info", type: MovieInfo.self)
425425
)
426426
let result = try await openAI.chats(query: query)

Sources/OpenAI/Public/Models/Models/Models.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ public extension Model {
1212
// Chat Completion
1313
// GPT-4
1414

15-
/// `gpt-4o-2024-08-06`, latest snapshot that supports Structured Outputs
16-
static let gpt4_o_2024_08_06 = "gpt-4o-2024-08-06"
17-
1815
/// `gpt-4o`, currently the most advanced, multimodal flagship model that's cheaper and faster than GPT-4 Turbo.
1916
static let gpt4_o = "gpt-4o"
2017

Tests/OpenAITests/OpenAITests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,46 @@ class OpenAITests: XCTestCase {
115115
let result = try await openAI.chats(query: query)
116116
XCTAssertEqual(result, chatResult)
117117
}
118+
119+
func testChatQueryWithStructuredOutput() async throws {
120+
121+
let chatResult = ChatResult(id: "id-12312", object: "foo", created: 100, model: .gpt3_5Turbo, choices: [
122+
], usage: .init(completionTokens: 200, promptTokens: 100, totalTokens: 300), systemFingerprint: nil)
123+
try self.stub(result: chatResult)
124+
125+
enum MovieGenre: String, Codable, StructuredOutputEnum {
126+
case action, drama, comedy, scifi
127+
var caseNames: [String] { Self.allCases.map { $0.rawValue } }
128+
}
129+
130+
struct MovieInfo: StructuredOutput {
131+
132+
let title: String
133+
let director: String
134+
let release: Date
135+
let genres: [MovieGenre]
136+
let cast: [String]
137+
138+
static let example: Self = {
139+
.init(
140+
title: "Earth",
141+
director: "Alexander Dovzhenko",
142+
release: Calendar.current.date(from: DateComponents(year: 1930, month: 4, day: 1))!,
143+
genres: [.drama],
144+
cast: ["Stepan Shkurat", "Semyon Svashenko", "Yuliya Solntseva"]
145+
)
146+
}()
147+
}
148+
149+
let query = ChatQuery(
150+
messages: [.system(.init(content: "Return a structured response."))],
151+
model: .gpt4_o,
152+
responseFormat: .jsonSchema(name: "movie-info", type: MovieInfo.self)
153+
)
154+
155+
let result = try await openAI.chats(query: query)
156+
XCTAssertEqual(result, chatResult)
157+
}
118158

119159
func testChatsFunction() async throws {
120160
let query = ChatQuery(messages: [

0 commit comments

Comments
 (0)