Skip to content

This repository demonstrates how to create a LangGraph that can interact with a Copilot Studio Agent, handle requests with FastAPI, and respond to user queries.

License

Notifications You must be signed in to change notification settings

lshade/copilot-langgraph-agent-fastapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

copilot-langgraph-agent-fastapi

About

This repository demonstrates how to create a LangGraph that can interact with a Copilot Studio Agent, handle requests with FastAPI, and respond to user queries.

Sources

The following instructions and the code in this repo are adapted from these two samples:

Copilot Studio Client

This is a sample to show how to use the @microsoft/agents-copilotstudio-client package to talk to an Agent hosted in CopilotStudio.

Prerequisite

To set up this sample, you will need the following:

  1. An Azure Open AI resource with a deployed model.
  2. An Agent Created in Microsoft Copilot Studio or access to an existing Agent.
  3. Ability to Create an Application Identity in Azure for a Public Client/Native App Registration Or access to an existing Public Client/Native App registration with the CopilotStudio.Copilots.Invoke API Permission assigned.

Authentication

The Copilot Studio Client requires a User Token to operate. For this sample, we are using a user interactive flow to get the user token for the application ID created above. Other flows are allowed.

Important

The token is cached in the user machine

Create an Agent in Copilot Studio

  1. Create an Agent in Copilot Studio
    1. Publish your newly created Copilot
    2. Goto Settings => Advanced => Metadata and copy the following values, You will need them later:
      1. Schema name
      2. Environment Id

Create an Application Registration in Entra ID

This step will require permissions to create application identities in your Azure tenant. For this sample, you will create a Native Client Application Identity, which does not have secrets.

  1. Open https://portal.azure.com
  2. Navigate to Entra Id
  3. Create a new App Registration in Entra ID
    1. Provide a Name
    2. Choose "Accounts in this organization directory only"
    3. In the "Select a Platform" list, Choose "Public Client/native (mobile & desktop)
    4. In the Redirect URI url box, type in http://localhost (note: use HTTP, not HTTPS)
    5. Then click register.
  4. In your newly created application
    1. On the Overview page, Note down for use later when configuring the example application:
      1. The Application (client) ID
      2. The Directory (tenant) ID
    2. Go to API Permissions in Manage section
    3. Click Add Permission
      1. In the side panel that appears, Click the tab API's my organization uses
      2. Search for Power Platform API.
        1. If you do not see Power Platform API see the note at the bottom of this section.
      3. In the Delegated permissions list, choose CopilotStudio and Check CopilotStudio.Copilots.Invoke
      4. Click Add Permissions
    4. (Optional) Click Grant Admin consent for copilotsdk

Tip

If you do not see Power Platform API in the list of API's your organization uses, you need to add the Power Platform API to your tenant. To do that, goto Power Platform API Authentication and follow the instructions on Step 2 to add the Power Platform Admin API to your Tenant

Instructions - Configure the Example Application

With the above information, you can now run the client CopilotStudioClient sample.

  1. Open the env.TEMPLATE file and rename it to .env.
  2. Configure the values based on what was recorded during the setup phase.
  OPENAI_API_KEY="" # API Key for the OpenAI resource, this is used to call the OpenAI model.
  OPENAI_ENDPOINT="" # Endpoint for the OpenAI resource, this should be in the format `https://<resource-name>.openai.azure.com/`.
  OPENAI_API_VERSION="" # API Version for the OpenAI resource, this should be in the format `2023-05-15`.
  OPENAI_MODEL="" # Model to use for the OpenAI resource, this should be in the format `gpt-4o-mini`.

  COPILOT_AGENT_NAME="" # Name of the Copilot to use, such as "SharePoint".
  ENVIRONMENT_ID="" # Environment ID of environment with the CopilotStudio App.
  AGENT_IDENTIFIER="" # Schema Name of the Copilot to use
  TENANT_ID="" # Tenant ID of the App Registration used to login, this should be in the same tenant as the Copilot.
  APP_CLIENT_ID="" # App ID of the App Registration used to login, this should be in the same tenant as the CopilotStudio environment.

Setting Up Virtual Environment and installing the SDK

This guide explains how to create and activate a Python virtual environment using venv for Python versions 3.9 to 3.11.

What is a Virtual Environment?

A virtual environment is an isolated Python environment that allows you to install packages for a specific project without affecting your system's global Python installation. This helps avoid package conflicts between different projects.

Prerequisites

  • Python 3.9, 3.10, or 3.11 installed on your system
  • Basic knowledge of command line operations

Creating a Virtual Environment

On Linux/macOS

  1. Open a terminal window
  2. Navigate to your project directory:
    cd the-root-of-this-project
  3. Create a virtual environment:
    python3 -m venv venv

On Windows

  1. Open Command Prompt or PowerShell
  2. Navigate to your project directory:
    cd the-root-of-this-project
    
  3. Create a virtual environment:
    python -m venv venv
    

Activating the Virtual Environment

On Linux/macOS

source venv/bin/activate

On Windows

Command Prompt

venv\Scripts\activate.bat

PowerShell

venv\Scripts\Activate.ps1

Once activated, you'll notice your command prompt changes to show the name of the activated environment. For example:

(venv) $

Deactivating the Virtual Environment

When you're done working in the virtual environment, you can deactivate it by running:

deactivate

Verifying Python Version

To verify the Python version in your virtual environment:

python --version

Make sure it shows a version between 3.9 and 3.11 as required.

Installing Test Packages

After activating your virtual environment, you can install packages from pypi test (the test index will be replaced with the regular index before going out of preview):

pip install -r requirements.txt
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-core
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-authorization
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-connector
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-client
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-builder
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-authentication-msal
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-copilotstudio-client
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-hosting-aiohttp
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ microsoft-agents-storage

Running the FastAPI Server

  1. To run the FastAPI server, you can use the following command:
    uvicorn app:app --host 0.0.0.0 --port 8000
  2. This should challenge you to login and connect to the Copilot Studio Hosted agent.
  3. Send a POST request to the '/chat' endpoint with a JSON body containing the query in the 'query' field. For example, you can use curl to send a request like this in another terminal:
    curl -Method POST "http://localhost:8000/chat" -Headers @{"Content-Type" = "application/json"} -Body '{"query": "your_query_here"}'

About

This repository demonstrates how to create a LangGraph that can interact with a Copilot Studio Agent, handle requests with FastAPI, and respond to user queries.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages