Skip to content
Merged
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
11 changes: 5 additions & 6 deletions .github/ISSUE_TEMPLATE/02-breaking-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ labels:
- Pri1
- doc-idea
assignees:
- gewarren
- camsoper
body:
- type: textarea
id: description
Expand All @@ -21,15 +21,14 @@ body:
label: Version
description: What version of .NET introduced the breaking change?
options:
- .NET 8 GA
- .NET 9 Preview 7
- .NET 9 RC 1
- .NET 9 RC 2
- .NET 9 GA
- .NET 8
- .NET 9
- .NET 10 Preview 1
- .NET 10 Preview 2
- .NET 10 Preview 3
- .NET 10 Preview 4
- .NET 10 Preview 5
- .NET 10 Preview 6
- Other (please put exact version in description textbox)
validations:
required: true
Expand Down
2 changes: 1 addition & 1 deletion docs/ai/quickstarts/get-started-openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ In this quickstart, you learn how to create a conversational .NET console chat a

:::zone-end

[!INCLUDE [semantic-kernel](../../azure/includes/semantic-kernel.md)]
[!INCLUDE [semantic-kernel](includes/semantic-kernel.md)]

[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]

Expand Down
2 changes: 1 addition & 1 deletion docs/ai/quickstarts/quickstart-azure-openai-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ In this quickstart, you create a .NET console AI chat app to connect to an AI mo

:::zone-end

[!INCLUDE [semantic-kernel](../../azure/includes/semantic-kernel.md)]
[!INCLUDE [semantic-kernel](includes/semantic-kernel.md)]

[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]

Expand Down
22 changes: 12 additions & 10 deletions docs/ai/quickstarts/quickstart-local-ai.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
title: Quickstart - Connect to and chat with a local AI using .NET and Semantic Kernel
description: Set up a local AI model and chat with it using a .NET console app and the Semantic Kernel SDK
title: Quickstart - Connect to and chat with a local AI using .NET
description: Set up a local AI model and chat with it using a .NET console app and the Microsoft.Extensions.AI libraries
ms.date: 11/24/2024
ms.topic: quickstart
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
author: alexwolfmsft
ms.author: alexwolf
---

# Chat with a local AI model using .NET and Semantic Kernel
# Chat with a local AI model using .NET

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.
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.

## Prerequisites

Expand Down Expand Up @@ -51,14 +51,13 @@ Complete the following steps to create a .NET console app that will connect to y
1. In a terminal window, navigate to an empty directory on your device and create a new app with the `dotnet new` command:

