forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletionAPI.swift
More file actions
119 lines (103 loc) · 3.52 KB
/
CompletionAPI.swift
File metadata and controls
119 lines (103 loc) · 3.52 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import AIModel
import Foundation
import Preferences
typealias CompletionAPIBuilder = (String, ChatModel, URL, CompletionRequestBody)
-> CompletionAPI
protocol CompletionAPI {
func callAsFunction() async throws -> CompletionResponseBody
}
/// https://platform.openai.com/docs/api-reference/chat/create
struct CompletionResponseBody: Codable, Equatable {
struct Message: Codable, Equatable {
/// The role of the message.
var role: ChatMessage.Role
/// The content of the message.
var content: String?
/// When we want to reply to a function call with the result, we have to provide the
/// name of the function call, and include the result in `content`.
///
/// - important: It's required when the role is `function`.
var name: String?
/// When the bot wants to call a function, it will reply with a function call in format:
/// ```json
/// {
/// "name": "weather",
/// "arguments": "{ \"location\": \"earth\" }"
/// }
/// ```
var function_call: CompletionRequestBody.MessageFunctionCall?
}
struct Choice: Codable, Equatable {
var message: Message
var index: Int
var finish_reason: String
}
struct Usage: Codable, Equatable {
var prompt_tokens: Int
var completion_tokens: Int
var total_tokens: Int
}
var id: String?
var object: String
var model: String
var usage: Usage
var choices: [Choice]
}
struct CompletionAPIError: Error, Codable, LocalizedError {
struct E: Codable {
var message: String
var type: String
var param: String
var code: String
}
var error: E
var errorDescription: String? { error.message }
}
struct OpenAICompletionAPI: CompletionAPI {
var apiKey: String
var endpoint: URL
var requestBody: CompletionRequestBody
var model: ChatModel
init(
apiKey: String,
model: ChatModel,
endpoint: URL,
requestBody: CompletionRequestBody
) {
self.apiKey = apiKey
self.endpoint = endpoint
self.requestBody = requestBody
self.requestBody.stream = false
self.model = model
}
func callAsFunction() async throws -> CompletionResponseBody {
var request = URLRequest(url: endpoint)
request.httpMethod = "POST"
let encoder = JSONEncoder()
request.httpBody = try encoder.encode(requestBody)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
if !apiKey.isEmpty {
switch model.format {
case .openAI, .openAICompatible:
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
case .azureOpenAI:
request.setValue(apiKey, forHTTPHeaderField: "api-key")
}
}
let (result, response) = try await URLSession.shared.data(for: request)
guard let response = response as? HTTPURLResponse else {
throw ChatGPTServiceError.responseInvalid
}
guard response.statusCode == 200 else {
let error = try? JSONDecoder().decode(CompletionAPIError.self, from: result)
throw error ?? ChatGPTServiceError
.otherError(String(data: result, encoding: .utf8) ?? "Unknown Error")
}
do {
return try JSONDecoder().decode(CompletionResponseBody.self, from: result)
} catch {
dump(error)
fatalError()
}
}
}