-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathChat.cs
More file actions
72 lines (61 loc) · 2.29 KB
/
Chat.cs
File metadata and controls
72 lines (61 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.GS.DPS.Model.ChatHost;
using Microsoft.GS.DPS.API;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.HttpResults;
using System.Text;
using System.Text.Json;
namespace Microsoft.GS.DPSHost.API
{
public class Chat
{
public static void AddAPIs(WebApplication app)
{
//RegisterAsync the chat API
app.MapPost("/chat", async (ChatRequest request,
ChatRequestValidator validator,
ChatHost chatHost) =>
{
if (!validator.Validate(request).IsValid)
{
return Results.BadRequest();
}
var result = await chatHost.Chat(request);
return Results.Ok<ChatResponse>(result);
})
.DisableAntiforgery();
///<summary>
//RegisterAsync the chat API
//</summary>
app.MapPost("/chatAsync", async (HttpContext ctx,
ChatRequest request,
ChatRequestValidator validator,
ChatHost chatHost) =>
{
if (!validator.Validate(request).IsValid)
{
return Results.BadRequest();
}
ctx.Response.ContentType = "text/plain";
//Make a response as a stream
var result = chatHost.ChatAsync(request).Result;
//Create a dynamic object to store the response
var response = new
{
result.ChatSessionId,
result.DocumentIds,
result.SuggestingQuestions
};
//Add the response to the header
ctx.Response.Headers.Add("RESPONSE", JsonSerializer.Serialize(response));
// Stream the response
await foreach (var word in result.AnswerWords)
{
await ctx.Response.WriteAsync(word);
await ctx.Response.WriteAsync(" ");
}
return Results.Ok();
})
.DisableAntiforgery();
}
}
}