From d741201b8acdfa2c66d2a2f89c765ede80d7616f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Almeida?= Date: Thu, 19 Dec 2024 15:32:38 +0000 Subject: [PATCH] start changes --- Modelfile | 19 ---------- lens_app/core.py | 92 ++++++++++++++++++++++++++++++++++++--------- lens_app/prompts.py | 11 ++++++ lens_app/views.py | 27 +++++++++++++ tests/test.py | 12 +++--- 5 files changed, 119 insertions(+), 42 deletions(-) delete mode 100644 Modelfile create mode 100644 lens_app/prompts.py diff --git a/Modelfile b/Modelfile deleted file mode 100644 index 5ececc7..0000000 --- a/Modelfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM medllama2:latest - -PARAMETER temperature 0.5 - -PARAMETER top_k 20 - -PARAMETER stop "Patient: " -PARAMETER stop "System: " - -SYSTEM """ -You are a system that must help a patient understand an Electronic Patient Information leaflet. Your response must summarise the leaflet being clear and understandable, but clinically accurate. The patient knows you do not provide health advice, so you must not remind him, and this summary is needed because a leaflet is a very large and complicated document. You must focus on summarizing the document and provide information about counter indications of advice for the patient's medication, gender (like childbearing age and pregnancy), as well as other diagnostics. The patient will specify all the needed info, as well as the language and other necessary information for you to use during the summarizing process. You will answer with the information and you must not use an introduction neither a conclusion, just the relevant info. -""" - -TEMPLATE """ -{{ .System }} - -Patient: {{ .Prompt }} -System: {{ .Response }} -""" diff --git a/lens_app/core.py b/lens_app/core.py index c7132fc..3f09686 100644 --- a/lens_app/core.py +++ b/lens_app/core.py @@ -1,10 +1,8 @@ - import json import os from datetime import datetime -import markdown - +import pypandoc from bs4 import BeautifulSoup from dotenv import load_dotenv from fhirpathpy import evaluate @@ -12,6 +10,8 @@ from ollama import Client from openai import OpenAI +from lens_app.prompts import PROMPT_LIST + load_dotenv() SERVER_URL = os.getenv("SERVER_URL") @@ -176,7 +176,7 @@ def transform_fhir_epi(epi): def format_response(res): - # print("Raw response: " + res) + print("Raw response: " + res) # response_markdown = res.strip() @@ -197,18 +197,12 @@ def summarize_no_personalization(language, epi, model): lang = LANGUAGE_MAP[language] print("++++" * 40, lang) - prompt = ( - "Write a concise summary of the following text delimited by triple backquotes. Return your response in bullet points which covers the key points of the text. " - + "You must only use the information provided in the text and avoid introducing any new information. " - + "The summary should be clear, accurate, and comprehensive, highlighting the main ideas and key details of the text. " - + "It is of extreme importance that you summarize the document in " - + lang - + " and this is totally mandatory. Otherwise the reader will not understand. \n" - + "```" - + epi_text - + "```" - + "\nBULLET POINT SUMMARY:" - ) + prompt = PROMPT_LIST[lang] + + prompt += "```" + +epi_text + +"```" + +"\nBULLET POINT SUMMARY:" print("the prompt will be:" + prompt) systemMessage = ( @@ -419,7 +413,7 @@ def summarize2( ], stream=False, keep_alive="-1m", - options={"seed": 1234, "temperature": 0} + options={"seed": 1234, "temperature": 0}, ) response = format_response(result["message"]["content"]) @@ -489,3 +483,67 @@ def summarize2( "model": model, "lens": "summarize2", } + + +def summarize3(language, epi, gender, age, diagnostics, medications, model): + epi_text = transform_fhir_epi(epi) + + lang = LANGUAGE_MAP[language] + print("++++" * 40, lang) + prompt = ( + "Write a concise summary of the following text delimited by triple backquotes. Return the 5 most important questions about it and answer them. " + + "You must only use the information provided in the text and avoid introducing any new information. " + + "It is of extreme importance that you summarize the document in " + + lang + + " and this is totally mandatory. Otherwise the reader will not understand. \n" + + "It should be written in a way that a person of gender: " + + gender + + " and with " + + str(age) + + " of age should be able to understand." + "You must reference highlight something that relates with the following topics and terms:" + + "|".join(diagnostics) + + "|".join(medications) + + "```" + + epi_text + + "```" + + "\nBULLET POINT SUMMARY:" + ) + print("the prompt will be:" + prompt) + + if model in [ + "llama-3.1-70b-Versatile", + "Mixtral-8x7b-32768", + "Llama3-70b-8192", + "Llama3-8b-8192", + "Llama-3.2-90b-Text-Preview", + ]: + chat_completion = groqclient.chat.completions.create( + messages=[ + {"role": "user", "content": prompt}, + ], + model=model, + temperature=0.05, + ) + + response = format_response(chat_completion.choices[0].message.content) + else: + result = client.chat( + model=model, + messages=[ + {"content": prompt, "role": "assistant"}, + ], + stream=False, + keep_alive="-1m", + options={"seed": 1234, "temperature": 0}, + ) + + response = format_response(result["message"]["content"]) + + return { + "response": response, + "prompt": prompt, + "datetime": datetime.now(), + "model": model, + "lens": "summarize", + } diff --git a/lens_app/prompts.py b/lens_app/prompts.py new file mode 100644 index 0000000..808c52e --- /dev/null +++ b/lens_app/prompts.py @@ -0,0 +1,11 @@ +PROMPT_LIST = { + "en": "Write a concise summary of the following text delimited by triple backquotes. Return your response in bullet points which covers the key points of the text. " + + "You must only use the information provided in the text and avoid introducing any new information. " + + "The summary should be clear, accurate, and comprehensive, highlighting the main ideas and key details of the text. \n", + "es": "Escribe un resumen conciso del siguiente texto delimitado por comillas triples. Devuelve tu respuesta en viñetas que cubran los puntos clave del texto. " + + "Debes usar únicamente la información proporcionada en el texto y evitar introducir información nueva. " + + "El resumen debe ser claro, preciso y completo, destacando las ideas principales y los detalles clave del texto. \n", + "da": "Skriv et kortfattet resumé af den følgende tekst, afgrænset af tredobbelte anførselstegn. Returner dit svar i punktform, der dækker tekstens vigtigste punkter." + + "Du må kun bruge de oplysninger, der er givet i teksten, og undgå at tilføje ny information." + + "Resuméet skal være klart, præcist og omfattende og fremhæve tekstens hovedideer og nøgleoplysninger. \n", +} diff --git a/lens_app/views.py b/lens_app/views.py index 6d9379c..f7395f9 100644 --- a/lens_app/views.py +++ b/lens_app/views.py @@ -12,6 +12,7 @@ process_ips, summarize, summarize2, + summarize3, summarize_no_personalization, ) @@ -47,6 +48,18 @@ def lens_app(bundleid=None): lenses = request.args.get("lenses", "") patientIdentifier = request.args.get("patientIdentifier", "") model = request.args.get("model", "") + if model not in [ + "graviting-llama", + "mistral", + "llama3", + "llama3.1", + "llama-3.1-70b-Versatile", + "Mixtral-8x7b-32768", + "Llama3-70b-8192", + "Llama3-8b-8192", + "Llama-3.2-90b-Text-Preview", + ]: + return "Error: Unknown Model", 404 print(lenses, patientIdentifier) if lenses not in ["lens-summary", "lens-summary-2"]: return "Error: lens not supported", 404 @@ -95,6 +108,7 @@ def lens_app(bundleid=None): # print(language, epi, gender, age, diagnostics, medications) if ips is None: + print("NO IPS") if model not in [ "mistral", "llama3", @@ -109,10 +123,23 @@ def lens_app(bundleid=None): else: response = summarize_no_personalization(language, epi, model) elif lenses == "lens-summary": + model = "llama3.1" if model == "graviting-llama" else model response = summarize( language, epi, gender, age, diagnostics, medications, model ) elif lenses == "lens-summary-2": + model = "llama3.1" if model == "graviting-llama" else model + # to prevent change on the app 19/12/2024 + response = summarize( + language, epi, gender, age, diagnostics, medications, model + ) + elif lenses == "lens-summary-3": + model = "llama3.1" if model == "graviting-llama" else model + response = summarize3( + language, epi, gender, age, diagnostics, medications, model + ) + elif lenses == "lens-summary-2-2": + model = "llama3.1" if model == "graviting-llama" else model response = summarize2( language, drug_name, gender, age, diagnostics, medications, model ) diff --git a/tests/test.py b/tests/test.py index 3558b4f..fd7ffd1 100644 --- a/tests/test.py +++ b/tests/test.py @@ -14,12 +14,12 @@ HOST = "http://localhost:5005/summary/" # HOST = "http://gravitate-health.lst.tfo.upm.es/ai/summary/" -for e in EPI: - myreq = HOST + e + "?lenses=lens-summary-2&model=graviting-llama" - print(myreq) - response = requests.get(myreq) - print("ePI", e, response.status_code) - print(response.json()) +# for e in EPI: +## myreq = HOST + e + "?lenses=lens-summary-2&model=graviting-llama" +# print(myreq) +## response = requests.get(myreq) +# print("ePI", e, response.status_code) +# print(response.json()) for p in PAT: for e in EPI: