> ## 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认知服务工具包集成

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

此工具包允许您与**Azure认知服务API**交互，以在LangChain中启用多模态功能。

目前，该工具包包括：

* **AzureCogsImageAnalysisTool** — 从图像中提取标题、对象、标签和文本
* **AzureCogsFormRecognizerTool** — 从文档中提取文本、表格和键值对
* **AzureCogsSpeech2TextTool** — 将语音转录为文本
* **AzureCogsText2SpeechTool** — 将文本合成为语音
* **AzureCogsTextAnalyticsHealthTool** — 提取医疗实体

> ⚠️ 图像分析工具在macOS上不可用，因为`azure-ai-vision`目前仅支持Windows/Linux。

***

## 安装

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

# 仅限Windows / Linux
pip install -qU azure-ai-vision
```

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

os.environ["OPENAI_API_KEY"] = "sk-..."
os.environ["AZURE_COGS_KEY"] = ""
os.environ["AZURE_COGS_ENDPOINT"] = ""
os.environ["AZURE_COGS_REGION"] = ""
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
## 创建工具包
from langchain_community.agent_toolkits import AzureCognitiveServicesToolkit

toolkit = AzureCognitiveServicesToolkit()
toolkit.get_tools()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
['Azure Cognitive Services Image Analysis',
 'Azure Cognitive Services Form Recognizer',
 'Azure Cognitive Services Speech2Text',
 'Azure Cognitive Services Text2Speech']

## 在Agent中使用
from langchain.agents import create_agent
from langchain_openai import OpenAI

llm = OpenAI(temperature=0)

agent = create_agent(
    model=llm,
    tools=toolkit.get_tools(),
)
```

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

text

> 进入新的AgentExecutor链...

Action:

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "action": "Azure Cognitive Services Image Analysis",
  "action_input": "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png"
}
```

Observation: 标题：碗里的鸡蛋和面粉
对象：鸡蛋，鸡蛋，食物
标签：乳制品，食材，室内，增稠剂，食物，搅拌碗，粉末，面粉，鸡蛋，碗
Thought: 我可以使用对象和标签来建议食谱
Action:

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "action": "Final Answer",
  "action_input": "你可以用这些食材做煎饼、煎蛋卷或乳蛋饼！"
}
```

> 链完成。
> text
> '你可以用这些食材做煎饼、煎蛋卷或乳蛋饼！'

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
audio_file = agent.invoke("给我讲个笑话并大声读出来。")

audio_file
```

text

> 进入新的AgentExecutor链...
> Action:

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "action": "Azure Cognitive Services Text2Speech",
  "action_input": "为什么鸡要穿过操场？为了到另一边的滑梯！"
}
```

Observation: /tmp/tmpa3uu\_j6b.wav
Thought: 我有了笑话的音频文件
Action:

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "action": "Final Answer",
  "action_input": "/tmp/tmpa3uu_j6b.wav"
}
```

> 链完成。
> text
> '/tmp/tmpa3uu\_j6b.wav'

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

display.Audio(audio_file)
```

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

列出所有诊断。
"""
)
```

text

> 进入新的AgentExecutor链...
> Action:

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

Observation: 文本包含以下医疗实体：54岁是类型为年龄的医疗实体，男性是类型为性别的医疗实体，进行性心绞痛是类型为诊断的医疗实体，过去几个月是类型为时间的医疗实体，心脏导管检查是类型为检查名称的医疗实体，今年7月是类型为时间的医疗实体，完全是类型为病情限定词的医疗实体，闭塞是类型为症状或体征的医疗实体，右冠状动脉是类型为身体结构的医疗实体，50是类型为测量值的医疗实体，%是类型为测量单位的医疗实体，左主干是类型为身体结构的医疗实体，疾病是类型为诊断的医疗实体，家族是类型为家庭关系的医疗实体，冠心病是类型为诊断的医疗实体，兄弟是类型为家庭关系的医疗实体，死亡是类型为诊断的医疗实体，52是类型为年龄的医疗实体，心肌梗死是类型为诊断的医疗实体，兄弟是类型为家庭关系的医疗实体，冠状动脉搭桥手术是类型为治疗名称的医疗实体，负荷超声心动图是类型为检查名称的医疗实体，2001年7月是类型为时间的医疗实体，室壁运动异常是类型为症状或体征的医疗实体，体型是类型为症状或体征的医疗实体，六分钟是类型为时间的医疗实体，轻微是类型为病情限定词的医疗实体，前外侧导联ST段压低是类型为症状或体征的医疗实体，疲劳是类型为症状或体征的医疗实体，手腕疼痛是类型为症状或体征的医疗实体，心绞痛等同症状是类型为症状或体征的医疗实体，加重是类型为病程的医疗实体，症状是类型为症状或体征的医疗实体，家族是类型为家庭关系的医疗实体，左是类型为方向的医疗实体，主干是类型为身体结构的医疗实体，疾病是类型为诊断的医疗实体，偶尔是类型为病程的医疗实体，右冠状动脉是类型为身体结构的医疗实体，血运重建是类型为治疗名称的医疗实体，开胸手术是类型为治疗名称的医疗实体
Thought: 我知道如何回应
Action:

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "action": "Final Answer",
  "action_input": "文本包含以下诊断：进行性心绞痛、冠心病、心肌梗死和冠状动脉搭桥手术。"
}
```

> 链完成。
> text
> '文本包含以下诊断：进行性心绞痛、冠心病、心肌梗死和冠状动脉搭桥手术。'

***

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

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