-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Integrating .NET AI Chat Template with ABP Framework #22884
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
Merged
hikalkan
merged 7 commits into
dev
from
Integrating-.NET-AI-Chat-Template-with-ABP-Framework
May 30, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f890e8
Integrating .NET AI Chat Template with ABP Framework
maliming 61fd380
Optimised images with calibre/image-actions
github-actions[bot] 0d72024
Update cover.png
maliming 757c72e
Update docs/en/Community-Articles/2025-05-11-AI-Chat/POST.md
maliming 0ce00c3
Update POST.md
maliming 46b4fc9
Add Source code section.
maliming 5cfed3d
Update POST.md
maliming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-4.75 KB
(83%)
docs/en/Community-Articles/2025-04-25-AI-Comment-Summarization/summary-card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# Integrating .NET AI Chat Template with ABP Framework | ||
|
||
This article demonstrates how to integrate the [.NET AI Chat Template](https://devblogs.microsoft.com/dotnet/announcing-dotnet-ai-template-preview2/) into an ABP Framework application, enabling powerful AI chat capabilities in your ABP-based solution. | ||
|
||
 | ||
|
||
## Step 1: Create a New ABP Project | ||
|
||
First, let's create a new [single-layer Blazor Server project](https://abp.io/docs/latest/solution-templates/single-layer-web-application/overview) named `AbpAiChat` using ABP Studio, You can also use the following ABP CLI command to create the project: | ||
|
||
```bash | ||
abp new AbpAiChat -t app-nolayers --ui-framework blazor-server --use-open-source-template | ||
``` | ||
|
||
## Step 2: Integrate AI Chat Template | ||
|
||
The integration process involves copying and adapting the .NET AI Chat Template code into our ABP project. The template code is already included in our sample project, so you don't need to install it separately. | ||
|
||
### 2.1 Project Structure Changes | ||
|
||
1. Copy Blazor components to the `Components` folder | ||
2. Copy AI service classes to the `Services` folder | ||
3. Add required entities(`IngestedDocument`, `IngestedRecord`) to the `AbpAiChatDbContext` and add new migration | ||
4. Copy frontend resources to the `wwwroot` folder | ||
5. Adjust some styles to capatible with the ABP theme | ||
|
||
### 2.2 Required NuGet Packages | ||
|
||
Add the following packages to `AbpAiChat.csproj`: | ||
|
||
```xml | ||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.3-preview.1.25230.7" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" /> | ||
<PackageReference Include="Microsoft.Extensions.AI" Version="9.4.3-preview.1.25230.7" /> | ||
<PackageReference Include="Microsoft.SemanticKernel.Core" Version="1.47.0" /> | ||
<PackageReference Include="PdfPig" Version="0.1.9" /> | ||
<PackageReference Include="System.Linq.Async" Version="6.0.1" /> | ||
``` | ||
|
||
### 2.3 Configure AI Services | ||
|
||
Add the following configuration to `AbpAiChatModule.cs`: | ||
|
||
```csharp | ||
private void ConfigureAi(ServiceConfigurationContext context) | ||
{ | ||
var credential = new ApiKeyCredential(context.Services.GetConfiguration()["GitHubToken"] ?? throw new InvalidOperationException("Missing configuration: GitHubToken. See the README for details.")); | ||
var openAiOptions = new OpenAIClientOptions() | ||
{ | ||
Endpoint = new Uri("https://models.inference.ai.azure.com") | ||
}; | ||
|
||
var ghModelsClient = new OpenAIClient(credential, openAiOptions); | ||
var chatClient = ghModelsClient.GetChatClient("gpt-4o-mini").AsIChatClient(); | ||
var embeddingGenerator = ghModelsClient.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator(); | ||
|
||
var vectorStore = new JsonVectorStore(Path.Combine(AppContext.BaseDirectory, "vector-store")); | ||
|
||
context.Services.AddSingleton<IVectorStore>(vectorStore); | ||
context.Services.AddScoped<DataIngestor>(); | ||
context.Services.AddSingleton<SemanticSearch>(); | ||
context.Services.AddChatClient(chatClient).UseFunctionInvocation().UseLogging(); | ||
context.Services.AddEmbeddingGenerator(embeddingGenerator); | ||
|
||
context.Services.Configure<AbpAspNetCoreContentOptions>(options => | ||
{ | ||
options.ContentTypeMaps.Add(".mjs", "application/javascript"); | ||
}); | ||
} | ||
``` | ||
|
||
The `ConfigureAi` method is called in the `ConfigureServices` method of `AbpAiChatModule`. It sets up the AI services, including the OpenAI client, chat client, embedding generator, and vector store. | ||
|
||
### 2.4 Configure GitHub Token | ||
|
||
Add your GitHub Personal Access Token to `appsettings.json`: | ||
|
||
```json | ||
{ | ||
"GitHubToken": "your-github-token" | ||
} | ||
``` | ||
|
||
You can obtain your token from [GitHub Personal Access Tokens](https://github.com/settings/personal-access-tokens). | ||
|
||
## Step 3: Add Custom AI Functionality | ||
|
||
Let's add a custom AI function to retrieve the current user's information. Update the `Chat.razor` component: | ||
|
||
```csharp | ||
chatOptions.Tools = | ||
[ | ||
AIFunctionFactory.Create(SearchAsync), | ||
AIFunctionFactory.Create(GetWeather), | ||
AIFunctionFactory.Create(GetCurrentUserInfo) | ||
]; | ||
|
||
[Description("Get current user information")] | ||
private Task<string> GetCurrentUserInfo() | ||
{ | ||
return Task.FromResult(CurrentUser.IsAuthenticated ? | ||
$"UserId: {CurrentUser.Id}, Name: {CurrentUser.UserName}, Email: {CurrentUser.Email}, Roles: {string.Join(", ", CurrentUser.Roles)}" : | ||
"No user information available."); | ||
} | ||
``` | ||
|
||
## Step 4: Add Navigation | ||
|
||
Add a `Chat` menu item in `AbpAiChatMenuContributor` to navigate to the AI Chat component. | ||
|
||
## Running the Application | ||
|
||
After completing the integration, you can run the application and access the AI chat functionality. The chat interface allows you to: | ||
|
||
- Get weather information | ||
- Ask questions about PDF content | ||
- Retrieve current user information | ||
- And more! | ||
|
||
 | ||
|
||
## Conclusion | ||
|
||
This integration demonstrates how to leverage the power of AI in your ABP Framework applications. The .NET AI Chat Template provides a solid foundation for building intelligent chat interfaces, and ABP Framework makes it more powerful. | ||
|
||
## References | ||
|
||
- [ABP Single Layer Solution](https://abp.io/docs/latest/solution-templates/single-layer-web-application/overview) | ||
- [.NET AI Template Documentation](https://devblogs.microsoft.com/dotnet/announcing-dotnet-ai-template-preview1/) | ||
- [GitHub Personal Access Tokens Guide](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) | ||
|
||
## Source Code | ||
|
||
- [AbpAiChat Source Code](https://github.com/abpframework/abp-samples/tree/master/AIChat) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-129 KB
(92%)
docs/en/ui-themes/lepton-x/images/account-layout-background-style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.