11// ******************************************************************************
22// © 2019 Sebastiaan Dammann | damsteen.nl
3- //
3+ //
44// File: : NoteGroupBase.cs
55// Project : Return.Web
66// ******************************************************************************
77
88namespace Return . Web . Components ;
99
1010using System ;
11+ using System . Collections . Generic ;
1112using System . ComponentModel ;
13+ using System . Diagnostics ;
1214using System . Diagnostics . CodeAnalysis ;
1315using System . Threading . Tasks ;
1416using Application . Common . Models ;
1517using Application . NoteGroups . Commands ;
1618using Domain . ValueObjects ;
1719using Microsoft . AspNetCore . Components ;
20+ using Microsoft . Extensions . AI ;
1821using Microsoft . Extensions . Logging ;
1922
2023#nullable disable
@@ -27,6 +30,9 @@ public class NoteGroupBase : MediatorComponent {
2730 [ Inject ]
2831 public ILogger < NoteGroup > Logger { get ; set ; }
2932
33+ [ Inject ]
34+ public IChatClient ChatClient { get ; set ; }
35+
3036 [ Parameter ]
3137 public RetrospectiveNoteGroup Data { get ; set ; } = new RetrospectiveNoteGroup ( ) ;
3238
@@ -67,6 +73,47 @@ protected async Task UpdateTitle() {
6773 }
6874 }
6975
76+ protected async Task AutoSummarizeNoteGroupTitle ( )
77+ {
78+ ChatOptions chatOptions = new ( )
79+ {
80+ Temperature = 0.2f ,
81+ ResponseFormat = ChatResponseFormat . Text ,
82+ } ;
83+
84+ List < ChatMessage > chatMessages =
85+ [
86+ new (
87+ ChatRole . System ,
88+ $@ "A retrospective has been performed. Summarize the sentiment following notes grouped in the '{ this . Container . Lane . Name } ' lane. Use a maximum of 5 words. Don't prefix anything.
89+ Only write a 5 word summary of the notes group. Do not use punctuations. Do not mention 'Notes' - only give a short 5 word title for these notes."
90+ )
91+ ] ;
92+
93+ foreach ( RetrospectiveNote note in this . Data . Notes )
94+ {
95+ chatMessages . Add ( new ( ChatRole . User , $ "Note: { note . Text } ") ) ;
96+ }
97+
98+ long startTime = Stopwatch . GetTimestamp ( ) ;
99+
100+ Logger . LogDebug ( "Invoking AI with {Count} messages" , chatMessages . Count ) ;
101+ try
102+ {
103+ ChatCompletion response = await this . ChatClient . CompleteAsync ( chatMessages , chatOptions ) ;
104+ this . Data . Title = response . ToString ( ) ;
105+ Logger . LogTrace ( "Total generated summary: {RawSummary}" , this . Data . Title ) ;
106+ }
107+ catch ( Exception ex )
108+ {
109+ Logger . LogError ( ex , "Error invoking AI" ) ;
110+ }
111+
112+ Logger . LogDebug ( "Completed AI invocation in {Elapsed}" , Stopwatch . GetElapsedTime ( startTime ) ) ;
113+
114+ await this . UpdateTitle ( ) ;
115+ }
116+
70117 protected async Task DeleteNoteGroup ( ) {
71118 try {
72119 await this . Mediator . Send ( new DeleteNoteGroupCommand ( this . RetroId . StringId , this . Data . Id ) ) ;
0 commit comments