Skip to content

Instantly share code, notes, and snippets.

@vman
Last active November 5, 2024 09:34
Show Gist options
  • Save vman/e1402f25d02e891741af84bd93d877c5 to your computer and use it in GitHub Desktop.
Save vman/e1402f25d02e891741af84bd93d877c5 to your computer and use it in GitHub Desktop.
var azureClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key));
OpenAIFileClient fileClient = azureClient.GetOpenAIFileClient();
AssistantClient assistantClient = azureClient.GetAssistantClient();
OpenAIFile infoFile = await fileClient.UploadFileAsync("C:\\Users\\vardh\\Documents\\Customers.xlsx", FileUploadPurpose.Assistants);
AssistantCreationOptions assistantOptions = new()
{
Name = "CodeInterpreterProMAX",
Instructions =
"You are an assistant that looks up sales data and helps visualize the information based"
+ " on user queries. When asked to generate a graph, chart, or other visualization, use"
+ " the code interpreter tool to do so.",
Tools =
{
new CodeInterpreterToolDefinition()
},
ToolResources = new()
{
CodeInterpreter = new()
{
FileIds = { infoFile.Id },
}
}
};
Assistant assistant = assistantClient.CreateAssistant(deploymentName, assistantOptions);
ThreadCreationOptions threadOptions = new()
{
InitialMessages = { "Can you plot a bar graph for all customers and their purchases?" },
};
ThreadRun threadRun = assistantClient.CreateThreadAndRun(assistant.Id, threadOptions);
do
{
Thread.Sleep(TimeSpan.FromSeconds(1));
Console.WriteLine($"Thread run status: {threadRun.Status}");
threadRun = assistantClient.GetRun(threadRun.ThreadId, threadRun.Id);
} while (!threadRun.Status.IsTerminal);
CollectionResult<ThreadMessage> messages = assistantClient.GetMessages(threadRun.ThreadId, new MessageCollectionOptions() { Order = MessageCollectionOrder.Ascending });
foreach (ThreadMessage message in messages)
{
Console.Write($"[{message.Role.ToString().ToUpper()}]: ");
foreach (MessageContent contentItem in message.Content)
{
if (!string.IsNullOrEmpty(contentItem.Text))
{
Console.WriteLine($"{contentItem.Text}");
}
if (!string.IsNullOrEmpty(contentItem.ImageFileId))
{
OpenAIFile imageInfo = fileClient.GetFile(contentItem.ImageFileId);
BinaryData imageBytes = fileClient.DownloadFile(contentItem.ImageFileId);
using FileStream stream = File.OpenWrite($"{imageInfo.Filename}.png");
imageBytes.ToStream().CopyTo(stream);
Console.WriteLine($"<image: {imageInfo.Filename}.png>");
}
}
}

[USER]: Can you plot a bar graph for all customers and their purchases?

[ASSISTANT] : Sure, let's first inspect the contents of the uploaded file to understand its structure and extract the necessary data.

[ASSISTANT] : The dataset contains the following columns:

  • Customer Name
  • Email
  • Licenses Purchased
  • Total Amount Paid

To create a bar graph of all customers and their purchases, we will use the "Customer Name" and "Licenses Purchased" columns.

Let's proceed with creating the bar graph.

[ASSISTANT] : <image: ea8db0b2-2f7a-420e-9c77-c081b7bd0132.png>

Here is the bar graph showing the number of licenses purchased by each customer. If you need any further analysis or additional visualizations, please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment