> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Azure AI 服务工具包集成

> 使用 LangChain Python 集成 Azure AI 服务工具包。

此工具包用于与 `Azure AI Services API` 交互，以实现一些多模态功能。

目前，此工具包中捆绑了五个工具：

* **AzureAiServicesImageAnalysisTool**：用于从图像中提取标题、对象、标签和文本。
* **AzureAiServicesDocumentIntelligenceTool**：用于从文档中提取文本、表格和键值对。
* **AzureAiServicesSpeechToTextTool**：用于将语音转录为文本。
* **AzureAiServicesTextToSpeechTool**：用于将文本合成为语音。
* **AzureAiServicesTextAnalyticsForHealthTool**：用于提取医疗实体。

首先，你需要设置一个 Azure 帐户并创建一个 AI 服务资源。你可以按照 [Azure AI 服务多服务资源说明](https://learn.microsoft.com/en-us/azure/ai-services/multi-service-resource) 来创建资源。

然后，你需要获取资源的端点、密钥和区域，并将它们设置为环境变量。你可以在资源的“密钥和端点”页面中找到它们。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU  azure-ai-formrecognizer > /dev/null
pip install -qU  azure-cognitiveservices-speech > /dev/null
pip install -qU  azure-ai-textanalytics > /dev/null
pip install -qU  azure-ai-vision-imageanalysis > /dev/null
pip install -qU langchain-community
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os

os.environ["OPENAI_API_KEY"] = "sk-"
os.environ["AZURE_AI_SERVICES_KEY"] = ""
os.environ["AZURE_AI_SERVICES_ENDPOINT"] = ""
os.environ["AZURE_AI_SERVICES_REGION"] = ""
```

## 创建工具包

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.agent_toolkits import AzureAiServicesToolkit

toolkit = AzureAiServicesToolkit()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[tool.name for tool in toolkit.get_tools()]
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
['azure_ai_services_document_intelligence',
 'azure_ai_services_image_analysis',
 'azure_ai_services_speech_to_text',
 'azure_ai_services_text_to_speech',
 'azure_ai_services_text_analytics_for_health']
```

## 在代理中使用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_classic import hub
from langchain.agents import AgentExecutor, create_structured_chat_agent
from langchain_openai import OpenAI
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
llm = OpenAI(temperature=0)
tools = toolkit.get_tools()
prompt = hub.pull("hwchase17/structured-chat-agent")
agent = create_structured_chat_agent(llm, tools, prompt)

agent_executor = AgentExecutor(
    agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent_executor.invoke(
    {
        "input": "用这些食材我能做什么？ "
        + "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png"
    }
)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 进入新的 AgentExecutor 链...

思考：我需要使用 azure_ai_services_image_analysis 工具来分析食材的图像。
操作：
\`\`\`
{
  "action": "azure_ai_services_image_analysis",
  "action_input": "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png"
}
\`\`\`
标题：碗里的一群鸡蛋和面粉
对象：鸡蛋，鸡蛋，食物
标签：乳制品，食材，室内，增稠剂，食物，搅拌碗，粉末，面粉，鸡蛋，碗
操作：
\`\`\`
{
  "action": "Final Answer",
  "action_input": "你可以用这些食材制作蛋糕或其他烘焙食品。"
}
\`\`\`



> 链接完成。
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{'input': '用这些食材我能做什么？ https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png',
 'output': '你可以用这些食材制作蛋糕或其他烘焙食品。'}
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tts_result = agent_executor.invoke({"input": "给我讲个笑话并读出来。"})
audio_file = tts_result.get("output")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 进入新的 AgentExecutor 链...

思考：我可以使用 Azure AI Services Text to Speech API 将文本转换为语音。
操作：
\`\`\`
{
  "action": "azure_ai_services_text_to_speech",
  "action_input": "为什么科学家不相信原子？因为它们构成了一切。"
}
\`\`\`
/tmp/tmpe48vamz0.wav


> 链接完成。
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from IPython import display

audio = display.Audio(data=audio_file, autoplay=True, rate=22050)
display.display(audio)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
sample_input = """
患者是一位54岁的男性，过去几个月有进行性心绞痛病史。
患者于今年7月进行了心脏导管检查，显示右冠状动脉完全闭塞和左主干病变50%，
有强烈的冠心病家族史，其兄弟之一在52岁时死于心肌梗死，
另一兄弟接受了冠状动脉搭桥手术。患者于2001年7月进行了负荷超声心动图检查，
结果显示无室壁运动异常，但由于体型原因，这是一项困难的研究。患者进行了六分钟，
前外侧导联出现轻微ST段压低，认为是由于疲劳和手腕疼痛，即其心绞痛等同症状。由于患者
症状加重、家族史以及左主干病变史伴右冠状动脉完全闭塞，被转诊进行血运重建和开胸手术。

列出所有诊断。
"""

agent_executor.invoke({"input": sample_input})
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 进入新的 AgentExecutor 链...

思考：患者有进行性心绞痛病史，有强烈的冠心病家族史，以及先前的心脏导管检查显示右冠状动脉完全闭塞和左主干病变50%。
操作：
\`\`\`
{
  "action": "azure_ai_services_text_analytics_for_health",
  "action_input": "患者是一位54岁的男性，过去几个月有进行性心绞痛病史。患者于今年7月进行了心脏导管检查，显示右冠状动脉完全闭塞和左主干病变50%，有强烈的冠心病家族史，其兄弟之一在52岁时死于心肌梗死，另一兄弟接受了冠状动脉搭桥手术。患者于2001年7月进行了负荷超声心动图检查，结果显示无室壁运动异常，但由于体型原因，这是一项困难的研究。患者进行了六分钟，前外侧导联出现轻微ST段压低，认为是由于疲劳和手腕疼痛，即其心绞痛等同症状。由于患者症状加重、家族史以及左主干病变史伴右冠状动脉完全闭塞，被转诊进行血运重建和开胸手术。"
文本包含以下医疗实体：54岁是类型为年龄的医疗实体，男性是类型为性别的医疗实体，进行性心绞痛是类型为诊断的医疗实体，过去几个月是类型为时间的医疗实体，心脏导管检查是类型为检查名称的医疗实体，今年7月是类型为时间的医疗实体，完全是类型为病情限定词的医疗实体，闭塞是类型为症状或体征的医疗实体，右冠状动脉是类型为身体结构的医疗实体，50是类型为测量值的医疗实体，%是类型为测量单位的医疗实体，左主干病变是类型为诊断的医疗实体，家族是类型为家庭关系的医疗实体，冠心病是类型为诊断的医疗实体，兄弟是类型为家庭关系的医疗实体，死亡是类型为诊断的医疗实体，52是类型为年龄的医疗实体，心肌梗死是类型为诊断的医疗实体，兄弟是类型为家庭关系的医疗实体，冠状动脉搭桥手术是类型为治疗名称的医疗实体，负荷超声心动图是类型为检查名称的医疗实体，2001年7月是类型为时间的医疗实体，室壁运动异常是类型为症状或体征的医疗实体，体型是类型为症状或体征的医疗实体，六分钟是类型为时间的医疗实体，轻微是类型为病情限定词的医疗实体，前外侧导联ST段压低是类型为症状或体征的医疗实体，疲劳是类型为症状或体征的医疗实体，手腕疼痛是类型为症状或体征的医疗实体，心绞痛是类型为症状或体征的医疗实体，加重是类型为病程的医疗实体，症状是类型为症状或体征的医疗实体，家族是类型为家庭关系的医疗实体，左主干病变是类型为诊断的医疗实体，完全闭塞是类型为病程的医疗实体，右冠状动脉是类型为身体结构的医疗实体，血运重建是类型为治疗名称的医疗实体，开胸手术是类型为治疗名称的医疗实体
操作：
\`\`\`
{
  "action": "Final Answer",
  "action_input": "患者的诊断包括进行性心绞痛、右冠状动脉完全闭塞、左主干病变50%、冠心病、心肌梗死以及冠心病家族史。"
}



> 链接完成。
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{'input': "\n患者是一位54岁的男性，过去几个月有进行性心绞痛病史。\n患者于今年7月进行了心脏导管检查，显示右冠状动脉完全闭塞和左主干病变50%，\n有强烈的冠心病家族史，其兄弟之一在52岁时死于心肌梗死，\n另一兄弟接受了冠状动脉搭桥手术。患者于2001年7月进行了负荷超声心动图检查，\n结果显示无室壁运动异常，但由于体型原因，这是一项困难的研究。患者进行了六分钟，\n前外侧导联出现轻微ST段压低，认为是由于疲劳和手腕疼痛，即其心绞痛等同症状。由于患者\n症状加重、家族史以及左主干病变史伴右冠状动脉完全闭塞，被转诊进行血运重建和开胸手术。\n\n列出所有诊断。\n",
 'output': "患者的诊断包括进行性心绞痛、右冠状动脉完全闭塞、左主干病变50%、冠心病、心肌梗死以及冠心病家族史。"}
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/azure_ai_services.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
