Skip to content
Prev Previous commit
Next Next commit
Simplify the GitHub and OpenAI key config vars for aiagent-webapi
  • Loading branch information
jeffhandley committed Nov 11, 2025
commit 0e4e4d915dd7f3575dae45bf1db25b35bb7783ea
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@
// You will need to set the token to your own value
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
// cd this-project-directory
// dotnet user-secrets set GitHubModels:Token YOUR-GITHUB-TOKEN
// dotnet user-secrets set "GITHUB_TOKEN" "your-github-models-token-here"
var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["GitHubModels:Token"] ?? throw new InvalidOperationException("Missing configuration: GitHubModels:Token.")),
new ApiKeyCredential(builder.Configuration["GITHUB_TOKEN"] ?? throw new InvalidOperationException("Missing configuration: GITHUB_TOKEN")),
new OpenAIClientOptions { Endpoint = new Uri("https://models.inference.ai.azure.com") })
.AsIChatClient();
#elif (IsOpenAI)
// You will need to set the API key to your own value
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
// cd this-project-directory
// dotnet user-secrets set OpenAI:Key YOUR-API-KEY
// dotnet user-secrets set "OPENAI_KEY" "your-openai-api-key-here"
var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["OpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: OpenAI:Key.")))
new ApiKeyCredential(builder.Configuration["OPENAI_KEY"] ?? throw new InvalidOperationException("Missing configuration: OPENAI_KEY")))
.AsIChatClient();
#elif (IsAzureOpenAI)
// You will need to set the endpoint to your own value
Expand All @@ -51,7 +51,7 @@
#if (!IsManagedIdentity)
// dotnet user-secrets set AzureOpenAI:Key YOUR-API-KEY
#endif
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Endpoint.")), "/openai/v1");
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Endpoint")), "/openai/v1");

#if (IsManagedIdentity)
#pragma warning disable OPENAI001 // The overload accepting an AuthenticationPolicy is experimental and may change or be removed in future releases.
Expand All @@ -64,7 +64,7 @@
#else
var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["AzureOpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Key.")),
new ApiKeyCredential(builder.Configuration["AzureOpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Key")),
new OpenAIClientOptions { Endpoint = azureOpenAIEndpoint })
.AsIChatClient();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ This application uses GitHub Models (model: gpt-4o-mini) for AI functionality. Y
**Option A: Using User Secrets (Recommended for Development)**

```bash
dotnet user-secrets set "GitHubModels:Token" "your-github-models-token-here"
dotnet user-secrets set "GITHUB_TOKEN" "your-github-models-token-here"
```

**Option B: Using Environment Variables**

Set the `GitHubModels__Token` environment variable:
Set the `GITHUB_TOKEN` environment variable:

- **Windows (PowerShell)**:
```powershell
$env:GitHubModels__Token = "your-github-models-token-here"
$env:GITHUB_TOKEN = "your-github-models-token-here"
```

- **Linux/macOS**:
```bash
export GitHubModels__Token="your-github-models-token-here"
export GITHUB_TOKEN="your-github-models-token-here"
```

#### Get a GitHub Models Token
Expand All @@ -59,21 +59,21 @@ This application uses the OpenAI Platform (model: gpt-4o-mini). You'll need to c
**Using User Secrets (Recommended for Development)**

```bash
dotnet user-secrets set "OpenAI:Key" "your-openai-api-key-here"
dotnet user-secrets set "OPENAI_KEY" "your-openai-api-key-here"
```

**Using Environment Variables**

Set the `OpenAI__Key` environment variable:
Set the `OPENAI_KEY` environment variable:

- **Windows (PowerShell)**:
```powershell
$env:OpenAI__Key = "your-openai-api-key-here"
$env:OPENAI_KEY = "your-openai-api-key-here"
```

- **Linux/macOS**:
```bash
export OpenAI__Key="your-openai-api-key-here"
export OPENAI_KEY="your-openai-api-key-here"
```

