Skip to content

Commit 297bf2f

Browse files
committed
Update project configuration and dependencies
1 parent 95b82e1 commit 297bf2f

7 files changed

+242
-2
lines changed

.devcontainer/devcontainer.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "Ollama with Phi-3",
3+
"image": "mcr.microsoft.com/dotnet/sdk:8.0",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/github-cli:1": {},
7+
"ghcr.io/devcontainers/features/common-utils:2": {},
8+
"ghcr.io/devcontainers/features/dotnet:2": {
9+
"version": "none",
10+
"dotnetRuntimeVersions": "8.0",
11+
"aspNetCoreRuntimeVersions": "8.0"
12+
},
13+
"sshd": "latest"
14+
},
15+
"customizations": {
16+
"vscode": {
17+
"extensions": [
18+
"ms-vscode.vscode-node-azure-pack",
19+
"github.vscode-github-actions",
20+
"ms-dotnettools.csdevkit",
21+
"ms-dotnettools.vscode-dotnet-runtime",
22+
"github.copilot",
23+
"ms-azuretools.vscode-docker"
24+
]
25+
}
26+
},
27+
"forwardPorts": [
28+
32000,
29+
32001
30+
],
31+
"postCreateCommand": "",
32+
"postStartCommand": "",
33+
"remoteUser": "vscode",
34+
"hostRequirements": {
35+
"memory": "4gb",
36+
"cpus": 2
37+
},
38+
"portsAttributes": {
39+
"32001": {
40+
"label": "Back End"
41+
},
42+
"32000": {
43+
"label": "Front End"
44+
}
45+
}
46+
}

README.md

