|
| 1 | +--- |
| 2 | +title: Quickstart - Create a minimal AI assistant using .NET |
| 3 | +description: Learn to create a minimal AI assistant with tooling capabilities using .NET and the Azure OpenAI SDK libraries |
| 4 | +ms.date: 01/25/2025 |
| 5 | +ms.topic: quickstart |
| 6 | +ms.custom: devx-track-dotnet, devx-track-dotnet-ai |
| 7 | +author: alexwolfmsft |
| 8 | +ms.author: alexwolf |
| 9 | +zone_pivot_groups: openai-library |
| 10 | +--- |
| 11 | + |
| 12 | +# Create a minimal AI assistant using .NET |
| 13 | + |
| 14 | +In this quickstart, you'll learn how to create a minimal AI assistant using the OpenAI or Azure OpenAI SDK libraries. AI assistants provide agentic functionality to help users complete tasks using AI tools and models. In the sections ahead, you'll learn the following: |
| 15 | + |
| 16 | +- Core components and concepts of AI assistants |
| 17 | +- How to create an assistant using the Azure OpenAI SDK |
| 18 | +- How to enhance and customize the capabilities of an assistant |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +::: zone pivot="openai" |
| 23 | + |
| 24 | +* [Install .NET 8.0](https://dotnet.microsoft.com/download) or higher |
| 25 | +* [Visual Studio Code](https://code.visualstudio.com/) (optional) |
| 26 | +* [Visual Studio](https://visualstudio.com/) (optional) |
| 27 | +* An access key for an OpenAI model |
| 28 | + |
| 29 | +:::zone-end |
| 30 | + |
| 31 | +::: zone pivot="azure-openai" |
| 32 | + |
| 33 | +* [Install .NET 8.0](https://dotnet.microsoft.com/download) or higher |
| 34 | +* [Visual Studio Code](https://code.visualstudio.com/) (optional) |
| 35 | +* [Visual Studio](https://visualstudio.com/) (optional) |
| 36 | +* Access to an Azure OpenAI instance via Azure Identity or an access key |
| 37 | + |
| 38 | +:::zone-end |
| 39 | + |
| 40 | +## Core components of AI assistants |
| 41 | + |
| 42 | +AI assistants are based around conversational threads with a user. The user sends prompts to the assistant on a conversation thread, which directs the assistant to complete tasks using the tools it has available. Assistants can process and analyze data, make decisions, and interact with users or other systems to achieve specific goals. Most assistants include the following components: |
| 43 | + |
| 44 | +| **Component** | **Description** | |
| 45 | +|---|---| |
| 46 | +| **Assistant** | The core AI client and logic that uses Azure OpenAI models, manages conversation threads, and utilizes configured tools. | |
| 47 | +| **Thread** | A conversation session between an assistant and a user. Threads store messages and automatically handle truncation to fit content into a model's context. | |
| 48 | +| **Message** | A message created by an assistant or a user. Messages can include text, images, and other files. Messages are stored as a list on the thread. | |
| 49 | +| **Run** | Activation of an assistant to begin running based on the contents of the thread. The assistant uses its configuration and the thread's messages to perform tasks by calling models and tools. As part of a run, the assistant appends messages to the thread. | |
| 50 | +| **Run steps** | A detailed list of steps the assistant took as part of a run. An assistant can call tools or create messages during its run. Examining run steps allows you to understand how the assistant is getting to its final results. | |
| 51 | + |
| 52 | +Assistants can also be configured to use multiple tools in parallel to complete tasks, including the following: |
| 53 | + |
| 54 | +- **Code interpreter tool**: Writes and runs code in a sandboxed execution environment. |
| 55 | +- **Function calling**: Runs local custom functions you define in your code. |
| 56 | +- **File search capabilities**: Augments the assistant with knowledge from outside its model. |
| 57 | + |
| 58 | +By understanding these core components and how they interact, you can build and customize powerful AI assistants to meet your specific needs. |
| 59 | + |
| 60 | +## Create the .NET app |
| 61 | + |
| 62 | +Complete the following steps to create a .NET console app and add the package needed to work with assistants: |
| 63 | + |
| 64 | +::: zone pivot="openai" |
| 65 | + |
| 66 | +1. In a terminal window, navigate to an empty directory on your device and create a new app with the `dotnet new` command: |
| 67 | + |
| 68 | + ```dotnetcli |
| 69 | + dotnet new console -o AIAssistant |
| 70 | + ``` |
| 71 | +
|
| 72 | +1. Add the [OpenAI](https://www.nuget.org/packages/OpenAI) package to your app: |
| 73 | +
|
| 74 | + ```dotnetcli |
| 75 | + dotnet add package OpenAI --prerelease |
| 76 | + ``` |
| 77 | +
|
| 78 | +1. Open the new app in your editor of choice, such as Visual Studio Code. |
| 79 | +
|
| 80 | + ```dotnetcli |
| 81 | + code . |
| 82 | + ``` |
| 83 | +
|
| 84 | +::: zone-end |
| 85 | +
|
| 86 | +::: zone pivot="azure-openai" |
| 87 | +
|
| 88 | +1. In a terminal window, navigate to an empty directory on your device and create a new app with the `dotnet new` command: |
| 89 | +
|
| 90 | + ```dotnetcli |
| 91 | + dotnet new console -o AIAssistant |
| 92 | + ``` |
| 93 | +
|
| 94 | +1. Add the [Azure.AI.OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI) package to your app: |
| 95 | +
|
| 96 | + ```dotnetcli |
| 97 | + dotnet add package Azure.AI.OpenAI --prerelease |
| 98 | + ``` |
| 99 | +
|
| 100 | +1. Open the new app in your editor of choice, such as Visual Studio Code. |
| 101 | +
|
| 102 | + ```dotnetcli |
| 103 | + code . |
| 104 | + ``` |
| 105 | +
|
| 106 | +::: zone-end |
| 107 | +
|
| 108 | +## Create the AI assistant client |
| 109 | +
|
| 110 | +1. Open the _Program.cs_ file and replace the contents of the file with the following code to create the required clients: |
| 111 | +
|
| 112 | + :::code language="csharp" source="snippets/assistants/program.cs" range="0-17" ::: |
| 113 | +
|
| 114 | +1. Create an in-memory sample document and upload it to the `OpenAIFileClient`: |
| 115 | +
|
| 116 | + :::code language="csharp" source="snippets/assistants/program.cs" range="19-53" ::: |
| 117 | +
|
| 118 | +1. Enable file search and code interpreter tooling capabilities via the `AssistantCreationOptions`: |
| 119 | +
|
| 120 | + :::code language="csharp" source="snippets/assistants/program.cs" range="55-78" ::: |
| 121 | +
|
| 122 | +1. Create the `Assistant` and a thread to manage interactions between the user and the assistant: |
| 123 | +
|
| 124 | + :::code language="csharp" source="snippets/assistants/program.cs" range="80-105" ::: |
| 125 | +
|
| 126 | +1. Print the messages and save the generated image from the conversation with the assistant: |
| 127 | +
|
| 128 | + :::code language="csharp" source="snippets/assistants/program.cs" range="107-147" ::: |
| 129 | +
|
| 130 | + Locate and open the saved image in the app *bin* directory, which should resemble the following: |
| 131 | +
|
| 132 | + :::image type="content" source="../media/assistants/generated-sales-graph.png" alt-text="A graph showing the visualization generated by the AI model."::: |
| 133 | +
|
| 134 | +## Next steps |
| 135 | +
|
| 136 | +- [Quickstart - Get insight about your data from an .NET Azure AI chat app](../how-to/work-with-local-models.md) |
| 137 | +- [Generate text and conversations with .NET and Azure OpenAI Completions](/training/modules/open-ai-dotnet-text-completions/) |
0 commit comments