-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathChatHost.cs
More file actions
293 lines (244 loc) · 13.1 KB
/
ChatHost.cs
File metadata and controls
293 lines (244 loc) · 13.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using Microsoft.GS.DPS.Model.ChatHost;
using Microsoft.GS.DPS.Storage.ChatSessions.Entities;
using Microsoft.GS.DPS.Storage.ChatSessions;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Text.Json;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Microsoft.KernelMemory.Context;
namespace Microsoft.GS.DPS.API
{
internal static class JsonSerializationOptionsCache
{
static internal JsonSerializerOptions JsonSerializationOptionsIgnoreCase { get; set; } = new JsonSerializerOptions() {
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
}
public class ChatHost(MemoryWebClient kmClient, Kernel kernel, API.KernelMemory kernelMemory, ChatSessionRepository chatSessions)
{
private MemoryWebClient _kmClient = kmClient;
private Kernel _kernel = kernel;
private API.KernelMemory _kernelMemory = kernelMemory;
private IChatCompletionService _chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
private ChatSessionRepository _chatSessions = chatSessions;
private static string s_systemPrompt;
private static string s_assistancePrompt;
private static string s_additionalPrompt;
string sessionId = string.Empty;
ChatHistory chatHistory = null;
ChatSession chatSession = null;
//static constructor to load the system prompt text at once
static ChatHost()
{
//Set Location of the System Prompt under running Assembly directory location.
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var assemblyDirectory = System.IO.Path.GetDirectoryName(assemblyLocation);
// binding assembly directory with file path (Prompts/Chat_SystemPrompt.txt)
var systemPromptFilePath = System.IO.Path.Combine(assemblyDirectory, "Prompts", "Chat_SystemPrompt.txt");
ChatHost.s_systemPrompt = System.IO.File.ReadAllText(systemPromptFilePath);
ChatHost.s_assistancePrompt =
@"
Hello, I can provide you with knowledge based on registered documents and contents.
Please feel free to ask me any questions related to those documents and contents.
However, please note that I cannot provide answers for forecasting, prediction, or projections.
";
// ChatHost.s_additionalPrompt = """
// If available, please include the name of the referencing document and its page number in your responses.
// Show the detail as much as possible in your answers.
// Data should be provided in the form of a table or a list.
// Do not use your own or general knowledge to formulate an answer.
// You should choose one of actions
// - Make an answer with contents and recent chat history
// - List up chatting history between user and you.
// """;
ChatHost.s_additionalPrompt = "\n You should add citation (Document name and Page) per each every your answer statements.";
}
private async Task<ChatSession> makeNewSession(string? chatSessionId)
{
if(string.IsNullOrEmpty(chatSessionId))
{
this.sessionId = Guid.NewGuid().ToString();
}
else
{
this.sessionId = chatSessionId;
}
//Create New Chat History
this.chatHistory = new ChatHistory();
//Add the system prompt to the chat history
this.chatHistory.AddSystemMessage(ChatHost.s_systemPrompt);
//Create a new ChatSession Entity for Saving into Azure Cosmos
return new ChatSession()
{
SessionId = this.sessionId, // New Session ID
StartTime = DateTime.UtcNow // Session Created Time
};
}
private async IAsyncEnumerable<string> GetAnswerWords(string answer)
{
var words = answer.Split(' ');
foreach (var word in words)
{
yield return word;
await Task.Delay(30);
}
}
public async Task<ChatResponseAsync> ChatAsync(ChatRequest chatRequest)
{
var chatResponse = await Chat(chatRequest);
return new ChatResponseAsync()
{
ChatSessionId = chatResponse.ChatSessionId,
AnswerWords = GetAnswerWords(chatResponse.Answer),
Answer = chatResponse.Answer,
DocumentIds = chatResponse.DocumentIds,
SuggestingQuestions = chatResponse.SuggestingQuestions
};
}
public async Task<ChatResponse> Chat(ChatRequest chatRequest)
{
this.chatSession = await _chatSessions.GetSessionAsync(chatRequest.ChatSessionId);
//just in case there is no chatSession in persistant storage
//create a new chatSession
if (this.chatSession == null) this.chatSession = await makeNewSession(chatRequest.ChatSessionId);
//Rehydrate the ChatHistory from the ChatSession.ChatHistoryJson Field.
//Due to BSON Deserializer issue, we are using JSON Deserializer
if (this.chatSession != null && !String.IsNullOrEmpty(this.chatSession.ChatHistoryJson))
{
ChatHistory deserializedChatHistory = JsonSerializer.Deserialize<ChatHistory>(chatSession.ChatHistoryJson);
this.chatHistory = deserializedChatHistory;
}
if (chatRequest.DocumentIds == null) chatRequest.DocumentIds = Array.Empty<string>();
//define custom context for asking the question (max token)
RequestContext context = new RequestContext()
{
Arguments = new Dictionary<string, object?>()
{
{ Microsoft.KernelMemory.Constants.CustomContext.Rag.Temperature, 0},
{ Microsoft.KernelMemory.Constants.CustomContext.Rag.MaxTokens, 10000 }
}
};
//Calculate prompt token size of prompt for the question and additional prompt with using Tiktoken
//var tokenSize = chatRequest.Question.Length + ChatHost.s_additionalPrompt.Length;
//Get the answer from the Kernel Memory
var answer = await _kernelMemory.Ask(chatRequest.Question + ChatHost.s_additionalPrompt, chatRequest.DocumentIds, context: context);
answer.Result = System.Text.Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(answer.Result));
Console.WriteLine($"Question: {answer.Question}");
Console.WriteLine($"Answer: {answer.Result}");
//UpdateAsync System Prompt with the answer
//replace {$answer} place holder in s_systemPrompt with the actual answer
this.chatHistory[0].Content = s_systemPrompt.Replace("{$answer}", answer.Result);
this.chatHistory[0].Role = AuthorRole.System;
//Add User Message to the Chat History
this.chatHistory.AddUserMessage("Currently Selected Documents are as below: \n" + string.Join("\n", answer.RelevantSources.Select(x => x.SourceName)) + "\n" + chatRequest.Question + ChatHost.s_additionalPrompt);
////Check History Rows and remove the oldest row if it exceeds max history count
var historyCount = 10;
// System prompt and first assistant prompt will be always there
if (this.chatHistory.Count > historyCount + 2)
{
//Remove the oldest rows - Question and Answer
this.chatHistory.RemoveRange(2, this.chatHistory.Count - (historyCount));
}
//UpdateAsync PromptExecutionSettings with the temperature
var executionSettings = new PromptExecutionSettings()
{
ExtensionData = new Dictionary<string, object>
{
{ "Temperature", 0.5 },
{ "MaxTokens", 16384 }
}
};
ChatMessageContent returnedChatMessageContent;
try
{
//Get Response from ChatCompletionService
returnedChatMessageContent = await _chatCompletionService.GetChatMessageContentAsync(chatHistory, executionSettings);
}
catch (HttpOperationException ex) when (ex.Message.Contains("content_filter", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"Exception Message: {ex.Message}");
//if content filter triggered providing fallback response
returnedChatMessageContent = new ChatMessageContent
{
Content = "Sorry, your request couldn't be processed as it may contain sensitive or restricted content. Please rephrase your query and try again."
};
}
catch(Exception ex)
{
Console.WriteLine($"unexpected error: {ex.Message}");
returnedChatMessageContent = new ChatMessageContent
{
Content = "An error occured while processing request, try again"
};
}
if (returnedChatMessageContent == null)
{
returnedChatMessageContent = new ChatMessageContent
{
Content = "No response"
};
}
//Just in case returnedChatMessageContent.Content has ```json ``` block, Strip it first
if (returnedChatMessageContent.Content != null && returnedChatMessageContent.Content.Contains("```json", StringComparison.OrdinalIgnoreCase))
returnedChatMessageContent.Content = returnedChatMessageContent.Content.Replace("```json", "").Replace("```", "");
Answer answerObject = null;
try
{
if (returnedChatMessageContent != null && !string.IsNullOrWhiteSpace(returnedChatMessageContent.Content))
{
//Adding for non English Response.
returnedChatMessageContent.Content = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.UTF8.GetBytes(returnedChatMessageContent.Content));
answerObject = JsonSerializer.Deserialize<Answer>(returnedChatMessageContent.Content, options: new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});
}
else
{
throw new NullReferenceException("returnedChatMessageContent or its Content is null.");
}
}
catch
{
answerObject = new Answer()
{
Response = returnedChatMessageContent.Content,
Followings = new string[] { }
};
}
if (returnedChatMessageContent.Content.Contains("I don't have enough information to provide an answer.", StringComparison.OrdinalIgnoreCase) ||
returnedChatMessageContent.Content.Contains("No Information", StringComparison.OrdinalIgnoreCase))
{
answerObject.Response = "I don't have enough information to provide an answer. Would you please rephrase your question and ask me again?";
}
//Add Assistant Message and Data to the Chat History
this.chatHistory.AddAssistantMessage($"this is the content for creating answer :\n{answer.Result}");
this.chatHistory.AddAssistantMessage(returnedChatMessageContent.Content);
//UpdateAsync last message updated Time
this.chatSession.EndTime = DateTime.UtcNow;
//Hydrate Chathistory back to ChatSession.ChatHistoryJson Field
this.chatSession.ChatHistoryJson = JsonSerializer.Serialize<ChatHistory>(chatHistory);
//UpdateAsync ChatSession Entity
await _chatSessions.UpdateSessionAsync(this.chatSession);
return new ChatResponse()
{
ChatSessionId = this.chatSession.SessionId,
Answer = answerObject.Response,
DocumentIds = chatRequest.DocumentIds,
SuggestingQuestions = answerObject.Followings,
Keywords = answerObject.Keywords
};
}
}
}