-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpenAIService.cs
More file actions
398 lines (333 loc) · 16.8 KB
/
OpenAIService.cs
File metadata and controls
398 lines (333 loc) · 16.8 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Unity.GrantManager.Integrations;
using Unity.Modules.Shared.Http;
using Volo.Abp.DependencyInjection;
namespace Unity.GrantManager.AI
{
public class OpenAIService : IAIService, ITransientDependency
{
private readonly HttpClient _httpClient;
private readonly IConfiguration _configuration;
private readonly ILogger<OpenAIService> _logger;
private readonly ITextExtractionService _textExtractionService;
private readonly IResilientHttpRequest _resilientHttpRequest;
private readonly IEndpointManagementAppService _endpointManagementAppService;
private string? ApiKey => _configuration["AI:OpenAI:ApiKey"];
private string? ApiUrl => _configuration["AI:OpenAI:ApiUrl"] ?? "https://api.openai.com/v1/chat/completions";
private readonly string NoKeyError = "OpenAI API key is not configured";
private readonly string NoSummaryMsg = "No summary generated.";
public OpenAIService(HttpClient httpClient, IConfiguration configuration, ILogger<OpenAIService> logger, ITextExtractionService textExtractionService, IResilientHttpRequest resilientHttpRequest, IEndpointManagementAppService endpointManagementAppService)
{
_httpClient = httpClient;
_configuration = configuration;
_logger = logger;
_textExtractionService = textExtractionService;
_resilientHttpRequest = resilientHttpRequest;
_endpointManagementAppService = endpointManagementAppService;
}
public Task<bool> IsAvailableAsync()
{
if (string.IsNullOrEmpty(ApiKey))
{
_logger.LogWarning("Error: {Message}", NoKeyError);
return Task.FromResult(false);
}
return Task.FromResult(true);
}
//Unity AI method
public async Task<string> GenerateSummaryAsync(string content, string? prompt = null, int maxTokens = 150)
{
if (string.IsNullOrEmpty(ApiKey))
{
_logger.LogWarning("Error: {Message}", NoKeyError);
return "AI analysis not available - service not configured.";
}
_logger.LogDebug("Calling OpenAI with prompt: {Prompt}", content);
try
{
var systemPrompt = prompt ?? "You are a professional grant analyst for the BC Government.";
var requestBody = new
{
messages = new[]
{
new { role = "system", content = systemPrompt },
new { role = "user", content = content }
},
max_tokens = maxTokens,
temperature = 0.3
};
var json = JsonSerializer.Serialize(requestBody);
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("Authorization", ApiKey);
var response = await _httpClient.PostAsync(ApiUrl, httpContent);
var responseContent = await response.Content.ReadAsStringAsync();
_logger.LogDebug("Response: {Response}", responseContent);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("OpenAI API request failed: {StatusCode} - {Content}", response.StatusCode, responseContent);
return "AI analysis failed - service temporarily unavailable.";
}
using var jsonDoc = JsonDocument.Parse(responseContent);
var choices = jsonDoc.RootElement.GetProperty("choices");
if (choices.GetArrayLength() > 0)
{
var message = choices[0].GetProperty("message");
return message.GetProperty("content").GetString() ?? NoSummaryMsg;
}
return NoSummaryMsg;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error generating AI summary");
return "AI analysis failed - please try again later.";
}
}
//AI agent method: Once the AI agent code has hosted domain we can able to use this and update the endpoint management
private async Task<string> GenerateAgenticSummaryAsync(
string userPrompt,
string systemPrompt,
int maxTokens = 150)
{
int numSelfConsistency = 3;
int numCot = 1;
string model = "fast";
double temperature = 0.3;
var aiAgentBaseUri = await _endpointManagementAppService.GetUgmUrlByKeyNameAsync(DynamicUrlKeyNames.AGENTIC_AI);
try
{
var requestBody = new
{
prompt = userPrompt,
system_prompt = systemPrompt ?? "You are a professional grant analyst for the BC Government.",
num_self_consistency = numSelfConsistency,
num_cot = numCot,
model,
temperature,
max_tokens = maxTokens
};
var response = await _resilientHttpRequest.HttpAsync(
HttpMethod.Post,
aiAgentBaseUri!,
requestBody
);
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
_logger.LogError("Agentic AI API request failed: {StatusCode} - {Content}",
response.StatusCode, errorContent);
return "AI analysis failed - service temporarily unavailable.";
}
var responseContent = await response.Content.ReadAsStringAsync();
_logger.LogDebug("Response: {Response}", responseContent);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("OpenAI API request failed: {StatusCode} - {Content}", response.StatusCode, responseContent);
return "AI analysis failed - service temporarily unavailable.";
}
using var jsonDoc = JsonDocument.Parse(responseContent);
var msg = jsonDoc.RootElement.GetProperty("final_answer");
if (jsonDoc.RootElement.TryGetProperty("final_answer", out var finalAnswer) && msg.ValueKind != JsonValueKind.Null)
{
return msg.GetString() ?? NoSummaryMsg;
}
return NoSummaryMsg;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error calling Agentic AI API");
return "AI analysis failed - please try again later.";
}
}
public async Task<string> GenerateAttachmentSummaryAsync(string fileName, byte[] fileContent, string contentType)
{
try
{
var extractedText = await _textExtractionService.ExtractTextAsync(fileName, fileContent, contentType);
string contentToAnalyze;
string prompt;
if (!string.IsNullOrWhiteSpace(extractedText))
{
_logger.LogDebug("Extracted {TextLength} characters from {FileName}", extractedText.Length, fileName);
contentToAnalyze = $"Document: {fileName}\nType: {contentType}\nContent:\n{extractedText}";
prompt = "Please analyze this document and provide a concise summary of its content, purpose, and key information, for use by your fellow grant analysts. It should be 1-2 sentences long and about 46 tokens.";
}
else
{
_logger.LogDebug("No text extracted from {FileName}, analyzing metadata only", fileName);
contentToAnalyze = $"File: {fileName}, Type: {contentType}, Size: {fileContent.Length} bytes";
prompt = "Please analyze this document and provide a concise summary of its content, purpose, and key information, for use by your fellow grant analysts. It should be 1-2 sentences long and about 46 tokens.";
}
return await GenerateSummaryAsync(contentToAnalyze, prompt, 150);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error generating attachment summary for {FileName}", fileName);
return $"AI analysis not available for this attachment ({fileName}).";
}
}
public async Task<string> AnalyzeApplicationAsync(string applicationContent, List<string> attachmentSummaries, string rubric)
{
if (string.IsNullOrEmpty(ApiKey))
{
_logger.LogWarning("{Message}", NoKeyError);
return "AI analysis not available - service not configured.";
}
try
{
var attachmentSummariesText = attachmentSummaries?.Count > 0
? string.Join("\n- ", attachmentSummaries.Select((s, i) => $"Attachment {i + 1}: {s}"))
: "No attachments provided.";
var analysisContent = $@"APPLICATION CONTENT:
{applicationContent}
ATTACHMENT SUMMARIES:
- {attachmentSummariesText}
EVALUATION RUBRIC:
{rubric}
Please analyze this grant application against the provided rubric and return your findings in the following JSON format:
{{
""overall_score"": ""HIGH/MEDIUM/LOW"",
""warnings"": [
{{
""category"": ""Category Name"",
""message"": ""Specific warning message"",
""severity"": ""WARNING""
}}
],
""errors"": [
{{
""category"": ""Category Name"",
""message"": ""Specific error message"",
""severity"": ""ERROR""
}}
],
""recommendations"": [
""Specific recommendation for improvement""
]
}}";
var systemPrompt = @"You are an expert grant application reviewer for the BC Government.
Analyze the provided application against the rubric and identify any issues, missing requirements, or areas of concern.
Be thorough but fair in your assessment. Focus on compliance, completeness, and alignment with program requirements.
Respond only with valid JSON in the exact format requested.";
return await GenerateSummaryAsync(analysisContent, systemPrompt, 1000);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error analyzing application");
return "AI analysis failed - please try again later.";
}
}
public async Task<string> GenerateScoresheetAnswersAsync(string applicationContent, List<string> attachmentSummaries, string scoresheetQuestions)
{
if (string.IsNullOrEmpty(ApiKey))
{
_logger.LogWarning("{Message}", NoKeyError);
return "{}";
}
try
{
var attachmentSummariesText = attachmentSummaries?.Count > 0
? string.Join("\n- ", attachmentSummaries.Select((s, i) => $"Attachment {i + 1}: {s}"))
: "No attachments provided.";
var analysisContent = $@"APPLICATION CONTENT:
{applicationContent}
ATTACHMENT SUMMARIES:
- {attachmentSummariesText}
SCORESHEET QUESTIONS:
{scoresheetQuestions}
Please analyze this grant application and provide appropriate answers for each scoresheet question.
For numeric questions, provide a numeric value within the specified range.
For yes/no questions, provide either 'Yes' or 'No'.
For text questions, provide a concise, relevant response.
For select list questions, choose the most appropriate option from the provided choices.
For text area questions, provide a detailed but concise response.
Base your answers on the application content and attachment summaries provided. Be objective and fair in your assessment.
Return your response as a JSON object where each key is the question ID and the value is the appropriate answer:
{{
""question-id-1"": ""answer-value-1"",
""question-id-2"": ""answer-value-2""
}}
Do not return any markdown formatting, just the JSON by itself";
var systemPrompt = @"You are an expert grant application reviewer for the BC Government.
Analyze the provided application and generate appropriate answers for the scoresheet questions based on the application content.
Be thorough, objective, and fair in your assessment. Base your answers strictly on the provided application content.
Respond only with valid JSON in the exact format requested.";
return await GenerateSummaryAsync(analysisContent, systemPrompt, 2000);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error generating scoresheet answers");
return "{}";
}
}
public async Task<string> GenerateScoresheetSectionAnswersAsync(string applicationContent, List<string> attachmentSummaries, string sectionJson, string sectionName)
{
if (string.IsNullOrEmpty(ApiKey))
{
_logger.LogWarning("{Message}", NoKeyError);
return "{}";
}
try
{
var attachmentSummariesText = attachmentSummaries?.Count > 0
? string.Join("\n- ", attachmentSummaries.Select((s, i) => $"Attachment {i + 1}: {s}"))
: "No attachments provided.";
var analysisContent = $@"APPLICATION CONTENT:
{applicationContent}
ATTACHMENT SUMMARIES:
- {attachmentSummariesText}
SCORESHEET SECTION: {sectionName}
{sectionJson}
Please analyze this grant application and provide appropriate answers for each question in the ""{sectionName}"" section only.
For each question, provide:
1. Your answer based on the application content
2. A brief cited description (1-2 sentences) explaining your reasoning with specific references to the application content
3. A confidence score from 0-100 indicating how confident you are in your answer based on available information
Guidelines for answers:
- For numeric questions, provide a numeric value within the specified range
- For yes/no questions, provide either 'Yes' or 'No'
- For text questions, provide a concise, relevant response
- For select list questions, respond with ONLY the number from the 'number' field (1, 2, 3, etc.) of your chosen option. NEVER return 0 - the lowest valid answer is 1. For example: if you want '(0 pts) No outcomes provided', choose the option where number=1, not 0.
- For text area questions, provide a detailed but concise response
- Base your confidence score on how clearly the application content supports your answer
Return your response as a JSON object where each key is the question ID and the value contains the answer, citation, and confidence:
{{
""question-id-1"": {{
""answer"": ""your-answer-here"",
""citation"": ""Brief explanation with specific reference to application content"",
""confidence"": 85
}},
""question-id-2"": {{
""answer"": ""3"",
""citation"": ""Based on the project budget of $50,000 mentioned in the application, this falls into the medium budget category"",
""confidence"": 90
}}
}}
IMPORTANT FOR SELECT LIST QUESTIONS: If a question has availableOptions like:
[{{""number"":1,""value"":""Low (Under $25K)""}}, {{""number"":2,""value"":""Medium ($25K-$75K)""}}, {{""number"":3,""value"":""High (Over $75K)""}}]
Then respond with ONLY the number (e.g., ""3"" for ""High (Over $75K)""), not the text value.
Do not return any markdown formatting, just the JSON by itself";
var systemPrompt = @"You are an expert grant application reviewer for the BC Government.
Analyze the provided application and generate appropriate answers for the scoresheet section questions based on the application content.
Be thorough, objective, and fair in your assessment. Base your answers strictly on the provided application content.
Always provide citations that reference specific parts of the application content to support your answers.
Be honest about your confidence level - if information is missing or unclear, reflect this in a lower confidence score.
Respond only with valid JSON in the exact format requested.";
return await GenerateSummaryAsync(analysisContent, systemPrompt, 2000);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error generating scoresheet section answers for section {SectionName}", sectionName);
return "{}";
}
}
}
}