DEV Community

Cover image for Building an Effective and User-Friendly Medical Chatbot with OpenAI and CometLLM: A Step-by-Step Guide
 Oluseye Jeremiah
Oluseye Jeremiah

Posted on

Building an Effective and User-Friendly Medical Chatbot with OpenAI and CometLLM: A Step-by-Step Guide

The application of artificial intelligence (AI) is transforming patient involvement and information sharing in the quickly changing field of healthcare technology.
This article walks you through building a cutting-edge Doctor Chatbot as it explores the fascinating field of conversational AI.
We will explore step-by-step directions to create an intelligent yet friendly chatbot designed for medical interactions, utilizing the potent powers of OpenAI and CometLLM.

Learn how CometLLM, a dynamic platform for machine learning experimentation, and OpenAI, a trailblazing force in AI research, are collaborating to transform the healthcare experience. This post offers a thorough road map for developers and healthcare professionals alike, covering everything from comprehending the nuances of OpenAI's cutting-edge models to building a seamless chatbot architecture.

About CometLLM

Comet's LLMOps toolbox provides customers with access to state-of-the-art prompt management innovations, including quicker iterations, better performance bottleneck diagnosis, and a visual tour of Comet's ecosystem's internal prompt chain operations.
Comet excels in accelerating progress in the following critical areas with its LLMOps tools:

1

Prompt History Mastery:

Keeping accurate records of prompts, responses, and chains is critical in the world of machine-learning products powered by big language models. Comet's LLMOps tool offers a very user-friendly interface for thorough, fast history tracking, and analysis.
Users can learn much about how their prompts and answers have changed over time.

  1. #### Prompt Playground Adventure

One of the most creative features in the LLMOps toolbox is the Prompt Playground, a dynamic environment where Prompt Engineers can conduct quick explorations. This allows them to quickly test out different prompt templates and see how they affect different scenarios. During the iterative process, users are empowered to make well-informed decisions thanks to their increased experimentation agility.

  1. #### Prompt Usage Surveillance Using paid APIs may be necessary to navigate the world of huge language models. Precise usage tracking is available at the project and experiment levels using Comet's LLMOps tool. Users may better understand API use with the help of this meticulously thorough tracking system, which makes resource allocation and optimization easier.

In summary, Comet's LLMOps toolset is an essential tool for engineers, developers, and academics who are delving into the intricacies of huge language models. Their approach is not only streamlined, but it also provides increased transparency and efficiency, which makes it easier to design and refine ML-driven apps.

Building a Doc-Bot OpenAI and CometLLM

Before delving into the intricacies of code, it's crucial to grasp the foundational components and key features of the chatbot we're about to build-DocBot. Tasked with the role of a virtual health assistant, DocBot is designed to cater to a spectrum of user needs within the realm of healthcare.

Main Components and Features:

  1. General Health Inquiries: DocBot serves as a reliable source for users seeking information on general health and wellness. Users can ask about maintaining a healthy lifestyle, dietary recommendations, and other holistic health practices.
  2. Advice on Common Ailments With DocBot, users can seek guidance on common health issues such as colds, headaches, and stress management. The chatbot provides practical advice, suggesting remedies and lifestyle adjustments to alleviate common ailments.
  3. Specialized Health Tips: DocBot extends its capabilities to offer specialized advice for users with chronic conditions, mental health concerns, and those navigating the various facets of healthy aging. This personalized guidance ensures a tailored approach to individual health needs.
  4. Emergency Situations Guidance: In critical situations, DocBot steps up as a virtual first responder, providing users with essential first-aid information for emergencies. From burns to CPR guidelines, the chatbot imparts crucial knowledge to users in times of urgency.

Guiding Principles:
Empathy: DocBot is designed to engage with users empathetically, understanding the importance of human touch even in virtual interactions. The chatbot responds with compassion, acknowledging the sensitivity of health-related queries.
Informativeness: In each interaction, DocBot aims to be informative and educational. Whether offering advice on healthy living or guiding users through emergency procedures, the chatbot prioritizes the dissemination of accurate and valuable information.
User-Centric Approach: DocBot places users at the center of its functionality. By addressing a spectrum of health-related inquiries, the chatbot ensures a user-centric experience, tailoring responses to meet individual needs.
Safety and Responsibility: Recognizing the critical nature of health advice, DocBot operates with a commitment to safety and responsibility. The chatbot encourages users to consult healthcare professionals for personalized guidance in specific situations.
Step 1: Install all dependencies

