|
| 1 | +// |
| 2 | +// Copyright (c) Vatsal Manot |
| 3 | +// |
| 4 | + |
| 5 | +import NetworkKit |
| 6 | +import FoundationX |
| 7 | +import Swallow |
| 8 | + |
| 9 | +extension VoyageAI { |
| 10 | + public enum APIError: APIErrorProtocol { |
| 11 | + public typealias API = VoyageAI.APISpecification |
| 12 | + |
| 13 | + case apiKeyMissing |
| 14 | + case incorrectAPIKeyProvided |
| 15 | + case rateLimitExceeded |
| 16 | + case badRequest(API.Request.Error) |
| 17 | + case runtime(AnyError) |
| 18 | + |
| 19 | + public var traits: ErrorTraits { |
| 20 | + [.domain(.networking)] |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public struct APISpecification: RESTAPISpecification { |
| 25 | + public typealias Error = APIError |
| 26 | + |
| 27 | + public struct Configuration: Codable, Hashable { |
| 28 | + public var apiKey: String |
| 29 | + } |
| 30 | + |
| 31 | + public let configuration: Configuration |
| 32 | + |
| 33 | + public var host: URL { |
| 34 | + URL(string: "https://api.voyageai.com/v1/")! |
| 35 | + } |
| 36 | + |
| 37 | + public var id: some Hashable { |
| 38 | + configuration |
| 39 | + } |
| 40 | + |
| 41 | + @POST |
| 42 | + @Path("embeddings") |
| 43 | + public var createEmbeddings = Endpoint<RequestBodies.CreateEmbedding, VoyageAI.Embeddings, Void>() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +extension VoyageAI.APISpecification { |
| 48 | + public final class Endpoint<Input, Output, Options>: BaseHTTPEndpoint<VoyageAI.APISpecification, Input, Output, Options> { |
| 49 | + override public func buildRequestBase( |
| 50 | + from input: Input, |
| 51 | + context: BuildRequestContext |
| 52 | + ) throws -> Request { |
| 53 | + let configuration = context.root.configuration |
| 54 | + |
| 55 | + return try super |
| 56 | + .buildRequestBase(from: input, context: context) |
| 57 | + .jsonBody(input, keyEncodingStrategy: .convertToSnakeCase) |
| 58 | + .header(.contentType(.json)) |
| 59 | + .header(.accept(.json)) |
| 60 | + .header(.authorization(.bearer, configuration.apiKey)) |
| 61 | + } |
| 62 | + |
| 63 | + struct _ErrorWrapper: Codable, Hashable, Sendable { |
| 64 | + struct Error: Codable, Hashable, Sendable { |
| 65 | + let detail: String |
| 66 | + } |
| 67 | + |
| 68 | + let error: Error |
| 69 | + } |
| 70 | + |
| 71 | + override public func decodeOutputBase( |
| 72 | + from response: Request.Response, |
| 73 | + context: DecodeOutputContext |
| 74 | + ) throws -> Output { |
| 75 | + do { |
| 76 | + try response.validate() |
| 77 | + } catch { |
| 78 | + let apiError: Error |
| 79 | + |
| 80 | + if let error = error as? Request.Error { |
| 81 | + if let error = try? response.decode( |
| 82 | + _ErrorWrapper.self, |
| 83 | + keyDecodingStrategy: .convertFromSnakeCase |
| 84 | + ).error { |
| 85 | + if error.detail.contains("You didn't provide an API key") { |
| 86 | + throw Error.apiKeyMissing |
| 87 | + } else if error.detail.contains("Incorrect API key provided") { |
| 88 | + throw Error.incorrectAPIKeyProvided |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + if response.statusCode.rawValue == 429 { |
| 94 | + apiError = .rateLimitExceeded |
| 95 | + } else { |
| 96 | + apiError = .badRequest(error) |
| 97 | + } |
| 98 | + } else { |
| 99 | + apiError = .runtime(error) |
| 100 | + } |
| 101 | + |
| 102 | + throw apiError |
| 103 | + } |
| 104 | + |
| 105 | + return try response.decode( |
| 106 | + Output.self, |
| 107 | + keyDecodingStrategy: .convertFromSnakeCase |
| 108 | + ) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +extension VoyageAI.APISpecification { |
| 114 | + public enum RequestBodies: _StaticSwift.Namespace { |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + public enum ResponseBodies: _StaticSwift.Namespace { |
| 119 | + |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +extension VoyageAI.APISpecification.RequestBodies { |
| 124 | + public struct CreateEmbedding: Codable, Hashable { |
| 125 | + |
| 126 | + /// Name of the model. Recommended options: voyage-2, voyage-large-2, voyage-finance-2, voyage-multilingual-2, voyage-law-2, voyage-code-2. |
| 127 | + public let model: VoyageAI.Model |
| 128 | + |
| 129 | + /// A single text string, or a list of texts as a list of strings. Currently, we have two constraints on the list: |
| 130 | + /// The maximum length of the list is 128. |
| 131 | + /// The total number of tokens in the list is at most 320K for voyage-2, and 120K for voyage-large-2, voyage-finance-2, voyage-multilingual-2, voyage-law-2, and voyage-code-2. |
| 132 | + public let input: [String] |
| 133 | + |
| 134 | + /// Type of the input text. Defaults to nil. Other options: query, document. |
| 135 | + public let inputType: String? |
| 136 | + |
| 137 | + /// Whether to truncate the input texts to fit within the context length. Defaults to true. |
| 138 | + /// If true, over-length input texts will be truncated to fit within the context length, before vectorized by the embedding model. |
| 139 | + /// If false, an error will be raised if any given text exceeds the context length. |
| 140 | + public let truncation: Bool |
| 141 | + |
| 142 | + /// Format in which the embeddings are encoded. We support two options: |
| 143 | + /// If not specified (defaults to null): the embeddings are represented as lists of floating-point numbers; |
| 144 | + /// base64: the embeddings are compressed to base64 encodings. |
| 145 | + public let encodingFormat: EncodingFormat? |
| 146 | + public enum EncodingFormat: String, Codable, Hashable, Sendable { |
| 147 | + case float |
| 148 | + case base64 |
| 149 | + } |
| 150 | + |
| 151 | + init( |
| 152 | + model: VoyageAI.Model, |
| 153 | + input: [String], |
| 154 | + inputType: String? = nil, |
| 155 | + truncation: Bool = true, |
| 156 | + encodingFormat: EncodingFormat? = nil |
| 157 | + ) { |
| 158 | + self.model = model |
| 159 | + self.input = input |
| 160 | + self.inputType = inputType |
| 161 | + self.truncation = truncation |
| 162 | + self.encodingFormat = encodingFormat == .base64 ? .base64 : nil |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments