|
| 1 | +using System.Threading.Tasks; |
| 2 | + |
| 3 | +namespace OpenAI_API.Embedding |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// OpenAI’s text embeddings measure the relatedness of text strings by generating an embedding, which is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. |
| 7 | + /// </summary> |
| 8 | + public class EmbeddingEndpoint : EndpointBase |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// This allows you to send request to the recommended model without needing to specify. Every request uses the <see cref="Model.AdaTextEmbedding"/> model |
| 12 | + /// </summary> |
| 13 | + public EmbeddingRequest DefaultEmbeddingRequestArgs { get; set; } = new EmbeddingRequest() { Model = Model.AdaTextEmbedding }; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// The name of the endpoint, which is the final path segment in the API URL. For example, "embeddings". |
| 17 | + /// </summary> |
| 18 | + protected override string Endpoint { get { return "embeddings"; } } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Embeddings"/>. |
| 22 | + /// </summary> |
| 23 | + /// <param name="api"></param> |
| 24 | + internal EmbeddingEndpoint(OpenAIAPI api) : base(api) { } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> |
| 28 | + /// </summary> |
| 29 | + /// <param name="input">Text to be embedded</param> |
| 30 | + /// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns> |
| 31 | + public async Task<EmbeddingResult> CreateEmbeddingAsync(string input) |
| 32 | + { |
| 33 | + EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input); |
| 34 | + return await CreateEmbeddingAsync(req); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Ask the API to embedd text using a custom request |
| 39 | + /// </summary> |
| 40 | + /// <param name="request">Request to be send</param> |
| 41 | + /// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns> |
| 42 | + public async Task<EmbeddingResult> CreateEmbeddingAsync(EmbeddingRequest request) |
| 43 | + { |
| 44 | + return await HttpPost<EmbeddingResult>(postData: request); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> |
| 49 | + /// </summary> |
| 50 | + /// <param name="input">Text to be embedded</param> |
| 51 | + /// <returns>Asynchronously returns the first embedding result as an array of floats.</returns> |
| 52 | + public async Task<float[]> GetEmbeddingsAsync(string input) |
| 53 | + { |
| 54 | + EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input); |
| 55 | + var embeddingResult = await CreateEmbeddingAsync(req); |
| 56 | + return embeddingResult?.Data?[0]?.Embedding; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments