# 引言
在现代人工智能应用中,多模态能力正变得越来越重要,能够处理图像、文本、语音等多种数据形式的工具受到广泛关注。Azure AI Services Toolkit 是微软提供的一套强大的API工具,能够从图像提取信息、从文档中提取关键数据、进行语音识别和合成,以及分析医疗文本等多模态任务。本文将深入探讨这套工具的用法,并提供实用的代码示例。
# 主要内容
## 工具集概览
Azure AI Services Toolkit包含五个主要工具:
1. **AzureAiServicesImageAnalysisTool**:用于从图像中提取标题、对象、标签和文本。
2. **AzureAiServicesDocumentIntelligenceTool**:用于从文档中提取文本、表格和键值对。
3. **AzureAiServicesSpeechToTextTool**:用于将语音转录为文本。
4. **AzureAiServicesTextToSpeechTool**:用于将文本合成为语音。
5. **AzureAiServicesTextAnalyticsForHealthTool**:用于提取医疗实体。
## 环境设置
首先,您需要设置Azure账户并创建AI服务资源。请跟随[此处的说明](https://azure.microsoft.com/en-us/free/)完成资源创建过程。接下来,获取您资源的端点、密钥和区域信息,并将它们设置为环境变量:
```python
import os
# 将您的Azure密钥、端点和区域设置为环境变量
os.environ["AZURE_AI_SERVICES_KEY"] = "<Your_Azure_Key>"
os.environ["AZURE_AI_SERVICES_ENDPOINT"] = "<Your_Azure_Endpoint>"
os.environ["AZURE_AI_SERVICES_REGION"] = "<Your_Azure_Region>"
库安装
确保已安装所需的Python库:
%pip install --upgrade --quiet azure-ai-formrecognizer azure-cognitiveservices-speech azure-ai-textanalytics azure-ai-vision-imageanalysis langchain-community
代码示例
下面是一个如何使用Azure AI Services Toolkit进行图像分析和语音合成的示例:
from langchain_community.agent_toolkits import AzureAiServicesToolkit
from langchain import hub
from langchain.agents import AgentExecutor, create_structured_chat_agent
from langchain_openai import OpenAI
from IPython import display
# 初始化工具包
toolkit = AzureAiServicesToolkit() # 使用API代理服务提高访问稳定性
tools = toolkit.get_tools()
# 创建代理
llm = OpenAI(temperature=0)
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
)
# 图像分析示例
image_input = {
"input": "What can I make with these ingredients? https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png"
}
print(agent_executor.invoke(image_input))
# 语音合成示例
tts_result = agent_executor.invoke({"input": "Tell me a joke and read it out for me."})
audio_file = tts_result.get("output")
audio = display.Audio(data=audio_file, autoplay=True, rate=22050)
display.display(audio)
常见问题和解决方案
- 网络访问受限:在某些地区,你可能需要使用API代理服务来提高访问的稳定性。
- 环境变量设置不正确:确保正确设置了Azure服务的密钥、端点和区域。
总结和进一步学习资源
Azure AI Services Toolkit 提供了丰富的多模态处理工具,在提升应用智能化方面具有广泛的应用前景。可以访问Azure官方文档以获取更多关于如何充分利用这些工具的信息。
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---