openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Internal/InternalChatCompletionRequestMessageContentPartImageImageUrl.cs
59lines · modecode
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Text.RegularExpressions; |
| 4 | |
| 5 | namespace OpenAI.Chat; |
| 6 | |
| 7 | [CodeGenType("ChatCompletionRequestMessageContentPartImageImageUrl")] |
| 8 | [CodeGenSuppress("InternalChatCompletionRequestMessageContentPartImageImageUrl", typeof(string))] |
| 9 | internal partial class InternalChatCompletionRequestMessageContentPartImageImageUrl |
| 10 | { |
| 11 | private Uri _imageUri; |
| 12 | private BinaryData _imageBytes; |
| 13 | private string _imageBytesMediaType; |
| 14 | |
| 15 | // CUSTOM: Changed type from Uri to string to be able to support data URIs properly. |
| 16 | /// <summary> Either a URL of the image or the base64 encoded image data. </summary> |
| 17 | [CodeGenMember("Url")] |
| 18 | internal string InternalUrl |
| 19 | { |
| 20 | get => _internalUrl; |
| 21 | set |
| 22 | { |
| 23 | _internalUrl = value; |
| 24 | if (value is not null && !DataEncodingHelpers.TryParseDataUri(value, out _imageBytes, out _imageBytesMediaType)) |
| 25 | { |
| 26 | _imageUri = new Uri(value); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | private string _internalUrl; |
| 31 | |
| 32 | /// <summary> Initializes a new instance of <see cref="InternalChatCompletionRequestMessageContentPartImageImageUrl"/>. </summary> |
| 33 | /// <param name="uri"> Either a URL of the image or the base64 encoded image data. </param> |
| 34 | /// <exception cref="ArgumentNullException"> <paramref name="uri"/> is null. </exception> |
| 35 | public InternalChatCompletionRequestMessageContentPartImageImageUrl(Uri uri, ChatImageDetailLevel? detailLevel = default) |
| 36 | : this(detailLevel, null, null) |
| 37 | { |
| 38 | Argument.AssertNotNull(uri, nameof(uri)); |
| 39 | _imageUri = uri; |
| 40 | _internalUrl = uri.ToString(); |
| 41 | } |
| 42 | |
| 43 | public InternalChatCompletionRequestMessageContentPartImageImageUrl(BinaryData imageBytes, string imageBytesMediaType, ChatImageDetailLevel? detailLevel = default) |
| 44 | : this(detailLevel, null, null) |
| 45 | { |
| 46 | Argument.AssertNotNull(imageBytes, nameof(imageBytes)); |
| 47 | Argument.AssertNotNull(imageBytesMediaType, nameof(imageBytesMediaType)); |
| 48 | |
| 49 | _imageBytes = imageBytes; |
| 50 | _imageBytesMediaType = imageBytesMediaType; |
| 51 | _internalUrl = DataEncodingHelpers.CreateDataUri(imageBytes, imageBytesMediaType); |
| 52 | } |
| 53 | |
| 54 | public Uri ImageUri => _imageUri; |
| 55 | |
| 56 | public BinaryData ImageBytes => _imageBytes; |
| 57 | |
| 58 | public string ImageBytesMediaType => _imageBytesMediaType; |
| 59 | } |
| 60 | |