Skip to content

Commit a7a34fa

Browse files
committed
Fix embedding empty array
1 parent 316d91d commit a7a34fa

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Tool/Sources/OpenAIService/APIs/OpenAIEmbeddingService.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
2323

2424
public func embed(texts text: [String]) async throws -> EmbeddingResponse {
2525
guard let url = URL(string: endpoint) else { throw ChatGPTServiceError.endpointIncorrect }
26+
if text.isEmpty {
27+
return .init(
28+
data: [],
29+
model: model.info.modelName,
30+
usage: .init(prompt_tokens: 0, total_tokens: 0)
31+
)
32+
}
2633
var request = URLRequest(url: url)
2734
request.httpMethod = "POST"
2835
let encoder = JSONEncoder()
@@ -55,6 +62,13 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
5562

5663
public func embed(tokens: [[Int]]) async throws -> EmbeddingResponse {
5764
guard let url = URL(string: endpoint) else { throw ChatGPTServiceError.endpointIncorrect }
65+
if tokens.isEmpty {
66+
return .init(
67+
data: [],
68+
model: model.info.modelName,
69+
usage: .init(prompt_tokens: 0, total_tokens: 0)
70+
)
71+
}
5872
var request = URLRequest(url: url)
5973
request.httpMethod = "POST"
6074
let encoder = JSONEncoder()
@@ -126,7 +140,7 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
126140
}
127141
}
128142
}
129-
143+
130144
static func setupExtraHeaderFields(_ request: inout URLRequest, model: EmbeddingModel) {
131145
for field in model.info.customHeaderInfo.headers where !field.key.isEmpty {
132146
request.setValue(field.value, forHTTPHeaderField: field.key)

0 commit comments

Comments
 (0)