Skip to content

Update example according to new AI Assistant reuse API #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
new AzureKeyCredential(EnvSettings.AzureOpenAIKey));
var chatClient = azureOpenAIClient.GetChatClient(EnvSettings.DeploymentName).AsIChatClient();

var assistantCreator = new AIAssistantCreator(azureOpenAIClient, EnvSettings.DeploymentName);

builder.Services.AddSingleton(chatClient);
builder.Services.AddSingleton(assistantCreator);
builder.Services.AddSingleton<IAIAssistantProvider, AIAssistantProvider>();
builder.Services.AddDevExpressAI(config =>
{
Expand Down
53 changes: 53 additions & 0 deletions CS/Services/AIAssistantCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.ClientModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using OpenAI;
using OpenAI.Assistants;
using OpenAI.Files;

namespace DashboardAIAssistant.Services {
#pragma warning disable OPENAI001
public class AIAssistantCreator {
readonly AssistantClient assistantClient;
readonly OpenAIFileClient fileClient;
readonly string deployment;

public AIAssistantCreator(OpenAIClient client, string deployment) {
assistantClient = client.GetAssistantClient();
fileClient = client.GetOpenAIFileClient();
this.deployment = deployment;
}

public async Task<(string assistantId, string threadId)> CreateAssistantAsync(Stream data, string fileName, string instructions, bool useFileSearchTool = true, CancellationToken ct = default) {
data.Position = 0;

ClientResult<OpenAIFile> fileResponse = await fileClient.UploadFileAsync(data, fileName, FileUploadPurpose.Assistants, ct);
OpenAIFile file = fileResponse.Value;

var resources = new ToolResources() {
CodeInterpreter = new CodeInterpreterToolResources(),
FileSearch = useFileSearchTool ? new FileSearchToolResources() : null
};
resources.FileSearch?.NewVectorStores.Add(new VectorStoreCreationHelper([file.Id]));
resources.CodeInterpreter.FileIds.Add(file.Id);

AssistantCreationOptions assistantCreationOptions = new AssistantCreationOptions() {
Name = Guid.NewGuid().ToString(),
Instructions = instructions,
ToolResources = resources
};
assistantCreationOptions.Tools.Add(new CodeInterpreterToolDefinition());
if (useFileSearchTool) {
assistantCreationOptions.Tools.Add(new FileSearchToolDefinition());
}

ClientResult<Assistant> assistantResponse = await assistantClient.CreateAssistantAsync(deployment, assistantCreationOptions, ct);
ClientResult<AssistantThread> threadResponse = await assistantClient.CreateThreadAsync(cancellationToken: ct);

return (assistantResponse.Value.Id, threadResponse.Value.Id);
}
}
#pragma warning restore OPENAI001
}
41 changes: 20 additions & 21 deletions CS/Services/AIAssistantProvider.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
using DevExpress.AIIntegration.OpenAI.Services;
using DevExpress.AIIntegration.Services.Assistant;
using DevExpress.Utils;
using System;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading.Tasks;
using DevExpress.AIIntegration.Services.Assistant;
using DevExpress.Utils;

namespace DashboardAIAssistant.Services {
public class AIAssistantProvider : IAIAssistantProvider {
private readonly IAIAssistantFactory assistantFactory;
private readonly AIAssistantCreator assistantCreator;
private ConcurrentDictionary<string, IAIAssistant> Assistants { get; set; } = new();

public AIAssistantProvider(IAIAssistantFactory assistantFactory) {
public AIAssistantProvider(IAIAssistantFactory assistantFactory, AIAssistantCreator assistantCreator) {
this.assistantFactory = assistantFactory;
this.assistantCreator = assistantCreator;
}

public async Task<string> CreateAssistant(Stream fileContent, string prompt) {
Guard.ArgumentNotNull(fileContent, nameof(fileContent));
Guard.ArgumentIsNotNullOrEmpty(prompt, nameof(prompt));

string assistantId = Guid.NewGuid().ToString();

IAIAssistant assistant = await assistantFactory.CreateAssistant(assistantId);
Assistants.TryAdd(assistantId, assistant);
string assistantName = Guid.NewGuid().ToString();
(string assistantId, string threadId) = await assistantCreator.CreateAssistantAsync(fileContent, $"{assistantName}.xlsx", prompt, false);

IAIAssistant assistant = await assistantFactory.GetAssistant(assistantId, threadId);
await assistant.InitializeAsync();

await assistant.InitializeAsync(new OpenAIAssistantOptions($"{assistantId}.xlsx", fileContent) {
Instructions = prompt,
UseFileSearchTool = false,
});
Assistants.TryAdd(assistantName, assistant);

return assistantId;
return assistantName;
}

public IAIAssistant GetAssistant(string assistantId) {
Guard.ArgumentIsNotNullOrEmpty(assistantId, nameof(assistantId));
public IAIAssistant GetAssistant(string assistantName) {
Guard.ArgumentIsNotNullOrEmpty(assistantName, nameof(assistantName));

IAIAssistant assistant = null;

if(!Assistants.TryGetValue(assistantId, out assistant)) {
throw new ArgumentException($"Incorrect assistant id: {assistantId}");
if(!Assistants.TryGetValue(assistantName, out assistant)) {
throw new ArgumentException($"Incorrect assistant id: {assistantName}");
}

return assistant;
}

public void DisposeAssistant(string assistantId) {
Guard.ArgumentIsNotNullOrEmpty(assistantId, nameof(assistantId));
public void DisposeAssistant(string assistantName) {
Guard.ArgumentIsNotNullOrEmpty(assistantName, nameof(assistantName));

if(Assistants.TryRemove(assistantId, out IAIAssistant assistant)) {
if(Assistants.TryRemove(assistantName, out IAIAssistant assistant)) {
assistant.Dispose();
}
}
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<!-- default badges list -->
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/938296554/25.1.2%2B)
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1279614)
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
Expand Down