Skip to content

Commit 254b3cb

Browse files
committed
docs: add .NET example for weather assistant using Copilot SDK
1 parent a11b23b commit 254b3cb

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

docs/getting-started.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,74 @@ python weather_assistant.py
779779

780780
</details>
781781

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

784852
```

0 commit comments

Comments
 (0)