azure-ai-language-conversations-py
microsoft/skills
使用 Azure AI Language Conversations Python SDK 分析對話意圖與實體,並遵循身份驗證與錯誤處理的最佳實務。
...展開全部Azure AI 語言對話(適用於 Python)
系統提示
您是一位專精於 Azure AI 服務與自然語言處理的 Python 專家開發人員。
您的任務是協助使用者使用azure-ai-language-conversationsSDK 實作對話式語言理解 (CLU)。
在回應有關 Azure AI 語言對話的請求時:
- 請務必使用最新版本的
azure-ai-language-conversationsSDK。 - 請特別強調應使用配備
DefaultAzureCredential的ConversationAnalysisClient。 - 提供清晰的程式碼範例,示範如何建構對話載荷。
- 妥善處理例外狀況。
驗證與生命週期
🔑 以下每個程式碼範例均須遵循兩項規則:
- 優先使用
DefaultAzureCredential。它既可在本地端(Azure CLI / VS Code / Developer CLI)運作,也能在 Azure 中(受管身分識別、工作負載身分識別)運作,且無需修改程式碼。請避免使用連線字串、帳戶/API 金鑰——這些會繞過 Entra 稽核與金鑰輪替機制。
- 本地開發:
DefaultAzureCredential可直接使用。- 生產環境:設定
AZURE_TOKEN_CREDENTIALS=prod(或AZURE_TOKEN_CREDENTIALS=),以將憑證鏈限制為符合生產環境安全標準的憑證。- 將每個客戶端封裝在上下文管理器中,以確保 HTTP 傳輸、套接字和憑證快取能以確定性方式釋放:
- 同步模式:
使用 ``(...) as client: - 非同步:
使用 `以及(...)` 作為 `client` 的非同步操作, 使用 `DefaultAzureCredential()` 作為 `credential` 的非同步操作:(來自azure.identity.aio)程式碼片段可能會簡化此設定,但生產環境的程式碼應始終遵循這兩項規則。
ConversationAnalysisClient接受TokenCredential(例如DefaultAzureCredential)。請使用此令牌憑證 — 它既可在本地端(Azure CLI / VS Code / Developer CLI)運作,亦可在 Azure 環境中(託管身分識別、工作負載身分識別)運作,且無需修改程式碼。
舊版:API 金鑰(現有的基於金鑰的部署)
新程式碼應使用DefaultAzureCredential。僅當您擁有尚未遷移至 Entra ID 的既有基於金鑰的部署時,才應使用AzureKeyCredential—— 例如,仍在完成 Entra 部署的受監管環境。
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations import ConversationAnalysisClient
endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
key = os.environ["AZURE_CONVERSATIONS_KEY"]
with ConversationAnalysisClient(endpoint, AzureKeyCredential(key)) as client:
# 有關 analyze_conversation 有效載荷的詳細資訊,請參閱下方的「基本對話分析」
...
最佳實務
- 請選擇同步或非同步模式,並保持一致。請勿在同一個呼叫路徑中混合使用
azure.ai.language.conversations的同步客戶端與azure.ai.language.conversations.aio的非同步客戶端。每個模組應選擇一種模式。 - 請務必為客戶端和非同步憑證使用上下文管理器。將每個客戶端
以 `ConversationAnalysisClient(...) as client:`(同步)或`async with ConversationAnalysisClient(...) as client:`(非同步)進行封裝。 對於來自azure.identity.aio的非同步DefaultAzureCredential,也請搭配非同步模式並使用credential:,以便清理令牌和傳輸資料。 - 請使用
DefaultAzureCredential實現本地開發環境與 Azure 之間的可攜式驗證(避免使用 API 金鑰;因其會繞過 Entra 的稽核與輪替機制)。 - 請使用環境變數設定端點、專案名稱及部署名稱。
- 請在
conversationItem有效負載中明確對應participantId與id。
範例
基本對話分析
import os
from azure.identity import DefaultAzureCredential
from azure.ai.language.conversations import ConversationAnalysisClient
endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
project_name = os.environ["AZURE_CONVERSATIONS_PROJECT"]
deployment_name = os.environ["AZURE_CONVERSATIONS_DEPLOYMENT"]
# DefaultAzureCredential 可在本地端及 Azure 環境中運作,無需修改程式碼。
credential = DefaultAzureCredential()
with ConversationAnalysisClient(endpoint, credential) as client:
query = "請寄一封電子郵件給 Carol,關於明天的會議"
result = client.analyze_conversation(
task={
"kind": "Conversation",
"analysisInput": {
"conversationItem": {
"participantId": "1",
"id": "1",
"modality": "text",
"language": "en",
"text": query
},
"isLoggingEnabled": False
},
"parameters": {
"projectName": project_name,
"deploymentName": deployment_name,
"verbose": True
}
}
)
print(f"首要意圖:{result['result']['prediction']['topIntent']}")
---
name: azure-ai-language-conversations-py
description: Analyze conversation intent and entities using the Azure AI Language Conversations Python SDK with best practices for authentication and error handling.
license: MIT
---
# Azure AI Language Conversations for Python
## System Prompt
You are an expert Python developer specializing in Azure AI Services and Natural Language Processing.
Your task is to help users implement Conversational Language Understanding (CLU) using the `azure-ai-language-conversations` SDK.
When responding to requests about Azure AI Language Conversations:
1. Always use the latest version of the `azure-ai-language-conversations` SDK.
2. Emphasize the use of `ConversationAnalysisClient` with `DefaultAzureCredential`.
3. Provide clear code examples demonstrating how to structure the conversation payload.
4. Handle exceptions properly.
## Authentication & Lifecycle
> **🔑 Two rules apply to every code sample below:**
>
> 1. **Prefer `DefaultAzureCredential`.** It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
> - Local dev: `DefaultAzureCredential` works as-is.
> - Production: set `AZURE_TOKEN_CREDENTIALS=prod` (or `AZURE_TOKEN_CREDENTIALS=<specific_credential>`) to constrain the credential chain to production-safe credentials.
> 2. **Wrap every client in a context manager** so HTTP transports, sockets, and token caches are released deterministically:
> - Sync: `with <Client>(...) as client:`
> - Async: `async with <Client>(...) as client:` **and** `async with DefaultAzureCredential() as credential:` (from `azure.identity.aio`)
>
> Snippets may abbreviate this setup, but production code should always follow both rules.
`ConversationAnalysisClient` accepts a `TokenCredential` such as `DefaultAzureCredential`. Use the token credential — it works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change.
### Legacy: API Key (existing keyed deployments)
New code should use `DefaultAzureCredential`. Use `AzureKeyCredential` only if you have an existing keyed deployment that hasn't been migrated to Entra ID yet — for example, regulated environments still completing their Entra rollout.
```python
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations import ConversationAnalysisClient
endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
key = os.environ["AZURE_CONVERSATIONS_KEY"]
with ConversationAnalysisClient(endpoint, AzureKeyCredential(key)) as client:
# See "Basic Conversation Analysis" below for the analyze_conversation payload
...
```
## Best Practices
- **Pick sync OR async and stay consistent.** Do not mix `azure.ai.language.conversations` sync clients with `azure.ai.language.conversations.aio` async clients in the same call path. Choose one mode per module.
- **Always use context managers for clients and async credentials.** Wrap every client in `with ConversationAnalysisClient(...) as client:` (sync) or `async with ConversationAnalysisClient(...) as client:` (async). For async `DefaultAzureCredential` from `azure.identity.aio`, also use `async with credential:` so tokens and transports are cleaned up.
- **Use `DefaultAzureCredential`** for portable auth across local dev and Azure (avoid API keys; they bypass Entra audit and rotation).
- Use environment variables for the endpoint, project name, and deployment name.
- Clearly map the `participantId` and `id` in the `conversationItem` payload.
## Examples
### Basic Conversation Analysis
```python
import os
from azure.identity import DefaultAzureCredential
from azure.ai.language.conversations import ConversationAnalysisClient
endpoint = os.environ["AZURE_CONVERSATIONS_ENDPOINT"]
project_name = os.environ["AZURE_CONVERSATIONS_PROJECT"]
deployment_name = os.environ["AZURE_CONVERSATIONS_DEPLOYMENT"]
# DefaultAzureCredential works locally and in Azure with no code change.
credential = DefaultAzureCredential()
with ConversationAnalysisClient(endpoint, credential) as client:
query = "Send an email to Carol about the tomorrow's meeting"
result = client.analyze_conversation(
task={
"kind": "Conversation",
"analysisInput": {
"conversationItem": {
"participantId": "1",
"id": "1",
"modality": "text",
"language": "en",
"text": query
},
"isLoggingEnabled": False
},
"parameters": {
"projectName": project_name,
"deploymentName": deployment_name,
"verbose": True
}
}
)
print(f"Top intent: {result['result']['prediction']['topIntent']}") 所有檔案
0 個檔案安裝 azure-ai-language-conversations-py
請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/azure-ai-language-conversations-py # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
