Skip to content

A utility for retrieving AI model information from various providers. Supports Anthropic, Cohere, Google, Ollama, and OpenAI (including OpenAI-compatible APIs).

License

Notifications You must be signed in to change notification settings

kevinhermawan/swift-ai-model-retriever

Repository files navigation

AIModelRetriever

A utility for retrieving AI model information from various providers.

Overview

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).

Installation

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:

  1. Open your project in Xcode.
  2. Click on File -> Swift Packages -> Add Package Dependency...
  3. Enter the repository URL: https://github.com/kevinhermawan/swift-ai-model-retriever.git
  4. Choose the version you want to add. You probably want to add the latest version.
  5. Click Add Package.

Documentation

You can find the documentation here: https://kevinhermawan.github.io/swift-ai-model-retriever/documentation/aimodelretriever

Usage

Initialization

To start using the AIModelRetriever package, first import it and create an instance of the AIModelRetriever struct:

import AIModelRetriever

let modelRetriever = AIModelRetriever()

Retrieving Anthropic Models

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.

Retrieving Cohere Models

let models = modelRetriever.cohere(apiKey: "your-cohere-api-key")

for model in models {
    print("Model ID: \(model.id), Name: \(model.name)")
}

Retrieving Google Models

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.

Retrieving Ollama Models

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)")
}

Retrieving OpenAI Models

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)")
}

Retrieving Models from OpenAI-compatible APIs

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)")
}

Error Handling

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)")
}

Support

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! ❤️

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any suggestions or improvements.

License

This repository is available under the Apache License 2.0.

About

A utility for retrieving AI model information from various providers. Supports Anthropic, Cohere, Google, Ollama, and OpenAI (including OpenAI-compatible APIs).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages