temp save
This commit is contained in:
parent
78579fe4fe
commit
0a0da61db3
|
|
@ -15,11 +15,4 @@ public class InstructFileModel : FileBase
|
|||
[JsonPropertyName("file_url")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? FileUrl { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// File MIME type
|
||||
/// </summary>
|
||||
[JsonPropertyName("content_type")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? ContentType { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@ public partial class FileInstructService
|
|||
Files = images?.Select(x => new BotSharpFile
|
||||
{
|
||||
FileUrl = x.FileUrl,
|
||||
FileData = x.FileData,
|
||||
ContentType = x.ContentType
|
||||
FileData = x.FileData
|
||||
}).ToList() ?? []
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -85,38 +85,31 @@ public partial class FileInstructService
|
|||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
var downloadTasks = files.Select(x => DownloadFile(x));
|
||||
await Task.WhenAll(downloadTasks);
|
||||
|
||||
var locs = new List<string>();
|
||||
foreach (var file in files)
|
||||
for (int i = 0; i < files.Count; i++)
|
||||
{
|
||||
var binary = downloadTasks.ElementAt(i).Result;
|
||||
if (binary == null || binary.IsEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var binary = BinaryData.Empty;
|
||||
if (!string.IsNullOrEmpty(file.FileUrl))
|
||||
{
|
||||
var http = _services.GetRequiredService<IHttpClientFactory>();
|
||||
using var client = http.CreateClient();
|
||||
var bytes = await client.GetByteArrayAsync(file.FileUrl);
|
||||
binary = BinaryData.FromBytes(bytes);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(file.FileData))
|
||||
{
|
||||
(_, binary) = FileUtility.GetFileInfoFromData(file.FileData);
|
||||
}
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
var fileDir = _fileStorage.BuildDirectory(dir, guid);
|
||||
DeleteIfExistDirectory(fileDir, createNew: true);
|
||||
|
||||
if (!binary.IsEmpty)
|
||||
{
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
var fileDir = _fileStorage.BuildDirectory(dir, guid);
|
||||
DeleteIfExistDirectory(fileDir, createNew: true);
|
||||
|
||||
var outputDir = _fileStorage.BuildDirectory(fileDir, $"{guid}.{extension}");
|
||||
_fileStorage.SaveFileBytesToPath(outputDir, binary);
|
||||
locs.Add(outputDir);
|
||||
}
|
||||
var outputDir = _fileStorage.BuildDirectory(fileDir, $"{guid}.{extension}");
|
||||
_fileStorage.SaveFileBytesToPath(outputDir, binary);
|
||||
locs.Add(outputDir);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Error when saving {extension} file.");
|
||||
_logger.LogWarning(ex, $"Error when saving #{i + 1} {extension} file.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,19 +35,28 @@ public partial class FileInstructService : IFileInstructService
|
|||
private async Task<BinaryData> DownloadFile(InstructFileModel file)
|
||||
{
|
||||
var binary = BinaryData.Empty;
|
||||
if (!string.IsNullOrEmpty(file.FileUrl))
|
||||
{
|
||||
var http = _services.GetRequiredService<IHttpClientFactory>();
|
||||
using var client = http.CreateClient();
|
||||
var bytes = await client.GetByteArrayAsync(file.FileUrl);
|
||||
binary = BinaryData.FromBytes(bytes);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(file.FileData))
|
||||
{
|
||||
(_, binary) = FileUtility.GetFileInfoFromData(file.FileData);
|
||||
}
|
||||
|
||||
return binary;
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(file.FileUrl))
|
||||
{
|
||||
var http = _services.GetRequiredService<IHttpClientFactory>();
|
||||
using var client = http.CreateClient();
|
||||
var bytes = await client.GetByteArrayAsync(file.FileUrl);
|
||||
binary = BinaryData.FromBytes(bytes);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(file.FileData))
|
||||
{
|
||||
(_, binary) = FileUtility.GetFileInfoFromData(file.FileData);
|
||||
}
|
||||
|
||||
return binary;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Error when downloading file {file.FileUrl}");
|
||||
return binary;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string?> GetAgentTemplate(string agentId, string? templateName)
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
#region Read image
|
||||
[HttpPost("/instruct/multi-modal")]
|
||||
public async Task<string> MultiModalCompletion([FromBody] MultiModalRequest input)
|
||||
public async Task<string> MultiModalCompletion([FromBody] MultiModalFileRequest input)
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
|
|
@ -141,28 +141,25 @@ public class InstructModeController : ControllerBase
|
|||
}
|
||||
|
||||
[HttpPost("/instruct/multi-modal/upload")]
|
||||
public async Task<MultiModalViewModel> MultiModalCompletion(IFormFile file, [FromForm] string text, [FromForm] string? provider = null,
|
||||
[FromForm] string? model = null, [FromForm] List<MessageState>? states = null,
|
||||
[FromForm] string? agentId = null, [FromForm] string? templateName = null)
|
||||
public async Task<MultiModalViewModel> MultiModalCompletion([FromForm] IEnumerable<IFormFile> files, [FromForm] MultiModalRequest request)
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
states?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
request?.States?.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
var viewModel = new MultiModalViewModel();
|
||||
|
||||
try
|
||||
{
|
||||
var data = FileUtility.BuildFileDataFromFile(file);
|
||||
var files = new List<InstructFileModel>
|
||||
var fileModels = files.Select(x => new InstructFileModel
|
||||
{
|
||||
new InstructFileModel { FileData = data }
|
||||
};
|
||||
FileData = FileUtility.BuildFileDataFromFile(x)
|
||||
}).ToList();
|
||||
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
|
||||
var content = await fileInstruct.ReadImages(text, files, new InstructOptions
|
||||
var content = await fileInstruct.ReadImages(request?.Text ?? string.Empty, fileModels, new InstructOptions
|
||||
{
|
||||
Provider = provider,
|
||||
Model = model,
|
||||
AgentId = agentId,
|
||||
TemplateName = templateName
|
||||
Provider = request?.Provider,
|
||||
Model = request?.Model,
|
||||
AgentId = request?.AgentId,
|
||||
TemplateName = request?.TemplateName
|
||||
});
|
||||
viewModel.Content = content;
|
||||
return viewModel;
|
||||
|
|
@ -424,7 +421,7 @@ public class InstructModeController : ControllerBase
|
|||
|
||||
#region Pdf
|
||||
[HttpPost("/instruct/pdf-completion")]
|
||||
public async Task<PdfCompletionViewModel> PdfCompletion([FromBody] MultiModalRequest input)
|
||||
public async Task<PdfCompletionViewModel> PdfCompletion([FromBody] MultiModalFileRequest input)
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
||||
|
|
|
|||
|
|
@ -24,7 +24,10 @@ public class MultiModalRequest : InstructBaseRequest
|
|||
{
|
||||
[JsonPropertyName("text")]
|
||||
public string Text { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class MultiModalFileRequest : MultiModalRequest
|
||||
{
|
||||
[JsonPropertyName("files")]
|
||||
public List<InstructFileModel> Files { get; set; } = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -289,7 +289,7 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
if (!string.IsNullOrEmpty(file.FileData))
|
||||
{
|
||||
var (contentType, binary) = FileUtility.GetFileInfoFromData(file.FileData);
|
||||
var contentPart = ChatMessageContentPart.CreateImagePart(binary, contentType ?? file.ContentType, ChatImageDetailLevel.Auto);
|
||||
var contentPart = ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(binary.ToArray()), contentType, ChatImageDetailLevel.Auto);
|
||||
contentParts.Add(contentPart);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(file.FileStorageUrl))
|
||||
|
|
|
|||
Loading…
Reference in a new issue