|
| 1 | +# What this sample demonstrates |
| 2 | + |
| 3 | +A [LangGraph](https://langchain-ai.github.io/langgraph/) agent hosted using the **Invocations protocol** with session management. It uses the configuration-driven `langchain_azure_ai.agents.hosting.run` entrypoint instead of constructing an `InvocationsHostServer` in application code. |
| 4 | + |
| 5 | +> **No agent code changes are required to migrate an existing LangChain |
| 6 | +> agent to this approach.** Keep the existing graph implementation as-is, |
| 7 | +> point `langgraph.json` at its exported graph, and launch the hosting |
| 8 | +> entrypoint with the desired protocol. |
| 9 | +
|
| 10 | +Multi-turn continuity is provided by a LangGraph `MemorySaver` |
| 11 | +checkpointer: the resolved `agent_session_id` is forwarded to the graph |
| 12 | +as `RunnableConfig.configurable.thread_id`, so each session's history is |
| 13 | +preserved in process memory. |
| 14 | + |
| 15 | +## How It Works |
| 16 | + |
| 17 | +### Graph and model integration |
| 18 | + |
| 19 | +The agent uses `langchain_openai.ChatOpenAI` with an Azure bearer token |
| 20 | +provider from `DefaultAzureCredential` and an OpenAI-compatible endpoint |
| 21 | +from `azure.ai.projects.AIProjectClient` (`az login` is enough for local |
| 22 | +development). The graph is the same no-tool `create_agent` graph with |
| 23 | +`MemorySaver` used by the other minimal Invocations samples in this repo. |
| 24 | + |
| 25 | +See [src/langgraph-run-invocations/main.py](src/langgraph-run-invocations/main.py) |
| 26 | +for the graph and [src/langgraph-run-invocations/langgraph.json](src/langgraph-run-invocations/langgraph.json) |
| 27 | +for its entrypoint configuration. |
| 28 | + |
| 29 | +### Migrating an Existing Agent |
| 30 | + |
| 31 | +An existing LangChain agent that exports a compiled graph does not need a |
| 32 | +hosting wrapper, an `InvocationsHostServer` import, or a new `main()` |
| 33 | +function. Add a `langgraph.json` file that references the existing graph |
| 34 | +symbol, then use the command shown below. The agent's model, tools, |
| 35 | +prompts, state, checkpointer, and graph code remain unchanged. |
| 36 | + |
| 37 | +### Agent Hosting |
| 38 | + |
| 39 | +The `langgraph.json` file maps the `agent` name to the compiled graph in |
| 40 | +`main.py`. The hosting entrypoint loads that graph and exposes it through |
| 41 | +the protocol selected by the required `--protocol` argument. This sample |
| 42 | +selects `invocations` in both its local command and Docker image. |
| 43 | + |
| 44 | +## Option 1: Azure Developer CLI (`azd`) |
| 45 | + |
| 46 | +### Prerequisites |
| 47 | + |
| 48 | +1. **Azure Developer CLI (`azd`)** — [Install azd](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/install-azd) |
| 49 | +2. Install the Foundry extension: |
| 50 | + |
| 51 | + ```bash |
| 52 | + azd ext install microsoft.foundry |
| 53 | + ``` |
| 54 | + |
| 55 | +3. Authenticate: |
| 56 | + |
| 57 | + ```bash |
| 58 | + azd auth login |
| 59 | + ``` |
| 60 | + |
| 61 | +### Initialize the agent project |
| 62 | + |
| 63 | +No cloning required. Create a new folder and initialize from the manifest: |
| 64 | + |
| 65 | +```bash |
| 66 | +mkdir hosted-langgraph-agent && cd hosted-langgraph-agent |
| 67 | +azd ai agent init -m https://github.com/microsoft-foundry/foundry-samples/blob/main/samples/python/hosted-agents/langgraph/invocations/03-run/azure.yaml |
| 68 | +``` |
| 69 | + |
| 70 | +Follow the prompts to configure your Foundry project and model deployment. |
| 71 | + |
| 72 | +### Provision Azure resources (if needed) |
| 73 | + |
| 74 | +If you don't already have a Foundry project and model deployment: |
| 75 | + |
| 76 | +```bash |
| 77 | +azd provision |
| 78 | +``` |
| 79 | + |
| 80 | +### Run the agent locally |
| 81 | + |
| 82 | +```bash |
| 83 | +azd ai agent run --no-client |
| 84 | +``` |
| 85 | + |
| 86 | +The agent host will start on `http://localhost:8088`. |
| 87 | + |
| 88 | +### Invoke the local agent |
| 89 | + |
| 90 | +In a separate terminal, invoke the running agent: |
| 91 | + |
| 92 | +```bash |
| 93 | +azd ai agent invoke --local '{"message": "Hello!"}' |
| 94 | +``` |
| 95 | + |
| 96 | +Or invoke directly with curl. The returned `x-agent-session-id` header is used for multi-turn conversations: |
| 97 | + |
| 98 | +```bash |
| 99 | +curl -i -X POST http://localhost:8088/invocations \ |
| 100 | + -H "Content-Type: application/json" \ |
| 101 | + -d '{"message": "Hello!"}' |
| 102 | +``` |
| 103 | + |
| 104 | +### Deploy to Foundry |
| 105 | + |
| 106 | +Once tested locally, deploy to Microsoft Foundry: |
| 107 | + |
| 108 | +```bash |
| 109 | +azd deploy |
| 110 | +``` |
| 111 | + |
| 112 | +## Option 2: VS Code (Foundry Toolkit) |
| 113 | + |
| 114 | +### Prerequisites |
| 115 | + |
| 116 | +1. **VS Code** with the **[Foundry Toolkit](https://marketplace.visualstudio.com/items?itemName=ms-windows-ai-studio.windows-ai-studio)** extension installed. |
| 117 | +2. Install the **[Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python)** extension pack if you want to debug the sample locally. |
| 118 | + |
| 119 | +### Set up the Python virtual environment |
| 120 | + |
| 121 | +- Open the Command Palette (`Ctrl+Shift+P`) and run **Python: Create Environment...** to create a virtual environment in the project. |
| 122 | +- Ensure `pip` is version 26.1 or newer (check with `pip --version`). Older versions fail to resolve this sample's dependencies. Upgrade if needed: |
| 123 | + |
| 124 | + ```bash |
| 125 | + python -m pip install --upgrade pip |
| 126 | + ``` |
| 127 | + |
| 128 | +- Install dependencies in the virtual environment: |
| 129 | + |
| 130 | + ```bash |
| 131 | + pip install -r src/langgraph-run-invocations/requirements.txt |
| 132 | + ``` |
| 133 | + |
| 134 | +### Run locally |
| 135 | + |
| 136 | +```bash |
| 137 | +cd src/langgraph-run-invocations |
| 138 | +python -m langchain_azure_ai.agents.hosting.run --protocol invocations |
| 139 | +``` |
| 140 | + |
| 141 | +Then invoke it: |
| 142 | + |
| 143 | +```bash |
| 144 | +curl -X POST http://127.0.0.1:8088/invocations \ |
| 145 | + -H "Content-Type: application/json" \ |
| 146 | + -d '{"message": "Hello!"}' |
| 147 | +``` |
| 148 | + |
| 149 | +## Troubleshooting |
| 150 | + |
| 151 | +### Azure OpenAI Permission Denied (401) |
| 152 | + |
| 153 | +This sample calls the project's model endpoint through LangChain. The hosted agent's managed identity needs the **Foundry User** role on the project. Assign that role at the project scope and retry after role propagation completes. |
0 commit comments