forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddingAPIDefinitions.swift
More file actions
57 lines (48 loc) · 1.6 KB
/
EmbeddingAPIDefinitions.swift
File metadata and controls
57 lines (48 loc) · 1.6 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
import AIModel
import Foundation
import Preferences
import CodableWrappers
protocol EmbeddingAPI {
func embed(text: String) async throws -> EmbeddingResponse
func embed(texts: [String]) async throws -> EmbeddingResponse
func embed(tokens: [[Int]]) async throws -> EmbeddingResponse
}
extension EmbeddingAPI {
static func setupExtraHeaderFields(
_ request: inout URLRequest,
model: EmbeddingModel,
apiKey: String
) async {
let parser = HeaderValueParser()
for field in model.info.customHeaderInfo.headers where !field.key.isEmpty {
let value = await parser.parse(
field.value,
context: .init(modelName: model.info.modelName, apiKey: apiKey)
)
request.setValue(value, forHTTPHeaderField: field.key)
}
}
}
public struct EmbeddingResponse: Decodable {
public struct Object: Decodable {
public var embedding: [Float]
public var index: Int
@FallbackDecoding<EmptyString>
public var object: String
}
@FallbackDecoding<EmptyArray>
public var data: [Object]
@FallbackDecoding<EmptyString>
public var model: String
public struct Usage: Decodable {
@FallbackDecoding<EmptyInt>
public var prompt_tokens: Int
@FallbackDecoding<EmptyInt>
public var total_tokens: Int
public struct Fallback: FallbackValueProvider {
public static var defaultValue: Usage { Usage(prompt_tokens: 0, total_tokens: 0) }
}
}
@FallbackDecoding<Usage.Fallback>
public var usage: Usage
}