使用LangGraph构建自主RAG2

设置向量存储(Chroma DB)

创建ChromaDB客户端

我们使用chromadb库创建ChromDB客户端。

 
import chromadb
# Setting up the Chromadb
client = chromadb.PersistentClient(path="./chroma_db")


创建集合

我们将创建两个集合,一个用于设备手册数据集,一个用于问答数据集

可以把集合想象成数据库中的表。对于每个源,我们将创建一个表。

(如果您刚接触ChromaDB,请参考我的另一篇文章此处,该文章介绍了ChromaDB的基础知识和实现方法)

集合1:医疗问答数据集

我们将创建一个新的集合“medical_q_n_a”,并向其中添加数据。

 
# Collection 1 for medical Q&A Dataset
collection1 = client.get_or_create_collection(name="medical_q_n_a")

# Add data to collection
# here the chroma db will use default embeddings (sentence transformers)
collection1.add(
    documents=df_qa['combined_text'].tolist(),
    metadatas=df_qa.to_dict(orient="records"),
    ids=df_qa.index.astype(str).tolist(),
)
# quick check
query = "What are the treatments for Kawasaki disease ?"
results = collection1.query(query_texts=[query],
    n_results=3
)
print(results)

输出:

 
{'ids': [['1', '393', '60']],
 'embeddings': None,
 'documents': [["Question: What are the treatments for Kawasaki disease ?. Answer: These resources address the diagnosis or management of Kawasaki disease:  - Cincinnati Children's Hospital Medical Center  - Genetic Testing Registry: Acute febrile mucocutaneous lymph node syndrome  - National Heart, Lung, and Blood Institute: How is Kawasaki Disease Treated?   These resources from MedlinePlus offer information about the diagnosis and management of various health conditions:  - Diagnostic Tests  - Drug Therapy  - Surgery and Rehabilitation  - Genetic Counseling   - Palliative Care. Type: treatment. ",
   'Question: What are the treatments for Krabbe Disease ?. Answer: There is no cure for Krabbe disease. Results of a very small clinical trial of children with infantile Krabbe disease found that children who received umbilical cord blood stem cells from unrelated donors prior to symptom onset developed with little neurological impairment. Bone marrow transplantation may help some people. Generally, treatment for the disorder is symptomatic and supportive. Physical therapy may help maintain or increase muscle tone and circulation.. Type: treatment. ',
   'Question: What are the treatments for Ehlers-Danlos syndrome, progeroid type ?. Answer: How might Ehlers-Danlos syndrome progeroid type be treated? Individuals with Ehlers-Danlos Syndrome progeroid type can benefit from a variety of treatments depending on their symptoms. Affected children with weak muscle tone and delayed development might benefit from physiotherapy to improve muscle strength and coordination. Affected individuals with joint pain might benefit from anti-inflammatory drugs. Lifestyle changes or precautions during exercise or intense physical activity may be advised to reduce the chance of accidents to the skin and bone. It is recommended that affected individuals discuss treatment options with their healthcare provider.. Type: treatment. ']],
 'uris': None,
 'included': ['metadatas', 'documents', 'distances'],
 'data': None,
 'metadatas': [[{'qtype': 'treatment',
    'combined_text': "Question: What are the treatments for Kawasaki disease ?. Answer: These resources address the diagnosis or management of Kawasaki disease:  - Cincinnati Children's Hospital Medical Center  - Genetic Testing Registry: Acute febrile mucocutaneous lymph node syndrome  - National Heart, Lung, and Blood Institute: How is Kawasaki Disease Treated?   These resources from MedlinePlus offer information about the diagnosis and management of various health conditions:  - Diagnostic Tests  - Drug Therapy  - Surgery and Rehabilitation  - Genetic Counseling   - Palliative Care. Type: treatment. ",
    'Answer': "These resources address the diagnosis or management of Kawasaki disease:  - Cincinnati Children's Hospital Medical Center  - Genetic Testing Registry: Acute febrile mucocutaneous lymph node syndrome  - National Heart, Lung, and Blood Institute: How is Kawasaki Disease Treated?   These resources from MedlinePlus offer information about the diagnosis and management of various health conditions:  - Diagnostic Tests  - Drug Therapy  - Surgery and Rehabilitation  - Genetic Counseling   - Palliative Care",
    'Question': 'What are the treatments for Kawasaki disease ?'},
   {'qtype': 'treatment',
    'combined_text': 'Question: What are the treatments for Krabbe Disease ?. Answer: There is no cure for Krabbe disease. Results of a very small clinical trial of children with infantile Krabbe disease found that children who received umbilical cord blood stem cells from unrelated donors prior to symptom onset developed with little neurological impairment. Bone marrow transplantation may help some people. Generally, treatment for the disorder is symptomatic and supportive. Physical therapy may help maintain or increase muscle tone and circulation.. Type: treatment. ',
    'Answer': 'There is no cure for Krabbe disease. Results of a very small clinical trial of children with infantile Krabbe disease found that children who received umbilical cord blood stem cells from unrelated donors prior to symptom onset developed with little neurological impairment. Bone marrow transplantation may help some people. Generally, treatment for the disorder is symptomatic and supportive. Physical therapy may help maintain or increase muscle tone and circulation.',
    'Question': 'What are the treatments for Krabbe Disease ?'},
   {'Question': 'What are the treatments for Ehlers-Danlos syndrome, progeroid type ?',
    'qtype': 'treatment',
    'Answer': 'How might Ehlers-Danlos syndrome progeroid type be treated? Individuals with Ehlers-Danlos Syndrome progeroid type can benefit from a variety of treatments depending on their symptoms. Affected children with weak muscle tone and delayed development might benefit from physiotherapy to improve muscle strength and coordination. Affected individuals with joint pain might benefit from anti-inflammatory drugs. Lifestyle changes or precautions during exercise or intense physical activity may be advised to reduce the chance of accidents to the skin and bone. It is recommended that affected individuals discuss treatment options with their healthcare provider.',
    'combined_text': 'Question: What are the treatments for Ehlers-Danlos syndrome, progeroid type ?. Answer: How might Ehlers-Danlos syndrome progeroid type be treated? Individuals with Ehlers-Danlos Syndrome progeroid type can benefit from a variety of treatments depending on their symptoms. Affected children with weak muscle tone and delayed development might benefit from physiotherapy to improve muscle strength and coordination. Affected individuals with joint pain might benefit from anti-inflammatory drugs. Lifestyle changes or precautions during exercise or intense physical activity may be advised to reduce the chance of accidents to the skin and bone. It is recommended that affected individuals discuss treatment options with their healthcare provider.. Type: treatment. '}]],
 'distances': [[0.4760013818740845, 0.9692587852478027, 1.0237324237823486]]}

