|
8 | 8 | [](https://developer.apple.com/xcode/)
|
9 | 9 | [](https://github.com/apple/swift-package-manager)
|
10 | 10 |
|
| 11 | + |
11 | 12 | An open-source Swift package that simplifies LLM message completions, inspired by [liteLLM](https://litellm.ai/) and adapted for Swift developers, following Swift conventions.
|
12 | 13 |
|
13 |
| -Call different LLM APIs using the OpenAI format; currently supporting OpenAI and Anthropic, with more models, including Gemini, coming soon. |
| 14 | +## Table of Contents |
| 15 | + |
| 16 | +- [Description](#description) |
| 17 | +- [Installation](#installation) |
| 18 | +- [Usage](#usage) |
| 19 | +- [Message](#message) |
| 20 | +- [Collaboration](#collaboration) |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +Call different LLM APIs using the OpenAI format; currently supporting [OpenAI](https://github.com/jamesrochabrun/SwiftOpenAI) and [Anthropic](https://github.com/jamesrochabrun/SwiftAnthropic), with more models, including Gemini, coming soon. |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +### Swift Package Manager |
| 29 | + |
| 30 | +1. Open your Swift project in Xcode. |
| 31 | +2. Go to `File` -> `Add Package Dependency`. |
| 32 | +3. In the search bar, enter [this URL](https://github.com/jamesrochabrun/PolyAI). |
| 33 | +4. Choose the version you'd like to install. |
| 34 | +5. Click `Add Package`. |
| 35 | + |
| 36 | +### Important |
| 37 | + |
| 38 | +⚠️ Please take precautions to keep your API keys secure. |
| 39 | + |
| 40 | +> Remember that your API keys are a secret! Do not share it with others or expose |
| 41 | +> it in any client-side code (browsers, apps). Production requests must be |
| 42 | +> routed through your backend server where your API keys can be securely |
| 43 | +> loaded from an environment variable or key management service. |
| 44 | +
|
| 45 | +## Usage |
| 46 | + |
| 47 | +To interface with different LLMs, you need only to supply the corresponding LLM configuration and adjust the parameters accordingly. |
| 48 | + |
| 49 | +First, import the PolyAI package: |
| 50 | + |
| 51 | +```swift |
| 52 | +import PolyAI |
| 53 | +``` |
| 54 | + |
| 55 | +Then, define the LLM configurations. Currently, OpenAI and Anthropic are supported: |
| 56 | + |
| 57 | +```swift |
| 58 | +let openAIConfiguration: LLMConfiguration = .openAI(apiKey: "your_openai_api_key_here") |
| 59 | +let anthropicConfiguration: LLMConfiguration = .anthropic(apiKey: "your_anthropic_api_key_here") |
| 60 | +let configurations = [openAIConfiguration, anthropicConfiguration] |
| 61 | +``` |
| 62 | + |
| 63 | +With the configurations set, initialize the service: |
| 64 | + |
| 65 | +```swift |
| 66 | +let service = PolyAIServiceFactory.serviceWith(configurations) |
| 67 | +``` |
| 68 | + |
| 69 | +Now, you have access to both the OpenAI and Anthropic APIs in a single package, with Gemini coming soon! 🚀 |
| 70 | + |
| 71 | +## Message |
| 72 | + |
| 73 | +To send a message using OpenAI: |
| 74 | + |
| 75 | +```swift |
| 76 | +let prompt = "How are you today?" |
| 77 | +let parameters: LLMParameter = .openAI(model: .gpt4turbo, messages: [.init(role: .user, content: prompt)]) |
| 78 | +let stream = try await service.streamMessage(parameters) |
| 79 | +``` |
| 80 | + |
| 81 | +To interact with Anthropic instead, all you need to do is update the parameters: |
| 82 | + |
| 83 | +```swift |
| 84 | +let prompt = "How are you today?" |
| 85 | +let parameters: LLMParameter = .anthropic(model: .claude3Sonnet, messages: [.init(role: .user, content: prompt)], maxTokens: 1024) |
| 86 | +let stream = try await service.streamMessage(parameters) |
| 87 | +``` |
| 88 | + |
| 89 | +## Collaboration |
| 90 | + |
| 91 | +Open a PR for any proposed change pointing it to `main` branch. |
0 commit comments