|
| 1 | +// |
| 2 | +// ChatPredictedOutputDemoView.swift |
| 3 | +// SwiftOpenAIExample |
| 4 | +// |
| 5 | +// Created by James Rochabrun on 1/3/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import SwiftUI |
| 10 | +import SwiftOpenAI |
| 11 | + |
| 12 | +/// https://platform.openai.com/docs/guides/predicted-outputs |
| 13 | +struct ChatPredictedOutputDemoView: View { |
| 14 | + |
| 15 | + @State private var chatProvider: ChatProvider |
| 16 | + @State private var isLoading = false |
| 17 | + @State private var prompt = "" |
| 18 | + |
| 19 | + init(service: OpenAIService) { |
| 20 | + chatProvider = ChatProvider(service: service) |
| 21 | + } |
| 22 | + |
| 23 | + var body: some View { |
| 24 | + ScrollView { |
| 25 | + VStack { |
| 26 | + textArea |
| 27 | + Text(chatProvider.errorMessage) |
| 28 | + .foregroundColor(.red) |
| 29 | + chatCompletionResultView |
| 30 | + } |
| 31 | + } |
| 32 | + .overlay( |
| 33 | + Group { |
| 34 | + if isLoading { |
| 35 | + ProgressView() |
| 36 | + } else { |
| 37 | + EmptyView() |
| 38 | + } |
| 39 | + } |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + var textArea: some View { |
| 44 | + HStack(spacing: 4) { |
| 45 | + TextField("Enter prompt", text: $prompt, axis: .vertical) |
| 46 | + .textFieldStyle(.roundedBorder) |
| 47 | + .padding() |
| 48 | + Button { |
| 49 | + Task { |
| 50 | + isLoading = true |
| 51 | + defer { isLoading = false } // ensure isLoading is set to false when the |
| 52 | + |
| 53 | + let content: ChatCompletionParameters.Message.ContentType = .text(prompt) |
| 54 | + prompt = "" |
| 55 | + let parameters = ChatCompletionParameters( |
| 56 | + messages: [ |
| 57 | + .init(role: .system, content: .text(systemMessage)), |
| 58 | + .init(role: .user, content: content), |
| 59 | + .init(role: .user, content: .text(predictedCode))], // Sending the predicted code as another user message. |
| 60 | + model: .gpt4o, |
| 61 | + prediction: .init(content: .text(predictedCode))) |
| 62 | + try await chatProvider.startChat(parameters: parameters) |
| 63 | + |
| 64 | + } |
| 65 | + } label: { |
| 66 | + Image(systemName: "paperplane") |
| 67 | + } |
| 68 | + .buttonStyle(.bordered) |
| 69 | + } |
| 70 | + .padding() |
| 71 | + } |
| 72 | + |
| 73 | + /// stream = `false` |
| 74 | + var chatCompletionResultView: some View { |
| 75 | + ForEach(Array(chatProvider.messages.enumerated()), id: \.offset) { idx, val in |
| 76 | + VStack(spacing: 0) { |
| 77 | + Text("\(val)") |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +let systemMessage = """ |
| 84 | +You are a code editor assistant. I only output code without any explanations, commentary, or additional text. I follow these rules: |
| 85 | +
|
| 86 | +1. Respond with code only, never any text or explanations |
| 87 | +2. Use appropriate syntax highlighting/formatting |
| 88 | +3. If the code needs to be modified/improved, output the complete updated code |
| 89 | +4. Do not include caveats, introductions, or commentary |
| 90 | +5. Do not ask questions or solicit feedback |
| 91 | +6. Do not explain what changes were made |
| 92 | +7. Assume the user knows what they want and will review the code themselves |
| 93 | +""" |
| 94 | + |
| 95 | +let predictedCode = """ |
| 96 | +struct ChatPredictedOutputDemoView: View { |
| 97 | + |
| 98 | + @State private var chatProvider: ChatProvider |
| 99 | + @State private var isLoading = false |
| 100 | + @State private var prompt = "" |
| 101 | +
|
| 102 | + init(service: OpenAIService) { |
| 103 | + chatProvider = ChatProvider(service: service) |
| 104 | + } |
| 105 | + |
| 106 | + var body: some View { |
| 107 | + ScrollView { |
| 108 | + VStack { |
| 109 | + textArea |
| 110 | + Text(chatProvider.errorMessage) |
| 111 | + .foregroundColor(.red) |
| 112 | + streamedChatResultView |
| 113 | + } |
| 114 | + } |
| 115 | + .overlay( |
| 116 | + Group { |
| 117 | + if isLoading { |
| 118 | + ProgressView() |
| 119 | + } else { |
| 120 | + EmptyView() |
| 121 | + } |
| 122 | + } |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + var textArea: some View { |
| 127 | + HStack(spacing: 4) { |
| 128 | + TextField("Enter prompt", text: $prompt, axis: .vertical) |
| 129 | + .textFieldStyle(.roundedBorder) |
| 130 | + .padding() |
| 131 | + Button { |
| 132 | + Task { |
| 133 | + isLoading = true |
| 134 | + defer { isLoading = false } // ensure isLoading is set to false when the |
| 135 | + |
| 136 | + let content: ChatCompletionParameters.Message.ContentType = .text(prompt) |
| 137 | + prompt = "" |
| 138 | + let parameters = ChatCompletionParameters( |
| 139 | + messages: [.init( |
| 140 | + role: .user, |
| 141 | + content: content)], |
| 142 | + model: .gpt4o) |
| 143 | + } |
| 144 | + } label: { |
| 145 | + Image(systemName: "paperplane") |
| 146 | + } |
| 147 | + .buttonStyle(.bordered) |
| 148 | + } |
| 149 | + .padding() |
| 150 | + } |
| 151 | + |
| 152 | + /// stream = `true` |
| 153 | + var streamedChatResultView: some View { |
| 154 | + VStack { |
| 155 | + Button("Cancel stream") { |
| 156 | + chatProvider.cancelStream() |
| 157 | + } |
| 158 | + Text(chatProvider.message) |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | +""" |
0 commit comments