xbotter / chat-copilot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chat Copilot Sample Application

This sample allows you to build your own integrated large language model (LLM) chat copilot. The sample is built on Microsoft Semantic Kernel and has two components: a frontend React web app and a backend .NET web API service.

These quick-start instructions run the sample locally. To deploy the sample to Azure, please view Deploying Chat Copilot.

IMPORTANT: This sample is for educational purposes only and is not recommended for production deployments.

IMPORTANT: Each chat interaction will call Azure OpenAI/OpenAI which will use tokens that you may be billed for.

ChatCopilot

Requirements

You will need the following items to run the sample:

AI Service Requirement
Azure OpenAI - Access
- Resource
- Deployed models (gpt-35-turbo and text-embedding-ada-002)
- Endpoint
- API key
OpenAI - Account
- API key

Instructions

Register an application

  1. Follow these instructions and use the values below:
    • Supported account types: "Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)"
    • Redirect URI (optional): Single-page application (SPA) and use http://localhost:3000.
  2. Take note of the Application (client) ID. Chat Copilot will use this ID for authentication.

Windows

  1. Open PowerShell as an administrator.

  2. Setup your environment.

    cd <path to chat-copilot>\scripts\
    .\Install.ps1

    NOTE: This script will install Chocolatey, dotnet-7.0-sdk, nodejs, and yarn.

  3. Configure Chat Copilot.

    .\Configure.ps1 -AIService {AI_SERVICE} -APIKey {API_KEY} -Endpoint {AZURE_OPENAI_ENDPOINT} -ClientId {AZURE_APPLICATION_ID}
    • AI_SERVICE: AzureOpenAI or OpenAI.

    • API_KEY: The API key for Azure OpenAI or for OpenAI.

    • AZURE_OPENAI_ENDPOINT: The Azure OpenAI resource Endpoint address. Omit -Endpoint if using OpenAI.

    • AZURE_APPLICATION_ID: The Application (client) ID associated with the registered application.

    • (Optional): To set a specific Tenant Id, use the parameter:

      -TenantId {TENANT_ID}
    • IMPORTANT: For AzureOpenAI, if you deployed models gpt-35-turbo and text-embedding-ada-002 with custom names (instead of each own's given name), also use the parameters:

      -CompletionModel {DEPLOYMENT_NAME} -EmbeddingModel {DEPLOYMENT_NAME} -PlannerModel {DEPLOYMENT_NAME}
  4. Run Chat Copilot locally. This step starts both the backend API and frontend application.

    .\Start.ps1

    It may take a few minutes for Yarn packages to install on the first run.

    NOTE: Confirm pop-ups are not blocked and you are logged in with the same account used to register the application.

Linux/macOS

  1. Open Bash as an administrator.

  2. Configure environment.

    cd <path to chat-copilot>/scripts/
    chmod +x *.sh

    Ubuntu/Debian Linux

    ./Install-apt.sh

    NOTE: This script uses apt to install dotnet-sdk-7.0, nodejs, and yarn.

    macOS

    ./Install-brew.sh

    NOTE: This script uses homebrew to install dotnet-sdk, nodejs, and yarn.

  3. Configure Chat Copilot.

    ./Configure.sh --aiservice {AI_SERVICE} --apikey {API_KEY} --endpoint {AZURE_OPENAI_ENDPOINT} --clientid {AZURE_APPLICATION_ID}
    • AI_SERVICE: AzureOpenAI or OpenAI.

    • API_KEY: The API key for Azure OpenAI or for OpenAI.

    • AZURE_OPENAI_ENDPOINT: The Azure OpenAI resource Endpoint address. Omit --endpoint if using OpenAI.

    • AZURE_APPLICATION_ID: The Application (client) ID associated with the registered application.

    • (Optional): To set a specific Tenant Id, use the parameter:

      --tenantid {TENANT_ID}
    • IMPORTANT: For AzureOpenAI, if you deployed models gpt-35-turbo and text-embedding-ada-002 with custom names (instead of each own's given name), also use the parameters:

      --completionmodel {DEPLOYMENT_NAME} --embeddingmodel {DEPLOYMENT_NAME} --plannermodel {DEPLOYMENT_NAME}
  4. Run Chat Copilot locally. This step starts both the backend API and frontend application.

    ./Start.sh

    It may take a few minutes for Yarn packages to install on the first run.

    NOTE: Confirm pop-ups are not blocked and you are logged in with the same account used to register the application.

(Optional) Enable backend authorization via Azure AD

  1. Ensure you created the required application registration mentioned in Register an application

  2. Create a second application registration to represent the web api

    For more details on creating an application registration, go here.

    1. Give the app registration a name

    2. As Supported account type choose Accounts in any organizational directory and personal Microsoft Accounts

    3. Do not configure a Redirect Uri

  3. Expose an API within the second app registration

    1. Select Expose an API from the menu

    2. Add an Application ID URI

      1. This will generate an api:// URI with a generated for you

      2. Click Save to store the generated URI

    3. Add a scope for access_as_user

      1. Click Add scope

      2. Set Scope name to access_as_user

      3. Set Who can consent to Admins and users

      4. Set Admin consent display name and User consent display name to Access copilot chat as a user

      5. Set Admin consent description and User consent description to Allows the accesses to the Copilot chat web API as a user

  4. Add permissions to web app frontend to access web api as user

    1. Open app registration for web app frontend

    2. Go to API Permissions

    3. Click Add a permission

    4. Select the tab My APIs

    5. Choose the app registration representing the web api backend

    6. Select permissions access_as_user

    7. Click Add permissions

  5. Update frontend web app configuration

    1. Open .env file

    2. Set the value of REACT_APP_AAD_API_SCOPE to your application ID URI followed by the scope access_as_user, e.g. api://12341234-1234-1234-1234-123412341234/access_as_user

  6. Update backend web api configuration

    1. Open appsettings.json

    2. Set the value of Authorization:AzureAd:Audience to your application ID URI

Troubleshooting

  1. Issue: Unable to load chats.

    Details: interactionin_progress: Interaction is currently in progress.

    Explanation: The WebApp can display this error when the application is configured for a different AAD tenant from the browser, (e.g., personal/MSA account vs work/school account).

    Solution: Either use a private/incognito browser tab or clear your browser credentials/cookies. Confirm you are logged in with the same account used to register the application.

  2. Issue:: Challenges using text completion models, such as text-davinci-003

    Solution: For OpenAI, see model endpoint compatibility for the complete list of current models supporting chat completions. For Azure OpenAI, see model summary table and region availability.

  3. Issue: Localhost SSL certificate errors / CORS errors

    Cert-Issue

    Explanation: Your browser may be blocking the frontend access to the backend while waiting for your permission to connect.

    Solution:

    1. Confirm the backend service is running. Open a web browser and navigate to https://localhost:40443/healthz
      • You should see a confirmation message: Healthy
      • If your browser asks you to acknowledge the risks of visiting an insecure website, you must acknowledge this before the frontend can connect to the backend server.
    2. Navigate to http://localhost:3000 or refresh the page to use the Chat Copilot application.
  4. Issue: Yarn is not working.

    Explanation: You may have the wrong Yarn version installed such as v2.x+.

    Solution: Use the classic version.

    npm install -g yarn
    yarn set version classic
  5. Issue: Missing /usr/share/dotnet/host/fxr folder.

    Details: "A fatal error occurred. The folder [/usr/share/dotnet/host/fxr] does not exist" when running dotnet commands on Linux.

    Explanation: When .NET (Core) was first released for Linux, it was not yet available in the official Ubuntu repo. So instead, many of us added the Microsoft APT repo in order to install it. Now, the packages are part of the Ubuntu repo, and they are conflicting with the Microsoft packages. This error is a result of mixed packages. (Source: StackOverflow)

    Solution:

    # Remove all existing packages to get to a clean state:
    sudo apt remove --assume-yes dotnet*;
    sudo apt remove --assume-yes aspnetcore*;
    sudo apt remove --assume-yes netstandard*;
    
    # Set the Microsoft package provider priority
    echo -e "Package: *\nPin: origin \"packages.microsoft.com\"\nPin-Priority: 1001" | sudo tee /etc/apt/preferences.d/99microsoft-dotnet.pref;
    
    # Update and install dotnet
    sudo apt update;
    sudo apt install --assume-yes dotnet-sdk-7.0;

About

License:MIT License


Languages

Language:TypeScript 48.5%Language:C# 42.6%Language:Shell 3.5%Language:PowerShell 2.6%Language:Bicep 2.5%Language:JavaScript 0.2%Language:CSS 0.1%Language:HTML 0.0%