ProxyKit LogoProxyKit Docs
iOS SDK

Basic Usage

Learn how to make AI requests with ProxyKit iOS SDK

Basic Usage

After configuring the SDK, you can make AI requests using the simple API.

Simple Chat Interface

Basic Chat

import ProxyKit

let proxy = ProxyKit(model: .openai(.gpt4))

// Remembers previous messages
let response1 = try await proxy.chat(message: "What's Swift?")
let response2 = try await proxy.chat(message: "Tell me more") // Knows context

Chat with Images

import ProxyKit

let proxy = ProxyKit(model: .openai(.gpt4))

// Analyze images
let response1 = try await proxy.chat(
    message: "How many calories in this dish?",
    images: [.image(UIImage(named: "food-photo"), compressionQuality: 0.5)]
)
let response2 = try await proxy.chat(message: "What about fat amount in this dish?")

Advanced API (ProxyKitAdvance)

OpenAI

import ProxyKit

// Full control over request
let response = try await ProxyKitAdvance.openai.chat.completions.create(
    model: "gpt-4",
    messages: [
        .system("You are a helpful assistant"),
        .user("What is the capital of France?")
    ]
)

print(response.choices.first?.message.content ?? "")

Anthropic

// Using Claude
let response = try await ProxyKitAdvance.anthropic.chat.completions.create(
    model: "claude-3-opus-20240229",
    messages: [
        .user("Explain quantum computing in simple terms")
    ]
)

Message Types

// System message - Sets AI behavior
.system("You are a helpful assistant")

// User message - User input
.user("Hello, how are you?")

// Assistant message - AI response (for conversation history)
.assistant("I'm doing well, thank you!")

Optional Parameters

let response = try await ProxyKitAdvance.openai.chat.completions.create(
    model: "gpt-4",
    messages: messages,
    temperature: 0.7,        // Creativity (0.0-2.0)
    maxTokens: 1000,        // Maximum response length
    topP: 0.9,              // Nucleus sampling
    frequencyPenalty: 0.5,  // Reduce repetition
    presencePenalty: 0.5    // Encourage new topics
)

Error Handling

do {
    let response = try await ProxyKit(model: .openai(.gpt4)).chat(message: "What's Swift?")
} catch {
    print("Error: \(error)")
}

SwiftUI Example

ProxyKit is @Observable and can be passed as SwiftUI environment. If you need to use older ObservableObject, use ProxyKitObservableObject class instead.

struct ChatView: View {
    @State private var messages: [String] = []
    @State private var input = ""
    let proxy = ProxyKit(model: .openai(.gpt4))
    
    var body: some View {
        VStack {
            ScrollView {
                ForEach(messages, id: \.self) { message in
                    Text(message)
                        .padding()
                }
            }
            
            HStack {
                TextField("Message", text: $input)
                Button("Send") {
                    Task {
                        let userMessage = input
                        messages.append("You: \(userMessage)")
                        input = ""
                        
                        if let response = try? await proxy.chat(message: userMessage) {
                            messages.append("AI: \(response)")
                        }
                    }
                }
            }
            .padding()
        }
    }
}

Model Constants

// OpenAI
ChatModel.gpt4              // "gpt-4"
ChatModel.gpt4Turbo         // "gpt-4-turbo"
ChatModel.gpt35Turbo        // "gpt-3.5-turbo"

// Anthropic
ChatModel.claude3Opus       // "claude-3-opus-20240229"
ChatModel.claude3Sonnet     // "claude-3-sonnet-20240229"
ChatModel.claude3Haiku      // "claude-3-haiku-20240307"

Next Steps