A utility for retrieving AI model information from various providers.
The AIModelRetriever
package provides a simple and unified way to fetch AI model information from different providers such as Anthropic, Google, Ollama, and OpenAI (including OpenAI-compatible APIs).
You can add AIModelRetriever
as a dependency to your project using Swift Package Manager by adding it to the dependencies value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/kevinhermawan/swift-ai-model-retriever.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
/// ...
dependencies: [.product(name: "AIModelRetriever", package: "swift-ai-model-retriever")])
]
Alternatively, in Xcode:
- Open your project in Xcode.
- Click on
File
->Swift Packages
->Add Package Dependency...
- Enter the repository URL:
https://github.com/kevinhermawan/swift-ai-model-retriever.git
- Choose the version you want to add. You probably want to add the latest version.
- Click
Add Package
.
You can find the documentation here: https://kevinhermawan.github.io/swift-ai-model-retriever/documentation/aimodelretriever
To start using the AIModelRetriever
package, first import it and create an instance of the AIModelRetriever
struct:
import AIModelRetriever
let modelRetriever = AIModelRetriever()
let models = modelRetriever.anthropic()
for model in models {
print("Model ID: \(model.id), Name: \(model.name)")
}
Note
The Anthropic models are hardcoded. They do not require an API call to retrieve.
let models = modelRetriever.cohere(apiKey: "your-cohere-api-key")
for model in models {
print("Model ID: \(model.id), Name: \(model.name)")
}
let models = modelRetriever.google()
for model in models {
print("Model ID: \(model.id), Name: \(model.name)")
}
Note
The Google models are hardcoded. They do not require an API call to retrieve.
do {
let models = try await retriever.ollama()
for model in models {
print("Model ID: \(model.id), Name: \(model.name)")
}
} catch {
print("Error retrieving Ollama models: \(error)")
}
do {
let models = try await retriever.openAI(apiKey: "your-openai-api-key")
for model in models {
print("Model ID: \(model.id), Name: \(model.name)")
}
} catch {
print("Error retrieving OpenAI models: \(error)")
}
The openAI(apiKey:endpoint:headers:)
method can also be used with OpenAI-compatible APIs by specifying a custom endpoint:
let customEndpoint = URL(string: "https://api.your-openai-compatible-service.com/v1/models")!
do {
let models = try await modelRetriever.openAI(apiKey: "your-api-key", endpoint: customEndpoint)
for model in models {
print("Model ID: \(model.id), Name: \(model.name)")
}
} catch {
print("Error retrieving models from OpenAI-compatible API: \(error)")
}
AIModelRetrieverError
provides structured error handling through the AIModelRetrieverError
enum. This enum contains several cases that represent different types of errors you might encounter:
do {
let models = try await modelRetriever.openAI(apiKey: "your-api-key")
} catch let error as AIModelRetrieverError {
switch error {
case .serverError(let statusCode, let message):
// Handle server-side errors (e.g., invalid API key, rate limits)
print("Server Error [\(statusCode)]: \(message)")
case .networkError(let error):
// Handle network-related errors (e.g., no internet connection)
print("Network Error: \(error.localizedDescription)")
case .decodingError(let error):
// Handle errors that occur when the response cannot be decoded
print("Decoding Error: \(error)")
case .cancelled:
// Handle requests that are cancelled
print("Request was cancelled")
}
} catch {
// Handle any other errors
print("An unexpected error occurred: \(error)")
}
If you find AIModelRetriever
helpful and would like to support its development, consider making a donation. Your contribution helps maintain the project and develop new features.
Your support is greatly appreciated! ❤️
Contributions are welcome! Please open an issue or submit a pull request if you have any suggestions or improvements.
This repository is available under the Apache License 2.0.