+81-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
# GithubModelsAndSemanticKernel
2-
Sample on how to use GitHub Models and Semantic Kernel
1+
# GitHub Models and Semantic Kernel Sample
2+
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](/LICENSE)
4+
[![Twitter: elbruno](https://img.shields.io/twitter/follow/elbruno.svg?style=social)](https://twitter.com/elbruno)
5+
![GitHub: elbruno](https://img.shields.io/github/followers/elbruno?style=social)
6+
7+
This project is designed to be opened in GitHub Codespaces as an easy way for anyone to try out the GitHub Models entirely in the browser.
8+
9+
1. Create a new Codespace using the `Code` button at the top of the repository.
10+
![create Codespace](./imgs/05CreateCodeSpace.png)
11+
12+
1. Generate a [GitHub Personal Access Token (PAT)](https://github.com/settings/tokens)
13+
14+
1. Open terminal in the src folder and save the PAT as a User Secret with the following command:
15+
16+
```bash
17+
dotnet user-secrets init
18+
dotnet user-secrets set "GH_PAT" "< PAT >"
19+
```
20+
21+
1. Select a model from the [GitHub Model Marketplace](https://github.com/marketplace/models). For the current demo, we will use [Phi-3.5-mini instruct (128k)](https://github.com/marketplace/models/azureml/Phi-3-5-mini-instruct)
22+
23+
1. Edit the file [program.cs]().
24+
25+
Complete the following information:
26+
- ModelId
27+
- Uri Endpoint
28+
29+
Your code should look like this:
30+
![sample code](./imgs/05CreateCodeSpace.png)
31+
32+
4. In the terminal, run the project with the command:
33+
34+
```bash
35+
dotnet run
36+
```
37+
38+
The app should run a chat using the selected mode in the terminal.
39+
![run ollama and ask for a joke](./imgs/10ollamarunphi.gif)
40+
41+
## Video Tutorials
42+
43+
If you want to learn more about how to use this repo, check the following videos:
44+
45+
### GitHub Models and Semantic Kernel
46+
47+
[![GitHub Models and Semantic Kernel](./imgs/SK%20and%20GH%20Models.png)](https://youtu.be/zFnju6Sm08U)
48+
49+
## References
50+
51+
- [GitHub Models](https://github.com/marketplace/models)
52+
- [Semantic Kernel main repository](https://github.com/microsoft/semantic-kernel)
53+
- [Phi-3 Cookbook](https://aka.ms/Phi-3CookBook)
54+
55+
## Author
56+
57+
👤 **Bruno Capuano**
58+
59+
* Website: https://elbruno.com
60+
* Twitter: [@elbruno](https://twitter.com/elbruno)
61+
* Github: [@elbruno](https://github.com/elbruno)
62+
* LinkedIn: [@elbruno](https://linkedin.com/in/elbruno)
63+
64+
## 🤝 Contributing
65+
66+
Contributions, issues and feature requests are welcome!
67+
68+
Feel free to check [issues page](https://github.com/elbruno/phi3-labs//issues).
69+
70+
## Show your support
71+
72+
Give a ⭐️ if this project helped you!
73+
74+
75+
## 📝 License
76+
77+
Copyright &copy; 2024 [Bruno Capuano](https://github.com/elbruno).
78+
79+
This project is [MIT](/LICENSE) licensed.
80+
81+
***

imgs/05CreateCodeSpace.png

82.1 KB
Loading

imgs/SK and GH Models.png

772 KB
Loading

src/GithubModelsAndSemanticKernel.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sk-ghmodels-01", "sk-ghmodels-01.csproj", "{50B89113-A04E-4641-93C0-758597A06BE2}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{50B89113-A04E-4641-93C0-758597A06BE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{50B89113-A04E-4641-93C0-758597A06BE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{50B89113-A04E-4641-93C0-758597A06BE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{50B89113-A04E-4641-93C0-758597A06BE2}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C56EBC21-8FCF-4CBB-95D1-D37566A956FE}
24+
EndGlobalSection
25+
EndGlobal

src/Program.cs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2024
2+
// Author : Bruno Capuano
3+
// Change Log :
4+
// - Sample console application to use OpenAI and Semantic Kernel
5+
//
6+
// The MIT License (MIT)
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
26+
27+
using Microsoft.Extensions.Configuration;
28+
using Microsoft.SemanticKernel;
29+
using Microsoft.SemanticKernel.ChatCompletion;
30+
using OpenAI;
31+
using System.ClientModel;
32+
using System.Text;
33+
34+
// Create a chat completion service with a model from GitHub Models
35+
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
36+
var modelId = config["GHMODELS_PHI35_NAME"];
37+
var uri = config["GHMODELS_PHI35_URI"];
38+
var githubPAT = config["GH_PAT"];
39+
40+
// create client
41+
var client = new OpenAIClient(new ApiKeyCredential(githubPAT), new OpenAIClientOptions { Endpoint = new Uri(uri) });
42+
43+
// Create a chat completion service
44+
var builder = Kernel.CreateBuilder();
45+
builder.AddOpenAIChatCompletion(modelId, client);
46+
47+
// Get the chat completion service
48+
Kernel kernel = builder.Build();
49+
var chat = kernel.GetRequiredService<IChatCompletionService>();
50+
51+
var history = new ChatHistory();
52+
history.AddSystemMessage("You are a useful chatbot. If you don't know an answer, say 'I don't know!'. Always reply in a funny ways. Use emojis if possible.");
53+
54+
while (true)
55+
{
56+
Console.Write("Q: ");
57+
var userQ = Console.ReadLine();
58+
if (string.IsNullOrEmpty(userQ))
59+
{
60+
break;
61+
}
62+
history.AddUserMessage(userQ);
63+
64+
var sb = new StringBuilder();
65+
var result = chat.GetStreamingChatMessageContentsAsync(history);
66+
Console.Write("AI: ");
67+
await foreach (var item in result)
68+
{
69+
sb.Append(item);
70+
Console.Write(item.Content);
71+
}
72+
Console.WriteLine();
73+
74+
history.AddAssistantMessage(sb.ToString());
75+
}

src/sk-ghmodels-01.csproj

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>sk_ghmodels_01</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<UserSecretsId>12dca67c-fb2c-4c2f-9149-dcaf6a5d7c9f</UserSecretsId>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0-preview.7.24405.7" />
13+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.18.2" />
14+
</ItemGroup>
15+
</Project>

0 commit comments

Comments
 (0)