%pip install "comet_llm>=1.4.1" "openai>1.0.0"
import os
from openai import OpenAI
import comet_llm
from IPython.display import display
import ipywidgets as widgets
import time
import comet_llm

comet_llm.init(project="Doc_bot_openai")
from openai import OpenAI

client = OpenAI()
Enter fullscreen mode Exit fullscreen mode

Step 2: Defining The Role of the Bot
Developing a user-friendly chatbot experience with a focus on empathy and informativeness in DoctorBot's responses is the main goal, encouraging interaction and engagement.
The code below attempts to improve user comprehension by classifying health information, making it a useful and trustworthy resource for health-related questions.
The final objective is to encourage users to seek expert medical counsel when needed and to make informed health decisions.

# Customize your medical advice list if necessary.
advice_list = '''
# Medical Advice List

## General Health:

- Healthy Diet  
  - Tips: Include a variety of fruits and vegetables in your diet. Limit processed foods.

- Regular Exercise  
  - Tips: Aim for at least 30 minutes of moderate exercise most days of the week.

- Adequate Sleep  
  - Tips: Ensure you get 7-9 hours of sleep per night for overall well-being.

## Common Ailments:
- Cold and Flu Remedies  
  - Tips: Stay hydrated, get plenty of rest, and consider over-the-counter cold remedies.

- Headache Relief  
  - Tips: Drink water, rest in a quiet room, and consider over-the-counter pain relievers.

- Stress Management  
  - Tips: Practice deep breathing, meditation, or engage in activities you enjoy.

## Common Symptoms and Solutions:
- Fever  
  - Tips: Stay hydrated, rest, and consider over-the-counter fever reducers.

- Cough  
  - Tips: Stay hydrated, use cough drops, and consider over-the-counter cough medicine.

- Sore Throat  
  - Tips: Gargle with warm saltwater, stay hydrated, and rest your voice.

- Fatigue  
  - Tips: Ensure you get enough sleep, maintain a balanced diet, and consider stress-reducing activities.

## Specialized Advice:
- Chronic Conditions  
  - Tips: Follow your prescribed treatment plan and attend regular check-ups.

- Mental Health Support  
  - Tips: Reach out to a mental health professional if you're struggling emotionally.

- Healthy Aging  
  - Tips: Stay socially active, exercise regularly, and attend routine health check-ups.

## Emergency Situations:
- First Aid for Burns  
  - Tips: Run cold water over the burn, cover with a clean cloth, and seek medical attention.

- CPR Guidelines  
  - Tips: Call for help, start chest compressions, and follow emergency protocols.

'''

context_doctor = [{'role': 'system',
                   'content': f"""
You are DoctorBot, an AI assistant providing medical advice and information.

Your role is to assist users with general health inquiries, provide advice on common ailments, offer specialized health tips, and guide users in emergency situations.

Be empathetic and informative in your interactions.

We offer a variety of medical advice across categories such as General Health, Common Ailments, Common Symptoms and Solutions, Specialized Advice, and Emergency Situations.

The Current Medical Advice List is as follows:

Enter fullscreen mode Exit fullscreen mode


{advice_list}



Enter fullscreen mode Exit fullscreen mode

Encourage users to ask questions about their health, provide relevant advice, and remind them to consult with a healthcare professional for personalized guidance.
"""}]
Step 3: Creating the Chatbot
After you configure your environment and define your advise_list, you can create your DocBot chatbot. The get_completion_from_messages function sends messages to the OpenAI GPT-3.5 Turbo model and retrieves responses from the model.

# Create a Chatbot
def get_completion_from_messages(messages, model="gpt-3.5-turbo"):
    client = OpenAI(
        api_key="OPEN_AI_KEY",
    )

    chat_completion = client.chat.completions.create(
        messages=messages,
        model=model,
    )
    return chat_completion.choices[0].message.content

