Skip to content

Serverless Azure OpenAI Assistant Quick Start Function Calling

License

Notifications You must be signed in to change notification settings

Azure-Samples/azure-openai-assistant-javascript

Repository files navigation

Serverless Azure OpenAI Assistant Quick Start
Function Calling

Open project in GitHub Codespaces Build Status Node version License

Azure OpenAI Assistants allows you to create AI assistants tailored to your needs through custom instructions and augmented by advanced tools like code interpreter, and custom functions.

(Like and fork this sample to receive lastest changes and updates)

Features Architecture Diagram Getting Started Guidance Resources Troubleshooting Contributing Trademarks License Give us a star

Screenshot showing the assistant app in action

This sample shows how to quickly get started with OpenAI Assistant on Azure. The application is hosted on Azure Static Web Apps and Azure Functions. You can use it as a starting point for building more complex Assistant and Agent applications.

Features

This project demonstrates how to build a simple Assistant application using Azure OpenAI Assistants. The Assistant can help you:

  • Retrieve financial information, such as stock values.
  • Answer questions.
  • Send emails,
  • Run custom functions.

Architecture Diagram

Screenshot showing the assistant app high level diagram

This application is built around two main components:

  • A simple HTML page with a vanilla CSS and JavaScript files, and hosted on Azure Static Web Apps. The code is located in the src folder.

  • A serverless API built with Azure Functions and using OpenAI JavaScript SDK. The code is located in the api folder.

Getting Started

You have a few options for getting started with this template. The quickest way to get started is GitHub Codespaces, since it will setup all the tools for you, but you can also set it up locally. You can also use a VS Code dev container

This template uses gpt-35-turbo version 1106 which may not be available in all Azure regions. Check for up-to-date region availability and select a region during deployment accordingly

  • We recommend using swedencentral

GitHub Codespaces

You can run this template virtually by using GitHub Codespaces. The button will open a web-based VS Code instance in your browser:

  1. Open the template (this may take several minutes) Open in GitHub Codespaces

  2. Open a terminal window

  3. Sign into your Azure account:

     azd auth login --use-device-code
  4. Provision the Azure resources and deploy your code:

     azd up
  5. Open a terminal and navigate to the root of the project, then run the API server first:

     npm install --prefix api
     npm start --prefix api
  6. Open another terminal and navigate to the root of the project, then run the webapp server:

     npm install
     npm start

Open the URL http://localhost:4280 in your browser to interact with the Assistant.

VS Code Dev Containers

A related option is VS Code Dev Containers, which will open the project in your local VS Code using the Dev Containers extension:

  1. Start Docker Desktop (install it if not already installed)

  2. Open the project: Open in Dev Containers

  3. In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window.

  4. Sign into your Azure account:

     azd auth login
  5. Provision the Azure resources and deploy your code:

     azd up
  6. Open a terminal and navigate to the root of the project, then run the API server first:

     npm install --prefix api
     npm start --prefix api
  7. Open another terminal and navigate to the root of the project, then run the webapp server:

     npm install
     npm start

Open the URL http://localhost:4280 in your browser to interact with the Assistant.

  1. Configure a CI/CD pipeline:

    azd pipeline config

Local Environment

Prerequisites

You need to install following tools to work on your local machine:

  • Node.js LTS
  • Azure Developer CLI
  • SWA CLI
  • Git
  • PowerShell 7+ (for Windows users only)
    • Important: Ensure you can run pwsh.exe from a PowerShell command. If this fails, you likely need to upgrade PowerShell.
    • Instead of Powershell, you can also use Git Bash or WSL to run the Azure Developer CLI commands.
  • (Optional - if you are not using SWA CLI) Azure Functions Core Tools (should be installed automatically with NPM, only install manually if the API fails to start)
  • This template uses gpt-35-turbo version 1106 which may not be available in all Azure regions. Check for up-to-date region availability and select a region during deployment accordingly
    • We recommend using swedencentral

Then you can get the project code:

  1. Fork the project to create your own copy of this repository.
  2. On your forked repository, select the Code button, then the Local tab, and copy the URL of your forked repository.
  3. Open a terminal and run this command to clone the repo: git clone <your-repo-url>

Quickstart (with Azure OpenAI models)

  1. Bring down the template code:

    azd init --template azure-openai-assistant-javascript

    This will perform a git clone

  2. Sign into your Azure account:

     azd auth login
  3. Install all dependencies:

     npm install
     npm install --prefix api
  4. Provision and deploy the project to Azure:

    azd up
  5. Configure a CI/CD pipeline:

    azd pipeline config

Once your deployment is complete, you should see a .env file in the api folder. This file contains the environment variables needed to run the application using Azure resources.

Also, in order for the Assistant to send emails, you need to provide the following env variables in the ./api/.env file:

EMAIL_RECEIVER="your email address"
EMAIL_SENDER_NAME="Azure OpenAI Assistant"
EMAIL_SENDER_USERNAME="sender email address"
# Generate an application password from the MFA mobile application
EMAIL_SENDER_APP_PASSWORD="foobar" 

Important: Please follow this guide to generate an Application Password if you are using MFA.

Local Development

To run the sample, run the following commands, which will start the web app, and the API locally.

  1. Open a terminal and navigate to the root of the project, then run the API server first:

     npm start --prefix api
  2. Open another terminal and navigate to the root of the project, then run the webapp server:

     npm start

Open the URL http://localhost:4280 in your browser to interact with the Assistant.

Guidance

Region Availability

This template uses gpt-35-turbo version 1106 which may not be available in all Azure regions. Check for up-to-date region availability and select a region during deployment accordingly

  • We recommend using swedencentral

Costs

Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. However, you can use the Azure pricing calculator for the resources below to get an estimate.

  • Azure Container Apps: Consumption plan, Free for the first 2M executions. Pricing per execution and memory used. Pricing
  • Azure OpenAI: Standard tier, GPT and Ada models. Pricing per 1K tokens used, and at least 1K tokens are used per question. Pricing

Warning

To avoid unnecessary costs, remember to take down your app if it's no longer in use, either by deleting the resource group in the Portal or running azd down --purge.

Security

Note

When implementing this template please specify whether the template uses Managed Identity or Key Vault

This template has either Managed Identity or Key Vault built in to eliminate the need for developers to manage these credentials. Applications can use managed identities to obtain Microsoft Entra tokens without having to manage any credentials. Additionally, we have added a GitHub Action tool that scans the infrastructure-as-code files and generates a report containing any detected issues. To ensure best practices in your repo we recommend anyone creating solutions based on our templates ensure that the Github secret scanning setting is enabled in your repos.

Resources

Here are some resources to learn more about the technologies used in this sample:

You can also find more Azure AI samples here.

Troubleshooting

If you can't find a solution to your problem, please open an issue in this repository.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.