#### Get an OpenAI API Key
Expand Down Expand Up @@ -236,7 +236,7 @@ dotnet new aiagent-webapi --provider ollama --chat-model llama3.1
## Troubleshooting

<!--#if (IsGHModels) -->
**Problem**: Application fails with "Missing configuration: GitHubModels:Token"
**Problem**: Application fails with "Missing configuration: GITHUB_TOKEN"

**Solution**: Make sure you've configured your GitHub Models API token using one of the methods described above.

Expand All @@ -245,7 +245,7 @@ dotnet new aiagent-webapi --provider ollama --chat-model llama3.1
**Solution**: Verify your GitHub Models token is valid and hasn't expired. You may need to regenerate it from the GitHub Models website.

<!--#elif (IsOpenAI) -->
**Problem**: Application fails with "Missing configuration: OpenAI:Key"
**Problem**: Application fails with "Missing configuration: OPENAI_KEY"

**Solution**: Make sure you've configured your OpenAI API key using one of the methods described above.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// cd this-project-directory
// dotnet user-secrets set AzureOpenAI:Endpoint https://YOUR-DEPLOYMENT-NAME.openai.azure.com
// dotnet user-secrets set AzureOpenAI:Key YOUR-API-KEY
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Endpoint.")), "/openai/v1");
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Endpoint")), "/openai/v1");

var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["AzureOpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Key.")),
new ApiKeyCredential(builder.Configuration["AzureOpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Key")),
new OpenAIClientOptions { Endpoint = azureOpenAIEndpoint })
.AsIChatClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
// cd this-project-directory
// dotnet user-secrets set AzureOpenAI:Endpoint https://YOUR-DEPLOYMENT-NAME.openai.azure.com
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Endpoint.")), "/openai/v1");
var azureOpenAIEndpoint = new Uri(new Uri(builder.Configuration["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("Missing configuration: AzureOpenAI:Endpoint")), "/openai/v1");

#pragma warning disable OPENAI001 // The overload accepting an AuthenticationPolicy is experimental and may change or be removed in future releases.
var chatClient = new ChatClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// You will need to set the token to your own value
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
// cd this-project-directory
// dotnet user-secrets set GitHubModels:Token YOUR-GITHUB-TOKEN
// dotnet user-secrets set "GITHUB_TOKEN" "your-github-models-token-here"
var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["GitHubModels:Token"] ?? throw new InvalidOperationException("Missing configuration: GitHubModels:Token.")),
new ApiKeyCredential(builder.Configuration["GITHUB_TOKEN"] ?? throw new InvalidOperationException("Missing configuration: GITHUB_TOKEN")),
new OpenAIClientOptions { Endpoint = new Uri("https://models.inference.ai.azure.com") })
.AsIChatClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ This application uses GitHub Models (model: gpt-4o-mini) for AI functionality. Y
**Option A: Using User Secrets (Recommended for Development)**

```bash
dotnet user-secrets set "GitHubModels:Token" "your-github-models-token-here"
dotnet user-secrets set "GITHUB_TOKEN" "your-github-models-token-here"
```

**Option B: Using Environment Variables**

Set the `GitHubModels__Token` environment variable:
Set the `GITHUB_TOKEN` environment variable:

- **Windows (PowerShell)**:
```powershell
$env:GitHubModels__Token = "your-github-models-token-here"
$env:GITHUB_TOKEN = "your-github-models-token-here"
```

- **Linux/macOS**:
```bash
export GitHubModels__Token="your-github-models-token-here"
export GITHUB_TOKEN="your-github-models-token-here"
```

#### Get a GitHub Models Token
Expand Down Expand Up @@ -119,7 +119,7 @@ dotnet new aiagent-webapi --provider ollama --chat-model llama3.1

## Troubleshooting

**Problem**: Application fails with "Missing configuration: GitHubModels:Token"
**Problem**: Application fails with "Missing configuration: GITHUB_TOKEN"

**Solution**: Make sure you've configured your GitHub Models API token using one of the methods described above.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// You will need to set the token to your own value
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
// cd this-project-directory
// dotnet user-secrets set GitHubModels:Token YOUR-GITHUB-TOKEN
// dotnet user-secrets set "GITHUB_TOKEN" "your-github-models-token-here"
var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["GitHubModels:Token"] ?? throw new InvalidOperationException("Missing configuration: GitHubModels:Token.")),
new ApiKeyCredential(builder.Configuration["GITHUB_TOKEN"] ?? throw new InvalidOperationException("Missing configuration: GITHUB_TOKEN")),
new OpenAIClientOptions { Endpoint = new Uri("https://models.inference.ai.azure.com") })
.AsIChatClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ This application uses GitHub Models (model: gpt-4o-mini) for AI functionality. Y
**Option A: Using User Secrets (Recommended for Development)**

