医疗保健中的AI探索CrewAI在医疗辅助方面的潜力

医疗保健中的AI探索CrewAI在医疗辅助方面的潜力

Barry Lv6

医疗保健行业正处于十字路口,面临着日益增长的需求与有限资源之间的矛盾。长时间的等待、医疗专业人员的压力以及对更高效解决方案的不断需求,推动了新技术的探索。人工智能,特别是以AI助手的形式,正逐渐成为弥合这一差距、改善医疗服务交付的潜在工具。

CrewAI,一个用于构建AI驱动应用程序的框架,为开发此类助手提供了一种独特的方法。通过利用AI代理、基于Web的工具和大型语言模型的力量,CrewAI使开发者能够创建能够理解复杂信息并提供深刻见解的复杂系统。

本文深入探讨了CrewAI在医疗保健领域的一个实际应用示例:一个旨在提供初步医疗信息和支持的医疗AI助手。虽然它不能替代合格的医疗专业人员,但这个助手展示了AI如何用于简化信息收集、提供潜在诊断并建议治疗途径,以便与医疗提供者进一步讨论。

理解目标:该代码旨在构建一个模拟简化医疗咨询的Web应用程序。用户可以输入他们的症状和病史,由CrewAI代理驱动的应用程序将提供初步诊断并建议潜在的治疗计划。

代码分解:

  1. 导入必要的库:
1
2
3
4
5
6
7
8
9
from crewai import Agent, Task, Crew
from crewai_tools import ScrapeWebsiteTool, SerperDevTool
import os
import streamlit as st
from dotenv import load_dotenv
from docx import Document
from io import BytesIO
import base64
from langchain_community.llms import Ollama
  • CrewAI框架: Agent、Task和Crew是CrewAI的核心组件,支持创建和管理AI代理及其任务。
  • 网页交互工具: ScrapeWebsiteTool允许机器人从网站提取信息,而SerperDevTool利用Serper搜索引擎API进行更广泛的网络搜索。
  • 应用与环境: os与操作系统交互,streamlit构建网页应用界面,dotenv安全加载环境变量(包括API密钥)。
  • 文档生成: docx支持创建Microsoft Word文档,io处理文件输入输出,base64对数据进行编码以便网页下载。
  • 语言模型: langchain_community.llms中的Ollama提供语言理解和生成能力,使用“phi3”模型。

2. 安全API密钥管理:

1
2
load_dotenv()
os.environ['SERPER_API_KEY'] = os.getenv('SERPER_API_KEY')
  • 此部分确保API密钥的安全处理。代码从.env文件加载SERPER_API_KEY,而非硬编码敏感信息,增强了安全性和可维护性。

3. 定义辅助函数:

1
2
3
4
5
6
7
8
9
10
11
12
def generate_docx(result):
doc = Document()
doc.add_heading("Healthcare Diagnosis and Treatment Recommendations", 0)
doc.add_paragraph(result)
bio = BytesIO()
doc.save(bio)
bio.seek(0)
return bio

def get_download_link(bio, filename):
b64 = base64.b64decode(bio.read()).decode()
return f'<a href="data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{b64}" download="{filename}">Download Diagnosis and Treatment Plan</a>'
  • generate_docx: 该函数接收生成的医疗信息(诊断和治疗计划),并使用docx库创建可下载的.docx文件。
  • get_download_link: 该函数生成一个HTML链接,允许用户下载包含医疗建议的生成.docx文件。

4. 构建Streamlit用户界面:

1
2
3
4
5
6
st.set_page_config(layout='wide')
st.title("Medical AI Bot")
gender = st.selectbox('Select Gender', ("Male", "Female", "Other"))
age = st.number_input("Enter Age", min_value=0, max_value=120, value=25)
symptoms = st.text_area("Enter Symptoms", 'e.g., Fever, Cough, Headache')
medical_history = st.text_area("Enter your medical history", 'e.g., Diabetes, Hypertension')

此部分设置网页应用的布局和元素:

  • st.set_page_config(layout=’wide’):配置更宽的布局以提升用户体验。
  • st.title(“Medical AI Bot”):设置网页标题。
  • 使用st.selectbox选择性别、st.number_input输入年龄、st.text_area输入症状和病史等字段收集用户信息。

5. 初始化工具和语言模型:

1
2
3
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()
llm = Ollama(model = "phi3")
  • 创建网页抓取工具(scrape_tool)、网页搜索工具(search_tool)和Ollama语言模型(llm)实例,使这些资源可供AI代理使用。

6. 定义CrewAI代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
diagonostician = Agent(
role ="Medical Diagonostician"
goal = "Analyze patient symptoms and medical history to provide a perliminary diagnosis.",
backstory = "This agent specializes in diagnosing medical conditions based on patient-reported symptoms and medical history. It uses advanded algorithms and medical knowledge to identify potential health issues",
verbose = True,
allow_delegation = False
tools = [search_tool, scrape_tool],
llm = llm

)

