Usage
Step 1. Get an API key
- Visit OpenAI.
- Sign up for an account or log in if you already have one.
- Navigate to the API key page and follow the instructions to generate a new API key.
For more information, consult OpenAI's official documentation.
⚠️ Please take precautions to keep your API key secure per OpenAI's guidance:
Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps). Production requests must be routed through your backend server where your API key can be securely loaded from an environment variable or key management service.
SwiftOpenAI has built-in support for AIProxy, which is a backend for AI apps, to satisfy this requirement. To configure AIProxy, see the instructions here.
Step 2. Add SwiftOpenAI SDK
- Go to File -> Add Package Dependency.
- In the search bar, enter this URL.
- Choose the version you'd like to install.
- Click Add Package.
Step 3. Send chat completion request
AI.shared.sendCompletion(prompt: T##String, shouldRetry: T##Bool, shouldUseLatestModel: T##Bool?, completion: T##(String?, Error?) -> ())