```dotnetcli
dotnet new console
dotnet new console -o LocalAI
```

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:
1. Add the [Microsoft.Extensions.AI.Ollama](https://aka.ms/meai-ollama-nuget) packages to your app:

```dotnetcli
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Connectors.Ollama
dotnet add package Microsoft.Extensions.AI.Ollama --prerelease
```

1. Open the new app in your editor of choice, such as Visual Studio Code.
Expand All @@ -75,8 +74,11 @@ The Semantic Kernel SDK provides many services and features to connect to AI mod

:::code language="csharp" source="snippets/local-ai/program.cs" :::

The preceding code accomplishes the following tasks:
- Creates a `Kernel` object and uses it to retrieve a chat completion service.
The preceding code accomplishes the following:

- Creates an `OllamaChatClient` that implements the `IChatClient` interface.
- This interface provides a loosely coupled abstraction you can use to chat with AI Models.
- You can later change the underlying chat client implementation to another model, such as Azure OpenAI, without changing any other code.
- Creates a `ChatHistory` object to store the messages between the user and the AI model.
- Retrieves a prompt from the user and stores it in the `ChatHistory`.
- Sends the chat data to the AI model to generate a response.
Expand Down
161 changes: 63 additions & 98 deletions docs/ai/quickstarts/quickstart-openai-generate-images.md
Original file line number Diff line number Diff line change
@@ -1,167 +1,134 @@
---
title: Quickstart - Generate images using AI with .NET
description: Create a simple app using OpenAI to generate postal card images.
description: Create a simple app using to generate images using .NET and the OpenAI or Azure OpenAI models.
ms.date: 07/17/2024
ms.topic: quickstart
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
author: fboucher
ms.author: frbouche
zone_pivot_groups: openai-library
# 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.
# 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.
---

# Generate images using AI with .NET

:::zone target="docs" pivot="openai"
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.

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.
:::zone target="docs" pivot="openai"

[!INCLUDE [download-alert](includes/prerequisites-openai.md)]
[!INCLUDE [openai-prereqs](includes/prerequisites-openai.md)]

:::zone-end

:::zone target="docs" pivot="azure-openai"

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.

[!INCLUDE [download-alert](includes/prerequisites-azure-openai.md)]
[!INCLUDE [azure-openai-prereqs](includes/prerequisites-azure-openai.md)]

:::zone-end

## Get the sample project
[!INCLUDE [semantic-kernel](includes/semantic-kernel.md)]

[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]

:::zone target="docs" pivot="azure-openai"
## Create the app

## Create the Azure OpenAI service
Complete the following steps to create a .NET console app to connect to an AI model.

# [Azure Developer CLI](#tab/azd)
1. In an empty directory on your computer, use the `dotnet new` command to create a new console app:

[!INCLUDE [deploy-azd](includes/deploy-azd.md)]
```dotnetcli
dotnet new console -o ImagesAI
```

# [Azure CLI](#tab/azure-cli)
1. Change directory into the app folder:

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.
```dotnetcli
cd ImagesAI
```

1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\05-HikeImages` directory.
1. Install the required packages:

1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
:::zone target="docs" pivot="azure-openai"

```bash
dotnet user-secrets init
dotnet user-secrets set OpenAIKey <your-openai-key>
dotnet add package Azure.AI.OpenAI
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
```

# [Azure Portal](#tab/azure-portal)
:::zone-end

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.
:::zone target="docs" pivot="openai"

1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\05-HikeImages` directory.
```bash
dotnet add package OpenAI
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
```

1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
:::zone-end

1. Open the app in Visual Studio Code or your editor of choice.

```bash
dotnet user-secrets init
dotnet user-secrets set OpenAIKey <your-openai-key>
code .
```

---
:::zone target="docs" pivot="azure-openai"

:::zone-end
[!INCLUDE [create-ai-service](includes/create-ai-service.md)]

## Try the the hiking images sample
:::zone-end

:::zone target="docs" pivot="openai"

1. Clone the repository: [dotnet/ai-samples](https://github.com/dotnet/ai-samples)
## Configure the app

1. Navigate to the root of your .NET project from a terminal or command prompt.

1. Run the following commands to configure your OpenAI API key as a secret for the sample app:

```bash
dotnet user-secrets init
dotnet user-secrets set OpenAIKey <your-openai-key>
```

1. Use the `dotnet run` command to run the app:

```dotnetcli
dotnet run
dotnet user-secrets set ModelName <your-openai-model-name>
```

:::zone-end

:::zone target="docs" pivot="azure-openai"
## Add the app code

1. From a terminal or command prompt, navigate to the `azure-openai\semantic-kernel\05-HikeImages` directory.
1. In the **Program.cs** file, add the following code to connect and authenticate to the AI model.

2. Use the `dotnet run` command to run the app:
:::zone target="docs" pivot="azure-openai"

```dotnetcli
dotnet run
```

> [!TIP]
> If you get an error message, the Azure OpenAI resources might not have finished deploying. Wait a couple of minutes and try again.

:::zone-end

## Explore the code

:::zone target="docs" pivot="openai"

The application uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the OpenAI service.

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.

```csharp
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string key = config["OpenAIKey"];
```

The `OpenAITextToImageService` service facilitates the requests and responses.

```csharp
OpenAITextToImageService textToImageService = new(key, null);
```

:::zone-end

:::zone target="docs" pivot="azure-openai"
:::code language="csharp" source="snippets/image-generation/azure-openai/program.cs" :::

The application uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the Azure OpenAI service.
> [!NOTE]
> <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).

The _Program.cs_ file contains all of the app code. The first several lines of code load secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning.
:::zone-end

```csharp
// Retrieve the local secrets saved during the Azure deployment
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
string deployment = config["AZURE_OPENAI_GPT_NAME"];
```
:::zone target="docs" pivot="openai"

The `AzureOpenAITextToImageService` service facilitates the requests and responses.
:::code language="csharp" source="snippets/image-generation/openai/program.cs" :::

```csharp
AzureOpenAITextToImageService textToImageService = new(deployment, endpoint, new DefaultAzureCredential(), null);
```
:::zone-end

:::zone-end
The preceding code:

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.
- Reads essential configuration values from the project user secrets to connect to the AI model
- Creates an `ImageClient` to connect to the AI model
- Sends a prompt to the model that describes the desired image
- Prints the URL of the generated image to the console output

The `GenerateImageAsync` function instructs the model to generate a response based on the user prompt and image size and quality configurations.

```csharp
// Generate the image
string imageUrl = await textToImageService.GenerateImageAsync("""
A postal card with a happy hiker waving and a beautiful mountain in the background.
There is a trail visible in the foreground.
The postal card has text in red saying: 'You are invited for a hike!'
""", 1024, 1024);
1. Use the `dotnet run` command to run the app:

Console.WriteLine($"The generated image is ready at:\n{imageUrl}");
```
```dotnetcli
dotnet run
```

Customize the prompt to personalize the images generated by the model.
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.

:::zone target="docs" pivot="azure-openai"

Expand All @@ -173,11 +140,9 @@ When you no longer need the sample application or resources, remove the correspo
azd down
```

[!INCLUDE [troubleshoot](includes/troubleshoot.md)]

:::zone-end

## Next steps

- [Quickstart - Summarize text using an AI chat app with .NET](quickstart-openai-summarize-text.md)
- [Quickstart - Build an AI chat app with .NET](get-started-openai.md)
- [Generate text and conversations with .NET and Azure OpenAI Completions](/training/modules/open-ai-dotnet-text-completions/)
4 changes: 2 additions & 2 deletions docs/ai/quickstarts/quickstart-openai-summarize-text.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Quickstart - Summarize text using an AI chat app with .NET
description: Create a simple chat app using Microsoft.Extensions.AI and the Semantic Kernel SDK to summarize a text.
description: Create a simple chat app using Microsoft.Extensions.AI to summarize a text.
ms.date: 07/17/2024
ms.topic: quickstart
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
Expand Down Expand Up @@ -29,7 +29,7 @@ In this quickstart, you learn how to create a .NET console chat app to connect t

:::zone-end

[!INCLUDE [semantic-kernel](../../azure/includes/semantic-kernel.md)]
[!INCLUDE [semantic-kernel](includes/semantic-kernel.md)]

[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />

</ItemGroup>

</Project>
Loading
Loading