Skip to content

Commit

Permalink
feat: update rag ai search template for dotnet (#12341)
Browse files Browse the repository at this point in the history
* feat: add rag ai search template
  • Loading branch information
xzf0587 committed Sep 9, 2024
1 parent 2f6f18b commit 1fac52f
Show file tree
Hide file tree
Showing 41 changed files with 1,976 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export function getTemplateReplaceMap(inputs: Inputs): { [key: string]: string }
const llmService: string | undefined = inputs[QuestionNames.LLMService];
let openAIKey: string | undefined = inputs[QuestionNames.OpenAIKey];
let azureOpenAIKey: string | undefined = inputs[QuestionNames.AzureOpenAIKey];
let azureAISearchApiKey: string | undefined = inputs[QuestionNames.AzureAISearchApiKey];
const azureOpenAIEndpoint: string | undefined = inputs[QuestionNames.AzureOpenAIEndpoint];
const azureOpenAIDeploymentName: string | undefined =
inputs[QuestionNames.AzureOpenAIDeploymentName];
const azureAISearchEndpoint: string | undefined = inputs[QuestionNames.AzureAISearchEndpoint];
const openAIEmbeddingModel: string | undefined = inputs[QuestionNames.OpenAIEmbeddingModel];
const azureOpenAIEmbeddingDeploymentName: string | undefined =
inputs[QuestionNames.AzureOpenAIEmbeddingDeploymentName];

if (inputs.projectId !== undefined && (openAIKey || azureOpenAIKey)) {
const cryptoProvider = new LocalCrypto(inputs.projectId);
Expand All @@ -29,6 +34,10 @@ export function getTemplateReplaceMap(inputs: Inputs): { [key: string]: string }
const result = cryptoProvider.encrypt(azureOpenAIKey);
azureOpenAIKey = (result as any).value;
}
if (azureAISearchApiKey) {
const result = cryptoProvider.encrypt(azureAISearchApiKey);
azureAISearchApiKey = (result as any).value;
}
}

