Что я хочу Я пытаюсь использовать локальный файл изображения для OpenAI. Файл находится не в папке wwwroot, а в папке backend/assets/1.jpg.

Я написал базовый сервис для настройки всей информации, необходимой для отправки запроса в OpenAI. Но проблема в том, что я не могу отправить изображение.
Я постоянно получаю ошибки типа «слишком длинный URL-адрес» или «недопустимое изображение».
Вот мой код — OpenAiService:
Код: Выделить всё
using OpenAI.Chat;
namespace backend.Services
{
public class OpenAiService
{
private readonly ChatClient _chatClient;
private readonly ChatCompletionOptions _options;
public OpenAiService(IConfiguration configuration)
{
var apiKey = configuration.GetValue("OpenAI:Key");
_chatClient = new ChatClient("gpt-4o", apiKey);
_options = new ChatCompletionOptions()
{
MaxTokens = 300,
};
}
public async Task ExtractListOfItems()
{
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Assets", "1.jpg");
var localUrl = $"https://localhost:7068/assets/{Path.GetFileName(imagePath)}";
var messages = new List
{
new UserChatMessage(new List
{
ChatMessageContentPart.CreateTextMessageContentPart("Extract the items from the following image and return a list of items including prices and amount."),
ChatMessageContentPart.CreateImageMessageContentPart(new Uri(localUrl))
})
};
var completion = await _chatClient.CompleteChatAsync(messages, _options);
return completion.Value.ToString();
}
}
}
Код: Выделить всё
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using backend.Services;
using OpenAI;
using OpenAI.Chat;
namespace backend.Controllers;
[ApiController]
[Route("[controller]")]
public class OpenAiDemoController : ControllerBase
{
private readonly OpenAiService _openAiService;
public OpenAiDemoController(OpenAiService openAiService)
{
_openAiService = openAiService;
}
[HttpPost]
[Route("extract-items")]
public async Task CompleteSentence()
{
var completion = await _openAiService.ExtractListOfItems();
return Ok(completion);
}
}
Код: Выделить всё
program.cs
Код: Выделить всё
using backend.Configurations;
using backend.Services;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.Configure(builder.Configuration.GetSection("OpenAI"));
//add services
builder.Services.AddSingleton();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// builder.Services.AddScoped();
builder.Services.AddCors(opt =>
{
opt.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "Assets")),
RequestPath = "/assets"
});
app.UseStaticFiles(); // This serves files from wwwroot
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "Assets")),
RequestPath = "/assets"
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("AllowAll");
app.UseAuthorization();
app.MapControllers();
app.Run();
Подробнее здесь: https://stackoverflow.com/questions/787 ... -its-new-n