Swift Examples - SwiftOpenAI
Follow these examples if you're using the SwiftOpenAI package.
Step 1
Add the SwiftOpenAI package to your Xcode project
- Open your Xcode project
- Select File > Add Package Dependencies
- Paste https://github.com/jamesrochabrun/SwiftOpenAI into the package URL bar
- Click Add Package
SwiftOpenAI 3.4 contains security improvements for AIProxy customers. Please upgrade by navigating to the following section of your Xcode project:
Step 2
Initialize
Import SwiftOpenAI and initialize using the following code:
import SwiftOpenAI
let service:OpenAIService = OpenAIServiceFactory.service(
aiproxyPartialKey: "your_partial_key_goes_here",
aiproxyServiceURL: "your_service_url_goes_here"
)
Step 3
Setup DeviceCheck
Add the AIPROXY_DEVICE_CHECK_BYPASS env variable to your Xcode project:
- Type cmd-shift-comma to open up the "Edit Schemes" menu
- Select Run in the sidebar
- Add to the "Environment Variables" section (not the "Arguments Passed on Launch" section) an env variable with name AIPROXY_DEVICE_CHECK_BYPASS and value that we provided you in the AIProxy dashboard.
Chat completion example
import SwiftUI
import SwiftOpenAI
// Initialize service
// The "aiproxyPartialKey" is provided to you on the AIProxy dashboard.
let service:OpenAIService = OpenAIServiceFactory.service(
aiproxyPartialKey: "your_partial_key_goes_here",
aiproxyServiceURL: "your_service_url_goes_here"
)
struct ChatCompletionView: View {
@State var jokeText:String = ""
var body: some View {
VStack{
Text(jokeText)
Button("Tell a joke"){ tellJoke() }
}
}
func tellJoke() {
Task {
jokeText = ""
let prompt = "Tell me a joke"
let parameters = ChatCompletionParameters(messages: [.init(role: .user, content: .text(prompt))], model: .gpt4o)
let stream = try await service.startStreamedChat(parameters: parameters)
for try await result in stream {
guard let choice = result.choices.first,
let content = choice.delta.content else
{
return
}
jokeText += content
}
}
}
}
#Preview {
ChatCompletionView()
}
Translation example
import SwiftUI
import SwiftOpenAI
// Initialize service
// The "aiproxyPartialKey" is provided to you on the AIProxy dashboard.
let service:OpenAIService = OpenAIServiceFactory.service(
aiproxyPartialKey: "your_partial_key_goes_here",
aiproxyServiceURL: "your_service_url_goes_here"
)
struct TranslationView: View {
private let prompt = "The response is an exact translation from english to spanish. You don't respond with any english."
@State var translatedText = ""
var body: some View {
VStack{
Text(translatedText)
Button("Translate"){
Task {
await translate()
}
}
}
}
func translate() async {
let parameters = ChatCompletionParameters(
messages: [
.init(role: .system, content: .text(prompt)),
.init(role: .user, content: .text("what time is dinner?")),
],
model: .gpt4o
)
do {
let choices = try await service.startChat(parameters: parameters).choices
let message = choices.compactMap(\.message.content)
translatedText = message.first ?? ""
} catch {
print("Could not translate")
}
}
}
#Preview {
TranslationView()
}
Image generation example
import SwiftUI
import SwiftOpenAI
// Initialize service
// The "aiproxyPartialKey" is provided to you on the AIProxy dashboard.
let service:OpenAIService = OpenAIServiceFactory.service(
aiproxyPartialKey: "your_partial_key_goes_here",
aiproxyServiceURL: "your_service_url_goes_here"
)
struct ImageGenerationView: View {
@State private var photoURL: URL = URL(string: "https://picsum.photos/256")!
@State private var loading: Bool = false
var body: some View {
VStack(spacing:24){
ZStack{
AsyncImage(url: photoURL) { phase in
if let image = phase.image {
// Display the loaded image
image
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(14)
} else if phase.error != nil {
// Display a placeholder when loading failed
Image(systemName: "questionmark.diamond")
.imageScale(.large)
} else {
// Display a placeholder while loading
ProgressView()
}
}
if loading {
ProgressView()
.padding(24)
.background(.ultraThinMaterial)
.controlSize(.large)
.cornerRadius(14)
}
}
HStack{
Button("Generate Image"){
processChat(prompt: "a cactus wearing a sombrero")
}
.buttonStyle(.borderedProminent)
}
}
.padding()
}
func processChat(prompt:String) {
/// Show loading indicator
loading = true
/// Create image
let createParameters = ImageCreateParameters(prompt: prompt, model: .dalle3(.largeSquare))
Task {
let result = try await service.createImages(parameters: createParameters).data.map(\.url)
photoURL = result[0]!
loading = false
}
}
}
#Preview {
ImageGenerationView()
}
For more examples visit the resources page.
For more information about the SwiftOpenAI package visit the GitHub documentation.