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