You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Chat with a local AI model using .NET and Semantic Kernel
11
+
# Chat with a local AI model using .NET
12
12
13
-
Local AI models provide powerful and flexible options for building AI solutions. In this quickstart, you'll explore how to set up and connect to a local AI model using .NET and the Semantic Kernel SDK. For this example, you'll run the local AI model using Ollama.
13
+
In this quickstart, you learn how to create a conversational .NET console chat app using an OpenAI or Azure OpenAI model. The app uses the [`Microsoft.Extensions.AI`](https://www.nuget.org/packages/Microsoft.Extensions.AI) library so you can write code using AI abstractions rather than a specific SDK. AI abstractions enable you to change the underlying AI model with minimal code changes.
14
14
15
15
## Prerequisites
16
16
@@ -51,14 +51,13 @@ Complete the following steps to create a .NET console app that will connect to y
51
51
1. In a terminal window, navigate to an empty directory on your device and create a new app with the `dotnet new` command:
52
52
53
53
```dotnetcli
54
-
dotnet new console
54
+
dotnet new console -o LocalAI
55
55
```
56
56
57
-
1. Add the [Semantic Kernel SDK](https://www.nuget.org/packages/Microsoft.SemanticKernel) and the [Semantic Kernel Ollama Connector](https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.Ollama/1.25.0-alpha) packages to your app:
57
+
1. Add the [Microsoft.Extensions.AI.Ollama](https://aka.ms/meai-ollama-nuget) packages to your app:
# CustomerIntent: As a .NET developer new to Azure OpenAI, I want deploy and use sample code to interact to learn how to generate images from the sample code.
10
+
# CustomerIntent: As a .NET developer new to OpenAI, I want deploy and use sample code to interact to learn from the sample code to generate images.
11
11
---
12
12
13
13
# Generate images using AI with .NET
14
14
15
-
:::zone target="docs" pivot="openai"
15
+
In this quickstart, you learn how to create a .NET console app to generate images using an OpenAI or Azure OpenAI DALLe AI model, which are specifically designed to generate images based on text prompts.
16
16
17
-
Get started with AI by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `dall-e-3` model to generate postal card images so you can invite your friends for a hike! Follow these steps to get access to OpenAI and learn how to use Semantic Kernel.
Get started with AI by creating a simple .NET 8 console chat application. The application will run locally and use the OpenAI `dall-e-3` model to generate postal card images so you can invite your friends for a hike! Follow these steps to provision Azure OpenAI and learn how to use the .NET Azure OpenAI SDK.
Complete the following steps to create a .NET console app to connect to an AI model.
38
36
39
-
# [Azure Developer CLI](#tab/azd)
37
+
1. In an empty directory on your computer, use the `dotnet new` command to create a new console app:
40
38
41
-
[!INCLUDE [deploy-azd](includes/deploy-azd.md)]
39
+
```dotnetcli
40
+
dotnet new console -o ImagesAI
41
+
```
42
42
43
-
# [Azure CLI](#tab/azure-cli)
43
+
1. Change directory into the app folder:
44
44
45
-
1. To provision an Azure OpenAI service and model using the Azure CLI, complete the steps in the [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=cli) article.
45
+
```dotnetcli
46
+
cd ImagesAI
47
+
```
46
48
47
-
1.From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\05-HikeImages` directory.
49
+
1. Install the required packages:
48
50
49
-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
51
+
:::zone target="docs" pivot="azure-openai"
50
52
51
53
```bash
52
-
dotnet user-secrets init
53
-
dotnet user-secrets set OpenAIKey <your-openai-key>
1. To provision an Azure OpenAI service and model using the Azure portal, complete the steps in the [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) article.
61
+
:::zone target="docs" pivot="openai"
58
62
59
-
1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\05-HikeImages` directory.
1. Clone the repository: [dotnet/ai-samples](https://github.com/dotnet/ai-samples)
85
+
## Configure the app
86
+
87
+
1. Navigate to the root of your .NET project from a terminal or command prompt.
76
88
77
89
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
78
90
79
91
```bash
80
92
dotnet user-secrets init
81
93
dotnet user-secrets set OpenAIKey <your-openai-key>
82
-
```
83
-
84
-
1. Use the `dotnet run`command to run the app:
85
-
86
-
```dotnetcli
87
-
dotnet run
94
+
dotnet user-secrets set ModelName <your-openai-model-name>
88
95
```
89
96
90
97
:::zone-end
91
98
92
-
:::zone target="docs" pivot="azure-openai"
99
+
## Add the app code
93
100
94
-
1. From a terminal or command prompt, navigate to the `azure-openai\semantic-kernel\05-HikeImages` directory.
101
+
1. In the **Program.cs** file, add the following code to connect and authenticate to the AI model.
95
102
96
-
2. Use the `dotnet run`command to run the app:
103
+
:::zone target="docs" pivot="azure-openai"
97
104
98
-
```dotnetcli
99
-
dotnet run
100
-
```
101
-
102
-
> [!TIP]
103
-
> If you get an error message, the Azure OpenAI resources might not have finished deploying. Wait a couple of minutes and try again.
104
-
105
-
:::zone-end
106
-
107
-
## Explore the code
108
-
109
-
:::zone target="docs" pivot="openai"
110
-
111
-
The application uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the OpenAI service.
112
-
113
-
The _Program.cs_ file contains all of the app code. The first several lines of code set configuration values and get the OpenAI Key that was previously set using the `dotnet user-secrets` command.
114
-
115
-
```csharp
116
-
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
117
-
string key = config["OpenAIKey"];
118
-
```
119
-
120
-
The `OpenAITextToImageService` service facilitates the requests and responses.
The application uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the Azure OpenAI service.
107
+
> [!NOTE]
108
+
> <xref:Azure.Identity.DefaultAzureCredential> searches for authentication credentials from your local tooling. If you aren't using the `azd` template to provision the Azure OpenAI resource, you'll need to assign the `Azure AI Developer` role to the account you used to sign-in to Visual Studio or the Azure CLI. For more information, see [Authenticate to Azure AI services with .NET](/dotnet/ai/azure-ai-services-authentication).
131
109
132
-
The _Program.cs_ file contains all of the app code. The first several lines of code load secrets and configuration values that were setin the `dotnet user-secrets`for you during the application provisioning.
110
+
:::zone-end
133
111
134
-
```csharp
135
-
// Retrieve the local secrets saved during the Azure deployment
136
-
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
AzureOpenAITextToImageService textToImageService = new(deployment, endpoint, new DefaultAzureCredential(), null);
145
-
```
116
+
:::zone-end
146
117
147
-
:::zone-end
118
+
The preceding code:
148
119
149
-
Provide context and instructions to the model by adding a system prompt. A good image generation prompt requires a clear description of what the image is, which colors to use, the intended style, and other descriptors.
120
+
- Reads essential configuration values from the project user secrets to connect to the AI model
121
+
- Creates an `ImageClient` to connect to the AI model
122
+
- Sends a prompt to the model that describes the desired image
123
+
- Prints the URL of the generated image to the console output
150
124
151
-
The `GenerateImageAsync`functioninstructs the model to generate a response based on the user prompt and image size and quality configurations.
A postal card with a happy hiker waving and a beautiful mountain in the background.
157
-
There is a trail visible in the foreground.
158
-
The postal card has text in red saying: 'You are invited for a hike!'
159
-
""", 1024, 1024);
125
+
1. Use the `dotnet run` command to run the app:
160
126
161
-
Console.WriteLine($"The generated image is ready at:\n{imageUrl}");
162
-
```
127
+
```dotnetcli
128
+
dotnet run
129
+
```
163
130
164
-
Customize the prompt to personalize the images generated by the model.
131
+
Navigate to the image URL in the console output to view the generated image. Customize the text content of the prompt to create new images or modify the original.
165
132
166
133
:::zone target="docs" pivot="azure-openai"
167
134
@@ -173,11 +140,9 @@ When you no longer need the sample application or resources, remove the correspo
0 commit comments