Skip to content

Instantly share code, notes, and snippets.

@vman
Last active October 5, 2024 11:13
Show Gist options
  • Save vman/a6c059d64219fb6292a6ad5156b0f530 to your computer and use it in GitHub Desktop.
Save vman/a6c059d64219fb6292a6ad5156b0f530 to your computer and use it in GitHub Desktop.
string endpoint = "https://<myopenaiservice>.openai.azure.com/";
string key = "<my-open-ai-service-key>";
string deploymentName = "gpt-4o";
//We are using the Azure OpenAI Service so create an Azure OpenAI client
var azureClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key));
AssistantClient assistantClient = azureClient.GetAssistantClient();
//Create assistant
AssistantCreationOptions assistantOptions = new()
{
Name = "SharePoint Tutor",
Instructions =
"You are SharePoint Tutor, an expert assistant in guiding users through SharePoint. " +
"Your role is to provide clear, step-by-step instructions, troubleshooting tips, and best practices for managing and navigating SharePoint. " +
"Always respond in a friendly, approachable tone, and aim to make even complex tasks easy to understand. " +
"When users ask for help with specific tasks, provide detailed instructions. If they encounter issues, offer practical solutions and guidance. " +
"Ensure your responses are concise yet informative, and be ready to suggest tips that improve the user's overall SharePoint experience."
};
Assistant assistant = assistantClient.CreateAssistant(deploymentName, assistantOptions);
//Create thread
ThreadCreationOptions threadOptions = new()
{
InitialMessages = { "What is a webpart?" }
};
//Create a run
ThreadRun threadRun = assistantClient.CreateThreadAndRun(assistant.Id, threadOptions, new RunCreationOptions() { });
do
{
Thread.Sleep(TimeSpan.FromSeconds(1));
threadRun = assistantClient.GetRun(threadRun.ThreadId, threadRun.Id);
} while (!threadRun.Status.IsTerminal);
//Get messages from a thread. This includes the most recent created message as well.
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 (contentItem.TextAnnotations.Count > 0)
{
Console.WriteLine();
}
}
}
Console.WriteLine();
}

[USER]: What is a webpart?

[ASSISTANT]: A web part is a versatile and reusable building block in SharePoint that allows you to add dynamic content and functionality to your SharePoint pages. You can think of a web part as a widget that you can place on a page to display information or provide certain functionality, such as showing a calendar, a list of documents, news headlines, or even embedding video content.

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