Skip to content

Commit 8b7234d

Browse files
committed
Make OpenAIEmbeddingService less strict about the response
1 parent 2917628 commit 8b7234d

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

Tool/Sources/OpenAIService/APIs/EmbeddingAPIDefinitions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import AIModel
22
import Foundation
33
import Preferences
4+
import CodableWrappers
45

56
protocol EmbeddingAPI {
67
func embed(text: String) async throws -> EmbeddingResponse
@@ -12,17 +13,28 @@ public struct EmbeddingResponse: Decodable {
1213
public struct Object: Decodable {
1314
public var embedding: [Float]
1415
public var index: Int
16+
@FallbackDecoding<EmptyString>
1517
public var object: String
1618
}
1719

20+
@FallbackDecoding<EmptyArray>
1821
public var data: [Object]
22+
@FallbackDecoding<EmptyString>
1923
public var model: String
2024

2125
public struct Usage: Decodable {
26+
@FallbackDecoding<EmptyInt>
2227
public var prompt_tokens: Int
28+
@FallbackDecoding<EmptyInt>
2329
public var total_tokens: Int
30+
31+
public struct Fallback: FallbackValueProvider {
32+
public static var defaultValue: Usage { Usage(prompt_tokens: 0, total_tokens: 0) }
33+
}
2434
}
2535

36+
@FallbackDecoding<Usage.Fallback>
2637
public var usage: Usage
2738
}
2839

40+

Tool/Sources/OpenAIService/APIs/OpenAIEmbeddingService.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
5050
}
5151

5252
let embeddingResponse = try JSONDecoder().decode(EmbeddingResponse.self, from: result)
53-
#if DEBUG
54-
Logger.service.info("""
55-
Embedding usage
56-
- number of strings: \(text.count)
57-
- prompt tokens: \(embeddingResponse.usage.prompt_tokens)
58-
- total tokens: \(embeddingResponse.usage.total_tokens)
59-
60-
""")
61-
#endif
6253
return embeddingResponse
6354
}
6455

@@ -92,15 +83,6 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
9283
}
9384

9485
let embeddingResponse = try JSONDecoder().decode(EmbeddingResponse.self, from: result)
95-
#if DEBUG
96-
Logger.service.info("""
97-
Embedding usage
98-
- number of strings: \(tokens.count)
99-
- prompt tokens: \(embeddingResponse.usage.prompt_tokens)
100-
- total tokens: \(embeddingResponse.usage.total_tokens)
101-
102-
""")
103-
#endif
10486
return embeddingResponse
10587
}
10688

0 commit comments

Comments
 (0)