Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import SwiftOpenAI
import SwiftUI

struct ChatDemoView: View {
init(service: OpenAIService) {
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
_chatProvider = State(initialValue: ChatProvider(service: service))
}

Expand All @@ -18,6 +19,8 @@ struct ChatDemoView: View {
case chatCompeltionStream
}

let customModel: String?

var body: some View {
ScrollView {
VStack {
Expand Down Expand Up @@ -64,11 +67,18 @@ struct ChatDemoView: View {

let content = ChatCompletionParameters.Message.ContentType.text(prompt)
prompt = ""
let model: Model =
if let customModel, !customModel.isEmpty {
.custom(customModel)
} else {
.gpt4o
}

let parameters = ChatCompletionParameters(
messages: [.init(
role: .user,
content: content)],
model: .custom("claude-3-7-sonnet-20250219"))
model: model)
switch selectedSegment {
case .chatCompletion:
try await chatProvider.startChat(parameters: parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import SwiftOpenAI
import SwiftUI

struct ChatFunctionCallDemoView: View {
init(service: OpenAIService) {
_chatProvider = State(initialValue: ChatFunctionCallProvider(service: service))
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
_chatProvider = State(initialValue: ChatFunctionCallProvider(service: service, customModel: customModel))
}

let customModel: String?

var body: some View {
ScrollViewReader { proxy in
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ enum FunctionCallDefinition: String, CaseIterable {

@Observable
class ChatFunctionCallProvider {
// MARK: - Initializer

init(service: OpenAIService) {
init(service: OpenAIService, customModel: String? = nil) {
self.service = service
self.customModel = customModel
}

// MARK: - Initializer

let customModel: String?

// MARK: - Public Properties

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

let assistantMessage = ChatMessageDisplayModel(
content: .content(.init(text: "Generating images...")),
origin: .received(.gpt))
updateLastAssistantMessage(assistantMessage)

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

let dalleAssistantMessage = ChatMessageDisplayModel(
Expand Down Expand Up @@ -90,9 +92,16 @@ class ChatFunctionCallProvider {

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

let model: Model =
if let customModel, !customModel.isEmpty {
.custom(customModel)
} else {
.gpt41106Preview
}

let parameters = ChatCompletionParameters(
messages: chatMessageParameters,
model: .gpt41106Preview,
model: model,
toolChoice: ToolChoice.auto,
tools: tools)

Expand Down Expand Up @@ -149,9 +158,16 @@ class ChatFunctionCallProvider {

chatMessageParameters.insert(systemMessage, at: 0)

let model: Model =
if let customModel, !customModel.isEmpty {
.custom(customModel)
} else {
.gpt41106Preview
}

let paramsForChat = ChatCompletionParameters(
messages: chatMessageParameters,
model: .gpt41106Preview)
model: model)
do {
let chat = try await service.startChat(parameters: paramsForChat)
guard let assistantMessage = chat.choices?.first?.message else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ struct FunctionCallStreamedResponse {

@Observable
class ChatFunctionsCallStreamProvider {
// MARK: - Initializer

init(service: OpenAIService) {
init(service: OpenAIService, customModel: String? = nil) {
self.service = service
self.customModel = customModel
}

// MARK: - Initializer

let customModel: String?

// MARK: - Public Properties

/// To be used for UI purposes.
Expand Down Expand Up @@ -84,9 +87,16 @@ class ChatFunctionsCallStreamProvider {

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

let model: Model =
if let customModel, !customModel.isEmpty {
.custom(customModel)
} else {
.gpt35Turbo1106
}

let parameters = ChatCompletionParameters(
messages: chatMessageParameters,
model: .gpt35Turbo1106,
model: model,
toolChoice: ToolChoice.auto,
tools: tools)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import SwiftOpenAI
import SwiftUI

struct ChatFunctionsCalllStreamDemoView: View {
init(service: OpenAIService) {
_chatProvider = State(initialValue: ChatFunctionsCallStreamProvider(service: service))
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
_chatProvider = State(initialValue: ChatFunctionsCallStreamProvider(service: service, customModel: customModel))
}

let customModel: String?

var body: some View {
ScrollViewReader { proxy in
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import SwiftUI

@Observable
class ChatFluidConversationProvider {
// MARK: - Initializer

init(service: OpenAIService) {
init(service: OpenAIService, customModel: String? = nil) {
self.service = service
self.customModel = customModel
}

// MARK: - Initializer

let customModel: String?

// MARK: - Public Properties

/// A collection of messages for display in the UI, representing the conversation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import SwiftOpenAI
import SwiftUI

struct ChatStreamFluidConversationDemoView: View {
init(service: OpenAIService) {
_chatProvider = State(initialValue: ChatFluidConversationProvider(service: service))
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
_chatProvider = State(initialValue: ChatFluidConversationProvider(service: service, customModel: customModel))
}

enum GPTModel: String, CaseIterable {
case gpt3dot5 = "GPT-3.5"
case gpt4 = "GPT-4"
}

let customModel: String?

var body: some View {
ScrollViewReader { proxy in
VStack {
Expand Down Expand Up @@ -74,9 +77,16 @@ struct ChatStreamFluidConversationDemoView: View {
prompt = ""
}
/// Make the request
let model: Model =
if let customModel, !customModel.isEmpty {
.custom(customModel)
} else {
selectedModel == .gpt3dot5 ? .gpt35Turbo : .gpt4
}

try await chatProvider.startStreamedChat(parameters: .init(
messages: [.init(role: .user, content: .text(prompt))],
model: selectedModel == .gpt3dot5 ? .gpt35Turbo : .gpt4), prompt: prompt)
model: model), prompt: prompt)
}
} label: {
Image(systemName: "paperplane")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import SwiftOpenAI
import SwiftUI

struct ChatStructureOutputToolDemoView: View {
init(service: OpenAIService) {
chatProvider = .init(service: service)
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
chatProvider = .init(service: service, customModel: customModel)
}

let customModel: String?

var body: some View {
ScrollViewReader { proxy in
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ let responseFormatSchema = JSONSchemaResponseFormat(
// )

struct ChatStructuredOutputDemoView: View {
init(service: OpenAIService) {
_chatProvider = State(initialValue: ChatStructuredOutputProvider(service: service))
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
_chatProvider = State(initialValue: ChatStructuredOutputProvider(service: service, customModel: customModel))
}

enum ChatConfig {
case chatCompletion
case chatCompeltionStream
}

let customModel: String?

var body: some View {
ScrollView {
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import SwiftOpenAI

@Observable
final class ChatStructuredOutputProvider {
// MARK: - Initializer

init(service: OpenAIService) {
init(service: OpenAIService, customModel: String? = nil) {
self.service = service
self.customModel = customModel
}

// MARK: - Initializer

let customModel: String?

var message = ""
var messages = [String]()
var errorMessage = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import SwiftUI
/// }'```

struct LocalChatDemoView: View {
init(service: OpenAIService) {
init(service: OpenAIService, customModel: String? = nil) {
self.customModel = customModel
_chatProvider = State(initialValue: ChatProvider(service: service))
}

Expand All @@ -42,6 +43,8 @@ struct LocalChatDemoView: View {
case chatCompeltionStream
}

let customModel: String?

var body: some View {
ScrollView {
VStack {
Expand Down Expand Up @@ -88,12 +91,19 @@ struct LocalChatDemoView: View {

let content = ChatCompletionParameters.Message.ContentType.text(prompt)
prompt = ""
let model: Model =
if let customModel, !customModel.isEmpty {
.custom(customModel)
} else {
// Make sure you run `ollama pull llama3` in your terminal to download this model.
.custom("llama3")
}

let parameters = ChatCompletionParameters(
messages: [.init(
role: .user,
content: content)],
// Make sure you run `ollama pull llama3` in your terminal to download this model.
model: .custom("llama3"))
model: model)
switch selectedSegment {
case .chatCompletion:
try await chatProvider.startChat(parameters: parameters)
Expand Down
Loading