```bash
dotnet user-secrets set "GitHubModels:Token" "your-github-models-token-here"
dotnet user-secrets set "GITHUB_TOKEN" "your-github-models-token-here"
```

**Option B: Using Environment Variables**

Set the `GitHubModels__Token` environment variable:
Set the `GITHUB_TOKEN` environment variable:

- **Windows (PowerShell)**:
```powershell
$env:GitHubModels__Token = "your-github-models-token-here"
$env:GITHUB_TOKEN = "your-github-models-token-here"
```

- **Linux/macOS**:
```bash
export GitHubModels__Token="your-github-models-token-here"
export GITHUB_TOKEN="your-github-models-token-here"
```

#### Get a GitHub Models Token
Expand Down Expand Up @@ -119,7 +119,7 @@ dotnet new aiagent-webapi --provider ollama --chat-model llama3.1

## Troubleshooting

**Problem**: Application fails with "Missing configuration: GitHubModels:Token"
**Problem**: Application fails with "Missing configuration: GITHUB_TOKEN"

**Solution**: Make sure you've configured your GitHub Models API token using one of the methods described above.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// You will need to set the API key to your own value
// You can do this using Visual Studio's "Manage User Secrets" UI, or on the command line:
// cd this-project-directory
// dotnet user-secrets set OpenAI:Key YOUR-API-KEY
// dotnet user-secrets set "OPENAI_KEY" "your-openai-api-key-here"
var chatClient = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(builder.Configuration["OpenAI:Key"] ?? throw new InvalidOperationException("Missing configuration: OpenAI:Key.")))
new ApiKeyCredential(builder.Configuration["OPENAI_KEY"] ?? throw new InvalidOperationException("Missing configuration: OPENAI_KEY")))
.AsIChatClient();

builder.Services.AddChatClient(chatClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ This application uses the OpenAI Platform (model: gpt-4o-mini). You'll need to c
**Using User Secrets (Recommended for Development)**

```bash
dotnet user-secrets set "OpenAI:Key" "your-openai-api-key-here"
dotnet user-secrets set "OPENAI_KEY" "your-openai-api-key-here"
```

**Using Environment Variables**

Set the `OpenAI__Key` environment variable:
Set the `OPENAI_KEY` environment variable:

- **Windows (PowerShell)**:
```powershell
$env:OpenAI__Key = "your-openai-api-key-here"
$env:OPENAI_KEY = "your-openai-api-key-here"
```

- **Linux/macOS**:
```bash
export OpenAI__Key="your-openai-api-key-here"
export OPENAI_KEY="your-openai-api-key-here"
```

#### Get an OpenAI API Key
Expand Down Expand Up @@ -119,7 +119,7 @@ dotnet new aiagent-webapi --provider ollama --chat-model llama3.1

## Troubleshooting

**Problem**: Application fails with "Missing configuration: OpenAI:Key"
**Problem**: Application fails with "Missing configuration: OPENAI_KEY"

**Solution**: Make sure you've configured your OpenAI API key using one of the methods described above.

Expand Down