forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIEmbedding.swift
More file actions
175 lines (160 loc) · 6.63 KB
/
OpenAIEmbedding.swift
File metadata and controls
175 lines (160 loc) · 6.63 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Foundation
import OpenAIService
import TokenEncoder
public struct OpenAIEmbedding: Embeddings {
public var service: EmbeddingService
public var shouldAverageLongEmbeddings: Bool
/// Usually we won't hit the limit because the max token is 8191 and we will do text splitting
/// before embedding.
public var safe: Bool
public init(
configuration: EmbeddingConfiguration,
shouldAverageLongEmbeddings: Bool = false,
safe: Bool = false
) {
service = EmbeddingService(configuration: configuration)
self.shouldAverageLongEmbeddings = shouldAverageLongEmbeddings
self.safe = safe
}
public func embed(documents: [Document]) async throws -> [EmbeddedDocument] {
if safe {
return try await getLenSafeEmbeddings(documents: documents)
}
return try await getEmbeddings(documents: documents)
}
public func embed(query: String) async throws -> [Float] {
if safe {
return try await getLenSafeEmbeddings(documents: [.init(
pageContent: query,
metadata: [:]
)])
.first?
.embeddings ?? []
}
return try await getEmbeddings(documents: [.init(pageContent: query, metadata: [:])])
.first?
.embeddings ?? []
}
}
extension OpenAIEmbedding {
func getEmbeddings(
documents: [Document]
) async throws -> [EmbeddedDocument] {
try await withThrowingTaskGroup(
of: (document: Document, embeddings: [Float]).self
) { group in
for document in documents {
group.addTask {
var retryCount = 6
var previousError: Error?
while retryCount > 0 {
do {
let embeddings = try await service.embed(text: document.pageContent)
.data
.map(\.embedding).first ?? []
return (document, embeddings)
} catch {
retryCount -= 1
previousError = error
}
}
throw previousError ?? CancellationError()
}
}
var all = [EmbeddedDocument]()
for try await result in group {
all.append(.init(document: result.document, embeddings: result.embeddings))
}
return all
}
}
/// OpenAI's embedding API doesn't support embedding inputs longer than the max token.
/// https://github.com/openai/openai-cookbook/blob/main/examples/Embedding_long_inputs.ipynb
func getLenSafeEmbeddings(
documents: [Document]
) async throws -> [EmbeddedDocument] {
struct Text {
var document: Document
var chunkedTokens: [[Int]]
}
var texts = documents.map { Text(document: $0, chunkedTokens: []) }
let encoding = TiktokenCl100kBaseTokenEncoder()
for (index, text) in texts.enumerated() {
let token = encoding.encode(text: text.document.pageContent)
// just incase the calculation is incorrect
let maxToken = max(10, service.configuration.maxToken - 10)
for j in stride(from: 0, to: token.count, by: maxToken) {
texts[index].chunkedTokens.append(
Array(token[j..<min(j + maxToken, token.count)])
)
}
}
let batchedEmbeddings = try await withThrowingTaskGroup(
of: (Document, [[Float]]).self
) { group in
for text in texts {
group.addTask {
var retryCount = 6
var previousError: Error?
guard !text.chunkedTokens.isEmpty
else { return (text.document, []) }
while retryCount > 0 {
do {
if text.chunkedTokens.count <= 1 {
// if possible, we should just let OpenAI do the tokenization.
return (
text.document,
try await service.embed(text: text.document.pageContent)
.data
.map(\.embedding)
)
}
if shouldAverageLongEmbeddings {
return (
text.document,
try await service.embed(tokens: text.chunkedTokens)
.data
.map(\.embedding)
)
}
// if `shouldAverageLongEmbeddings` is false,
// we only embed the first chunk to save some money.
return (
text.document,
try await service.embed(tokens: [text.chunkedTokens.first ?? []])
.data
.map(\.embedding)
)
} catch {
retryCount -= 1
previousError = error
}
}
throw previousError ?? CancellationError()
}
}
var result = [(document: Document, embeddings: [[Float]])]()
for try await response in group {
try Task.checkCancellation()
result.append((response.0, response.1))
}
return result
}
var results = [EmbeddedDocument]()
for (document, embeddings) in batchedEmbeddings {
if embeddings.count == 1, let first = embeddings.first {
results.append(.init(document: document, embeddings: first))
} else if embeddings.isEmpty {
results.append(.init(document: document, embeddings: []))
} else if shouldAverageLongEmbeddings {
// unimplemented
if let first = embeddings.first {
results.append(.init(document: document, embeddings: first))
}
} else if let first = embeddings.first {
results.append(.init(document: document, embeddings: first))
}
}
return results
}
}