Skip to content

Commit 19a1d09

Browse files
authored
docs: add .NET example for interactive weather assistant (#119)
* docs: add .NET example for weather assistant using Copilot SDK * fix: address code review feedback for .NET example - Use Random.Shared instead of new Random() for thread-safe random generation - Add SessionIdleEvent handler for consistent output formatting
1 parent 76fcc82 commit 19a1d09

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

docs/getting-started.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,77 @@ python weather_assistant.py
788788

789789
</details>
790790

791+
<details>
792+
<summary><strong>.NET</strong></summary>
793+
794+
Create a new console project and update `Program.cs`:
795+
796+
```csharp
797+
using GitHub.Copilot.SDK;
798+
using Microsoft.Extensions.AI;
799+
using System.ComponentModel;
800+
801+
// Define the weather tool using AIFunctionFactory
802+
var getWeather = AIFunctionFactory.Create(
803+
([Description("The city name")] string city) =>
804+
{
805+
var conditions = new[] { "sunny", "cloudy", "rainy", "partly cloudy" };
806+
var temp = Random.Shared.Next(50, 80);
807+
var condition = conditions[Random.Shared.Next(conditions.Length)];
808+
return new { city, temperature = $"{temp}°F", condition };
809+
},
810+
"get_weather",
811+
"Get the current weather for a city");
812+
813+
await using var client = new CopilotClient();
814+
await using var session = await client.CreateSessionAsync(new SessionConfig
815+
{
816+
Model = "gpt-4.1",
817+
Streaming = true,
818+
Tools = [getWeather]
819+
});
820+
821+
// Listen for response chunks
822+
session.On(ev =>
823+
{
824+
if (ev is AssistantMessageDeltaEvent deltaEvent)
825+
{
826+
Console.Write(deltaEvent.Data.DeltaContent);
827+
}
828+
if (ev is SessionIdleEvent)
829+
{
830+
Console.WriteLine();
831+
}
832+
});
833+
834+
Console.WriteLine("🌤️ Weather Assistant (type 'exit' to quit)");
835+
Console.WriteLine(" Try: 'What's the weather in Paris?' or 'Compare weather in NYC and LA'\n");
836+
837+
while (true)
838+
{
839+
Console.Write("You: ");
840+
var input = Console.ReadLine();
841+
842+
if (string.IsNullOrEmpty(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
843+
{
844+
break;
845+
}
846+
847+
Console.Write("Assistant: ");
848+
await session.SendAndWaitAsync(new MessageOptions { Prompt = input });
849+
Console.WriteLine("\n");
850+
}
851+
```
852+
853+
Run with:
854+
855+
```bash
856+
dotnet run
857+
```
858+
859+
</details>
860+
861+
791862
**Example session:**
792863

793864
```

0 commit comments

Comments
 (0)