|
2 | 2 | using System.Collections.Concurrent; |
3 | 3 | using System.IO; |
4 | 4 | using System.Threading.Tasks; |
5 | | -using DevExpress.AIIntegration.OpenAI.Services; |
6 | | -using DevExpress.AIIntegration.Services.Assistant; |
7 | 5 | using Microsoft.AspNetCore.Hosting; |
| 6 | +using DevExpress.AIIntegration.Services.Assistant; |
8 | 7 |
|
9 | 8 | namespace ReportingApp.Services { |
10 | 9 | public class AIAssistantProvider : IAIAssistantProvider { |
| 10 | + const string ASSISTANT_NOT_FOUND_ERROR = "Assistant not found"; |
| 11 | + const string DOCUMENTATION_FILE_NAME = "documentation.pdf"; |
| 12 | + const string DOCUMENT_ASSISTANT_PROMPT = "You are a data analysis assistant. Your task is to read information from PDF files and provide users with accurate data-driven answers based on the contents of these files. \n Key Responsibilities: \n - Perform data analysis, including data summaries, calculations, filtering, and trend identification.\n - Clearly explain your analysis process to ensure users understand how you reached your conclusions.\n - Provide precise and accurate responses strictly based on data in the file.\n - If the requested information is not available in the provided file's content, state: \"The requested information cannot be found in the data provided.\"\n - Avoid giving responses when data is insufficient for a reliable answer.\n - Ask clarifying questions when a user’s query is unclear or lacks detail.\n - Your primary goal is to deliver helpful insights that directly address user questions. Do not make assumptions or infer details not supported by data. Respond in plain text only, without sources, footnotes, or annotations.\n Avoid giving information about provided file name, assistants' IDs and other internal data"; |
| 13 | + const string USER_ASSISTANT_PROMPT = "You are a user interface assistant (you help people use a software program). Your role is to read information from documentation files in PDF format. You assist users by providing accurate answers to their questions based on information from these files. \r\n\r\nTasks:\r\nExtract relevant information from PDF documentation to answer user questions.\r\nClearly explain your reasoning process and give step by step solutions to ensure users understand how you arrived at your answers.\r\nAlways provide precise and accurate information based on content from the documentation file.\r\nIf you cannot find an answer based on provided documentation, explicitly state: 'The requested information cannot be found in documentation provided.'\r\n Respond in plain text only, without markdown, sources, footnotes, or annotations."; |
| 14 | + |
11 | 15 | private readonly IAIAssistantFactory assistantFactory; |
12 | 16 | private readonly IWebHostEnvironment environment; |
| 17 | + private readonly AIAssistantCreator assistantCreator; |
13 | 18 |
|
14 | 19 | private ConcurrentDictionary<string, IAIAssistant> Assistants { get; set; } = new (); |
15 | | - public AIAssistantProvider(IAIAssistantFactory assistantFactory, IWebHostEnvironment environment) { |
| 20 | + |
| 21 | + private async Task<string> CreateAssistant(Stream data, string fileName, string prompt) { |
| 22 | + (string assistantId, string threadId) = await assistantCreator.CreateAssistantAsync(data, fileName, prompt); |
| 23 | + |
| 24 | + IAIAssistant assistant = await assistantFactory.GetAssistant(assistantId, threadId); |
| 25 | + await assistant.InitializeAsync(); |
| 26 | + |
| 27 | + string assistantName = Guid.NewGuid().ToString(); |
| 28 | + Assistants.TryAdd(assistantName, assistant); |
| 29 | + |
| 30 | + return assistantName; |
| 31 | + } |
| 32 | + |
| 33 | + public AIAssistantProvider(IAIAssistantFactory assistantFactory, IWebHostEnvironment environment, AIAssistantCreator assistantCreator) { |
16 | 34 | this.assistantFactory = assistantFactory; |
17 | 35 | this.environment = environment; |
| 36 | + this.assistantCreator = assistantCreator; |
18 | 37 | } |
19 | | - async Task LoadDocumentation(IAIAssistant assistant, string prompt) { |
20 | | - var dirPath = Path.Combine(environment.ContentRootPath, "Data"); |
21 | | - var filePath = Path.Combine(dirPath, "documentation.pdf"); |
22 | | - |
23 | | - using(FileStream stream = File.OpenRead(filePath)) { |
24 | | - await assistant.InitializeAsync(new OpenAIAssistantOptions("documentation.pdf", stream, prompt)); |
25 | | - } |
| 38 | + public async Task<string> CreateDocumentAssistant(Stream data) { |
| 39 | + return await CreateAssistant(data, Guid.NewGuid().ToString() + ".pdf", DOCUMENT_ASSISTANT_PROMPT); |
26 | 40 | } |
27 | | - string GetPrompt(AssistantType assistantType) { |
28 | | - switch(assistantType) { |
29 | | - case AssistantType.UserAssistant: |
30 | | - return "You are a user interface assistant (you help people use a software program). Your role is to read information from documentation files in PDF format. You assist users by providing accurate answers to their questions based on information from these files. \r\n\r\nTasks:\r\nExtract relevant information from PDF documentation to answer user questions.\r\nClearly explain your reasoning process and give step by step solutions to ensure users understand how you arrived at your answers.\r\nAlways provide precise and accurate information based on content from the documentation file.\r\nIf you cannot find an answer based on provided documentation, explicitly state: 'The requested information cannot be found in documentation provided.'\r\n Respond in plain text only, without markdown, sources, footnotes, or annotations."; |
31 | | - case AssistantType.DocumentAssistant: |
32 | | - return "You are a data analysis assistant. Your task is to read information from PDF files and provide users with accurate data-driven answers based on the contents of these files. \n Key Responsibilities: \n - Perform data analysis, including data summaries, calculations, filtering, and trend identification.\n - Clearly explain your analysis process to ensure users understand how you reached your conclusions.\n - Provide precise and accurate responses strictly based on data in the file.\n - If the requested information is not available in the provided file's content, state: \"The requested information cannot be found in the data provided.\"\n - Avoid giving responses when data is insufficient for a reliable answer.\n - Ask clarifying questions when a user’s query is unclear or lacks detail.\n - Your primary goal is to deliver helpful insights that directly address user questions. Do not make assumptions or infer details not supported by data. Respond in plain text only, without sources, footnotes, or annotations.\n Avoid giving information about provided file name, assistants' IDs and other internal data"; |
33 | | - default: |
34 | | - return ""; |
35 | | - } |
| 41 | + public async Task<string> CreateUserAssistant() { |
| 42 | + string dirPath = Path.Combine(environment.ContentRootPath, "Data"); |
| 43 | + string filePath = Path.Combine(dirPath, DOCUMENTATION_FILE_NAME); |
| 44 | + |
| 45 | + using (FileStream stream = File.OpenRead(filePath)) |
| 46 | + return await CreateAssistant(stream, DOCUMENTATION_FILE_NAME, USER_ASSISTANT_PROMPT); |
36 | 47 | } |
37 | 48 | public void DisposeAssistant(string assistantName) { |
38 | 49 | if(Assistants.TryRemove(assistantName, out IAIAssistant assistant)) { |
39 | 50 | assistant.Dispose(); |
40 | 51 | } else { |
41 | | - throw new Exception("Assistant not found"); |
| 52 | + throw new Exception(ASSISTANT_NOT_FOUND_ERROR); |
42 | 53 | } |
43 | 54 | } |
44 | 55 | public IAIAssistant GetAssistant(string assistantName) { |
45 | 56 | if(!string.IsNullOrEmpty(assistantName) && Assistants.TryGetValue(assistantName, out var assistant)) { |
46 | 57 | return assistant; |
47 | 58 | } else { |
48 | | - throw new Exception("Assistant not found"); |
| 59 | + throw new Exception(ASSISTANT_NOT_FOUND_ERROR); |
49 | 60 | } |
50 | 61 | } |
51 | | - public async Task<string> CreateAssistant(AssistantType assistantType, Stream data) { |
52 | | - var assistantName = Guid.NewGuid().ToString(); |
53 | | - var assistant = await assistantFactory.CreateAssistant(assistantName); |
54 | | - Assistants.TryAdd(assistantName, assistant); |
55 | | - |
56 | | - var prompt = GetPrompt(assistantType); |
57 | | - if(assistantType == AssistantType.UserAssistant) { |
58 | | - await LoadDocumentation(assistant, prompt); |
59 | | - } else { |
60 | | - await assistant.InitializeAsync(new OpenAIAssistantOptions(Guid.NewGuid().ToString() + ".pdf", data, prompt)); |
61 | | - } |
62 | | - return assistantName; |
63 | | - } |
64 | | - |
65 | | - public Task<string> CreateAssistant(AssistantType assistantType) { |
66 | | - return CreateAssistant(assistantType, null); |
67 | | - } |
68 | 62 | } |
69 | 63 | } |
0 commit comments