Skip to content

Commit e6af72b

Browse files
Adding Azure and AIProxy support
1 parent 2c0586e commit e6af72b

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

PolyAIExample/PolyAIExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PolyAIExample/PolyAIExample/ApiKeysIntroView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct ApiKeyIntroView: View {
4444
HStack {
4545
TextField("Enter OpenAI API Key", text: $openAIAPIKey)
4646
Button {
47-
configurations.append(.openAI(apiKey: openAIAPIKey))
47+
configurations.append(.openAI(.api(key: openAIAPIKey)))
4848
openAIConfigAdded = true
4949
} label: {
5050
Image(systemName: "plus")

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ An open-source Swift package that simplifies LLM message completions, inspired b
1818
- [Usage](#usage)
1919
- [Message](#message)
2020
- [Collaboration](#collaboration)
21+
- [OpenAI Azure](OpenAI Azure)
22+
- [OpenAI AIProxy](AIProxy)
2123

2224
## Description
2325

@@ -55,7 +57,7 @@ import PolyAI
5557
Then, define the LLM configurations. Currently, OpenAI and Anthropic are supported:
5658

5759
```swift
58-
let openAIConfiguration: LLMConfiguration = .openAI(apiKey: "your_openai_api_key_here")
60+
let openAIConfiguration: LLMConfiguration = .openAI(.api(key: "your_openai_api_key_here"))
5961
let anthropicConfiguration: LLMConfiguration = .anthropic(apiKey: "your_anthropic_api_key_here")
6062
let configurations = [openAIConfiguration, anthropicConfiguration]
6163
```
@@ -86,6 +88,26 @@ let parameters: LLMParameter = .anthropic(model: .claude3Sonnet, messages: [.ini
8688
let stream = try await service.streamMessage(parameters)
8789
```
8890

91+
## OpenAI Azure
92+
93+
To access the OpenAI API via Azure, you can use the following configuration setup.
94+
95+
```swift
96+
let azureConfiguration: LLMConfiguration = .openAI(.azure(configuration: .init(resourceName: "YOUR_RESOURCE_NAME", openAIAPIKey: .apiKey("YOUR_API_KEY"), apiVersion: "THE_API_VERSION")))
97+
```
98+
99+
More information can be found [here](https://github.com/jamesrochabrun/SwiftOpenAI?tab=readme-ov-file#azure-openai).
100+
101+
## OpenAI AIProxy
102+
103+
To access the OpenAI API via AIProxy, use the following configuration setup.
104+
105+
```swift
106+
let aiProxyConfiguration: LLMConfiguration = .openAI(.aiProxy(aiproxyPartialKey: "hardcode_partial_key_here", aiproxyDeviceCheckBypass: "hardcode_device_check_bypass_here"))
107+
```
108+
109+
More information can be found [here](https://github.com/jamesrochabrun/SwiftOpenAI?tab=readme-ov-file#aiproxy).
110+
89111
## Collaboration
90112

91113
Open a PR for any proposed change pointing it to `main` branch.

Sources/PolyAI/Service/DefaultPolyAIService.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ struct DefaultPolyAIService: PolyAIService {
2222
{
2323
for configuration in configurations {
2424
switch configuration {
25-
case .openAI(let apiKey, let organizationID, let configuration, let decoder):
26-
openAIService = OpenAIServiceFactory.service(apiKey: apiKey, organizationID: organizationID, configuration: configuration, decoder: decoder)
25+
case .openAI(let configuration):
26+
switch configuration {
27+
case .api(let key, let organizationID, let configuration, let decoder):
28+
openAIService = OpenAIServiceFactory.service(apiKey: key, organizationID: organizationID, configuration: configuration, decoder: decoder)
29+
30+
case .azure(let azureConfiguration, let urlSessionConfiguration, let decoder):
31+
openAIService = OpenAIServiceFactory.service(azureConfiguration: azureConfiguration, urlSessionConfiguration: urlSessionConfiguration, decoder: decoder)
32+
33+
case .aiProxy(let aiproxyPartialKey, let aiproxyDeviceCheckBypass, let configuration, let decoder):
34+
openAIService = OpenAIServiceFactory.service(aiproxyPartialKey: aiproxyPartialKey, aiproxyDeviceCheckBypass: aiproxyDeviceCheckBypass, configuration: configuration, decoder: decoder)
35+
}
36+
2737
case .anthropic(let apiKey, let configuration):
2838
anthropicService = AnthropicServiceFactory.service(apiKey: apiKey, configuration: configuration)
2939
}

Sources/PolyAI/Service/PolyAIService.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,35 @@
66
//
77

88
import Foundation
9+
import SwiftOpenAI
910

1011
/// Represents configurations for different LLM providers.
1112
public enum LLMConfiguration {
13+
14+
case openAI(OpenAI)
1215

13-
/// Configuration for accessing OpenAI's API.
14-
/// - Parameters:
15-
/// - apiKey: The API key for authenticating requests to OpenAI.
16-
/// - organizationID: Optional organization ID for OpenAI usage.
17-
/// - configuration: The URLSession configuration to use for network requests. Defaults to `.default`.
18-
/// - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
19-
case openAI(apiKey: String, organizationID: String? = nil, configuration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
16+
public enum OpenAI {
17+
/// Configuration for accessing OpenAI's API.
18+
/// - Parameters:
19+
/// - apiKey: The API key for authenticating requests to OpenAI.
20+
/// - organizationID: Optional organization ID for OpenAI usage.
21+
/// - configuration: The URLSession configuration to use for network requests. Defaults to `.default`.
22+
/// - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
23+
case api(key: String, organizationID: String? = nil, configuration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
24+
/// Configuration for accessing OpenAI's API.
25+
/// - Parameters:
26+
/// - configuration: The AzureOpenAIConfiguration.
27+
/// - urlSessionConfiguration: The URLSession configuration to use for network requests. Defaults to `.default`.
28+
/// - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
29+
case azure(configuration: AzureOpenAIConfiguration, urlSessionConfiguration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
30+
/// Configuration for accessing OpenAI's API.
31+
/// - Parameters:
32+
/// - aiproxyPartialKey: The partial key provided in the 'API Keys' section of the AIProxy dashboard.
33+
/// - aiproxyDeviceCheckBypass: The bypass token that is provided in the 'API Keys' section of the AIProxy dashboard.
34+
/// - configuration: The URLSession configuration to use for network requests. Defaults to `.default`.
35+
/// - decoder: The JSON decoder used for decoding responses. Defaults to a new instance of `JSONDecoder`.
36+
case aiProxy(aiproxyPartialKey: String, aiproxyDeviceCheckBypass: String? = nil, configuration: URLSessionConfiguration = .default, decoder: JSONDecoder = .init())
37+
}
2038

2139
/// Configuration for accessing Anthropic's API.
2240
/// - Parameters:

0 commit comments

Comments
 (0)