Skip to content

Commit f533118

Browse files
chore: refactor example README.md to follow Windows first approach
1 parent 312a4ac commit f533118

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

examples/aspnet-core/Program.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
?? Environment.GetEnvironmentVariable("OPENAI_API_KEY")
1313
?? throw new InvalidOperationException("OpenAI API key not found")))
1414
);
15+
builder.Services.AddScoped<ChatHttpHandler>();
1516

1617

1718
var app = builder.Build();
@@ -25,17 +26,33 @@
2526

2627
app.UseHttpsRedirection();
2728

28-
// Chat completion endpoint using injected ChatClient client
29-
app.MapPost("/chat/complete", async (ChatRequest request, ChatClient client) =>
30-
{
31-
var completion = await client.CompleteChatAsync(request.Message);
29+
var chatHandler = app.Services.GetRequiredService<ChatHttpHandler>();
3230

33-
return new ChatResponse(completion.Value.Content[0].Text);
34-
});
31+
app.MapPost("/chat/complete", chatHandler.HandleChatRequest);
3532

3633
app.Run();
3734

38-
record ChatRequest(string Message);
39-
record ChatResponse(string Response);
40-
record EmbeddingRequest(string Text);
41-
record EmbeddingResponse(float[] Vector);
35+
public class ChatHttpHandler
36+
{
37+
private readonly ChatClient _client;
38+
private readonly ILogger<ChatHttpHandler> _logger;
39+
40+
// Chat completion endpoint using injected ChatClient client
41+
public ChatHttpHandler(ChatClient client, ILogger<ChatHttpHandler> logger)
42+
{
43+
_client = client;
44+
_logger = logger;
45+
}
46+
47+
public async Task<ChatResponse> HandleChatRequest(ChatRequest request)
48+
{
49+
_logger.LogInformation("Handling chat request: {Message}", request.Message);
50+
var completion = await _client.CompleteChatAsync(request.Message);
51+
return new ChatResponse(completion.Value.Content[0].Text);
52+
}
53+
}
54+
55+
public record ChatRequest(string Message);
56+
public record ChatResponse(string Response);
57+
public record EmbeddingRequest(string Text);
58+
public record EmbeddingResponse(float[] Vector);

examples/aspnet-core/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ This example demonstrates how to use the OpenAI .NET client library with ASP.NET
2020

2121
**Environment Variable (Recommended):**
2222

23-
```bash
24-
export OPENAI_API_KEY="your-api-key-here"
23+
```powershell
24+
$env:OPENAI_API_KEY = "your-api-key-here"
2525
```
2626

2727
**Configuration (appsettings.json):**
@@ -37,13 +37,13 @@ This example demonstrates how to use the OpenAI .NET client library with ASP.NET
3737

3838
2. **Install dependencies:**
3939

40-
```bash
40+
```powershell
4141
dotnet restore
4242
```
4343

4444
3. **Run the application:**
4545

46-
```bash
46+
```powershell
4747
dotnet run
4848
```
4949

@@ -68,14 +68,15 @@ This example demonstrates how to use the OpenAI .NET client library with ASP.NET
6868
}
6969
```
7070

71-
## Testing with cURL
71+
## Testing with PowerShell
7272

7373
**Chat Completion:**
7474

75-
```bash
76-
curl -X POST "https://localhost:7071/chat/complete" \
77-
-H "Content-Type: application/json" \
78-
-d '{"message": "What is the capital of France?"}'
75+
```powershell
76+
Invoke-RestMethod -Uri "https://localhost:5000/chat/complete" `
77+
-Method POST `
78+
-ContentType "application/json" `
79+
-Body '{"message": "What is the capital of France?"}'
7980
```
8081

8182
## Key Implementation Details

examples/aspnet-core/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"OpenAI":
1010
{
1111
"Model": "gpt-4.1-mini",
12-
"ApiKey": "YOUR_API_KEY"
12+
"ApiKey": ""
1313
}
1414
}

0 commit comments

Comments
 (0)