{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, pipeline\n", "from langchain.llms import HuggingFacePipeline\n", "from langchain import PromptTemplate, LLMChain\n", "import torch" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "\n", "tokenizer = AutoTokenizer.from_pretrained(\"rifatul123/Primary_doctor_v1\")\n", "\n", "model = AutoModelForCausalLM.from_pretrained(\"rifatul123/Primary_doctor_v1\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe = pipeline(\n", " \"text-generation\",\n", " model= model, \n", " tokenizer=tokenizer, \n", " max_length=256,\n", " temperature=0.9,\n", " top_p=0.95,\n", " repetition_penalty=1.2\n", ")\n", "\n", "local_llm = HuggingFacePipeline(pipeline=pipe)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from langchain.chains import ConversationChain\n", "from langchain.chains.conversation.memory import ConversationBufferWindowMemory" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# We are going to set the memory to go back 4 turns\n", "window_memory = ConversationBufferWindowMemory(k=0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "conversation = ConversationChain(\n", " llm=local_llm, \n", " verbose=True, \n", " memory=window_memory\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\\n\\nCurrent conversation:\\n{history}\\nHuman: {input}\\nAI:'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.prompt.template" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "conversation.prompt.template = '''Imagine a conversation with a medical AI. As an AI, it has access to a vast knowledge base of medical information and can provide treatment recommendations based on your input.\n", "\n", "{history}\n", "You: {input}\n", "AI:'''" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mImagine a conversation with a medical AI. As an AI, it has access to a vast knowledge base of medical information and can provide treatment recommendations based on your input.\n", "\n", "\n", "You: is fever is related to cancer?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' yes. Secondly, the correlation between a temperature controlled diet ( WC ) and changes in biomarkers of viral inflammation, cancer risk factors and Alzheimer disease dementia is relevant. ruled out : hypoactivity of serum @-isoxygenase type I collagen, high sensitivity C reactive protein ( hsCRP ) or homocysteine level as the cause of fever. Participants were randomly assigned in a crossover fashion to a WC of @C for @ weeks, followed by random assignment to change in either of the three biomarkers of viral inflammation and Alzheimer disease. Blood samples were taken at baseline, after intervention, and at the endline. '" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"is fever is related to cancer?\")" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# conversation.prompt.template = '''Imagine a conversation between a human and a medical AI. The AI has access to a vast knowledge base of medical information and can provide treatment recommendations.\n", "\n", "# Here is a sample of their conversation:\n", "# {history}\n", "# Human: {input}\n", "# AI:'''" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mImagine a conversation with a medical AI. As an AI, it has access to a vast knowledge base of medical information and can provide treatment recommendations based on your input.\n", "\n", "\n", "You: how to treat fever?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' what are the basic techniques for fever management? Cuevasartan : yes. Cuevasartan : did not there existed a randomized controlled trial ( RCT ) found that Cuevasartan was effective in patients suffering from advanced viral gastroenteritis? Are these findings replicated for the rest population? CuevasARTAN is a multi-center, double blind, placebo-controlled study. Participants were randomly assigned to receive oral administration either by mouth or by mouthhalf twice daily for two consecutive days. Recruited subjects were asked to answer the Rey Auditory Verbal Learning Test @ times at the end/after each day. '" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"how to treat fever?\")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mImagine a conversation between a human and a medical AI. The AI has access to a vast knowledge base of medical information and can provide treatment recommendations.\n", "\n", "Here is a sample of their conversation:\n", "\n", "Human: develop a method to treat cancer?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' it reduces fatigue and improves quality of life. Whilst there is some evidence of benefit from cognitive behavioural therapy ( CBT ) on cognition, it has never been tested using evidence-based treatments such as voxel based learning or environmental stress management training ( EST ). This paper describes the process evaluation conducted within the Mayo University Research Ethics Committee ( REPC ), which included a feasibility study, interviews with @ patients and three advisors to evaluate efficacy and acceptability ofventions in cancer patients. Current Controlled Trials ISRCTN@. Registered @ October @. In a two arm cluster randomised controlled trial, we investigated whetherayer informed patients that they had to enrol in one of six trials for a minimum of five sessions per month. Participants were recruited via NHS or local teaching hospitals across the UK and self treated with EQ questions at baseline and after one month follow-up. '" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"develop a method to treat cancer?\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }