Skip to content

Commit

Permalink
update teams chef bot
Browse files Browse the repository at this point in the history
  • Loading branch information
singhk97 committed Nov 29, 2023
1 parent c21be62 commit 1975c0d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 137 deletions.
8 changes: 4 additions & 4 deletions dotnet/samples/04.ai.a.teamsChefBot/ActionHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ namespace TeamsChefBot
public class ActionHandlers
{
[Action(AIConstants.FlaggedInputActionName)]
public async Task<bool> OnFlaggedInput([ActionTurnContext] ITurnContext turnContext, [ActionEntities] Dictionary<string, object> entities)
public async Task<string> OnFlaggedInput([ActionTurnContext] ITurnContext turnContext, [ActionParameters] Dictionary<string, object> entities)
{
string entitiesJsonString = JsonSerializer.Serialize(entities);
await turnContext.SendActivityAsync($"I'm sorry your message was flagged: {entitiesJsonString}");
return false;
return "";
}

[Action(AIConstants.FlaggedOutputActionName)]
public async Task<bool> OnFlaggedOutput([ActionTurnContext] ITurnContext turnContext)
public async Task<string> OnFlaggedOutput([ActionTurnContext] ITurnContext turnContext)
{
await turnContext.SendActivityAsync("I'm not allowed to talk about such things.");
return false;
return "";
}
}
}
22 changes: 0 additions & 22 deletions dotnet/samples/04.ai.a.teamsChefBot/ActivityHandlers.cs

This file was deleted.

126 changes: 40 additions & 86 deletions dotnet/samples/04.ai.a.teamsChefBot/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Teams.AI;
using Microsoft.Teams.AI.AI;
using Microsoft.Teams.AI.AI.Moderator;
using Microsoft.Teams.AI.AI.Planner;
using Microsoft.Teams.AI.AI.Prompt;
using Microsoft.Teams.AI.AI.Models;
using Microsoft.Teams.AI.AI.Planners;
using Microsoft.Teams.AI.AI.Prompts;
using Microsoft.Teams.AI.State;
using Microsoft.Teams.AI;
using TeamsChefBot;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -33,107 +32,62 @@

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

#region Use Azure OpenAI and Azure Content Safety
if (config.Azure == null
|| string.IsNullOrEmpty(config.Azure.OpenAIApiKey)
|| string.IsNullOrEmpty(config.Azure.OpenAIEndpoint)
|| string.IsNullOrEmpty(config.Azure.ContentSafetyApiKey)
|| string.IsNullOrEmpty(config.Azure.ContentSafetyEndpoint))
{
throw new Exception("Missing Azure configuration.");
}

builder.Services.AddSingleton(_ => new AzureOpenAIPlannerOptions(config.Azure.OpenAIApiKey, "text-davinci-003", config.Azure.OpenAIEndpoint)
{
LogRequests = true
});
builder.Services.AddSingleton(_ => new AzureContentSafetyModeratorOptions(config.Azure.ContentSafetyApiKey, config.Azure.ContentSafetyEndpoint, ModerationType.Both));
OpenAIModel? model = null;

// Create the Application.
builder.Services.AddTransient<IBot>(sp =>
if (!string.IsNullOrEmpty(config.OpenAI?.ApiKey))
{
ILoggerFactory loggerFactory = sp.GetService<ILoggerFactory>()!;

IPromptManager<TurnState> promptManager = new PromptManager<TurnState>("./Prompts");
IPlanner<TurnState> planner = new AzureOpenAIPlanner<TurnState>(sp.GetService<AzureOpenAIPlannerOptions>()!, loggerFactory);
IModerator<TurnState> moderator = new AzureContentSafetyModerator<TurnState>(sp.GetService<AzureContentSafetyModeratorOptions>()!);

ApplicationOptions<TurnState, TurnStateManager> applicationOptions = new()
{
AI = new AIOptions<TurnState>(planner, promptManager)
{
Moderator = moderator,
Prompt = "Chat",
History = new AIHistoryOptions()
{
AssistantHistoryType = AssistantHistoryType.Text
}
},
Storage = sp.GetService<IStorage>(),
LoggerFactory = loggerFactory
};

Application<TurnState, TurnStateManager> app = new(applicationOptions);

// Register AI actions
app.AI.ImportActions(new ActionHandlers());

// Listen for user to say "/history".
app.OnMessage("/history", ActivityHandlers.HistoryMessageHandler);

return app;
});
#endregion