Enter fullscreen mode Exit fullscreen mode

Step 4: Interacting with Patients
To interact with patients, use a simple user interface with text entry for Patient messages and a button to start a conversation. Thecollect_messages function processes user input, updates conversation context, and displays chat history.

def collect_messages(_):
    user_input = inp.value
    inp.value = ''

    context_doctor.append({'role':'user', 'content':f"{user_input}"})

    # Record the start time
    start_time = time.time()  

    response = get_completion_from_messages(context_doctor) 

    # Record the end time
    end_time = time.time()  

    # Calculate the duration
    duration = end_time - start_time
Enter fullscreen mode Exit fullscreen mode

Step 5: Log records into Comet
The next step involves using comet_llm to keep track of what patients ask, how the bot responds, and how long each interaction takes. The information is logged on the Comet website.
This helps in improving the model for future training. You can learn more about experiment tracking with Comet LLM.

 # Log to comet_llm
    comet_llm.log_prompt(
        prompt=user_input,
        output=response,
        duration=duration,
        metadata={
            "role": context_doctor[-1]['role'],
            "content": context_doctor[-1]['content'],
            "context": context_doctor,
            "advice_list": advice_list
        },
    )

    context_doctor.append({'role': 'assistant', 'content': f"{response}"})

    user_pane = widgets.Output()
    with user_pane:
        display(widgets.HTML(f"<b>User:</b> {user_input}"))

    assistant_pane = widgets.Output()
    with assistant_pane:
        display(widgets.HTML(f"<b>Assistant:</b> {response}"))

    display(widgets.VBox([user_pane, assistant_pane]))

inp = widgets.Text(value="Hi", placeholder='Enter text here…')
button_conversation = widgets.Button(description="Chat!")
button_conversation.on_click(collect_messages)

dashboard = widgets.VBox([inp, button_conversation])

display(dashboard)
Enter fullscreen mode Exit fullscreen mode

Image description

The prompts have been logged on the Comet website. By analyzing these logs, you can use various strategies to make responses quicker, improve accuracy, enhance customer satisfaction, and eliminate unnecessary steps in your medical operations.
More training is required for a DocBot chatbot that is more sophisticated.
Comet LLM is a useful tool for logging and viewing messages and threads, which streamlines the process of developing chatbot language models and enhances workflow. It offers insights for effective model building and optimization, simplifies problem-solving, assures workflow reproducibility, and aids in identifying successful methods.

Image description

Conclusion

In conclusion, this comprehensive article explains how to utilize OpenAI and CometLLM to build a strong and approachable medical chatbot. Through the utilization of CometLLM's machine learning experimentation capabilities and OpenAI's complex language models, developers and medical professionals can acquire valuable insights into developing a conversational AI that is specifically designed for medical interactions.In order to ensure that the chatbot, DocBot, competently helps users with general health inquiries, common ailments, specialist guidance, and emergency circumstances, the guide highlights the significance of user-centric design. The resulting chatbot, which is dedicated to empathy and informativeness, encourages users to seek expert help when necessary in addition to offering useful health information. This guide offers a preview of the future of intuitive and efficient digital health aides and demonstrates how cutting-edge technologies can revolutionize healthcare communication.
You can check the full code here.

Top comments (3)

Collapse
 
spetrethegray profile image
jonathan

This is sick but would you trust this to give you the correct data?
chat GPT is known for giving false data!

Collapse
 
spetrethegray profile image
jonathan

And maybe you should try to have it give you links to where it found the data!
For example, if I asked it how to give CPR it should tell me and then at the end tell me where it found the data so I know that it's not totally making it up.

Collapse
 
spetrethegray profile image
jonathan

I built a chatbot using chat gpt but I configured it so that if I ask it for the newest package of Python it would first send a request to a package manager for the newest version of Python and then respond with that instead of trusting its repo for the correct data.
RapidAPIs have some medical APIs maybe in a future project you should try hooking it up to send API requests to get up-to-date data to send back to the user!