forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGPTConfiguration.swift
More file actions
55 lines (49 loc) · 1.92 KB
/
ChatGPTConfiguration.swift
File metadata and controls
55 lines (49 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Foundation
import Preferences
public protocol ChatGPTConfiguration {
var featureProvider: ChatFeatureProvider { get }
var temperature: Double { get }
var model: String { get }
var endpoint: String { get }
var apiKey: String { get }
var stop: [String] { get }
var maxTokens: Int { get }
var minimumReplyTokens: Int { get }
var runFunctionsAutomatically: Bool { get }
}
public extension ChatGPTConfiguration {
func endpoint(for provider: ChatFeatureProvider) -> String {
switch provider {
case .openAI:
let baseURL = UserDefaults.shared.value(for: \.openAIBaseURL)
if baseURL.isEmpty { return "https://api.openai.com/v1/chat/completions" }
return "\(baseURL)/v1/chat/completions"
case .azureOpenAI:
let baseURL = UserDefaults.shared.value(for: \.azureOpenAIBaseURL)
let deployment = UserDefaults.shared.value(for: \.azureChatGPTDeployment)
let version = "2023-07-01-preview"
if baseURL.isEmpty { return "" }
return "\(baseURL)/openai/deployments/\(deployment)/chat/completions?api-version=\(version)"
}
}
func apiKey(for provider: ChatFeatureProvider) -> String {
switch provider {
case .openAI:
return UserDefaults.shared.value(for: \.openAIAPIKey)
case .azureOpenAI:
return UserDefaults.shared.value(for: \.azureOpenAIAPIKey)
}
}
func overriding(
_ overrides: OverridingChatGPTConfiguration.Overriding
) -> OverridingChatGPTConfiguration {
.init(overriding: self, with: overrides)
}
func overriding(
_ update: (inout OverridingChatGPTConfiguration.Overriding) -> Void = { _ in }
) -> OverridingChatGPTConfiguration {
var overrides = OverridingChatGPTConfiguration.Overriding()
update(&overrides)
return .init(overriding: self, with: overrides)
}
}