Skip to content

Issue 38: Update to use Microsoft.Extensions.AI.Abstractions library #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="Microsoft.Bcl.Numerics" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25161.3" />
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25161.3" />
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25161.3" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="6.0.33" />
Expand Down
7 changes: 0 additions & 7 deletions SmartComponents.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestBlazorApp", "test\testa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartComponents.Inference", "src\SmartComponents.Inference\SmartComponents.Inference.csproj", "{ED69BAFD-00BC-4086-A34D-466CFCEBB300}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartComponents.Inference.OpenAI", "src\SmartComponents.Inference.OpenAI\SmartComponents.Inference.OpenAI.csproj", "{7AE35372-45C8-4ACA-BEEA-265C8A272910}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartComponents.E2ETest.Common", "test\SmartComponents.E2ETest.Common\SmartComponents.E2ETest.Common.csproj", "{3970A925-2753-4E2C-B39C-420C3BC80373}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartComponents.E2ETest.Mvc", "test\SmartComponents.E2ETest.Mvc\SmartComponents.E2ETest.Mvc.csproj", "{A0530A9F-C57D-4FC7-89B3-2C0B0005ADF8}"
Expand Down Expand Up @@ -89,10 +87,6 @@ Global
{ED69BAFD-00BC-4086-A34D-466CFCEBB300}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED69BAFD-00BC-4086-A34D-466CFCEBB300}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED69BAFD-00BC-4086-A34D-466CFCEBB300}.Release|Any CPU.Build.0 = Release|Any CPU
{7AE35372-45C8-4ACA-BEEA-265C8A272910}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7AE35372-45C8-4ACA-BEEA-265C8A272910}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7AE35372-45C8-4ACA-BEEA-265C8A272910}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7AE35372-45C8-4ACA-BEEA-265C8A272910}.Release|Any CPU.Build.0 = Release|Any CPU
{3970A925-2753-4E2C-B39C-420C3BC80373}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3970A925-2753-4E2C-B39C-420C3BC80373}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3970A925-2753-4E2C-B39C-420C3BC80373}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -147,7 +141,6 @@ Global
{E139BF4E-3BC2-40FB-80BF-6B5DF2075DA2} = {7A830C0D-7E18-4674-A729-726085D9C0D1}
{1083F54F-D7D4-46FF-9554-278A1655A1D3} = {7A830C0D-7E18-4674-A729-726085D9C0D1}
{ED69BAFD-00BC-4086-A34D-466CFCEBB300} = {B1370349-29FA-49A1-A229-A31F7516A1FF}
{7AE35372-45C8-4ACA-BEEA-265C8A272910} = {B1370349-29FA-49A1-A229-A31F7516A1FF}
{3970A925-2753-4E2C-B39C-420C3BC80373} = {03710CDB-ACD6-4712-95C8-B780EEEFAA29}
{A0530A9F-C57D-4FC7-89B3-2C0B0005ADF8} = {03710CDB-ACD6-4712-95C8-B780EEEFAA29}
{86CB7231-2401-4286-BDD2-778F2F0EBB6C} = {03710CDB-ACD6-4712-95C8-B780EEEFAA29}
Expand Down
7 changes: 6 additions & 1 deletion samples/ExampleBlazorApp/ExampleBlazorApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@

<ItemGroup>
<ProjectReference Include="..\..\src\SmartComponents.AspNetCore\SmartComponents.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\SmartComponents.Inference.OpenAI\SmartComponents.Inference.OpenAI.csproj" />
<ProjectReference Include="..\..\src\SmartComponents.LocalEmbeddings\SmartComponents.LocalEmbeddings.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedSrcRoot)RepoSharedConfigUtil.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
</ItemGroup>

<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
<Import Project="$(RepoRoot)src\SmartComponents.LocalEmbeddings\build\SmartComponents.LocalEmbeddings.targets" />
</Project>
16 changes: 13 additions & 3 deletions samples/ExampleBlazorApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using ExampleBlazorApp.Components;
using SmartComponents.Inference.OpenAI;
using Microsoft.Extensions.AI;
using OpenAI;
using SmartComponents.Inference;
using SmartComponents.LocalEmbeddings;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -12,11 +14,19 @@
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddSmartComponents()
.WithInferenceBackend<OpenAIInferenceBackend>()
.WithAntiforgeryValidation();

builder.Services.AddSingleton<LocalEmbedder>();

// Note: the StartupKey value is just there so the app will start up.
builder.Services.AddSingleton(new OpenAIClient(builder.Configuration["AI:OpenAI:Key"] ?? "StartupKey"));
builder.Services.AddChatClient(services =>
{
var chatClient = new SmartComponentsChatClient(services.GetRequiredService<OpenAIClient>()
.AsChatClient(builder.Configuration["AI:OpenAI:Chat:ModelId"] ?? "gpt-4o-mini"));
return chatClient;
});
builder.Services.AddEmbeddingGenerator(services =>
services.GetRequiredService<OpenAIClient>().AsEmbeddingGenerator(builder.Configuration["AI:OpenAI:Embedding:ModelId"] ?? "text-embedding-3-small"));
var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@

<ItemGroup>
<ProjectReference Include="..\..\src\SmartComponents.AspNetCore\SmartComponents.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\SmartComponents.Inference.OpenAI\SmartComponents.Inference.OpenAI.csproj" />
<ProjectReference Include="..\..\src\SmartComponents.LocalEmbeddings\SmartComponents.LocalEmbeddings.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedSrcRoot)RepoSharedConfigUtil.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
</ItemGroup>

