File tree Expand file tree Collapse file tree 6 files changed +35
-84
lines changed Expand file tree Collapse file tree 6 files changed +35
-84
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright (c) Microsoft. All rights reserved.
2
+
3
+ namespace SharedWebComponents . Models ;
4
+
5
+ internal class StreamingMessage
6
+ {
7
+ public string Type { get ; set ; } = "" ;
8
+ public object ? Content { get ; set ; }
9
+ }
Original file line number Diff line number Diff line change 3
3
namespace SharedWebComponents . Pages ;
4
4
using Microsoft . AspNetCore . SignalR . Client ;
5
5
using System . Text . Json ;
6
+ using SharedWebComponents . Models ;
6
7
7
8
public sealed partial class Chat : IAsyncDisposable
8
9
{
@@ -250,28 +251,6 @@ private async Task OnAskQuestionAsync(string question)
250
251
await OnAskClickedAsync ( ) ;
251
252
}
252
253
253
- private async Task OnStreamingToggled ( bool isEnabled )
254
- {
255
- _useStreaming = isEnabled ;
256
- if ( isEnabled )
257
- {
258
- await ConnectToHub ( ) ;
259
- }
260
- else
261
- {
262
- await DisconnectFromHub ( ) ;
263
- }
264
- }
265
-
266
- private async Task DisconnectFromHub ( )
267
- {
268
- if ( _hubConnection is not null )
269
- {
270
- await _hubConnection . DisposeAsync ( ) ;
271
- _hubConnection = null ;
272
- }
273
- }
274
-
275
254
private void UpdateAnswerInMap ( string answer , string ? citationBaseUrl = null )
276
255
{
277
256
var currentResponse = _questionAndAnswerMap [ _currentQuestion ] ;
@@ -354,10 +333,4 @@ private void UpdateImagesInMap(SupportingImageRecord[] images)
354
333
} ) ;
355
334
}
356
335
}
357
- }
358
-
359
- internal class StreamingMessage
360
- {
361
- public string Type { get ; set ; } = "" ;
362
- public object ? Content { get ; set ; }
363
- }
336
+ }
Original file line number Diff line number Diff line change @@ -16,8 +16,6 @@ internal static WebApplication MapApi(this WebApplication app)
16
16
// Long-form chat w/ contextual history endpoint
17
17
api . MapPost ( "chat" , OnPostChatAsync ) ;
18
18
19
- api . MapPost ( "chat/stream" , OnPostChatStreamingAsync ) ;
20
-
21
19
// Upload a document
22
20
api . MapPost ( "documents" , OnPostDocumentAsync ) ;
23
21
@@ -93,24 +91,6 @@ private static async Task<IResult> OnPostChatAsync(
93
91
return Results . BadRequest ( ) ;
94
92
}
95
93
96
- private static async Task < IResult > OnPostChatStreamingAsync (
97
- ChatRequest request ,
98
- ReadRetrieveReadChatService chatService ,
99
- CancellationToken cancellationToken )
100
- {
101
- if ( request is { History . Length : > 0 } )
102
- {
103
- await chatService . ReplyStreamingAsync (
104
- request . History ,
105
- request . Overrides ,
106
- request . ConnectionId ,
107
- cancellationToken ) ;
108
-
109
- return TypedResults . Ok ( ) ;
110
- }
111
- return Results . BadRequest ( ) ;
112
- }
113
-
114
94
private static async Task < IResult > OnPostDocumentAsync (
115
95
[ FromForm ] IFormFileCollection files ,
116
96
[ FromServices ] AzureBlobStorageService service ,
Original file line number Diff line number Diff line change 1
1
using Microsoft . AspNetCore . SignalR ;
2
+ using System ;
2
3
3
4
namespace MinimalApi . Hubs ;
4
5
@@ -9,32 +10,14 @@ public class ChatHub : Hub
9
10
10
11
public ChatHub ( ReadRetrieveReadChatService chatService )
11
12
{
12
- _chatService = chatService ;
13
+ _chatService = chatService ?? throw new ArgumentNullException ( nameof ( chatService ) ) ;
13
14
}
14
15
15
16
public async Task SendChatRequest ( ChatRequest request )
16
17
{
17
- try
18
- {
19
- request . ConnectionId = Context . ConnectionId ;
20
- await _chatService . ReplyStreamingAsync (
21
- request . History ,
22
- request . Overrides ,
23
- request . ConnectionId ) ;
24
- }
25
- catch
26
- {
27
- throw ;
28
- }
29
- }
30
-
31
- public override async Task OnConnectedAsync ( )
32
- {
33
- await base . OnConnectedAsync ( ) ;
34
- }
35
-
36
- public override async Task OnDisconnectedAsync ( Exception ? exception )
37
- {
38
- await base . OnDisconnectedAsync ( exception ) ;
18
+ await _chatService . ReplyStreamingAsync (
19
+ request . History ,
20
+ request . Overrides ,
21
+ Context . ConnectionId ) ;
39
22
}
40
23
}
Original file line number Diff line number Diff line change 23
23
{
24
24
static string ? GetEnvVar ( string key ) => Environment . GetEnvironmentVariable ( key ) ;
25
25
26
- var endpoint = GetEnvVar ( "AZURE_SIGNALR_ENDPOINT" )
27
- ?? throw new InvalidOperationException ( "AZURE_SIGNALR_ENDPOINT is not configured" ) ;
28
- var clientId = GetEnvVar ( "AZURE_CLIENT_ID" )
29
- ?? throw new InvalidOperationException ( "AZURE_CLIENT_ID is not configured" ) ;
30
-
31
- options . Endpoints = new [ ]
32
- {
33
- new ServiceEndpoint ( new Uri ( endpoint ) , new ManagedIdentityCredential ( clientId ) )
34
- } ;
26
+ // Try to get endpoint and client ID first
27
+ var endpoint = GetEnvVar ( "AZURE_SIGNALR_ENDPOINT" ) ;
28
+ var clientId = GetEnvVar ( "AZURE_CLIENT_ID" ) ;
29
+
30
+ if ( endpoint != null && clientId != null )
31
+ {
32
+ options . Endpoints = new [ ]
33
+ {
34
+ new ServiceEndpoint ( new Uri ( endpoint ) , new ManagedIdentityCredential ( clientId ) )
35
+ } ;
36
+ }
37
+ else
38
+ {
39
+ // Fall back to connection string
40
+ var connectionString = GetEnvVar ( "AZURE_SIGNALR_CONNECTION_STRING" )
41
+ ?? throw new InvalidOperationException ( "Neither managed identity credentials nor connection string are configured for Azure SignalR" ) ;
42
+ options . ConnectionString = connectionString ;
43
+ }
35
44
} ) ;
36
45
37
46
if ( builder . Environment . IsDevelopment ( ) )
Original file line number Diff line number Diff line change @@ -10,7 +10,4 @@ public record class ChatRequest(
10
10
) : ApproachRequest ( Approach . RetrieveThenRead )
11
11
{
12
12
public string ? LastUserQuestion => History ? . Last ( m => m . Role == "user" ) ? . Content ;
13
-
14
- [ JsonPropertyName ( "connectionId" ) ]
15
- public string ? ConnectionId { get ; set ; }
16
13
}
You can’t perform that action at this time.
0 commit comments