|
1 | | -using DevExpress.AIIntegration.OpenAI.Services; |
2 | | -using DevExpress.AIIntegration.Services.Assistant; |
3 | | -using DevExpress.Utils; |
4 | | -using System; |
| 1 | +using System; |
5 | 2 | using System.Collections.Concurrent; |
6 | 3 | using System.IO; |
7 | 4 | using System.Threading.Tasks; |
| 5 | +using DevExpress.AIIntegration.Services.Assistant; |
| 6 | +using DevExpress.Utils; |
8 | 7 |
|
9 | 8 | namespace DashboardAIAssistant.Services { |
10 | 9 | public class AIAssistantProvider : IAIAssistantProvider { |
11 | 10 | private readonly IAIAssistantFactory assistantFactory; |
| 11 | + private readonly AIAssistantCreator assistantCreator; |
12 | 12 | private ConcurrentDictionary<string, IAIAssistant> Assistants { get; set; } = new(); |
13 | 13 |
|
14 | | - public AIAssistantProvider(IAIAssistantFactory assistantFactory) { |
| 14 | + public AIAssistantProvider(IAIAssistantFactory assistantFactory, AIAssistantCreator assistantCreator) { |
15 | 15 | this.assistantFactory = assistantFactory; |
| 16 | + this.assistantCreator = assistantCreator; |
16 | 17 | } |
17 | 18 |
|
18 | 19 | public async Task<string> CreateAssistant(Stream fileContent, string prompt) { |
19 | 20 | Guard.ArgumentNotNull(fileContent, nameof(fileContent)); |
20 | 21 | Guard.ArgumentIsNotNullOrEmpty(prompt, nameof(prompt)); |
21 | | - |
22 | | - string assistantId = Guid.NewGuid().ToString(); |
23 | 22 |
|
24 | | - IAIAssistant assistant = await assistantFactory.CreateAssistant(assistantId); |
25 | | - Assistants.TryAdd(assistantId, assistant); |
| 23 | + string assistantName = Guid.NewGuid().ToString(); |
| 24 | + (string assistantId, string threadId) = await assistantCreator.CreateAssistantAsync(fileContent, $"{assistantName}.xlsx", prompt, false); |
| 25 | + |
| 26 | + IAIAssistant assistant = await assistantFactory.GetAssistant(assistantId, threadId); |
| 27 | + await assistant.InitializeAsync(); |
26 | 28 |
|
27 | | - await assistant.InitializeAsync(new OpenAIAssistantOptions($"{assistantId}.xlsx", fileContent) { |
28 | | - Instructions = prompt, |
29 | | - UseFileSearchTool = false, |
30 | | - }); |
| 29 | + Assistants.TryAdd(assistantName, assistant); |
31 | 30 |
|
32 | | - return assistantId; |
| 31 | + return assistantName; |
33 | 32 | } |
34 | 33 |
|
35 | | - public IAIAssistant GetAssistant(string assistantId) { |
36 | | - Guard.ArgumentIsNotNullOrEmpty(assistantId, nameof(assistantId)); |
| 34 | + public IAIAssistant GetAssistant(string assistantName) { |
| 35 | + Guard.ArgumentIsNotNullOrEmpty(assistantName, nameof(assistantName)); |
37 | 36 |
|
38 | 37 | IAIAssistant assistant = null; |
39 | 38 |
|
40 | | - if(!Assistants.TryGetValue(assistantId, out assistant)) { |
41 | | - throw new ArgumentException($"Incorrect assistant id: {assistantId}"); |
| 39 | + if(!Assistants.TryGetValue(assistantName, out assistant)) { |
| 40 | + throw new ArgumentException($"Incorrect assistant id: {assistantName}"); |
42 | 41 | } |
43 | 42 |
|
44 | 43 | return assistant; |
45 | 44 | } |
46 | 45 |
|
47 | | - public void DisposeAssistant(string assistantId) { |
48 | | - Guard.ArgumentIsNotNullOrEmpty(assistantId, nameof(assistantId)); |
| 46 | + public void DisposeAssistant(string assistantName) { |
| 47 | + Guard.ArgumentIsNotNullOrEmpty(assistantName, nameof(assistantName)); |
49 | 48 |
|
50 | | - if(Assistants.TryRemove(assistantId, out IAIAssistant assistant)) { |
| 49 | + if(Assistants.TryRemove(assistantName, out IAIAssistant assistant)) { |
51 | 50 | assistant.Dispose(); |
52 | 51 | } |
53 | 52 | } |
|
0 commit comments