<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
<Import Project="$(RepoRoot)src\SmartComponents.LocalEmbeddings\build\SmartComponents.LocalEmbeddings.targets" />

Expand Down
15 changes: 13 additions & 2 deletions samples/ExampleMvcRazorPagesApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using SmartComponents.Inference.OpenAI;
using Microsoft.Extensions.AI;
using OpenAI;
using SmartComponents.Inference;
using SmartComponents.LocalEmbeddings;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -11,10 +13,19 @@
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddSmartComponents()
.WithInferenceBackend<OpenAIInferenceBackend>()
.WithAntiforgeryValidation();

builder.Services.AddSingleton<LocalEmbedder>();
// Note: the StartupKey value is just there so the app will start up.
builder.Services.AddSingleton(new OpenAIClient(builder.Configuration["AI:OpenAI:Key"] ?? "StartupKey"));
builder.Services.AddChatClient(services =>
{
var chatClient = new SmartComponentsChatClient(services.GetRequiredService<OpenAIClient>()
.AsChatClient(builder.Configuration["AI:OpenAI:Chat:ModelId"] ?? "gpt-4o-mini"));
return chatClient;
});
builder.Services.AddEmbeddingGenerator(services =>
services.GetRequiredService<OpenAIClient>().AsEmbeddingGenerator(builder.Configuration["AI:OpenAI:Embedding:ModelId"] ?? "text-embedding-3-small"));

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection;
using SmartComponents.StaticAssets.Inference;
using Microsoft.Extensions.AI;

namespace Microsoft.AspNetCore.Builder;

internal sealed class DefaultSmartComponentsBuilder(IServiceCollection services) : ISmartComponentsBuilder
{
public ISmartComponentsBuilder WithInferenceBackend<T>(string? name) where T : class, IInferenceBackend
public ISmartComponentsBuilder WithInferenceBackend<T>(string? name) where T : class, IChatClient
{
if (string.IsNullOrEmpty(name))
{
services.AddSingleton<IInferenceBackend, T>();
services.AddSingleton<IChatClient, T>();
}
else
{
services.AddKeyedSingleton<IInferenceBackend, T>(name);
services.AddKeyedSingleton<IChatClient, T>(name);
}

return this;
Expand Down
4 changes: 2 additions & 2 deletions src/SmartComponents.AspNetCore/ISmartComponentsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using SmartComponents.StaticAssets.Inference;
using Microsoft.Extensions.AI;

namespace Microsoft.AspNetCore.Builder;

public interface ISmartComponentsBuilder
{
public ISmartComponentsBuilder WithInferenceBackend<T>(string? name = null) where T : class, IInferenceBackend;
public ISmartComponentsBuilder WithInferenceBackend<T>(string? name = null) where T : class, IChatClient;

public ISmartComponentsBuilder WithAntiforgeryValidation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
<ProjectReference Include="..\SmartComponents.AspNetCore.Components\SmartComponents.AspNetCore.Components.csproj" />
<ProjectReference Include="..\SmartComponents.Inference\SmartComponents.Inference.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using SmartComponents.AspNetCore;
using SmartComponents.Inference;
using SmartComponents.Infrastructure;
using SmartComponents.StaticAssets.Inference;

namespace Microsoft.AspNetCore.Builder;

Expand Down Expand Up @@ -40,7 +40,7 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) =

builder.UseEndpoints(app =>
{
var smartPasteEndpoint = app.MapPost("/_smartcomponents/smartpaste", async ([FromServices] IInferenceBackend inference, HttpContext httpContext, [FromServices] IAntiforgery antiforgery, [FromServices] SmartPasteInference smartPasteInference) =>
var smartPasteEndpoint = app.MapPost("/_smartcomponents/smartpaste", async ([FromServices] IChatClient inference, HttpContext httpContext, [FromServices] IAntiforgery antiforgery, [FromServices] SmartPasteInference smartPasteInference) =>
{
// The rules about whether antiforgery are enabled by default vary across different
// ASP.NET Core versions. To make it consistent, we disable the default enablement on
Expand All @@ -62,7 +62,7 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) =
return result.BadRequest ? Results.BadRequest() : Results.Content(result.Response!);
});

var smartTextAreaEndpoint = app.MapPost("/_smartcomponents/smarttextarea", async ([FromServices] IInferenceBackend inference, HttpContext httpContext, [FromServices] IAntiforgery antiforgery, [FromServices] SmartTextAreaInference smartTextAreaInference) =>
var smartTextAreaEndpoint = app.MapPost("/_smartcomponents/smarttextarea", async ([FromServices] IChatClient inference, HttpContext httpContext, [FromServices] IAntiforgery antiforgery, [FromServices] SmartTextAreaInference smartTextAreaInference) =>
{
if (validateAntiforgery)
{
Expand Down
46 changes: 0 additions & 46 deletions src/SmartComponents.Inference.OpenAI/ApiConfig.cs

This file was deleted.

89 changes: 0 additions & 89 deletions src/SmartComponents.Inference.OpenAI/OpenAIInferenceBackend.cs

This file was deleted.

24 changes: 0 additions & 24 deletions src/SmartComponents.Inference.OpenAI/SelfHostedLlmTransport.cs

This file was deleted.

Loading