-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpTriggerSemanticKernelAskQuestion.cs
207 lines (159 loc) · 7.99 KB
/
HttpTriggerSemanticKernelAskQuestion.cs
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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using Azure;
using Azure.AI.OpenAI;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.Linq;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Memory;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.SemanticKernel.Memory;
using System.Collections.Generic;
namespace Company.Function
{
public static class HttpTriggerSemanticKernelAskQuestion
{
//function you can call to ask a question about a document.
[FunctionName("HttpTriggerSemanticKernelAskQuestion")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try{
string filename = req.Query["filename"];
string question = req.Query["question"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
log.LogInformation(requestBody);
dynamic data = JsonConvert.DeserializeObject(requestBody);
filename = filename ?? data?.filename;
question = question ?? data?.question;
log.LogInformation("filename = " + filename);
log.LogInformation("question = " + question);
IMemoryStore store;
store = new VolatileMemoryStore();
var openAIEndpoint = Environment.GetEnvironmentVariable("OpenAIEndpoint");
var chatModel = Environment.GetEnvironmentVariable("OpenAIChatModel");
var embeddingModel = Environment.GetEnvironmentVariable("OpenAIEmbeddingModel");
var apiKey = Environment.GetEnvironmentVariable("OpenAIKey");
var memoryWithCustomDb = new MemoryBuilder()
.WithAzureTextEmbeddingGenerationService(embeddingModel, openAIEndpoint, apiKey)
.WithMemoryStore(store)
.Build();
string nameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
var docFile = await GetBlobContentAsync(nameWithoutExtension, log);
await MMSemanticMemory.StoreMemoryAsync(memoryWithCustomDb, docFile, log);
var memories = await MMSemanticMemory.SearchMemoryAsync(memoryWithCustomDb, question, log);
//pass relevant memories to OpenAI - this will reduce the tokens for a prompt.
var responseMessage = await AskOpenAIAsync(filename, question, memories, log);
return new OkObjectResult(responseMessage);
}
catch (Exception ex)
{
return new OkObjectResult(ex.Message);
}
}
static async Task<string> AskOpenAIAsync(string filename,
string prompt, IAsyncEnumerable<MemoryQueryResult> memories, ILogger log)
{
log.LogInformation("Ask OpenAI Async A Question");
var openAIEndpoint = Environment.GetEnvironmentVariable("OpenAIEndpoint");
var chatModel = Environment.GetEnvironmentVariable("OpenAIChatModel");
var client = new OpenAIClient(new Uri(openAIEndpoint), new DefaultAzureCredential());
log.LogInformation("Ask OpenAI Async A Question 2");
//remove extension from file name if it is there.
var content = "";
await foreach (MemoryQueryResult memoryResult in memories)
{
log.LogInformation("Ask OpenAI Async A Question 3");
log.LogInformation("Memory Result = " + memoryResult.Metadata.Description);
content += memoryResult.Metadata.Description;
};
var chatCompletionsOptions = new ChatCompletionsOptions()
{
Messages =
{
new ChatMessage(ChatRole.System, @"You are a document answering bot. You will be provided with information from a document, and you are to answer the question based on the content provided. Your are not to make up answers. Use the content provided to answer the question."),
new ChatMessage(ChatRole.User, @"Document = " + content),
new ChatMessage(ChatRole.User, @"Question = " + prompt),
},
};
var completionsResponse = await client.GetChatCompletionsAsync(chatModel, chatCompletionsOptions);
string completion = completionsResponse.Value.Choices[0].Message.Content;
return completion;
}
public static async Task<Dictionary<string, string>> GetBlobContentAsync(string blobName, ILogger log)
{
string connectionString = Environment.GetEnvironmentVariable("StorageConnectionString") ?? "DefaultConnection";
string containerName = Environment.GetEnvironmentVariable("ExtractedContainerName") ?? "DefaultContainer";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobs = containerClient.GetBlobs(prefix: blobName);
log.LogInformation($"Number of blobs {blobs.Count()}");
var content = "";
Dictionary<string, string> docFile = new();
foreach (var blob in blobs)
{
blobName = blob.Name;
BlobClient blobClient = containerClient.GetBlobClient(blobName);
// Open the blob and read its contents.
using (Stream stream = await blobClient.OpenReadAsync())
{
using (StreamReader reader = new StreamReader(stream))
{
content += await reader.ReadToEndAsync();
docFile.Add(blob.Name, content);
}
}
}
return docFile;
}
}
public static class MMSemanticMemory
{
private const string MemoryCollectionName = "mmSemanticMemory";
const string memoryCollectionName = "aboutADoc";
public static async Task StoreMemoryAsync(ISemanticTextMemory memory, Dictionary<string, string> docFile, ILogger log)
{
log.LogInformation("Storing memory...");
var i = 0;
foreach (var entry in docFile)
{
await memory.SaveReferenceAsync(
collection: MemoryCollectionName,
externalSourceName: "BlobStorage",
externalId: entry.Key,
description: entry.Value,
text: entry.Value);
log.LogInformation($" #{++i} saved.");
}
log.LogInformation("\n----------------------");
}
public static async Task<IAsyncEnumerable<MemoryQueryResult>> SearchMemoryAsync(ISemanticTextMemory memory, string query, ILogger log)
{
log.LogInformation("\nQuery: " + query + "\n");
var memoryResults = memory.SearchAsync(MemoryCollectionName, query, limit: 2, minRelevanceScore: 0.5);
int i = 0;
await foreach (MemoryQueryResult memoryResult in memoryResults)
{
log.LogInformation($"Result {++i}:");
log.LogInformation(" URL: : " + memoryResult.Metadata.Id);
log.LogInformation(" Text : " + memoryResult.Metadata.Description);
log.LogInformation(" Relevance: " + memoryResult.Relevance);
}
log.LogInformation("----------------------");
return memoryResults;
}
}
}