openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Internal/InternalChatCompletionRequestMessageContentPartImageImageUrl.cs
80lines · modecode
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Text.RegularExpressions; |
| 4 | |
| 5 | namespace OpenAI.Chat; |
| 6 | |
| 7 | [CodeGenModel("ChatCompletionRequestMessageContentPartImageImageUrl")] |
| 8 | [CodeGenSuppress("InternalChatCompletionRequestMessageContentPartImageImageUrl", typeof(string))] |
| 9 | internal partial class InternalChatCompletionRequestMessageContentPartImageImageUrl |
| 10 | { |
| 11 | #if NET8_0_OR_GREATER |
| 12 | [GeneratedRegex(@"^data:(?<type>.+?);base64,(?<data>.+)$")] |
| 13 | private static partial Regex ParseDataUriRegex(); |
| 14 | #else |
| 15 | private static Regex ParseDataUriRegex() => s_parseDataUriRegex; |
| 16 | private static readonly Regex s_parseDataUriRegex = new(@"^data:(?<type>.+?);base64,(?<data>.+)$", RegexOptions.Compiled); |
| 17 | #endif |
| 18 | |
| 19 | private readonly Uri _imageUri; |
| 20 | private readonly BinaryData _imageBytes; |
| 21 | private readonly string _imageBytesMediaType; |
| 22 | |
| 23 | // CUSTOM: Changed type from Uri to string to be able to support data URIs properly. |
| 24 | /// <summary> Either a URL of the image or the base64 encoded image data. </summary> |
| 25 | [CodeGenMember("Url")] |
| 26 | internal string Url { get; } |
| 27 | |
| 28 | /// <summary> Initializes a new instance of <see cref="InternalChatCompletionRequestMessageContentPartImageImageUrl"/>. </summary> |
| 29 | /// <param name="uri"> Either a URL of the image or the base64 encoded image data. </param> |
| 30 | /// <exception cref="ArgumentNullException"> <paramref name="uri"/> is null. </exception> |
| 31 | public InternalChatCompletionRequestMessageContentPartImageImageUrl(Uri uri) |
| 32 | { |
| 33 | Argument.AssertNotNull(uri, nameof(uri)); |
| 34 | |
| 35 | _imageUri = uri; |
| 36 | |
| 37 | Url = uri.ToString(); |
| 38 | } |
| 39 | |
| 40 | public InternalChatCompletionRequestMessageContentPartImageImageUrl(BinaryData imageBytes, string imageBytesMediaType) |
| 41 | { |
| 42 | Argument.AssertNotNull(imageBytes, nameof(imageBytes)); |
| 43 | Argument.AssertNotNull(imageBytesMediaType, nameof(imageBytesMediaType)); |
| 44 | |
| 45 | _imageBytes = imageBytes; |
| 46 | _imageBytesMediaType = imageBytesMediaType; |
| 47 | |
| 48 | string base64EncodedData = Convert.ToBase64String(_imageBytes.ToArray()); |
| 49 | Url = $"data:{_imageBytesMediaType};base64,{base64EncodedData}"; |
| 50 | } |
| 51 | |
| 52 | /// <summary> Initializes a new instance of <see cref="InternalChatCompletionRequestMessageContentPartImageImageUrl"/>. </summary> |
| 53 | /// <param name="url"> Either a URL of the image or the base64 encoded image data. </param> |
| 54 | /// <param name="detail"> Specifies the detail level of the image. Learn more in the [Vision guide](/docs/guides/vision/low-or-high-fidelity-image-understanding). </param> |
| 55 | /// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param> |
| 56 | internal InternalChatCompletionRequestMessageContentPartImageImageUrl(string url, ChatImageDetailLevel? detail, IDictionary<string, BinaryData> serializedAdditionalRawData) |
| 57 | { |
| 58 | Match parsedDataUri = ParseDataUriRegex().Match(url); |
| 59 | |
| 60 | if (parsedDataUri.Success) |
| 61 | { |
| 62 | _imageBytes = BinaryData.FromBytes(Convert.FromBase64String(parsedDataUri.Groups["data"].Value)); |
| 63 | _imageBytesMediaType = parsedDataUri.Groups["type"].Value; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | _imageUri = new Uri(url); |
| 68 | } |
| 69 | |
| 70 | Url = url; |
| 71 | Detail = detail; |
| 72 | SerializedAdditionalRawData = serializedAdditionalRawData; |
| 73 | } |
| 74 | |
| 75 | public Uri ImageUri => _imageUri; |
| 76 | |
| 77 | public BinaryData ImageBytes => _imageBytes; |
| 78 | |
| 79 | public string ImageBytesMediaType => _imageBytesMediaType; |
| 80 | } |
| 81 | |