return {
Expand All @@ -50,8 +59,13 @@ export function getTemplateReplaceMap(inputs: Inputs): { [key: string]: string }
originalOpenAIKey: inputs[QuestionNames.OpenAIKey] ?? "",
azureOpenAIKey: azureOpenAIKey ?? "",
originalAzureOpenAIKey: inputs[QuestionNames.AzureOpenAIKey] ?? "",
azureAISearchApiKey: azureAISearchApiKey ?? "",
originalAzureAISearchApiKey: inputs[QuestionNames.AzureAISearchApiKey] ?? "",
azureOpenAIEndpoint: azureOpenAIEndpoint ?? "",
azureOpenAIDeploymentName: azureOpenAIDeploymentName ?? "",
azureOpenAIEmbeddingDeploymentName: azureOpenAIEmbeddingDeploymentName ?? "",
azureAISearchEndpoint: azureAISearchEndpoint ?? "",
openAIEmbeddingModel: openAIEmbeddingModel ?? "",
isNewProjectTypeEnabled: featureFlagManager.getBooleanValue(FeatureFlags.NewProjectType)
? "true"
: "",
Expand Down
4 changes: 4 additions & 0 deletions packages/fx-core/src/question/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ export enum QuestionNames {
CustomCopilotAssistant = "custom-copilot-agent",
LLMService = "llm-service",
OpenAIKey = "openai-key",
OpenAIEmbeddingModel = "openai-embedding-model",
AzureOpenAIKey = "azure-openai-key",
AzureOpenAIEndpoint = "azure-openai-endpoint",
AzureOpenAIDeploymentName = "azure-openai-deployment-name",
AzureOpenAIEmbeddingDeploymentName = "azure-openai-embedding-deployment-name",
AzureAISearchApiKey = "azure-ai-search-api-key",
AzureAISearchEndpoint = "azure-ai-search-endpoint",

Features = "features",
Env = "env",
Expand Down
14 changes: 14 additions & 0 deletions packages/fx-core/tests/component/generator/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,20 @@ describe("render template", () => {
assert.equal(vars.azureOpenAIDeploymentName, "test-deployment");
});

it("template variables with custom copilot - AI Search for csharp", async () => {
inputs.projectId = "test-id";
inputs[QuestionNames.AzureOpenAIKey] = "test-key";
inputs[QuestionNames.AzureAISearchApiKey] = "test-search-key";
inputs[QuestionNames.AzureAISearchEndpoint] = "test-search-endpoint";
inputs[QuestionNames.OpenAIEmbeddingModel] = "test-openai-embedding-model";
inputs[QuestionNames.AzureOpenAIEmbeddingDeploymentName] = "test-azure-embedding-deployment";
const vars = getTemplateReplaceMap(inputs);
assert.isTrue(vars.azureAISearchApiKey.startsWith("crypto_"));
assert.equal(vars.azureAISearchEndpoint, "test-search-endpoint");
assert.equal(vars.openAIEmbeddingModel, "test-openai-embedding-model");
assert.equal(vars.azureOpenAIEmbeddingDeploymentName, "test-azure-embedding-deployment");
});

it("template variables when contains auth", async () => {
sandbox.stub(process, "env").value({ TEAMSFX_TEST_TOOL: "false" });
const vars = Generator.getDefaultVariables("Test", "Test", "net6", false, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,39 @@ The app template is built using the Teams AI library, which provides the capabil
### Debug bot app in Teams Web Client

{{#useOpenAI}}
1. Fill in your OpenAI API Key in `env/.env.local.user`
1. Fill in your OpenAI API Key in `env/.env.local.user`.
```
SECRET_OPENAI_API_KEY="<your-openai-api-key>"
```
{{/useOpenAI}}
{{#useAzureOpenAI}}
1. Fill in your Azure OpenAI settings in `env/.env.local.user`
1. Fill in your Azure OpenAI settings in `env/.env.local.user`.
```
SECRET_AZURE_OPENAI_API_KEY="<your-azure-openai-api-key>"
AZURE_OPENAI_ENDPOINT="<your-azure-openai-endpoint>"
AZURE_OPENAI_DEPLOYMENT_NAME="<your-azure-openai-deployment-name>"
```
{{/useAzureOpenAI}}
1. In the debug dropdown menu, select Dev Tunnels > Create A Tunnel (set authentication type to Public) or select an existing public dev tunnel
2. Right-click your project and select Teams Toolkit > Prepare Teams App Dependencies
3. If prompted, sign in with a Microsoft 365 account for the Teams organization you want
to install the app to
1. Press F5, or select the Debug > Start Debugging menu in Visual Studio
2. In the launched browser, select the Add button to load the app in Teams
3. In the chat bar, type and send anything to your bot to trigger a response
1. In the debug dropdown menu, select Dev Tunnels > Create A Tunnel (set authentication type to Public) or select an existing public dev tunnel.
1. Right-click your project and select Teams Toolkit > Prepare Teams App Dependencies.
1. If prompted, sign in with a Microsoft 365 account for the Teams organization you want to install the app to.
1. Press F5, or select the Debug > Start Debugging menu in Visual Studio.
1. In the launched browser, select the Add button to load the app in Teams.
1. In the chat bar, type and send anything to your bot to trigger a response.

> For local debugging using Teams Toolkit CLI, you need to do some extra steps described in [Set up your Teams Toolkit CLI for local debugging](https://aka.ms/teamsfx-cli-debugging).

### Debug bot app in Teams App Test Tool
{{#useOpenAI}}
1. Fill in your OpenAI API Key in `appsettings.TestTool.json`
1. Fill in your OpenAI API Key in `appsettings.TestTool.json`.
```
"OpenAI": {
"ApiKey": "<your-openai-api-key>"
}
```
{{/useOpenAI}}
{{#useAzureOpenAI}}
1. Fill in your Azure OpenAI settings in `appsettings.TestTool.json`
1. Fill in your Azure OpenAI settings in `appsettings.TestTool.json`.
```
"Azure": {
"OpenAIApiKey": "<your-azure-openai-api-key>",
Expand All @@ -62,9 +61,9 @@ to install the app to
}
```
{{/useAzureOpenAI}}
1. Select `Teams App Test Tool (browser)` in debug dropdown menu
2. Press F5, or select the Debug > Start Debugging menu in Visual Studio
3. In Teams App Test Tool from the launched browser, type and send anything to your bot to trigger a response
1. Select `Teams App Test Tool (browser)` in debug dropdown menu.
1. Press F5, or select the Debug > Start Debugging menu in Visual Studio.
1. In Teams App Test Tool from the launched browser, type and send anything to your bot to trigger a response.

## Extend the AI Chat Bot template with more AI capabilities

Expand Down
30 changes: 30 additions & 0 deletions templates/csharp/custom-copilot-rag-azure-ai-search/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TeamsFx files
build
appPackage/build
env/.env.*.user
env/.env.local
appsettings.Development.json
appsettings.TestTool.json
.deployment

# User-specific files
*.user

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Notification local store
.notification.localstore.json
.notification.testtoolstore.json

# devTools
devTools/
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Overview of the Chat With Your Data (Custom Data Source) template

This app template showcases how to build one of the most powerful applications enabled by LLM - sophisticated question-answering (Q&A) chat bots that can answer questions about specific source information right in the Microsoft Teams.
This app template also demonstrates usage of techniques like:
- [Retrieval Augmented Generation](https://python.langchain.com/docs/use_cases/question_answering/#what-is-rag), or RAG.
- [Teams AI Library](https://learn.microsoft.com/microsoftteams/platform/bots/how-to/teams%20conversational%20ai/teams-conversation-ai-overview)

## Get started with the template

> **Prerequisites**
>
> To run the template in your local dev machine, you will need:
>
> - Prepare your own [Azure AI Search](https://azure.microsoft.com/en-us/products/ai-services/ai-search).
{{#useOpenAI}}
> - Prepare an account with [OpenAI](https://platform.openai.com).
{{/useOpenAI}}
{{#useAzureOpenAI}}
> - Prepare [Azure OpenAI](https://aka.ms/oai/access) resource
{{/useAzureOpenAI}}

## Create your Azure AI Search document index
**Before running or debugging your bot, please follow these steps to create your document index in Auzre AI Search.**

> This app template provides script `Indexer.ps1` to help create assistant. You can change the instructions and settings in the script to customize the document index.
{{#useOpenAI}}
1. Make sure your OpenAI and Azure AI search settings in `appsettings.TestTool.json`.
```
"OpenAI": {
"ApiKey": "<your-openai-api-key>",
"EmbeddingModel": "<your-openai-embedding-model>"
},
"Azure": {
"AISearchApiKey": "<your-azure-ai-search-api-key>",
"AISearchEndpoint": "<your-azure-ai-search-endpoint>"
}
```
{{/useOpenAI}}
{{#useAzureOpenAI}}
1. Make sure your Azure OpenAI and Azure AI search settings in `appsettings.TestTool.json`.
```
"Azure": {
"OpenAIApiKey": "<your-azure-openai-api-key>",
"OpenAIEndpoint": "<your-azure-openai-endpoint>",
"OpenAIDeploymentName": "<your-azure-openai-deployment-name>"
"OpenAIEmbeddingDeploymentName": "<your-azure-openai-embedding-deployment-name>",
"AISearchApiKey": "<your-azure-ai-search-api-key>",
"AISearchEndpoint": "<your-azure-ai-search-endpoint>"
}
```
{{/useAzureOpenAI}}
1. Open PowerShell, change the current working directory to this project root and run command `. ./Indexer.ps1 -run create`.
```
> . ./Indexer.ps1 -run create
```
1. Once you're done using the sample it's good practice to delete the index. You can do so with the `. ./Indexer.ps1 -run delete`.

### Debug bot app in Teams Web Client
1. Create your Azure AI Search document index as mentioned above.
{{#useOpenAI}}
1. Make sure your OpenAI and Azure AI search settings in `env/.env.local.user`.
```
SECRET_OPENAI_API_KEY="<your-openai-api-key>"
OPENAI_EMBEDDING_MODEL="<your-openai-embedding-model>"
SECRET_AI_SEARCH_API_KEY="<your-azure-ai-search-api-key>"
AI_SEARCH_ENDPOINT="<your-azure-ai-search-endpoint>"
```
{{/useOpenAI}}
{{#useAzureOpenAI}}
1. Make sure your Azure OpenAI and Azure AI search settings in `env/.env.local.user`.
```
SECRET_AZURE_OPENAI_API_KEY="<your-azure-openai-api-key>"
AZURE_OPENAI_ENDPOINT="<your-azure-openai-endpoint>"
AZURE_OPENAI_DEPLOYMENT_NAME="<your-azure-openai-deployment-name>"
AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME="<your-azure-openai-embedding-deployment-name>"
SECRET_AI_SEARCH_API_KEY="<your-azure-ai-search-api-key>"
AI_SEARCH_ENDPOINT="<your-azure-ai-search-endpoint>"
```
{{/useAzureOpenAI}}
1. Create your Azure AI Search document index as mentioned above.
1. In the debug dropdown menu, select Dev Tunnels > Create A Tunnel (set authentication type to Public) or select an existing public dev tunnel.
1. Right-click your project and select Teams Toolkit > Prepare Teams App Dependencies.
1. If prompted, sign in with a Microsoft 365 account for the Teams organization you want to install the app to
1. Press F5, or select the Debug > Start Debugging menu in Visual Studio.
1. In the launched browser, select the Add button to load the app in Teams.
1. In the chat bar, type and send anything to your bot to trigger a response.

> For local debugging using Teams Toolkit CLI, you need to do some extra steps described in [Set up your Teams Toolkit CLI for local debugging](https://aka.ms/teamsfx-cli-debugging).

### Debug bot app in Teams App Test Tool
1. Create your Azure AI Search document index as mentioned above.
1. Select `Teams App Test Tool (browser)` in debug dropdown menu.
1. Press F5, or select the Debug > Start Debugging menu in Visual Studio.
1. In Teams App Test Tool from the launched browser, type and send anything to your bot to trigger a response.

**Congratulations**! You are running an application that can now interact with users in Teams App Test Tool:

![RAG Bot](https://github.com/OfficeDev/TeamsFx/assets/13211513/f56e7602-a5d3-436a-ae01-78546d61717d)

## Extend the template

- Follow [Build a Basic AI Chatbot in Teams](https://aka.ms/teamsfx-basic-ai-chatbot) to extend the template with more AI capabilities.
- Understand more about [build your own data ingestion](https://aka.ms/teamsfx-rag-bot#build-your-own-data-ingestion).
- Understand more about [Azure AI Search as data source](https://aka.ms/teamsfx-rag-bot#azure-ai-search-as-data-source).

## Additional information and references

- [Teams Toolkit Documentations](https://docs.microsoft.com/microsoftteams/platform/toolkit/teams-toolkit-fundamentals)
- [Teams Toolkit CLI](https://aka.ms/teamsfx-toolkit-cli)
- [Teams Toolkit Samples](https://github.com/OfficeDev/TeamsFx-Samples)

## Report an issue

Select Visual Studio > Help > Send Feedback > Report a Problem.
Or, you can create an issue directly in our GitHub repository:
https://github.com/OfficeDev/TeamsFx/issues.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"profiles": {
{{#enableTestToolByDefault}}
// Launch project within Teams App Test Tool
"Teams App Test Tool (browser)": {
"commandName": "Project",
"launchTestTool": true,
"launchUrl": "http://localhost:56150",
},
{{/enableTestToolByDefault}}
// Launch project within Teams
"Microsoft Teams (browser)": {
"commandName": "Project",
"launchUrl": "https://teams.microsoft.com/l/app/${{TEAMS_APP_ID}}?installAppPackage=true&webjoin=true&appTenantId=${{TEAMS_APP_TENANT_ID}}&login_hint=${{TEAMSFX_M365_USER_NAME}}",
},
{{^enableTestToolByDefault}}
// Launch project within Teams App Test Tool
"Teams App Test Tool (browser)": {
"commandName": "Project",
"launchTestTool": true,
"launchUrl": "http://localhost:56150",
},
{{/enableTestToolByDefault}}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.TeamsFx.Sdk">
<ItemGroup>
<ProjectCapability Include="ProjectConfigurationsDeclaredDimensions" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
{{#enableTestToolByDefault}}
<ActiveDebugProfile>Teams App Test Tool (browser)</ActiveDebugProfile>
{{/enableTestToolByDefault}}
{{^enableTestToolByDefault}}
<ActiveDebugProfile>Microsoft Teams (browser)</ActiveDebugProfile>
{{/enableTestToolByDefault}}
</PropertyGroup>
</Project>
Loading

0 comments on commit 1fac52f

Please sign in to comment.