Edit

Share via


Build a custom engine agent

This tutorial shows how to create a custom engine agent using Microsoft 365 Agents Toolkit (previously known as Teams Toolkit) with OpenAI.

In this tutorial, learn:

  • How to create a new custom engine agent with Agents Toolkit.
  • How to interact with your Large Language Models (LLMs) and data.
  • How the project workspace of your custom engine agent is structured.
  • How to configure your custom engine agent and test it in Agent Playground

Note

Prerequisites

Ensure you install the following tools for building and deploying your apps.
Install For using...
Agents Toolkit A Microsoft Visual Studio Code extension that creates a project scaffolding for your app. Use the latest version.
Node.js and Node Package Manager (NPM) Back-end JavaScript runtime environment. For more information, see Node.js version compatibility table for project type.
Microsoft Edge (recommended) or Google Chrome A browser with developer tools.
Visual Studio Code JavaScript, TypeScript, or SharePoint Framework (SPFx) build environments. Use the latest version.
OpenAI First create your OpenAI API key to use OpenAI's Generative Pre-trained Transformer (GPT). If you want to host your app or access resources in Microsoft Azure, you must create an Azure OpenAI service before you begin.

Note

Add support for Microsoft 365 Copilot Chat in your custom engine agent

If you want your custom engine agent to support Microsoft 365 Copilot Chat, you must install a prerelease version of Microsoft 365 Agents Toolkit (previously known as Teams Toolkit). After you install the prerelease version, follow these steps:

  1. Ensure that you enable Fx-extension: Enable Custom Engine Agent in Agents Toolkit.

    Screenshot shows how to enable custom engine agent extension in Microsoft 365 Agents Toolkit.

  2. Perform step 1. to step 4. from create your custom engine agent and ensure you select the Basic AI Chatbot Works in Teams and Microsoft 365 Copilot for step 5. in Agents Toolkit. Continue with the rest of steps (step 6. to step 11.) to create your custom engine agent.
    Screenshot shows the basic AI chatbot in Agents Toolkit.

  3. Configure your custom engine agent. Ensure you perform the following steps before you debug:

    • Ensure that the app manifest contains copilotAgents and its sub property customEngineAgents:

      "copilotAgents": { 
         "customEngineAgents": [ 
            { 
               "type": "bot", 
               "id": "<Bot-Id-Guid>" 
            } 
         ] 
         }
      
      
    • Ensure that the scopes is set as personal for bots and commandLists:

      "bots": [ 
         { 
            "botId": "<Bot-Id-Guid>", 
            "scopes": [
                "personal",
                "team",
                "groupChat"
            ],
            "commandLists": [ 
               { 
               "scopes": ["personal"], 
               "commands": [ 
                  { 
                     "title": "Sample prompt title", 
                     "description": "Description of sample prompt" 
                  } 
               ] 
               }, 
               { 
               "scopes": ["personal"], 
               "commands": [ 
                  { 
                     "title": "Sample prompt title", 
                     "description": "Description of sample prompt" 
                  } 
               ] 
               } 
            ], 
         } 
         ], 
      
      

Back to top

Create your custom engine agent

Use Agents Toolkit to create a new custom engine agent.
  1. Go to Visual Studio Code.

  2. Select the Microsoft 365 Agents Toolkit icon in the Visual Studio Code Activity Bar.

  3. Select Create a New App.

    Screenshot shows how to create a new app in Visual Studio Code.

  4. Select Custom Engine Agent.

    Screenshot shows custom engine agent option in Visual Studio Code.

  5. Select Basic AI Chatbot.

    Screenshot shows basic ai chatbot in Visual Studio Code.

  6. Select JavaScript as the programming language.

    Screenshot shows programming language in Visual Studio Code.

  7. Select OpenAI.

    Screenshot shows the TypeScript option for programming language in Agents Toolkit.

  8. Enter OpenAI Key.

  9. Select Default folder to store your project root folder in default location.

    Screenshot shows the selection of default location.

    You can also change the default location by the following steps:

  10. Select Browse.

    Screenshot shows the selection of browse location option.

  11. Select the location for project workspace.

  12. Select the Select Folder.

    Screenshot shows the folder to select.

  13. Enter a suitable name for your app and then select Enter.

    Screenshot shows where to enter the app name.

    A dialog appears. Select Yes, I trust the authors or No, I don’t trust the authors based on your requirement.

    Screenshot shows the dialog to trust or not the authors of the files in this folder.

Your custom engine agent is created in a few seconds.

Screenshot shows the app created.

After your app is created, Agents Toolkit displays the following message:

Screenshot shows the message that the feature is successfully created.

Take a tour of the source code

Have a look at what's inside this custom engine agent > Basic AI Chatbot template.

Folder name Contents
.vscode Visual Studio Code files for debugging.
appPackage Templates for the Teams application manifest.
env Name or value pairs are stored in environment files and used by m365agents.yml to customize the provisioning and deployment rules.
infra Templates for provisioning Azure resources.
src/ The source code for the notification Teams application.
src/index.js Sets up the bot app server.
src/adapter.js Sets up the bot adapter.
src/config.js Defines the environment variables.
src/prompts/chat/skprompt.txt Defines the prompt.
src/prompts/chat/config.json Configures the prompt.
src/app/app.js Handles business logics for the Basic AI Chatbot.
m365agents.yml Main project file describes your application configuration and defines the set of actions to run in each lifecycle stages.
m365agents.local.yml This override m365agents.yml with actions that enable local execution and debugging.
m365agents.playground.yml This override m365agents.yml with actions that enable local execution and debugging in Microsoft 365 Agents Playground (previously known as Teams App Test Tool).

Back to top

Configure and debug your custom engine agent

Customize the prompt for your custom engine agent.

After you create your custom engine agent, let's configure it.

  1. Go to src/prompts/chat/skprompt.txt and update the following code in skprompt.txt file:

    The following is a conversation with an AI assistant, who is an expert on answering questions over the given context.
    Responses should be in a short journalistic style with no more than 80 words. 
    

    Screenshot shows skprompt in explorer in Visual Studio Code.a

  2. From the left pane, select Run and Debug (Ctrl+Shift+D).

  3. Select Debug in Microsoft 365 Agents Playground.

    Note

    If you want to debug in either Teams or Copilot, select "Debug in Teams or Debug in Copilot.

  4. Select the F5 key.

    Screenshot shows the debug in Agents Playground.

  5. Custom engine agent runs within Agents Playground, which opens in your browser.

Back to top

Complete challenge

Test your custom engine agent.

In Agents Playground, ask questions related to your document and chat with your custom engine agent to learn more about your data.

Screenshot shows the Agents Playground.

Congratulations, you completed this tutorial!

Back to top