Unable to send a local image that isn‘t under wwwroot to openai using its new .NET library

题意:无法使用OpenAI的新.NET库发送不在wwwroot目录下的本地图片

问题背景:

I have an ASP.NET Core 6 Web API with the new official library from OpenAI (GitHub - openai/openai-dotnet: The official .NET library for the OpenAI API).

我有一个使用OpenAI新官方库的ASP.NET Core 6 Web API ......

What I'm trying to do, is to use a local image file to OpenAI. The file isn't under wwwroot, but under backend/assets/1.jpg.

我正在尝试做的是使用本地图片文件发送到OpenAI。该文件不在wwwroot目录下,而是在backend/assets/1.jpg目录下

Sure, here's a simple demo on how to automatically add "@using ThatOtherNamespace" to a Blazor page: 1. In your Blazor project, navigate to the "wwwroot" folder and create a new folder called "utilities". 2. In the "utilities" folder, create a new file called "AutoAddNamespace.cs". 3. In the "AutoAddNamespace.cs" file, add the following code: ``` using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; namespace YourProjectNamespace.Utilities { public class AutoAddNamespace : ComponentBase { protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await JSRuntime.InvokeVoidAsync("autoAddNamespace"); } } [JSInvokable] public static void AddNamespace() { // Get the current page's markup var markup = RenderFragmentToString(builder => { builder.OpenComponent<AutoAddNamespace>(0); builder.CloseComponent(); }); // Check if the namespace is already added if (!markup.Contains("@using ThatOtherNamespace")) { // Add the namespace to the markup markup = markup.Replace("@page", "@using ThatOtherNamespace\n@page"); // Refresh the page with the updated markup JSRuntime.InvokeVoidAsync("updatePageMarkup", markup); } } private static string RenderFragmentToString(RenderFragment renderFragment) { var result = new StringBuilder(); var renderer = new HtmlRenderer(new RendererContext(new HtmlEncoder())) { Builder = new RenderTreeBuilder(new HtmlRenderer(new RendererContext(new HtmlEncoder()))), NewLine = "\n" }; renderer.Render(renderFragment); renderer.Builder.Flush(); result.Append(renderer.Builder.ToString()); return result.ToString(); } } } ``` 4. In your Blazor page, add the following code at the top: ``` @inject IJSRuntime JSRuntime @using YourProjectNamespace.Utilities ``` 5. At the bottom of your Blazor page, add the following code: ``` @code { protected override void OnInitialized() { JSRuntime.InvokeVoidAsync("registerAddNamespaceMethod"); } } ``` 6. In the "wwwroot/index.html" file, add the following code inside the "head" tag: ``` <script> function autoAddNamespace() { DotNet.invokeMethodAsync("YourProjectNamespace", "AddNamespace"); } function updatePageMarkup(markup) { document.documentElement.innerHTML = markup; } function registerAddNamespaceMethod() { DotNet.registerMethod("YourProjectNamespace", "AddNamespace", AutoAddNamespace.prototype.AddNamespace); } </script> ``` That's it! Now, every time the Blazor page is loaded, the "AutoAddNamespace" component will automatically check if "@using ThatOtherNamespace" is already added to the page markup. If not, it will add it and refresh the page with the updated markup.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值