Skip to content

Commit

Permalink
[C#] chore: enable AI log on .net samples (#1034)
Browse files Browse the repository at this point in the history
## Linked issues

closes: #1033

## Details

Enable prompt request and response logs on `OpenAIModel` samples.

#### Change details

- Add `LogRequests = true` to model options
- Add model as singleton and pass `loggerFactory` when creating new
model
- Set log level to `Trace` in *appsettings.development.json*

**screenshots**:
<img width="848" alt="image"
src="https://github.com/microsoft/teams-ai/assets/7642967/d86d66aa-a621-4211-8b83-89812686c367">

## Attestation Checklist

- [X] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (we use
[TypeDoc](https://typedoc.org/) to document our code)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes

### Additional information

> Feel free to add other relevant information below
  • Loading branch information
swatDong authored Dec 7, 2023
1 parent 748c374 commit 6506433
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 66 deletions.
30 changes: 20 additions & 10 deletions dotnet/samples/04.ai.a.teamsChefBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,32 @@

builder.Services.AddSingleton<IStorage, MemoryStorage>();

OpenAIModel? model = null;

// Create AI Model
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo"));
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo")
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
model = new(new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
)
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}

if (model == null)
else
{
throw new Exception("please configure settings for either OpenAI or Azure");
}
Expand All @@ -67,7 +77,7 @@
// Create ActionPlanner
ActionPlanner<TurnState> planner = new(
options: new(
model: model,
model: sp.GetService<OpenAIModel>()!,
prompts: prompts,
defaultPrompt: async (context, state, planner) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Warning",
"Microsoft.Teams.AI": "Trace"
}
},
"AllowedHosts": "*",
Expand Down
30 changes: 20 additions & 10 deletions dotnet/samples/04.ai.b.messageExtensions.gptME/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,32 @@
// Set PREVIEW_MODE to true to enable this feature and update your manifest accordingly.
bool PREVIEW_MODE = false;

OpenAIModel? model = null;

// Create AI Model
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo"));
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo")
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
model = new(new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
)
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}

if (model == null)
else
{
throw new Exception("please configure settings for either OpenAI or Azure");
}
Expand All @@ -72,7 +82,7 @@
// Create OpenAIPlanner
ActionPlanner<AppState> planner = new(
new(
model,
sp.GetService<OpenAIModel>()!,
prompts,
async (context, state, planner) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.Teams.AI": "Trace"
}
},
"AllowedHosts": "*",
Expand Down
30 changes: 20 additions & 10 deletions dotnet/samples/04.ai.c.actionMapping.lightBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,32 @@
// Create singleton instances for bot application
builder.Services.AddSingleton<IStorage, MemoryStorage>();

OpenAIModel? model = null;

// Create AI Model
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo"));
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo")
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
model = new(new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
)
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}

if (model == null)
else
{
throw new Exception("please configure settings for either OpenAI or Azure");
}
Expand All @@ -72,7 +82,7 @@
// Create ActionPlanner
ActionPlanner<AppState> planner = new(
options: new(
model: model,
model: sp.GetService<OpenAIModel>()!,
prompts: prompts,
defaultPrompt: async (context, state, planner) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.Teams.AI": "Trace"
}
},
"AllowedHosts": "*",
Expand Down
30 changes: 20 additions & 10 deletions dotnet/samples/04.ai.d.chainedActions.listBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,32 @@

builder.Services.AddSingleton<IStorage, MemoryStorage>();

OpenAIModel? model = null;

// Create AI Model
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo"));
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo")
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
model = new(new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
)
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}

if (model == null)
else
{
throw new Exception("please configure settings for either OpenAI or Azure");
}
Expand All @@ -63,7 +73,7 @@
// Create OpenAIPlanner
ActionPlanner<ListState> planner = new(
new(
model,
sp.GetService<OpenAIModel>()!,
prompts,
async (context, state, planner) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Microsoft.AspNetCore": "Warning",
"Microsoft.Teams.AI": "Trace"
}
},
"AllowedHosts": "*",
Expand Down
31 changes: 21 additions & 10 deletions dotnet/samples/04.ai.e.chainedActions.devOpsBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,32 @@
// Create singleton instances for bot application
builder.Services.AddSingleton<IStorage, MemoryStorage>();

OpenAIModel? model = null;

// Create AI Model
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo") { LogRequests = true });
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo")
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
model = new(new AzureOpenAIModelOptions(config.Azure.OpenAIApiKey, "gpt-35-turbo", config.Azure.OpenAIEndpoint)
{
LogRequests = true
});
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
)
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}

if (model == null)
else
{
throw new Exception("please configure settings for either OpenAI or Azure");
}
Expand All @@ -63,7 +74,7 @@

ActionPlanner<DevOpsState> planner = new(
new(
model,
sp.GetService<OpenAIModel>()!,
prompts,
async (context, state, planner) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.Teams.AI": "Trace"
}
},
"AllowedHosts": "*",
Expand Down
30 changes: 20 additions & 10 deletions dotnet/samples/04.e.twentyQuestions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,32 @@
// Create singleton instances for bot application
builder.Services.AddSingleton<IStorage, MemoryStorage>();

OpenAIModel? model = null;

// Create AI Model
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo"));
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo")
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
model = new(new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
builder.Services.AddSingleton<OpenAIModel>(sp => new(
new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
)
{
LogRequests = true
},
sp.GetService<ILoggerFactory>()
));
}

if (model == null)
else
{
throw new Exception("please configure settings for either OpenAI or Azure");
}
Expand All @@ -68,7 +78,7 @@
// Create ActionPlanner
ActionPlanner<GameState> planner = new(
options: new(
model: model,
model: sp.GetService<OpenAIModel>()!,
prompts: prompts,
defaultPrompt: async (context, state, planner) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.Teams.AI": "Trace"
}
},
"AllowedHosts": "*",
Expand Down

0 comments on commit 6506433

Please sign in to comment.