This project implements a simple medical chatbot application using a pretrained GPT-2 model from the Hugging Face Transformers library. The chatbot generates human-like responses to user queries within a medical assistant context, showcasing the practical application of Large Language Models (LLMs).
medical_chatbot(user_input, context) function
Constructs a prompt combining
A predefined context: "You are a helpful medical assistant."
User input.
Expected AI response section.
Tokenizes the prompt and moves it to the appropriate device.
Generates text using the model’s generate() method with:
max_length=200
temperature=0.7
top_p=0.9
Sampling enabled (do_sample=True)
Decodes the generated output, isolates the AI's response, and returns it.
Continuously takes user input.
Exits on typing 'exit' or 'quit'.
Passes user queries to the chatbot function.
Prints out AI-generated responses.
The chatbot generates coherent, context-aware medical replies based on the user's queries.
Demonstrates GPT-2’s capability in domain-specific text generation without fine-tuning, simply by steering context.
transformers
torch
✅ Real-time chatbot using a pretrained GPT-2 LLM
✅ Context-controlled text generation via prompt engineering
✅ Configurable sampling parameters for creativity control
✅ Simple interactive console-based chat loop
This project effectively demonstrates how pretrained Large Language Models (LLMs) like GPT-2 can be leveraged for specialized applications such as a medical chatbot. By designing a clear prompt structure and adjusting generation parameters, the model produces sensible and context-aware responses, even without domain-specific fine-tuning.
Google colab Notebook page :