Skip to content

Commit c4208c6

Browse files
committed
Add new assignments
1 parent b65140b commit c4208c6

12 files changed

Lines changed: 440 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Solution>
2+
<Project Path="Zadanie01/Zadanie01.csproj" />
3+
<Project Path="Zadanie02/Zadanie02.csproj" />
4+
<Project Path="Zadanie03/Zadanie03.csproj" />
5+
<Project Path="Zadanie04/Zadanie04.csproj" />
6+
</Solution>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
using Microsoft.SemanticKernel.Connectors.Ollama;
4+
5+
#pragma warning disable SKEXP0070
6+
7+
const string ModelId = "llama3.2";
8+
const string Endpoint = "http://localhost:11434";
9+
10+
var kernel = Kernel.CreateBuilder()
11+
.AddOllamaChatCompletion(ModelId, new Uri(Endpoint))
12+
.Build();
13+
14+
var chat = kernel.GetRequiredService<IChatCompletionService>();
15+
var history = new ChatHistory("Jesteś pomocnym asystentem. Odpowiadaj zwięźle po polsku.");
16+
17+
Console.WriteLine("=== Chat z LLM (Semantic Kernel + Ollama) ===");
18+
Console.WriteLine($"Model: {ModelId} | Endpoint: {Endpoint}");
19+
Console.WriteLine("Wpisz pytanie i naciśnij Enter. Wpisz 'exit' aby zakończyć.\n");
20+
21+
while (true)
22+
{
23+
Console.Write("Ty: ");
24+
var input = Console.ReadLine();
25+
if (string.IsNullOrWhiteSpace(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
26+
break;
27+
28+
history.AddUserMessage(input);
29+
30+
Console.Write("Asystent: ");
31+
var response = await chat.GetChatMessageContentAsync(history);
32+
Console.WriteLine(response.Content);
33+
Console.WriteLine();
34+
35+
history.AddAssistantMessage(response.Content ?? string.Empty);
36+
}
Lines changed: 15 additions & 0 deletions
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>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.33.0" />
12+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.33.0-alpha" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.ComponentModel;
2+
using Microsoft.SemanticKernel;
3+
4+
public sealed class MathPlugin
5+
{
6+
[KernelFunction, Description("Dodaje dwie liczby i zwraca wynik.")]
7+
public double Dodaj(
8+
[Description("Pierwsza liczba")] double a,
9+
[Description("Druga liczba")] double b) => a + b;
10+
11+
[KernelFunction, Description("Odejmuje drugą liczbę od pierwszej.")]
12+
public double Odejmij(
13+
[Description("Liczba, od której odejmujemy")] double a,
14+
[Description("Liczba odejmowana")] double b) => a - b;
15+
16+
[KernelFunction, Description("Mnoży dwie liczby i zwraca wynik.")]
17+
public double Pomnoz(
18+
[Description("Pierwsza liczba")] double a,
19+
[Description("Druga liczba")] double b) => a * b;
20+
21+
[KernelFunction, Description("Dzieli pierwszą liczbę przez drugą. Zwraca błąd przy dzieleniu przez zero.")]
22+
public string Podziel(
23+
[Description("Liczba do podzielenia")] double a,
24+
[Description("Dzielnik")] double b)
25+
=> b == 0 ? "Błąd: dzielenie przez zero" : (a / b).ToString();
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
using Microsoft.SemanticKernel.Connectors.Ollama;
4+
5+
#pragma warning disable SKEXP0070
6+
7+
const string ModelId = "llama3.2";
8+
const string Endpoint = "http://localhost:11434";
9+
10+
var kernel = Kernel.CreateBuilder()
11+
.AddOllamaChatCompletion(ModelId, new Uri(Endpoint))
12+
.Build();
13+
14+
kernel.Plugins.AddFromObject(new MathPlugin(), "Matematyka");
15+
16+
var chat = kernel.GetRequiredService<IChatCompletionService>();
17+
var settings = new OllamaPromptExecutionSettings
18+
{
19+
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
20+
};
21+
22+
var history = new ChatHistory(
23+
"Jesteś asystentem matematycznym. Gdy użytkownik poda operację matematyczną, " +
24+
"wywołaj odpowiednią funkcję i podaj wynik. Odpowiadaj po polsku.");
25+
26+
Console.WriteLine("=== Rozpoznawanie poleceń matematycznych (SK Function Calling) ===");
27+
Console.WriteLine("Przykłady: 'dodaj 5 do 6', 'ile to 12 minus 4?', 'pomnóż 7 przez 8'\n");
28+
29+
while (true)
30+
{
31+
Console.Write("Polecenie: ");
32+
var input = Console.ReadLine();
33+
if (string.IsNullOrWhiteSpace(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
34+
break;
35+
36+
history.AddUserMessage(input);
37+
38+
var response = await chat.GetChatMessageContentAsync(history, settings, kernel);
39+
Console.WriteLine($"Wynik: {response.Content}\n");
40+
41+
history.AddAssistantMessage(response.Content ?? string.Empty);
42+
}
Lines changed: 15 additions & 0 deletions
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>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.33.0" />
12+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.33.0-alpha" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
using Microsoft.SemanticKernel.Connectors.Ollama;
4+
5+
#pragma warning disable SKEXP0070
6+
7+
const string ModelId = "llama3.2";
8+
const string Endpoint = "http://localhost:11434";
9+
10+
var kernel = Kernel.CreateBuilder()
11+
.AddOllamaChatCompletion(ModelId, new Uri(Endpoint))
12+
.Build();
13+
14+
kernel.Plugins.AddFromObject(new SmartHomePlugin(), "SmartHome");
15+
16+
var chat = kernel.GetRequiredService<IChatCompletionService>();
17+
var settings = new OllamaPromptExecutionSettings
18+
{
19+
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
20+
};
21+
22+
var history = new ChatHistory(
23+
"Jesteś inteligentnym asystentem systemu domowej automatyki. " +
24+
"Gdy użytkownik wyda polecenie lub zada pytanie dotyczące domu, " +
25+
"wywołaj odpowiednią funkcję i podaj wynik. Odpowiadaj krótko i po polsku.");
26+
27+
Console.WriteLine("=== System Domowej Automatyki (Semantic Kernel) ===");
28+
Console.WriteLine("Przykłady: 'Która jest godzina?', 'Włącz światła w salonie',");
29+
Console.WriteLine(" 'Jaka jest temperatura?', 'Ustaw termostat na 22 stopnie'\n");
30+
31+
while (true)
32+
{
33+
Console.Write("Polecenie: ");
34+
var input = Console.ReadLine();
35+
if (string.IsNullOrWhiteSpace(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
36+
break;
37+
38+
history.AddUserMessage(input);
39+
40+
var response = await chat.GetChatMessageContentAsync(history, settings, kernel);
41+
Console.WriteLine($"Dom: {response.Content}\n");
42+
43+
history.AddAssistantMessage(response.Content ?? string.Empty);
44+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.ComponentModel;
2+
using Microsoft.SemanticKernel;
3+
4+
public sealed class SmartHomePlugin
5+
{
6+
private readonly Dictionary<string, bool> _lights = new()
7+
{
8+
["salon"] = false,
9+
["kuchnia"] = false,
10+
["sypialnia"] = false,
11+
["łazienka"] = false
12+
};
13+
14+
private double _thermostatTarget = 21.0;
15+
private bool _alarmEnabled = false;
16+
17+
[KernelFunction, Description("Zwraca aktualną godzinę i datę.")]
18+
public string PodajCzas() =>
19+
$"Teraz jest {DateTime.Now:HH:mm}, {DateTime.Now:dddd d MMMM yyyy}.";
20+
21+
[KernelFunction, Description("Włącza światło w podanym pomieszczeniu. Jeśli pomieszczenie nie zostanie podane, włącza wszystkie.")]
22+
public string WlaczSwiatla(
23+
[Description("Nazwa pomieszczenia: salon, kuchnia, sypialnia, łazienka. Puste = wszystkie.")] string pomieszczenie = "")
24+
{
25+
if (string.IsNullOrWhiteSpace(pomieszczenie))
26+
{
27+
foreach (var key in _lights.Keys.ToList()) _lights[key] = true;
28+
return "Włączono światła we wszystkich pomieszczeniach.";
29+
}
30+
var room = pomieszczenie.ToLower();
31+
if (!_lights.ContainsKey(room)) return $"Nieznane pomieszczenie: {pomieszczenie}";
32+
_lights[room] = true;
33+
return $"Włączono światło w: {room}.";
34+
}
35+
36+
[KernelFunction, Description("Wyłącza światło w podanym pomieszczeniu. Jeśli pomieszczenie nie zostanie podane, wyłącza wszystkie.")]
37+
public string WylaczSwiatla(
38+
[Description("Nazwa pomieszczenia. Puste = wszystkie.")] string pomieszczenie = "")
39+
{
40+
if (string.IsNullOrWhiteSpace(pomieszczenie))
41+
{
42+
foreach (var key in _lights.Keys.ToList()) _lights[key] = false;
43+
return "Wyłączono światła we wszystkich pomieszczeniach.";
44+
}
45+
var room = pomieszczenie.ToLower();
46+
if (!_lights.ContainsKey(room)) return $"Nieznane pomieszczenie: {pomieszczenie}";
47+
_lights[room] = false;
48+
return $"Wyłączono światło w: {room}.";
49+
}
50+
51+
[KernelFunction, Description("Podaje stan świateł we wszystkich pomieszczeniach.")]
52+
public string StanSwiatel()
53+
{
54+
var lines = _lights.Select(kv => $" {kv.Key}: {(kv.Value ? "włączone" : "wyłączone")}");
55+
return "Stan świateł:\n" + string.Join("\n", lines);
56+
}
57+
58+
[KernelFunction, Description("Zwraca aktualną temperaturę w domu (symulowana).")]
59+
public string PodajTemperature()
60+
{
61+
var current = _thermostatTarget - 0.5 + Random.Shared.NextDouble();
62+
return $"Aktualna temperatura: {current:F1}°C. Ustawiona na termostacie: {_thermostatTarget}°C.";
63+
}
64+
65+
[KernelFunction, Description("Ustawia docelową temperaturę na termostacie.")]
66+
public string UstawTemperature(
67+
[Description("Temperatura w stopniach Celsjusza")] double temperatura)
68+
{
69+
_thermostatTarget = temperatura;
70+
return $"Termostat ustawiony na {temperatura}°C.";
71+
}
72+
73+
[KernelFunction, Description("Włącza lub wyłącza alarm.")]
74+
public string ZarzadzajAlarmem(
75+
[Description("true = włącz alarm, false = wyłącz alarm")] bool wlacz)
76+
{
77+
_alarmEnabled = wlacz;
78+
return _alarmEnabled ? "Alarm został włączony." : "Alarm został wyłączony.";
79+
}
80+
81+
[KernelFunction, Description("Podaje ogólny status systemu domowej automatyki.")]
82+
public string StatusDomu()
83+
{
84+
var swiatla = _lights.Count(kv => kv.Value);
85+
return $"Status domu: {swiatla}/{_lights.Count} świateł włączonych, " +
86+
$"termostat: {_thermostatTarget}°C, alarm: {(_alarmEnabled ? "aktywny" : "nieaktywny")}.";
87+
}
88+
}
Lines changed: 15 additions & 0 deletions
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>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.33.0" />
12+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.33.0-alpha" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
using Microsoft.SemanticKernel.Connectors.Ollama;
4+
5+
#pragma warning disable SKEXP0070
6+
7+
const string ModelId = "llama3.2";
8+
const string Endpoint = "http://localhost:11434";
9+
10+
var kernel = Kernel.CreateBuilder()
11+
.AddOllamaChatCompletion(ModelId, new Uri(Endpoint))
12+
.Build();
13+
14+
kernel.Plugins.AddFromObject(new ReservationPlugin(), "Rezerwacje");
15+
16+
var chat = kernel.GetRequiredService<IChatCompletionService>();
17+
var settings = new OllamaPromptExecutionSettings
18+
{
19+
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
20+
};
21+
22+
var history = new ChatHistory("""
23+
Jesteś uprzejmym asystentem pomagającym w rezerwacjach. Obsługujesz:
24+
- rezerwacje stolików w restauracji,
25+
- zakup biletów do kina,
26+
- rezerwacje biletów do teatru.
27+
28+
Jeśli brakuje Ci informacji (np. imienia, terminu, liczby osób), zapytaj o nie.
29+
Przed dokonaniem rezerwacji potwierdź szczegóły z użytkownikiem.
30+
Odpowiadaj po polsku, krótko i uprzejmie.
31+
""");
32+
33+
Console.WriteLine("=== Asystent Rezerwacji (Semantic Kernel) ===");
34+
Console.WriteLine("Mogę pomóc zarezerwować stolik w restauracji, bilety do kina lub teatru.");
35+
Console.WriteLine("Wpisz 'moje rezerwacje' aby zobaczyć listę. Wpisz 'exit' aby zakończyć.\n");
36+
37+
while (true)
38+
{
39+
Console.Write("Ty: ");
40+
var input = Console.ReadLine();
41+
if (string.IsNullOrWhiteSpace(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
42+
break;
43+
44+
history.AddUserMessage(input);
45+
46+
var response = await chat.GetChatMessageContentAsync(history, settings, kernel);
47+
Console.WriteLine($"Asystent: {response.Content}\n");
48+
49+
history.AddAssistantMessage(response.Content ?? string.Empty);
50+
}
51+
52+
Console.WriteLine("\nDziękuję za skorzystanie z asystenta rezerwacji!");

0 commit comments

Comments
 (0)