Skip to content

Commit 7c89f2a

Browse files
Merge pull request #172 from jamesrochabrun/jroch-responses-api
Model Selector demo
2 parents 5240490 + 60b0218 commit 7c89f2a

File tree

16 files changed

+164
-53
lines changed

16 files changed

+164
-53
lines changed

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatDemo/ChatDemoView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import SwiftOpenAI
99
import SwiftUI
1010

1111
struct ChatDemoView: View {
12-
init(service: OpenAIService) {
12+
init(service: OpenAIService, customModel: String? = nil) {
13+
self.customModel = customModel
1314
_chatProvider = State(initialValue: ChatProvider(service: service))
1415
}
1516

@@ -18,6 +19,8 @@ struct ChatDemoView: View {
1819
case chatCompeltionStream
1920
}
2021

22+
let customModel: String?
23+
2124
var body: some View {
2225
ScrollView {
2326
VStack {
@@ -64,11 +67,18 @@ struct ChatDemoView: View {
6467

6568
let content = ChatCompletionParameters.Message.ContentType.text(prompt)
6669
prompt = ""
70+
let model: Model =
71+
if let customModel, !customModel.isEmpty {
72+
.custom(customModel)
73+
} else {
74+
.gpt4o
75+
}
76+
6777
let parameters = ChatCompletionParameters(
6878
messages: [.init(
6979
role: .user,
7080
content: content)],
71-
model: .custom("claude-3-7-sonnet-20250219"))
81+
model: model)
7282
switch selectedSegment {
7383
case .chatCompletion:
7484
try await chatProvider.startChat(parameters: parameters)

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallDemoView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import SwiftOpenAI
99
import SwiftUI
1010

1111
struct ChatFunctionCallDemoView: View {
12-
init(service: OpenAIService) {
13-
_chatProvider = State(initialValue: ChatFunctionCallProvider(service: service))
12+
init(service: OpenAIService, customModel: String? = nil) {
13+
self.customModel = customModel
14+
_chatProvider = State(initialValue: ChatFunctionCallProvider(service: service, customModel: customModel))
1415
}
1516

17+
let customModel: String?
18+
1619
var body: some View {
1720
ScrollViewReader { proxy in
1821
VStack {

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallProvider.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ enum FunctionCallDefinition: String, CaseIterable {
3737

3838
@Observable
3939
class ChatFunctionCallProvider {
40-
// MARK: - Initializer
41-
42-
init(service: OpenAIService) {
40+
init(service: OpenAIService, customModel: String? = nil) {
4341
self.service = service
42+
self.customModel = customModel
4443
}
4544

45+
// MARK: - Initializer
46+
47+
let customModel: String?
48+
4649
// MARK: - Public Properties
4750

4851
/// To be used for UI purposes.
@@ -52,14 +55,13 @@ class ChatFunctionCallProvider {
5255
func generateImage(arguments: String) async throws -> String {
5356
let dictionary = arguments.toDictionary()!
5457
let prompt = dictionary["prompt"] as! String
55-
let count = (dictionary["count"] as? Int) ?? 1
5658

5759
let assistantMessage = ChatMessageDisplayModel(
5860
content: .content(.init(text: "Generating images...")),
5961
origin: .received(.gpt))
6062
updateLastAssistantMessage(assistantMessage)
6163

62-
let urls = try await service.createImages(parameters: .init(prompt: prompt, model: .dallE2)).data?.compactMap(\.url)
64+
let urls = try await service.createImages(parameters: .init(prompt: prompt, model: .dallE3)).data?.compactMap(\.url)
6365
.compactMap { URL(string: $0) } ?? []
6466

6567
let dalleAssistantMessage = ChatMessageDisplayModel(
@@ -90,9 +92,16 @@ class ChatFunctionCallProvider {
9092

9193
let tools = FunctionCallDefinition.allCases.map(\.functionTool)
9294

95+
let model: Model =
96+
if let customModel, !customModel.isEmpty {
97+
.custom(customModel)
98+
} else {
99+
.gpt41106Preview
100+
}
101+
93102
let parameters = ChatCompletionParameters(
94103
messages: chatMessageParameters,
95-
model: .gpt41106Preview,
104+
model: model,
96105
toolChoice: ToolChoice.auto,
97106
tools: tools)
98107

@@ -149,9 +158,16 @@ class ChatFunctionCallProvider {
149158

150159
chatMessageParameters.insert(systemMessage, at: 0)
151160

161+
let model: Model =
162+
if let customModel, !customModel.isEmpty {
163+
.custom(customModel)
164+
} else {
165+
.gpt41106Preview
166+
}
167+
152168
let paramsForChat = ChatCompletionParameters(
153169
messages: chatMessageParameters,
154-
model: .gpt41106Preview)
170+
model: model)
155171
do {
156172
let chat = try await service.startChat(parameters: paramsForChat)
157173
guard let assistantMessage = chat.choices?.first?.message else { return }

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCallStreamProvider.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ struct FunctionCallStreamedResponse {
2323

2424
@Observable
2525
class ChatFunctionsCallStreamProvider {
26-
// MARK: - Initializer
27-
28-
init(service: OpenAIService) {
26+
init(service: OpenAIService, customModel: String? = nil) {
2927
self.service = service
28+
self.customModel = customModel
3029
}
3130

31+
// MARK: - Initializer
32+
33+
let customModel: String?
34+
3235
// MARK: - Public Properties
3336

3437
/// To be used for UI purposes.
@@ -84,9 +87,16 @@ class ChatFunctionsCallStreamProvider {
8487

8588
let tools = FunctionCallDefinition.allCases.map(\.functionTool)
8689

90+
let model: Model =
91+
if let customModel, !customModel.isEmpty {
92+
.custom(customModel)
93+
} else {
94+
.gpt35Turbo1106
95+
}
96+
8797
let parameters = ChatCompletionParameters(
8898
messages: chatMessageParameters,
89-
model: .gpt35Turbo1106,
99+
model: model,
90100
toolChoice: ToolChoice.auto,
91101
tools: tools)
92102

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCalllStreamDemoView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import SwiftOpenAI
99
import SwiftUI
1010

1111
struct ChatFunctionsCalllStreamDemoView: View {
12-
init(service: OpenAIService) {
13-
_chatProvider = State(initialValue: ChatFunctionsCallStreamProvider(service: service))
12+
init(service: OpenAIService, customModel: String? = nil) {
13+
self.customModel = customModel
14+
_chatProvider = State(initialValue: ChatFunctionsCallStreamProvider(service: service, customModel: customModel))
1415
}
1516

17+
let customModel: String?
18+
1619
var body: some View {
1720
ScrollViewReader { proxy in
1821
VStack {

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatStreamFluidConversationDemo/ChatFluidConversationProvider.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import SwiftUI
1010

1111
@Observable
1212
class ChatFluidConversationProvider {
13-
// MARK: - Initializer
14-
15-
init(service: OpenAIService) {
13+
init(service: OpenAIService, customModel: String? = nil) {
1614
self.service = service
15+
self.customModel = customModel
1716
}
1817

18+
// MARK: - Initializer
19+
20+
let customModel: String?
21+
1922
// MARK: - Public Properties
2023

2124
/// A collection of messages for display in the UI, representing the conversation.

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatStreamFluidConversationDemo/ChatStreamFluidConversationDemoView.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ import SwiftOpenAI
99
import SwiftUI
1010

1111
struct ChatStreamFluidConversationDemoView: View {
12-
init(service: OpenAIService) {
13-
_chatProvider = State(initialValue: ChatFluidConversationProvider(service: service))
12+
init(service: OpenAIService, customModel: String? = nil) {
13+
self.customModel = customModel
14+
_chatProvider = State(initialValue: ChatFluidConversationProvider(service: service, customModel: customModel))
1415
}
1516

1617
enum GPTModel: String, CaseIterable {
1718
case gpt3dot5 = "GPT-3.5"
1819
case gpt4 = "GPT-4"
1920
}
2021

22+
let customModel: String?
23+
2124
var body: some View {
2225
ScrollViewReader { proxy in
2326
VStack {
@@ -74,9 +77,16 @@ struct ChatStreamFluidConversationDemoView: View {
7477
prompt = ""
7578
}
7679
/// Make the request
80+
let model: Model =
81+
if let customModel, !customModel.isEmpty {
82+
.custom(customModel)
83+
} else {
84+
selectedModel == .gpt3dot5 ? .gpt35Turbo : .gpt4
85+
}
86+
7787
try await chatProvider.startStreamedChat(parameters: .init(
7888
messages: [.init(role: .user, content: .text(prompt))],
79-
model: selectedModel == .gpt3dot5 ? .gpt35Turbo : .gpt4), prompt: prompt)
89+
model: model), prompt: prompt)
8090
}
8191
} label: {
8292
Image(systemName: "paperplane")

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatStructureOutputTool/ChatStructureOutputToolDemoView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import SwiftOpenAI
1010
import SwiftUI
1111

1212
struct ChatStructureOutputToolDemoView: View {
13-
init(service: OpenAIService) {
14-
chatProvider = .init(service: service)
13+
init(service: OpenAIService, customModel: String? = nil) {
14+
self.customModel = customModel
15+
chatProvider = .init(service: service, customModel: customModel)
1516
}
1617

18+
let customModel: String?
19+
1720
var body: some View {
1821
ScrollViewReader { proxy in
1922
VStack {

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatStructuredOutputs/ChatStructuredOutputDemoView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,18 @@ let responseFormatSchema = JSONSchemaResponseFormat(
112112
// )
113113

114114
struct ChatStructuredOutputDemoView: View {
115-
init(service: OpenAIService) {
116-
_chatProvider = State(initialValue: ChatStructuredOutputProvider(service: service))
115+
init(service: OpenAIService, customModel: String? = nil) {
116+
self.customModel = customModel
117+
_chatProvider = State(initialValue: ChatStructuredOutputProvider(service: service, customModel: customModel))
117118
}
118119

119120
enum ChatConfig {
120121
case chatCompletion
121122
case chatCompeltionStream
122123
}
123124

125+
let customModel: String?
126+
124127
var body: some View {
125128
ScrollView {
126129
VStack {

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatStructuredOutputs/ChatStructuredOutputProvider.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ import SwiftOpenAI
1212

1313
@Observable
1414
final class ChatStructuredOutputProvider {
15-
// MARK: - Initializer
16-
17-
init(service: OpenAIService) {
15+
init(service: OpenAIService, customModel: String? = nil) {
1816
self.service = service
17+
self.customModel = customModel
1918
}
2019

20+
// MARK: - Initializer
21+
22+
let customModel: String?
23+
2124
var message = ""
2225
var messages = [String]()
2326
var errorMessage = ""

0 commit comments

Comments
 (0)