-
Notifications
You must be signed in to change notification settings - Fork 1
Port upstream examples (PR #512) and Azure Managed Identity docs (PR #498) #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f608bc6
Add 5 new examples and Azure Managed Identity docs
krukow e934bf0
Address review feedback: fix duplicate getToken, cleanup consistency,…
krukow 27a718a
Fix infinite_sessions.clj: use :system-message not :system-prompt
krukow 7cd9c35
Fix infinite_sessions.clj: :mode must be keyword :replace not string
krukow c2ee23b
Address review round 2: fix Azure token extraction, hook key normaliz…
krukow f68cc8d
Address review round 3: fix artifact coords, unused require, README text
krukow f2baca6
Fix BYOK limitations wording: static credentials, not key-based only
krukow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Azure Managed Identity with BYOK | ||
|
|
||
| The Copilot SDK's [BYOK mode](./byok.md) accepts static API keys, but Azure deployments often use **Managed Identity** (Entra ID) instead of long-lived keys. Since the SDK does not natively support Entra ID authentication, you can obtain a short-lived bearer token and pass it via the `:bearer-token` provider config field. | ||
|
|
||
| This guide shows how to use `DefaultAzureCredential` from the [Azure Identity SDK](https://learn.microsoft.com/java/api/overview/azure/identity-readme) to authenticate with Azure AI Foundry models through the Copilot SDK. | ||
|
|
||
| ## How It Works | ||
|
|
||
| Azure AI Foundry's OpenAI-compatible endpoint accepts bearer tokens from Entra ID in place of static API keys. The pattern is: | ||
|
|
||
| 1. Use `DefaultAzureCredential` to obtain a token for the `https://cognitiveservices.azure.com/.default` scope | ||
| 2. Pass the token as `:bearer-token` in the BYOK provider config | ||
| 3. Refresh the token before it expires (tokens are typically valid for ~1 hour) | ||
|
|
||
| ## Clojure Example | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| Add the Azure Identity SDK to your `deps.edn`: | ||
|
|
||
| ```clojure | ||
| ;; deps.edn | ||
| {:deps {com.azure/azure-identity {:mvn/version "1.15.4"} | ||
| io.github.copilot-community-sdk/copilot-sdk-clojure {:mvn/version "LATEST"}}} | ||
| ``` | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| <!-- docs-validate: skip --> | ||
| ```clojure | ||
| (require '[github.copilot-sdk :as copilot]) | ||
| (require '[github.copilot-sdk.helpers :as h]) | ||
|
|
||
| (import '[com.azure.identity DefaultAzureCredentialBuilder] | ||
| '[com.azure.core.credential TokenRequestContext]) | ||
|
|
||
| (def cognitive-services-scope "https://cognitiveservices.azure.com/.default") | ||
|
|
||
| (defn get-azure-token | ||
| "Obtain a short-lived bearer token string from Entra ID." | ||
| [] | ||
| (let [credential (.build (DefaultAzureCredentialBuilder.)) | ||
| context (doto (TokenRequestContext.) | ||
| (.addScopes (into-array String [cognitive-services-scope])))] | ||
| (-> (.getToken credential context) | ||
| (.block) | ||
| (.getToken)))) | ||
|
|
||
| (def foundry-url (System/getenv "AZURE_AI_FOUNDRY_RESOURCE_URL")) | ||
|
|
||
| (copilot/with-client-session [session | ||
| {:model "gpt-4.1" | ||
| :provider {:provider-type :openai | ||
| :base-url (str foundry-url "/openai/v1/") | ||
| :bearer-token (get-azure-token) | ||
| :wire-api :responses}}] | ||
| (println (h/query "Hello from Managed Identity!" :session session))) | ||
| ``` | ||
|
|
||
| ### Token Refresh for Long-Running Applications | ||
|
|
||
| Bearer tokens expire (typically after ~1 hour). For servers or long-running agents, refresh the token before creating each session: | ||
|
|
||
| <!-- docs-validate: skip --> | ||
| ```clojure | ||
| (require '[github.copilot-sdk :as copilot]) | ||
| (require '[github.copilot-sdk.helpers :as h]) | ||
|
|
||
| (import '[com.azure.identity DefaultAzureCredentialBuilder] | ||
| '[com.azure.core.credential TokenRequestContext]) | ||
|
|
||
| (def cognitive-services-scope "https://cognitiveservices.azure.com/.default") | ||
| (def credential (.build (DefaultAzureCredentialBuilder.))) | ||
| (def context (doto (TokenRequestContext.) | ||
| (.addScopes (into-array String [cognitive-services-scope])))) | ||
|
|
||
| (defn fresh-provider-config | ||
| "Build a provider config with a freshly obtained bearer token." | ||
| [foundry-url] | ||
| (let [token (-> (.getToken credential context) | ||
| (.block) | ||
| (.getToken))] | ||
| {:provider-type :openai | ||
| :base-url (str foundry-url "/openai/v1/") | ||
| :bearer-token token | ||
| :wire-api :responses})) | ||
|
|
||
| (def foundry-url (System/getenv "AZURE_AI_FOUNDRY_RESOURCE_URL")) | ||
|
|
||
| ;; Each session gets a fresh token | ||
| (copilot/with-client [client {}] | ||
| (dotimes [_ 3] | ||
| (copilot/with-session [session client | ||
| {:model "gpt-4.1" | ||
| :provider (fresh-provider-config foundry-url)}] | ||
| (println (h/query "Hello!" :session session))))) | ||
| ``` | ||
|
|
||
| ## Environment Configuration | ||
|
|
||
| | Variable | Description | Example | | ||
| |----------|-------------|---------| | ||
| | `AZURE_AI_FOUNDRY_RESOURCE_URL` | Your Azure AI Foundry resource URL | `https://myresource.openai.azure.com` | | ||
|
|
||
| No API key environment variable is needed — authentication is handled by `DefaultAzureCredential`, which automatically supports: | ||
|
|
||
| - **Managed Identity** (system-assigned or user-assigned) — for Azure-hosted apps | ||
| - **Azure CLI** (`az login`) — for local development | ||
| - **Environment variables** (`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) — for service principals | ||
| - **Workload Identity** — for Kubernetes | ||
|
|
||
| See the [DefaultAzureCredential documentation](https://learn.microsoft.com/java/api/com.azure.identity.defaultazurecredential) for the full credential chain. | ||
|
|
||
| ## When to Use This Pattern | ||
|
|
||
| | Scenario | Recommendation | | ||
| |----------|----------------| | ||
| | Azure-hosted app with Managed Identity | ✅ Use this pattern | | ||
| | App with existing Azure AD service principal | ✅ Use this pattern | | ||
| | Local development with `az login` | ✅ Use this pattern | | ||
| | Non-Azure environment with static API key | Use [standard BYOK](./byok.md) | | ||
| | GitHub Copilot subscription available | Use [GitHub auth](./index.md#github-signed-in-user) | | ||
|
|
||
| ## See Also | ||
|
|
||
| - [BYOK Setup Guide](./byok.md) — Static API key configuration | ||
| - [Authentication Overview](./index.md) — All authentication methods | ||
| - [Azure Identity documentation](https://learn.microsoft.com/java/api/overview/azure/identity-readme) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| (ns file-attachments | ||
| "Demonstrates sending file attachments with a prompt. | ||
| Attaches the project's deps.edn file and asks the model to analyze it." | ||
| (:require [github.copilot-sdk :as copilot])) | ||
|
|
||
| ;; See examples/README.md for usage | ||
|
|
||
| (def defaults | ||
| {:prompt "Summarize the dependencies in the attached file in 2-3 sentences." | ||
| :file-path "deps.edn"}) | ||
|
|
||
| (defn run | ||
| [{:keys [prompt file-path] | ||
| :or {prompt (:prompt defaults) file-path (:file-path defaults)}}] | ||
| (let [abs-path (.getAbsolutePath (java.io.File. file-path))] | ||
| (copilot/with-client-session [session {:on-permission-request copilot/approve-all | ||
| :model "claude-haiku-4.5"}] | ||
| (println "📎 Attaching:" abs-path) | ||
| (println "Q:" prompt) | ||
| (let [response (copilot/send-and-wait! | ||
| session | ||
| {:prompt prompt | ||
| :attachments [{:type :file :path abs-path}]})] | ||
| (println "🤖:" (get-in response [:data :content])))))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.