Skip to content

Commit

Permalink
perf: fix bug in python template and update readme (#12139)
Browse files Browse the repository at this point in the history
* perf: fix bug in python template

* docs: update readme for python template

* perf: update function
  • Loading branch information
KennethBWSong authored Aug 1, 2024
1 parent 882a285 commit 241189a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
7 changes: 4 additions & 3 deletions templates/python/custom-copilot-rag-custom-api/README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ This template showcases a bot app that responds to user questions like an AI ass
> - An account with [OpenAI](https://platform.openai.com/)
{{/useOpenAI}}
{{#useAzureOpenAI}}
> - Prepare your own [Azure OpenAI](https://aka.ms/oai/access) resource.
> - An account with [Azure OpenAI](https://aka.ms/oai/access).
{{/useAzureOpenAI}}
> - [Node.js](https://nodejs.org/) (supported versions: 16, 18) for local debug in Test Tool.

### Configurations
1. Open the command box and enter `Python: Create Environment` to create and activate your desired virtual environment. Remember to select `src/requirements.txt` as dependencies to install when creating the virtual environment.
Expand All @@ -32,14 +31,16 @@ This template showcases a bot app that responds to user questions like an AI ass

### Conversation with bot
1. Select the Teams Toolkit icon on the left in the VS Code toolbar.
1. In the Account section, sign in with your [Microsoft 365 account](https://docs.microsoft.com/microsoftteams/platform/toolkit/accounts) if you haven't already.
1. Press F5 to start debugging which launches your app in Teams App Test Tool using a web browser. Select `Debug in Test Tool`.
1. When Teams launches in the browser, select the Add button in the dialog to install your app to Teams.
1. You will receive a welcome message from the bot, or send any message to get a response.

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

> 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).

![ai chat bot](https://github.com/OfficeDev/TeamsFx/assets/9698542/9bd22201-8fda-4252-a0b3-79531c963e5e)
![custom api template](https://github.com/OfficeDev/TeamsFx/assets/63089166/19f4c825-c296-4d29-a957-bedb88b6aa5b)

## What's included in the template

Expand Down
4 changes: 2 additions & 2 deletions templates/python/custom-copilot-rag-custom-api/src/bot.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ async def on_error(context: TurnContext, error: Exception):


current_dir = os.path.dirname(os.path.abspath(__file__))
spec_path = os.path.join(current_dir, '../appPackage/apiSpecificationFile/openapi.yaml')
spec_path = os.path.join(current_dir, '../appPackage/apiSpecificationFile/{{OPENAPI_SPEC_PATH}}')
client = OpenAPIClient().load_spec_from_file(spec_path)

@prompts.function("get_actions")
@prompts.function("getAction")
async def get_actions(
_context: TurnContext,
state: MemoryBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Config:
APP_PASSWORD = os.environ.get("BOT_PASSWORD", "")
{{#useAzureOpenAI}}
AZURE_OPENAI_API_KEY = os.environ["AZURE_OPENAI_API_KEY"] # Azure OpenAI API key
AZURE_OPENAI_MODEL_DEPLOYMENT_NAME = os.environ["AZURE_OPENAI_MODEL_DEPLOYMENT_NAME"] # Azure OpenAI model deployment name
AZURE_OPENAI_MODEL_DEPLOYMENT_NAME = os.environ["AZURE_OPENAI_DEPLOYMENT"] # Azure OpenAI model deployment name
AZURE_OPENAI_ENDPOINT = os.environ["AZURE_OPENAI_ENDPOINT"] # Azure OpenAI endpoint
{{/useAzureOpenAI}}
{{#useOpenAI}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,19 @@ def __evaluate_boolean_expression(self, expression, data):
def __evaluate_if_expression(self, input_str, data):
# Only support expression like ${if(data,data,'value')} in TextBlock element
pattern = r"\$\{if\(([^,]+),([^,]+),([^)]+)\)\}"

def eval_match(match):
condition_var, true_var, false_var = match.groups()
condition_result = self.__evaluate_variable_value(condition_var, data)
if condition_result:
return self.__evaluate_variable_value(true_var, data)
true_value = self.__evaluate_variable_value(true_var, data)
return str(true_value)
else:
return self.__evaluate_variable_value(false_var, data)

false_value = self.__evaluate_variable_value(false_var, data)
return str(false_value)

result_str = re.sub(pattern, eval_match, input_str)

return result_str

def __evaluate_jsonStringify_expression(self, expression, data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ deploy:
# Generate runtime environment variables
- uses: file/createOrUpdateEnvironmentFile
with:
target: ./.localConfigs
target: ./.env
envs:
BOT_ID: ${{BOT_ID}}
BOT_PASSWORD: ${{SECRET_BOT_PASSWORD}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ deploy:
# Generate runtime environment variables
- uses: file/createOrUpdateEnvironmentFile
with:
target: ./.localConfigs.testTool
target: ./.env
envs:
{{#useOpenAI}}
OPENAI_API_KEY: ${{SECRET_OPENAI_API_KEY}}
Expand Down

0 comments on commit 241189a

Please sign in to comment.