SwiftyAISwiftyAI

Search documentation

Find a docs page by title or section

3

AICompletion

AICompletion is an observable state object for one prompt and one generated response. It is useful for rewrite tools, title generators, summary panels, and other non-chat UI.

Public state or configMeaning
input / promptEditable text sent to the model
output / completionGenerated text returned by the model
isLoadingTrue while generation is active
errorLast generation error
Constructor optionsPrivate configuration used by send()

State Model

@State private var completion = AICompletion(
    model: "openai/gpt-4o-mini",
    options: GenerationOptions(
        system: "Write concise UI copy.",
        temperature: 0.4
    )
)

prompt and completion are aliases for input and output.

Generate

completion.input = "Write a tooltip for archiving a project."
completion.send()

send() clears the previous output, starts a task, and writes the final response into output.

View Example

struct RewriteView: View {
    @State private var completion = AICompletion(model: "openai/gpt-4o-mini")
 
    var body: some View {
        VStack(alignment: .leading) {
            TextEditor(text: $completion.prompt)
                .frame(minHeight: 120)
 
            HStack {
                Button("Generate") {
                    completion.send()
                }
                .disabled(completion.isLoading || completion.prompt.isEmpty)
 
                Button("Reset") {
                    completion.reset()
                }
            }
 
            if completion.isLoading {
                ProgressView()
            }
 
            Text(completion.completion)
        }
    }
}

Cancellation

completion.stop()

stop() cancels the active task and resets isLoading. reset() also clears input, output, and error.

Related docs

Use AIChat for multi-turn streaming conversations.