Skip to content

Commit c6fadee

Browse files
authored
Merge pull request #13 from codemillmatt/lesson-2-updates
Lesson 2 updates
2 parents 3bcf3d1 + 755cd7c commit c6fadee

15 files changed

+355
-507
lines changed

.devcontainer/devcontainer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"ghcr.io/azure/azure-dev/azd:0": {
1414
"version": "stable"
15-
},
15+
},
16+
"ghcr.io/devcontainers/features/github-cli:1": {},
1617
"sshd": "latest"
1718
},
1819

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Setting Up the Development Environment for Azure OpenAI
2+
3+
If you want to use Azure AI Foundry models for your .NET AI apps in this course, follow the steps in this guide.
4+
5+
Don't want to use Azure OpenAI?
6+
7+
👉 [To use GitHub Models this is the guide for you](README.md)
8+
👉 [Here are the steps for Ollama](getting-started-ollama.md)
9+
10+
## Create the Azure AI Foundry resources
11+
12+
To use Azure AI Foundry models, you need to create a hub and project in the Azure AI Foundry portal. Then you'll need to deploy a model. This section will show you how to do that.
13+
14+
### Create a Hub and Project in Azure AI Foundry
15+
16+
1. Go to the [Azure AI Foundry Portal](https://ai.azure.com/).
17+
1. Sign in with your Azure account.
18+
1. Select **All hubs + projects** from the left-hand menu and then click the **+ New hub** from the dropdown. (Note: You may have to click on **+ New project** first to see the **+ New hub** option).
19+
![Create a new hub](./images/ai-foundry-hub-selection.png)
20+
1. A new window will open. Fill in the details for your hub:
21+
- Give your hub a name (e.g., "MyAIHub").
22+
- Choose a region closest to you.
23+
- Select the appropriate subscription and resource group.
24+
- You can leave the rest of the settings as they are.
25+
- Click **Next**.
26+
- Review the details and click **Create**.
27+
1. Once your hub is created, the portal will open its details page. Click the **Create Project** button.
28+
- Give your project a name (e.g., "GenAINET") or accept the default.
29+
- Click **Create**.
30+
31+
🎉 **Done!** You’ve just created your first project in Azure AI Foundry.
32+
33+
### Deploy a Language Model in Azure AI Foundry
34+
35+
Now, let’s deploy a **gpt-4o-mini** model to your project:
36+
37+
1. In the Azure AI Foundry portal, navigate to your project (it should automatically open after creating it).
38+
1. Click on **Models and Endpoints** from the left-hand menu and then the **Deploy Model** button.
39+
1. Select **Deploy base model** from the dropdown.
40+
1. Search for **gpt-4o-mini** in the model catalog.
41+
1. Select the model and click the **Confirm** button.
42+
1. Specify a deployment name (e.g., "gpt-4o-mini"). You can leave the rest of the options as they are.
43+
1. Click **Deploy** and wait for the model to be provisioned.
44+
1. Once deployed, note the **Model Name**, **Target URI**, and **API Key** from the model details page.
45+
46+
🎉 **Done!** You’ve deployed your first Large Language Model in Azure AI Foundry.
47+
48+
![Model deployed, copy model name, endpoint url and apikey](./images/deploytoazure-20-copymodelinfo.png)
49+
50+
> 📝 **Note:** The endpoint maybe similar to `https://< your hub name>.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-08-01-preview`. The endpoint name that we need is only `https://< your hub name >.openai.azure.com/`*.
51+
52+
## Adding the Azure AI API Key to your Codespace's Secrets
53+
54+
To be secure, let's add the API key you just created to your Codespace's secrets.
55+
56+
1. Make sure you have forked this repository to your GitHub account.
57+
1. Go to the **Settings** tab of your forked repository then expand **Secrets and variables** on the left-hand menu and select **Codespaces**.
58+
59+
![Adding a new Codespace secret](./images/codespaces-secret.jpeg)
60+
1. Name your secret **AZURE_AI_KEY**.
61+
1. Paste the API key you copied from the Azure AI Foundry portal into the **Secret** field.
62+
63+
## Creating a GitHub Codespace
64+
65+
Let's create a GitHub Codespace to develop with for the rest of this course.
66+
67+
1. Open this repository's main page in a new window by [right-clicking here](https://github.com/microsoft/Generative-AI-for-beginners-dotnet) and selecting **Open in new window** from the context menu
68+
1. Fork this repo into your GitHub account by clicking the **Fork** button in the top right corner of the page
69+
1. Click the **Code** dropdown button and then select the **Codespaces** tab
70+
1. Select the **...** option (the three dots) and choose **New with options...**
71+
72+
![Creating a Codespace with custom options](./images/creating-codespace.png)
73+
74+
### Choosing Your development container
75+
76+
From the **Dev container configuration** dropdown, select one of the following options:
77+
78+
**Option 1: C# (.NET)** : This is the option you should use if you plan to use GitHub Models or Azure OpenAI. It has all the core .NET development tools needed for the rest of the course and a fast startup time
79+
80+
**Option 2: C# (.NET) - Ollama**: Ollama allows you to run the demos without needing to connect to GitHub Models or Azure OpenAI. It includes all the core .NET development in addition to Ollama, but has a slower start-up time, five minutes on average. [Follow this guide](getting-started-ollama.md) if you want to use Ollama
81+
82+
You can leave the rest of the settings as they are. Click the **Create codespace** button to start the Codespace creation process.
83+
84+
![Selecting your development container configuration](./images/select-container-codespace.png)
85+
86+
## Update the sample code to use Azure OpenAI and your new model
87+
88+
Now let’s update the code to use the newly deployed model. First we'll need to add some NuGet packages to work with Azure OpenAI.
89+
90+
1. Open the terminal and switch to the project directory:
91+
92+
```bash
93+
cd 02-SettingUp.NETDev/src/BasicChat-01MEAI/
94+
```
95+
96+
1. Run the following commands to add the required package:
97+
98+
```bash
99+
dotnet add package Azure.AI.OpenAI
100+
dotnet add package Microsoft.Extensions.AI.OpenAI --version 9.1.0-preview.1.25064.3
101+
```
102+
103+
[More information about Azure.AI.OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI/2.1.0#show-readme-container).
104+
105+
1. Open `/workspaces/Generative-AI-for-beginners-dotnet/02-SettingUp.NETDev/src/BasicChat-01MEAI/Program.cs`.
106+
107+
Add the following using statements at the top of the file:
108+
109+
110+
```csharp
111+
using System.ClientModel;
112+
using Azure.AI.OpenAI;
113+
using Microsoft.Extensions.AI;
114+
115+
1. Create new variables to hold the model name, endpoint, and API key:
116+
117+
```csharp
118+
var deploymentName = "< deployment name > "; // e.g. "gpt-4o-mini"
119+
var endpoint = new Uri("< endpoint >"); // e.g. "https://< your hub name >.openai.azure.com/"
120+
var apiKey = new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_AI_SECRET"));
121+
```
122+
123+
Making sure to replace `< deployment name >`, and `< endpoint >` with the values you noted above.
124+
125+
1. Replace the `IChatClient` creation with the following code:
126+
127+
```csharp
128+
IChatClient client = new AzureOpenAIClient(
129+
endpoint,
130+
apiKey)
131+
.AsChatClient(deploymentName);
132+
```
133+
134+
1. Run the following command in the terminal:
135+
136+
```bash
137+
dotnet run
138+
```
139+
140+
1. You should see output similar to the following:
141+
142+
```bash
143+
Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. AI encompasses a variety of technologies and approaches that enable computers and systems to perform tasks that typically require human intelligence. These tasks include:
144+
145+
1. **Learning**: The ability to improve performance based on experience, often through algorithms that analyze data.
146+
147+
...
148+
```
149+
150+
> ⚠️ **Note**: Something not working? [Open an issue](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new?template=Blank+issue) and we'll help you out.
151+
152+
## Summary
153+
154+
In this lesson, you learned how to set up your development environment for the rest of the course. You created a GitHub Codespace and configured it to use Azure OpenAI. You also updated the sample code to use the newly deployed model in Azure AI Foundry.
155+
156+
### Additional Resources
157+
158+
- [Azure AI Foundry Documentation](https://learn.microsoft.com/en-us/azure/ai-services/)
159+
- [Working with GitHub Codespaces](https://docs.github.com/en/codespaces/getting-started)
160+
- [How to Deploy Models in Azure AI Foundry](https://learn.microsoft.com/en-us/azure/ai-services/deploy/)
161+
- [Azure.AI.OpenAI NuGet Package](https://www.nuget.org/packages/Azure.AI.OpenAI)
162+
163+
## Next Steps
164+
165+
Next, we'll explore how to create your first AI application! 🚀
166+
167+
👉 [Core Generative AI Techniques](../03-CoreGenerativeAITechniques/readme.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Setting Up the Development Envionment with Ollama
2+
3+
If you want to use Ollama to run local models for this course, follow the steps in this guide.
4+
5+
Don't want to use Azure OpenAI?
6+
7+
👉 [To use GitHub Models this is the guide for you](README.md)
8+
👉 [Here are the steps for Ollama](getting-started-ollama.md)
9+
10+
## Creating a GitHub Codespace
11+
12+
Let's create a GitHub Codespace to develop with for the rest of this course.
13+
14+
1. Open this repository's main page in a new window by [right-clicking here](https://github.com/microsoft/Generative-AI-for-beginners-dotnet) and selecting **Open in new window** from the context menu
15+
1. Fork this repo into your GitHub account by clicking the **Fork** button in the top right corner of the page
16+
1. Click the **Code** dropdown button and then select the **Codespaces** tab
17+
1. Select the **...** option (the three dots) and choose **New with options...**
18+
19+
![Creating a Codespace with custom options](./images/creating-codespace.png)
20+
21+
### Choosing Your development container
22+
23+
From the **Dev container configuration** dropdown, select one of the following options:
24+
25+
**Option 1: C# (.NET)** : This is the option you should use if you plan to use GitHub Models or Azure OpenAI and is our recommended way to complete this course. It has all the core .NET development tools needed for the rest of the course and a fast startup time
26+
27+
**Option 2: C# (.NET) - Ollama**: This is the one you want for running models locally with Ollama. It includes all the core .NET development in addition to Ollama, but has a slower start-up time, five minutes on average. [Follow this guide](getting-started-ollama.md) if you want to use Ollama
28+
29+
You can leave the rest of the settings as they are. Click the **Create codespace** button to start the Codespace creation process.
30+
31+
![Selecting your development container configuration](./images/select-container-codespace.png)
32+
33+
## Verifying your Codespace is running correctly with Ollama
34+
35+
Once your Codespace is fully loaded and configured, lets run a sample app to verify everything is working correctly:
36+
37+
1. Open the terminal. You can open a terminal window by typing **Ctrl+\`** (backtick) on Windows or **Cmd+`** on macOS.
38+
39+
1. Switch to the proper directory by running the following command:
40+
41+
```bash
42+
cd 02-SettingUp.NETDev/src/BasicChat-03Ollama/
43+
```
44+
45+
1. Then run the application with the following command:
46+
47+
```bash
48+
dotnet run
49+
```
50+
51+
1. It may take a couple of seconds, but eventually the application should output a message similar to the following:
52+
53+
```bash
54+
AI, or Artificial Intelligence, refers to the development of computer systems that can perform tasks that typically require human intelligence, such as:
55+
56+
1. Learning: AI systems can learn from data and improve their performance over time.
57+
2. Reasoning: AI systems can draw conclusions and make decisions based on the data they have been trained on.
58+
59+
...
60+
```
61+
62+
> ⚠️ **Note**: Something not working? [Open an issue](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new?template=Blank+issue) and we'll help you out.
63+
64+
## Swap out the model in Ollama
65+
66+
One of the cool things about Ollama is that it's easy to change models. The current app uses the "**llama3.2**" model. Let’s switch it up and try the "**phi3.5**" model instead.
67+
68+
1. Download the Phi3.5 model running the comamnd from the terminal:
69+
70+
```bash
71+
ollama pull phi3.5
72+
```
73+
74+
You can learn mode about the [Phi3.5](https://ollama.com/library/phi3.5) and other available models in the [Ollama library](https://ollama.com/library/).
75+
76+
1. Edit the initialization of the chat client in `Program.cs` to use the new model::
77+
78+
```csharp
79+
IChatClient client = new OllamaChatClient(new Uri("http://localhost:11434/"), "phi3.5");
80+
```
81+
82+
1. Finally, run the app with the following command:
83+
84+
```bash
85+
dotnet run
86+
```
87+
88+
1. You’ve just switched to a new model. Notice how the response is longer and more detailed.
89+
90+
```bash
91+
Artificial Intelligence (AI) refers to the simulation of human intelligence processes by machines, especially computer systems. These processes include learning (the acquisition of information and accumulation of knowledge), reasoning (using the acquired knowledge to make deductions or decisions), and self-correction. AI can manifest in various forms:
92+
93+
1. **Narrow AI** – Designed for specific tasks, such as facial recognition software, voice assistants like Siri or Alexa, autonomous vehicles, etc., which operate under a limited preprogrammed set of behaviors and rules but excel within their domain when compared to humans in these specialized areas.
94+
95+
2. **General AI** – Capable of understanding, learning, and applying intelligence broadly across various domains like human beings do (natural language processing, problem-solving at a high level). General AIs are still largely theoretical as we haven't yet achieved this form to the extent necessary for practical applications beyond narrow tasks.
96+
97+
...
98+
```
99+
100+
> ⚠️ **Note**: Something not working? [Open an issue](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new?template=Blank+issue) and we'll help you out.
101+
102+
## Summary
103+
104+
In this lesson, you learned how to set up your development environment for the rest of the course. You created a GitHub Codespace and configured it to use Ollama. You also updated the sample code to use change models easily.
105+
106+
### Additional Resources
107+
108+
- [Ollama Models](https://ollama.com/search)
109+
- [Working with GitHub Codespaces](https://docs.github.com/en/codespaces/getting-started)
110+
- [Microsoft Extensions for AI Documentation](https://learn.microsoft.com/en-us/dotnet/)
111+
112+
## Next Steps
113+
114+
Next, we'll explore how to create your first AI application! 🚀
115+
116+
👉 [Core Generative AI Techniques](../03-CoreGenerativeAITechniques/readme.md)

0 commit comments

Comments
 (0)