Thanks to visit codestin.com
Credit goes to github.com

Skip to content

zhenbzha/chainlit-agent

Chainlit Agent: Intelligent Assistant with Azure AI Agent

Open in GitHub Codespaces Open in Dev Containers

Table of Contents

Overview

This project implements an intelligent assistant that showcases:

  1. Azure AI Foundry SDK for building, deploying, and managing AI applications at scale
  2. Azure AI Agents for orchestrating complex tasks and building extensible AI solutions
  3. A Chainlit-based chat interface for interactive user engagement
  4. FastAPI endpoints for programmatic access

Features

The project provides the following features:

It also comes with:

  • Sample implementation of both chat and API interfaces
  • Development environment setup with VS Code
  • Azure service integration examples
  • Deployment configurations for Azure
  • Pre-populated with Contoso sample data (customizable for your specific data needs)

Pre-requisites

To deploy and explore the sample, you will need:

  1. An active Azure subscription - Signup for a free account here
  2. Azure OpenAI Services - Learn about Azure OpenAI
  3. Azure AI Search service - Learn more about AI Search
  4. Available Quota for GPT models in your preferred region

From a tooling perspective, familiarity with the following is useful:

  • Visual Studio Code (and extensions)
  • Python and FastAPI
  • Docker and containerization
  • Azure services and deployment

Getting Started

You have two options for setting up your development environment:

  1. Use GitHub Codespaces - for a prebuilt dev environment in the cloud
  2. Use Docker Desktop - for a prebuilt dev environment on local device

Choose the option that best suits your needs and development style.

GitHub Codespaces

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

    Open in GitHub Codespaces

  2. Once the codespaces environment is ready (this can take several minutes), open a new terminal in that VS Code instance - and proceed to the Development step.

VS Code Dev Containers

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

  1. Install Docker Desktop (if not installed), then start it.

  2. Open the project in your local VS Code by clicking the button below:

    Open in Dev Containers

  3. Once the VS Code window shows the project files (this can take several minutes), open a new terminal in that VS Code instance - and proceed to the Development step.

Development

Once you've completed the setup the project (using Codespaces, Dev Containers), you should now have a Visual Studio Code editor open, with the project files loaded, and a terminal open for running commands. Let's verify that all required tools are installed.

az version
azd version
python --version

We can now proceed with next steps - click to expand for detailed instructions.

1️⃣ | Authenticate With Azure
  1. Open a VS Code terminal and authenticate with Azure CLI. Use the --use-device-code option if authenticating from GitHub Codespaces. Complete the auth workflow as guided.

    az login --use-device-code
  2. Now authenticate with Azure Developer CLI in the same terminal. Complete the auth workflow as guided.

    azd auth login --use-device-code
  3. You should see: Logged in on Azure. This will create a folder under .azure/ in your project to store the configuration for this deployment. You may have multiple azd environments if desired.

2️⃣ | Provision-Deploy with AZD
  1. Run azd up to provision infrastructure and deploy the application, with one command. (You can also use azd provision, azd deploy separately if needed)

    azd up
  2. You will be asked for a subscription for provisioning resources, an environment name that maps to the resource group, and a location for deployment. Refer to the Region Availability guidance to select the region that has the desired models and quota available.

  3. The azd up command can take 15-20 minutes to complete. Successful completion sees a SUCCESS: ... messages posted to the console. We can now validate the outcomes.

3️⃣ | Validate and Deploy
  1. Test the FastAPI backend:

    • Get the API URL from the azd up output
    • Visit https://<your-api-app>.azurecontainerapps.io/docs in your browser
    • You should see the Swagger UI documentation
    • Test the /api/test endpoint with a sample query
    • Verify that responses from Azure OpenAI are working
  2. Test the Chainlit interface:

    • Get the Chainlit URL from the azd up output
    • Visit https://<your-chainlit-app>.azurecontainerapps.io in your browser
    • You should see the Chainlit chat interface
    • Try a test conversation
    • Verify that the chat responses are working correctly

✅ | Congratulations! - Your development environment is ready!

Testing

To test the application in your local environment:

  1. Start the FastAPI backend:

    ./start_fastapi.sh
    ```   This will start the API server on http://localhost:8000
    
  2. Start the Chainlit interface:

    ./start_chainlit.sh
  3. Testing the Components:

    • For API: Access http://localhost:8000/docs to test endpoints via Swagger UI
    • For Chat: Access http://localhost to test the chat interface
    • Try different types of queries to ensure proper handling
    • Verify Azure service integrations are working correctly

Guidance

Costs

Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. The majority of the Azure resources used in this infrastructure are on usage-based pricing tiers. However, Azure Container Registry has a fixed cost per registry per day.

You can try the Azure pricing calculator for the resources:

  • Azure OpenAI Service: S0 tier, GPT-4 model. Pricing is based on token count.
  • Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage.
  • Azure Container Registry: Basic tier.
  • Log analytics: Pay-as-you-go tier. Costs based on data ingested.

⚠️ 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.

Security Guidelines

This repository contains sample code and guidance for demonstration and learning purposes. It is not intended for production use without proper review and adaptation.

Required Security Measures

Before deploying to production:

  • Ensure proper security measures are implemented
  • Add appropriate error handling and logging
  • Test thoroughly in your environment
  • Follow your organization's deployment and security guidelines

Authentication and Authorization

This template uses Managed Identity for all Azure service communication.

Repository Security

To ensure continued best practices in your own repository, we recommend that anyone creating solutions based on our templates ensure that the Github secret scanning setting is enabled.

Additional Security Considerations

Consider implementing these additional security measures:

Resources

  1. Chainlit Documentation
  2. Azure OpenAI Documentation
  3. Azure AI Search Documentation
  4. Azure AI Agents Documentation
  5. FastAPI Documentation

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. Learn more here:

Responsible AI Guidelines

This project follows Microsoft's responsible AI guidelines and best practices:


About

No description or website provided.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published