openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Internal/InternalChatCompletionRequestMessageContentPartFileFile.cs
32lines · modecode
| 1 | using System; |
| 2 | using System.Text.RegularExpressions; |
| 3 | |
| 4 | namespace OpenAI.Chat; |
| 5 | |
| 6 | [CodeGenType("ChatCompletionRequestMessageContentPartFileFile")] |
| 7 | internal partial class InternalChatCompletionRequestMessageContentPartFileFile |
| 8 | { |
| 9 | private readonly BinaryData _fileBytes; |
| 10 | private readonly string _fileBytesMediaType; |
| 11 | |
| 12 | // CUSTOM: Changed type from Uri to string to be able to support data URIs properly. |
| 13 | /// <summary> Either a URL of the image or the base64 encoded image data. </summary> |
| 14 | [CodeGenMember("FileData")] |
| 15 | internal string FileData { get; } |
| 16 | |
| 17 | public InternalChatCompletionRequestMessageContentPartFileFile(BinaryData fileBytes, string fileBytesMediaType) |
| 18 | { |
| 19 | Argument.AssertNotNull(fileBytes, nameof(fileBytes)); |
| 20 | Argument.AssertNotNull(fileBytesMediaType, nameof(fileBytesMediaType)); |
| 21 | |
| 22 | _fileBytes = fileBytes; |
| 23 | _fileBytesMediaType = fileBytesMediaType; |
| 24 | |
| 25 | string base64EncodedData = Convert.ToBase64String(_fileBytes.ToArray()); |
| 26 | FileData = $"data:{_fileBytesMediaType};base64,{base64EncodedData}"; |
| 27 | } |
| 28 | |
| 29 | public BinaryData FileBytes => _fileBytes; |
| 30 | |
| 31 | public string FileBytesMediaType => _fileBytesMediaType; |
| 32 | } |
| 33 | |