集合2:医疗设备手册数据集

我们创建一个名为“medical_device_manual”的新集合,然后向其中添加数据。

 
collection2 = client.get_or_create_collection(name="medical_device_manual")
# Add data to collection
# here the chroma db will use default embeddings (sentence transformers)
collection2.add(
    documents=df_medical_device['combined_text'].tolist(),
    metadatas=df_medical_device.to_dict(orient="records"),
    ids=df_medical_device.index.astype(str).tolist(),
)
# quick check
query = "Which devices are suitable for neonatal patients?"

results = collection2.query(query_texts=[query],
    n_results=5
)
print(results)

输出

 
{
  "ids": [["218", "149", "235"]],
  "embeddings": null,
  "documents": [["Device Name: Pacemaker. Model: JOH663. Manufacturer: Johnson & Johnson. Indications: Designed for catheterization support in neonatal intensive care environments.. Contraindications: Avoid use in patients with cardiac devices or cochlear implants. Not suitable for chemical exposure areas.",
                 "Device Name: Infusion Pump. Model: Model 7800. Manufacturer: Intuitive Surgical. Indications: Designed for joint replacement support in neonatal intensive care environments.. Contraindications: Not recommended for use in geriatric patients or those with hepatic dysfunction.",
                 "Device Name: Anesthesia Machine. Model: STR623. Manufacturer: Stryker. Indications: Designed for tissue biopsy support in neonatal intensive care environments.. Contraindications: Do not use in combination with sedatives or oxygen therapy modalities."]],
  "uris": null,
  "included": ["metadatas", "documents", "distances"],
  "data": null,
  "metadatas": [[{"Manufacturer": "Johnson & Johnson", "Number_of_Cautions": 10, "Model_Number": "JOH663", "combined_text": "Device Name: Pacemaker. Model: JOH663. Manufacturer: Johnson & Johnson. Indications: Designed for catheterization support in neonatal intensive care environments.. Contraindications: Avoid use in patients with cardiac devices or cochlear implants. Not suitable for chemical exposure areas.", "Indications_for_Use": "Designed for catheterization support in neonatal intensive care environments.", "Device_Name": "Pacemaker", "Manual_Version": "2021-01-A", "Device_Class": "Class II", "Number_of_Warnings": 10, "Sterilization_Method": "Not Applicable", "Device_Weight_kg": 0.06, "Publication_Date": "2023-09-27", "Regulatory_Approval_ID": "H148086", "Patient_Population": "Adult (18-65)", "Contraindications": "Avoid use in patients with cardiac devices or cochlear implants. Not suitable for chemical exposure areas.", "Max_Operating_Temperature_C": 39.0},
                     {"Device_Class": "Class IIb", "Indications_for_Use": "Designed for joint replacement support in neonatal intensive care environments.", "Device_Lifetime_Years": 12.0, "Device_Name": "Infusion Pump", "Max_Operating_Temperature_C": 31.0, "Manual_Version": "2020-05-S", "Model_Number": "Model 7800", "Number_of_Warnings": 8, "Manufacturer": "Intuitive Surgical", "Sterilization_Method": "Dry Heat", "Contraindications": "Not recommended for use in geriatric patients or those with hepatic dysfunction.", "Device_Weight_kg": 2.83, "Patient_Population": "Adult and Pediatric", "Number_of_Cautions": 18, "combined_text": "Device Name: Infusion Pump. Model: Model 7800. Manufacturer: Intuitive Surgical. Indications: Designed for joint replacement support in neonatal intensive care environments.. Contraindications: Not recommended for use in geriatric patients or those with hepatic dysfunction.", "Regulatory_Approval_ID": "PMDA-473669", "Publication_Date": "2024-01-21"},
                     {"Max_Operating_Temperature_C": 31.0, "combined_text": "Device Name: Anesthesia Machine. Model: STR623. Manufacturer: Stryker. Indications: Designed for tissue biopsy support in neonatal intensive care environments.. Contraindications: Do not use in combination with sedatives or oxygen therapy modalities.", "Manual_Version": "Rev. 5.6", "Number_of_Cautions": 17, "Publication_Date": "2017-09-04", "Contraindications": "Do not use in combination with sedatives or oxygen therapy modalities.", "Regulatory_Approval_ID": "BLA811783", "Sterilization_Method": "Not Applicable", "Patient_Population": "Geriatric", "Number_of_Warnings": 16, "Device_Name": "Anesthesia Machine", "Device_Weight_kg": 51.65, "Device_Class": "Class I", "Indications_for_Use": "Designed for tissue biopsy support in neonatal intensive care environments.", "Manufacturer": "Stryker", "Model_Number": "STR623", "Device_Lifetime_Years": 14.0}]],
  "distances": [[1.0503746271133423, 1.0830397605895996, 1.0891238451004028]]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值