Skip to content

Serverless Azure OpenAI Assistant Quick Start Function Calling

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

Azure-Samples/azure-openai-assistant-javascript

Serverless Azure OpenAI Assistant Quick Start
Function Calling

Open project in GitHub Codespaces Build Status Node version License

⭐ Like and fork this sample to receive lastest changes and updates!

OverviewGet startedRun the sampleResources

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.

Overview

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. In this article, we provide an in-depth walkthrough of getting started with the Assistants API.

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.

Get started

There are multiple ways to get started with this project.

The quickest way is to use GitHub Codespaces that provides a preconfigured environment for you. Alternatively, you can set up your local environment following the instructions below.

Use your local environment

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)

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>

Use GitHub Codespaces

You can run this project directly in your browser by using GitHub Codespaces, which will open a web-based VS Code:

Open in GitHub Codespaces

Use a VS Code dev container

A similar option to Codespaces is VS Code Dev Containers, that will open the project in your local VS Code instance using the Dev Containers extension.

You will also need to have Docker installed on your machine to run the container.

Open in Dev Containers

Run the sample locally (with Azure OpenAI models)

First you need to provision the Azure resources needed to run the sample. Follow the instructions in the Deploy the sample to Azure section to deploy the sample to Azure, then you'll be able to run the sample locally using the deployed Azure resources.

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.

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 install --prefix api
npm start --prefix api
  1. 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.

Deploy the sample to Azure

Azure prerequisites

  • Azure account. If you're new to Azure, get an Azure account for free to get free Azure credits to get started. If you're a student, you can also get free credits with Azure for Students.
  • Azure subscription with access enabled for the Azure OpenAI service. You can request access with this form.

Deploy the sample

  1. Open a terminal and navigate to the root of the project.
  2. Authenticate with Azure by running azd auth login.
  3. Run azd up to deploy the application to Azure. This will provision Azure resources, deploy this sample.
    • You will be prompted to select a base location for the resources.
    • By default, the OpenAI resource will be deployed to swedencentral. You can set a different location with azd env set AZURE_OPENAI_RESOURCE_GROUP_LOCATION <location>. Currently only a short list of locations is accepted. Azure OpenAI Assistants are currently available in Sweden Central, East US 2, and Australia East. For more information about model availability in those regions, see the models guide.

The deployment process will take a few minutes. Once it's done, you'll see the URL of the web app in the terminal.

You can now open the web app in your browser and start chatting with the bot.

Clean up

To clean up all the Azure resources created by this sample:

  1. Run azd down --purge
  2. When asked if you are sure you want to continue, enter y

The resource group and all the resources will be deleted.

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.

About

Serverless Azure OpenAI Assistant Quick Start Function Calling

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Code of conduct

Security policy

Stars

Watchers

Forks