Skip to content

Example updated according to assistant reuse API changes #9

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

Open
wants to merge 1 commit into
base: 25.1.2+
Choose a base branch
from
Open
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: 1 addition & 2 deletions CS/ReportingApp/Controllers/AIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public AIController(IAIAssistantProvider assistantProvider) {
}

public async Task<string> CreateUserAssistant() {
var assistantName = await AIAssistantProvider.CreateAssistant(AssistantType.UserAssistant);
return assistantName;
return await AIAssistantProvider.CreateUserAssistant();
}

public async Task<string> GetAnswer([FromForm] string chatId, [FromForm] string text) {
Expand Down
3 changes: 3 additions & 0 deletions CS/ReportingApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@

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/ReportingApp/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 ReportingApp.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
}
70 changes: 32 additions & 38 deletions CS/ReportingApp/Services/AIAssistantProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,62 @@
using System.Collections.Concurrent;
using System.IO;
using System.Threading.Tasks;
using DevExpress.AIIntegration.OpenAI.Services;
using DevExpress.AIIntegration.Services.Assistant;
using Microsoft.AspNetCore.Hosting;
using DevExpress.AIIntegration.Services.Assistant;

namespace ReportingApp.Services {
public class AIAssistantProvider : IAIAssistantProvider {
const string ASSISTANT_NOT_FOUND_ERROR = "Assistant not found";
const string DOCUMENTATION_FILE_NAME = "documentation.pdf";
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";
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.";

private readonly IAIAssistantFactory assistantFactory;
private readonly IWebHostEnvironment environment;
private readonly AIAssistantCreator assistantCreator;

private ConcurrentDictionary<string, IAIAssistant> Assistants { get; set; } = new ();
public AIAssistantProvider(IAIAssistantFactory assistantFactory, IWebHostEnvironment environment) {

private async Task<string> CreateAssistant(Stream data, string fileName, string prompt) {
(string assistantId, string threadId) = await assistantCreator.CreateAssistantAsync(data, fileName, prompt);

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

string assistantName = Guid.NewGuid().ToString();
Assistants.TryAdd(assistantName, assistant);

return assistantName;
}

public AIAssistantProvider(IAIAssistantFactory assistantFactory, IWebHostEnvironment environment, AIAssistantCreator assistantCreator) {
this.assistantFactory = assistantFactory;
this.environment = environment;
this.assistantCreator = assistantCreator;
}
async Task LoadDocumentation(IAIAssistant assistant, string prompt) {
var dirPath = Path.Combine(environment.ContentRootPath, "Data");
var filePath = Path.Combine(dirPath, "documentation.pdf");

using(FileStream stream = File.OpenRead(filePath)) {
await assistant.InitializeAsync(new OpenAIAssistantOptions("documentation.pdf", stream, prompt));
}
public async Task<string> CreateDocumentAssistant(Stream data) {
return await CreateAssistant(data, Guid.NewGuid().ToString() + ".pdf", DOCUMENT_ASSISTANT_PROMPT);
}
string GetPrompt(AssistantType assistantType) {
switch(assistantType) {
case AssistantType.UserAssistant:
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.";
case AssistantType.DocumentAssistant:
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";
default:
return "";
}
public async Task<string> CreateUserAssistant() {
string dirPath = Path.Combine(environment.ContentRootPath, "Data");
string filePath = Path.Combine(dirPath, DOCUMENTATION_FILE_NAME);

using (FileStream stream = File.OpenRead(filePath))
return await CreateAssistant(stream, DOCUMENTATION_FILE_NAME, USER_ASSISTANT_PROMPT);
}
public void DisposeAssistant(string assistantName) {
if(Assistants.TryRemove(assistantName, out IAIAssistant assistant)) {
assistant.Dispose();
} else {
throw new Exception("Assistant not found");
throw new Exception(ASSISTANT_NOT_FOUND_ERROR);
}
}
public IAIAssistant GetAssistant(string assistantName) {
if(!string.IsNullOrEmpty(assistantName) && Assistants.TryGetValue(assistantName, out var assistant)) {
return assistant;
} else {
throw new Exception("Assistant not found");
throw new Exception(ASSISTANT_NOT_FOUND_ERROR);
}
}
public async Task<string> CreateAssistant(AssistantType assistantType, Stream data) {
var assistantName = Guid.NewGuid().ToString();
var assistant = await assistantFactory.CreateAssistant(assistantName);
Assistants.TryAdd(assistantName, assistant);

var prompt = GetPrompt(assistantType);
if(assistantType == AssistantType.UserAssistant) {
await LoadDocumentation(assistant, prompt);
} else {
await assistant.InitializeAsync(new OpenAIAssistantOptions(Guid.NewGuid().ToString() + ".pdf", data, prompt));
}
return assistantName;
}

public Task<string> CreateAssistant(AssistantType assistantType) {
return CreateAssistant(assistantType, null);
}
}
}
2 changes: 1 addition & 1 deletion CS/ReportingApp/Services/AIDocumentOperationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool CanPerformOperation(DocumentOperationRequest request) {
public override async Task<DocumentOperationResponse> PerformOperationAsync(DocumentOperationRequest request, PrintingSystemBase printingSystem, PrintingSystemBase printingSystemWithEditingFields) {
using(var stream = new MemoryStream()) {
printingSystem.ExportToPdf(stream, printingSystem.ExportOptions.Pdf);
var assistantName = await AIAssistantProvider.CreateAssistant(AssistantType.DocumentAssistant, stream);
var assistantName = await AIAssistantProvider.CreateDocumentAssistant(stream);
return new DocumentOperationResponse {
DocumentId = request.DocumentId,
CustomData = assistantName,
Expand Down
8 changes: 2 additions & 6 deletions CS/ReportingApp/Services/IAIAssistantProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
using System.Threading.Tasks;

namespace ReportingApp.Services {
public enum AssistantType {
DocumentAssistant,
UserAssistant
}
public interface IAIAssistantProvider {
IAIAssistant GetAssistant(string assistantName);
Task<string> CreateAssistant(AssistantType assistantType, Stream data);
Task<string> CreateAssistant(AssistantType assistantType);
Task<string> CreateDocumentAssistant(Stream data);
Task<string> CreateUserAssistant();
void DisposeAssistant(string assistantName);
}
}