forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIEmbeddingService.swift
More file actions
147 lines (128 loc) · 5.29 KB
/
OpenAIEmbeddingService.swift
File metadata and controls
147 lines (128 loc) · 5.29 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import AIModel
import Foundation
import Logger
struct OpenAIEmbeddingService: EmbeddingAPI {
struct EmbeddingRequestBody: Encodable {
var input: [String]
var model: String
}
struct EmbeddingFromTokensRequestBody: Encodable {
var input: [[Int]]
var model: String
}
let apiKey: String
let model: EmbeddingModel
let endpoint: String
public func embed(text: String) async throws -> EmbeddingResponse {
return try await embed(texts: [text])
}
public func embed(texts text: [String]) async throws -> EmbeddingResponse {
guard let url = URL(string: endpoint) else { throw ChatGPTServiceError.endpointIncorrect }
var request = URLRequest(url: url)
request.httpMethod = "POST"
let encoder = JSONEncoder()
request.httpBody = try encoder.encode(EmbeddingRequestBody(
input: text,
model: model.info.modelName
))
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Self.setupAppInformation(&request)
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
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(
OpenAIChatCompletionsService.CompletionAPIError.self,
from: result
)
throw error ?? ChatGPTServiceError
.otherError(String(data: result, encoding: .utf8) ?? "Unknown Error")
}
let embeddingResponse = try JSONDecoder().decode(EmbeddingResponse.self, from: result)
#if DEBUG
Logger.service.info("""
Embedding usage
- number of strings: \(text.count)
- prompt tokens: \(embeddingResponse.usage.prompt_tokens)
- total tokens: \(embeddingResponse.usage.total_tokens)
""")
#endif
return embeddingResponse
}
public func embed(tokens: [[Int]]) async throws -> EmbeddingResponse {
guard let url = URL(string: endpoint) else { throw ChatGPTServiceError.endpointIncorrect }
var request = URLRequest(url: url)
request.httpMethod = "POST"
let encoder = JSONEncoder()
request.httpBody = try encoder.encode(EmbeddingFromTokensRequestBody(
input: tokens,
model: model.info.modelName
))
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Self.setupAppInformation(&request)
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
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(
OpenAIChatCompletionsService.CompletionAPIError.self,
from: result
)
throw error ?? ChatGPTServiceError
.otherError(String(data: result, encoding: .utf8) ?? "Unknown Error")
}
let embeddingResponse = try JSONDecoder().decode(EmbeddingResponse.self, from: result)
#if DEBUG
Logger.service.info("""
Embedding usage
- number of strings: \(tokens.count)
- prompt tokens: \(embeddingResponse.usage.prompt_tokens)
- total tokens: \(embeddingResponse.usage.total_tokens)
""")
#endif
return embeddingResponse
}
static func setupAppInformation(_ request: inout URLRequest) {
if #available(macOS 13.0, *) {
if request.url?.host == "openrouter.ai" {
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
request.setValue(
"https://github.com/intitni/CopilotForXcode",
forHTTPHeaderField: "HTTP-Referer"
)
}
} else {
if request.url?.host == "openrouter.ai" {
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
request.setValue(
"https://github.com/intitni/CopilotForXcode",
forHTTPHeaderField: "HTTP-Referer"
)
}
}
}
static func setupAPIKey(_ request: inout URLRequest, model: EmbeddingModel, apiKey: String) {
if !apiKey.isEmpty {
switch model.format {
case .openAI:
if model.info.openAIInfo.organizationID.isEmpty {
request.setValue(
model.info.openAIInfo.organizationID,
forHTTPHeaderField: "OpenAI-Organization"
)
}
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
case .openAICompatible:
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
case .azureOpenAI:
request.setValue(apiKey, forHTTPHeaderField: "api-key")
case .ollama:
assertionFailure("Unsupported")
}
}
}
}