treatment_advisior = Agent(
role = "Treatment Advisior",
goal = "Recommend appropriate treatment plans based on the diagnosis provided by the medical Diagnostician",
backstory="This agent specializes in creating treatment plans tailored to individual patient needs. It considers the diagnosis, patient history, and current best practices in medicine to recommend effectice treatments.",
verbose = True,
allow_delegation = False,
tools = [search_tool, scrape_tool],
llm = llm
)

定义两个专门的AI代理,每个代理具有特定的角色、目标、背景故事和工具集:

  • diagonostician: 该代理专注于分析患者症状和病史,提供初步诊断。它利用网页搜索和抓取工具访问相关医疗信息。
  • treatment_advisior: 该代理根据diagonostician提供的诊断,推荐合适的治疗计划。它考虑患者的病史、当前症状和医学最佳实践。同样,它也使用网页工具收集额外信息。

7. 定义CrewAI任务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
diagnose_task = Task(
description = (
"1. Analyze the patient's symptoms ({symptoms}) and medical history({medical_history}).\n"
"2. Provide a preliminary diagnosis with possible conditions based on the provided information.\n"
"3. Limit the diagnosis to the most likely conditions."
),
expected_output = "A preliminary diadnosis with a list of pssible conditions.",
agent = diagonostician
)

treatment_task = Task(
description =(
"1. Based on the diagnosis, recommend appropriate treatment plans step by step.\n"
"2. Consider the patient's medical history ({medical_histoy}) and current symptoms ({symptoms}).\n "
"3. Provide detailed treatment recommendations, inculding medicatins, lifestyle changes, and follow-up care."
),
expected_output= " A comprehesive treatment plan tailored to the patient's needs",
agent = treatment_advisior
)

为每个代理定义具体任务,概述步骤和预期输出:

  • diagnose_task: 该任务指示diagonostician分析患者信息,生成初步诊断,并聚焦于最可能的病症。
  • treatment_task: 该任务指示treatment_advisior利用提供的诊断、患者病史和当前症状,制定全面的治疗计划,包括药物建议、生活方式改变和后续护理建议。

8. 创建CrewAI团队:

1
2
3
4
5
crew = Crew(
agents = [ diagonostician, treatment_advisior],
tasks=[diagnose_task, treatment_task],
verbose= 2
)
  • 创建一个crew对象,集合定义的代理及其各自任务。verbose=2设置确保过程详细记录,有助于调试和理解信息流。

9. 协调工作流程和用户交互:

1
2
3
4
5
6
7
8
if st.button(" Get Diagnosis and Treatment Plan"):
with st.spinner("Generating Recommendations....."):
results = crew.kickoff(inputs={"symptoms":symptoms, "medical_history": medical_history})
st.write(results)
docx_file = generate_docx(results)

download_link = get_download_link(docx_file, "diagnosis_and_treatment_paln.docx")
st.markdown(download_link, unsafe_allow_html=True)

此部分处理用户交互并触发诊断和治疗流程:

  • st.button(“ Get Diagnosis and Treatment Plan”):网页界面显示一个按钮,提示用户启动流程。

用户点击按钮时:

  • 显示加载消息(“Generating Recommendations…..”),表明流程正在进行。
  • crew.kickoff(inputs={“symptoms”:symptoms, “medical_history”: medical_history}):crew对象启动工作流程,将用户提供的症状和病史作为输入传递给代理。

生成的结果(包括诊断和治疗计划)显示在界面上。

  • generate_docx函数从结果创建可下载的Word文档,提供用户友好的信息保存格式。

最后,提供一个下载Word文档的链接给用户,允许他们保存医疗建议。

伦理考量与局限性:

必须强调,此代码仅为展示AI在医疗健康领域潜力的简化示例,不应用于实际的医疗诊断或治疗。现实中的医疗应用需经过严格测试、验证,遵守伦理准则,并获得监管机构的批准。

结论:

本文详细介绍了使用CrewAI构建的医疗AI助手。通过结合AI代理、网络工具和强大的语言模型,该应用展示了AI如何在初步医疗咨询中发挥潜在辅助作用。然而,使用此类技术时必须负责任,优先考虑患者安全,并始终咨询合格的医疗专业人员以获得准确的诊断和治疗。

附加资源:

官方网站: https://www.crewai.com/ 官方GitHub:https://github.com/joaomdmoura/crewAI 代码:https://github.com/imanoop7/medical-agent-using-crewai

欢迎探索这些资源,祝您学习愉快!如有更多问题,随时提问。😊

如果您喜欢这篇文章并希望支持我:

  1. 为我的文章鼓掌 10 次;这对我帮助很大。👏
  2. 在 Medium 上关注我免费订阅,获取我的最新文章🫶
  • 标题: 医疗保健中的AI探索CrewAI在医疗辅助方面的潜力
  • 作者: Barry
  • 创建于 : 2024-07-08 03:26:37
  • 更新于 : 2024-08-31 06:59:45
  • 链接: https://wx.role.fun/2024/07/08/c9f5e698c9e04370bfc1bc8a6f8e3a36/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
医疗保健中的AI探索CrewAI在医疗辅助方面的潜力