|
| 1 | +// |
| 2 | +// OpenAIEmbeddingRequestBody.swift |
| 3 | +// AIProxy |
| 4 | +// |
| 5 | +// Created by Lou Zell on 2/16/25. |
| 6 | +// |
| 7 | + |
| 8 | +/// From OpenAI's docs: |
| 9 | +/// |
| 10 | +/// > Embeddings are a numerical representation of text that can be used to measure the |
| 11 | +/// relatedness between two pieces of text. Embeddings are useful for search, clustering, |
| 12 | +/// recommendations, anomaly detection, and classification tasks. You can read more about our |
| 13 | +/// latest embedding models in the announcement blog post." |
| 14 | +/// |
| 15 | +/// References: |
| 16 | +/// - https://openai.com/blog/new-embedding-models-and-api-updates |
| 17 | +/// - https://platform.openai.com/docs/api-reference/embeddings/create |
| 18 | +public struct OpenAIEmbeddingRequestBody: Encodable { |
| 19 | + |
| 20 | + // Required |
| 21 | + |
| 22 | + /// Input text to embed, encoded as a string or array of tokens. To embed multiple inputs |
| 23 | + /// in a single request, pass an array of strings or array of token arrays. The input must |
| 24 | + /// not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002), |
| 25 | + /// cannot be an empty string, and any array must be 2048 dimensions or less. Example |
| 26 | + /// Python code for counting tokens. Some models may also impose a limit on total number of |
| 27 | + /// tokens summed across inputs. |
| 28 | + public let input: EmbeddingInput |
| 29 | + |
| 30 | + /// ID of the model to use. |
| 31 | + /// See the model list here: https://platform.openai.com/docs/models#embeddings |
| 32 | + /// |
| 33 | + /// text-embedding-3-large |
| 34 | + /// Most capable embedding model for both english and non-english tasks. |
| 35 | + /// Output dimension: 3,072 |
| 36 | + /// |
| 37 | + /// text-embedding-3-small |
| 38 | + /// Increased performance over 2nd generation ada embedding model. |
| 39 | + /// Output dimension: 1,536 |
| 40 | + /// |
| 41 | + /// text-embedding-ada-002 |
| 42 | + /// Most capable 2nd generation embedding model, replacing 16 first generation models. |
| 43 | + /// Output dimension: 1,536 |
| 44 | + public let model: String |
| 45 | + |
| 46 | + // Optional |
| 47 | + |
| 48 | + /// The format to return the embeddings in. |
| 49 | + /// Defaults to float |
| 50 | + public let encodingFormat: EncodingFormat? |
| 51 | + |
| 52 | + /// The number of dimensions the resulting output embeddings should have. Only supported in |
| 53 | + /// `text-embedding-3` and later models. |
| 54 | + public let dimensions: Int? |
| 55 | + |
| 56 | + /// A unique identifier representing your end-user, which can help OpenAI to monitor and |
| 57 | + /// detect abuse. |
| 58 | + public let user: String? |
| 59 | + |
| 60 | + private enum CodingKeys: String, CodingKey { |
| 61 | + case input |
| 62 | + case model |
| 63 | + |
| 64 | + case encodingFormat = "encoding_format" |
| 65 | + case dimensions |
| 66 | + case user |
| 67 | + } |
| 68 | + |
| 69 | + // This memberwise initializer is autogenerated. |
| 70 | + // To regenerate, use `cmd-shift-a` > Generate Memberwise Initializer |
| 71 | + // To format, place the cursor in the initializer's parameter list and use `ctrl-m` |
| 72 | + public init( |
| 73 | + input: OpenAIEmbeddingRequestBody.EmbeddingInput, |
| 74 | + model: String, |
| 75 | + encodingFormat: OpenAIEmbeddingRequestBody.EncodingFormat? = nil, |
| 76 | + dimensions: Int? = nil, |
| 77 | + user: String? = nil |
| 78 | + ) { |
| 79 | + self.input = input |
| 80 | + self.model = model |
| 81 | + self.encodingFormat = encodingFormat |
| 82 | + self.dimensions = dimensions |
| 83 | + self.user = user |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +extension OpenAIEmbeddingRequestBody { |
| 88 | + public enum EmbeddingInput: Encodable { |
| 89 | + case text(String) |
| 90 | + case textArray([String]) |
| 91 | + case intArray([Int]) |
| 92 | + |
| 93 | + public func encode(to encoder: any Encoder) throws { |
| 94 | + var container = encoder.singleValueContainer() |
| 95 | + switch self { |
| 96 | + case .text(let text): |
| 97 | + try container.encode(text) |
| 98 | + case .textArray(let arr): |
| 99 | + try container.encode(arr) |
| 100 | + case .intArray(let arr): |
| 101 | + try container.encode(arr) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +extension OpenAIEmbeddingRequestBody { |
| 108 | + public enum EncodingFormat: String, Encodable { |
| 109 | + case float |
| 110 | + case base64 |
| 111 | + } |
| 112 | +} |
0 commit comments