-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHttpTriggerOpenAISdkAskQuestion.cs
130 lines (97 loc) · 4.78 KB
/
HttpTriggerOpenAISdkAskQuestion.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
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 Company.Function.Models;
namespace Company.Function
{
public static class HttpTriggerAskAboutADoc
{
//function you can call to ask a question about a document.
[FunctionName("HttpTriggerOpenAiSdkAskQuestion")]
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);
string nameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
var responseMessage = await AskOpenAIAsync(nameWithoutExtension, question, log);
return new OkObjectResult(responseMessage);
}
catch (Exception ex)
{
return new OkObjectResult(ex.Message);
}
}
static async Task<string> AskOpenAIAsync(string filename, string prompt, 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");
var content = await GetBlobContentAsync(filename, log);
//log.LogInformation(content);
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, @"Content = " + 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<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 = "";
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))
{
var processedFile = JsonConvert.DeserializeObject<ProcessedFile>(await reader.ReadToEndAsync());
content += processedFile.Content;
}
}
}
return content;
}
}
}