#region Use OpenAI
/**
if (config.OpenAI == null || string.IsNullOrEmpty(config.OpenAI.ApiKey))
model = new(new OpenAIModelOptions(config.OpenAI.ApiKey, "gpt-3.5-turbo"));
}
else if (!string.IsNullOrEmpty(config.Azure?.OpenAIApiKey) && !string.IsNullOrEmpty(config.Azure.OpenAIEndpoint))
{
throw new Exception("Missing OpenAI configuration.");
model = new(new AzureOpenAIModelOptions(
config.Azure.OpenAIApiKey,
"gpt-35-turbo",
config.Azure.OpenAIEndpoint
));
}

builder.Services.AddSingleton(_ => new OpenAIPlannerOptions(config.OpenAI.ApiKey, "text-davinci-003")
if (model == null)
{
LogRequests = true
});
builder.Services.AddSingleton(_ => new OpenAIModeratorOptions(config.OpenAI.ApiKey, ModerationType.Both));
throw new Exception("please configure settings for either OpenAI or Azure");
}

// Create the Application.
// Create the bot as transient. In this case the ASP Controller is expecting an IBot.
builder.Services.AddTransient<IBot>(sp =>
{
// Create loggers
ILoggerFactory loggerFactory = sp.GetService<ILoggerFactory>()!;

IPromptManager<TurnState> promptManager = new PromptManager<TurnState>("./Prompts");
IPlanner<TurnState> planner = new OpenAIPlanner<TurnState>(sp.GetService<OpenAIPlannerOptions>()!, loggerFactory);
IModerator<TurnState> moderator = new OpenAIModerator<TurnState>(sp.GetService<OpenAIModeratorOptions>()!, loggerFactory);
ApplicationOptions<TurnState, TurnStateManager> applicationOptions = new()
// Create Prompt Manager
PromptManager prompts = new(new()
{
AI = new AIOptions<TurnState>(planner, promptManager)
{
Moderator = moderator,
Prompt = "Chat",
History = new AIHistoryOptions()
PromptFolder = "./Prompts"
});

// Create ActionPlanner
ActionPlanner<TurnState> planner = new(
options: new(
model: model,
prompts: prompts,
defaultPrompt: async (context, state, planner) =>
{
AssistantHistoryType = AssistantHistoryType.Text
PromptTemplate template = prompts.GetPrompt("Chat");
return await Task.FromResult(template);
}
},
Storage = sp.GetService<IStorage>(),
LoggerFactory = loggerFactory
};
)
{ LogRepairs = true },
loggerFactory: loggerFactory
);

Application<TurnState, TurnStateManager> app = new(applicationOptions);
Application<TurnState> app = new ApplicationBuilder<TurnState>()
.WithAIOptions(new(planner))
.WithStorage(sp.GetService<IStorage>()!)
.Build();

// Register AI actions
app.AI.ImportActions(new ActionHandlers());

// Listen for user to say "/history".
app.OnMessage("/history", ActivityHandlers.HistoryMessageHandler);
return app;
});
**/
#endregion

var app = builder.Build();

Expand Down
23 changes: 14 additions & 9 deletions dotnet/samples/04.ai.a.teamsChefBot/Prompts/Chat/config.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"schema": 1,
"description": "Chat with Teams Chef",
{
"schema": 1.1,
"description": "A bot that help developers build Teams apps",
"type": "completion",
"completion": {
"max_tokens": 150,
"temperature": 0.9,
"model": "gpt-3.5-turbo",
"completion_type": "chat",
"include_history": true,
"include_input": true,
"max_input_tokens": 2800,
"max_tokens": 1000,
"temperature": 0.2,
"top_p": 0.0,
"presence_penalty": 0.6,
"frequency_penalty": 0.0,
"stop_sequences": [
"Human:",
"AI:"
]
"stop_sequences": []
},
"augmentation": {
"augmentation_type": "none"
}
}
8 changes: 2 additions & 6 deletions dotnet/samples/04.ai.a.teamsChefBot/Prompts/Chat/skprompt.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
The following is a conversation with an AI assistant, its name is Teams Chef.
The following is a conversation with an AI assistant, its name is Teams Chef.
Teams Chef is an expert in Microsoft Teams apps development and the Human is junior developer learning Microsoft Teams development for the first time.
Teams Chef should always reply by explaining new concepts in simple terms using cooking as parallel concepts.
Teams Chef should always greet the human, ask them their name, and then guide the junior developer in his journey to build new apps for Microsoft Teams.

{{$history}}
Human: {{$input}}
TeamsChef:
Teams Chef should always greet the human, ask them their name, and then guide the junior developer in his journey to build new apps for Microsoft Teams.
5 changes: 2 additions & 3 deletions dotnet/samples/04.ai.a.teamsChefBot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Summary

This is a conversational bot for Microsoft Teams that thinks it's a Chef to help you cook Teams apps. The bot uses the text-davinci-003 model to chat with Teams users and respond in a polite and respectful manner, staying within the scope of the conversation.
This is a conversational bot for Microsoft Teams that thinks it's a Chef to help you cook Teams apps. The bot uses the `gpt-3.5-turbo` model to chat with Teams users and respond in a polite and respectful manner, staying within the scope of the conversation.

This sample illustrates basic conversational bot behavior in Microsoft Teams. The bot is built to allow GPT to facilitate the conversation on its behalf, using only a natural language prompt file to guide it.

Expand Down Expand Up @@ -56,8 +56,7 @@ Above steps use Azure OpenAI as AI service, optionally, you can also use OpenAI

**As prerequisites**

1. Prepare your own OpenAI service.
1. Modify source code `Program.cs`, comment out the "*#Use Azure OpenAI and Azure Content Safety*" part, and uncomment the "*#Use OpenAI*" part.
1. Get an OpenAI api key.

**For debugging (F5)**

Expand Down
7 changes: 0 additions & 7 deletions dotnet/samples/04.ai.a.teamsChefBot/TeamsChefBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
<PackageReference Include="Microsoft.Teams.AI" Version="1.0.*-*" />
</ItemGroup>

<ItemGroup>
<Content Include="Prompts\Chat\skprompt.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<!-- Exclude Teams Toolkit files from build output, but can still be viewed from Solution Explorer -->
<ItemGroup>
<Content Remove="appPackage/**/*" />
Expand Down

0 comments on commit 1975c0d

Please sign in to comment.