Skip to content

Commit

Permalink
start changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joofio committed Dec 19, 2024
1 parent c3fbd30 commit d741201
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 42 deletions.
19 changes: 0 additions & 19 deletions Modelfile

This file was deleted.

92 changes: 75 additions & 17 deletions lens_app/core.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

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
from groq import Groq
from ollama import Client
from openai import OpenAI

from lens_app.prompts import PROMPT_LIST

load_dotenv()

SERVER_URL = os.getenv("SERVER_URL")
Expand Down Expand Up @@ -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()

Expand All @@ -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 = (
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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",
}
11 changes: 11 additions & 0 deletions lens_app/prompts.py
Original file line number Diff line number Diff line change
@@ -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",
}
27 changes: 27 additions & 0 deletions lens_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
process_ips,
summarize,
summarize2,
summarize3,
summarize_no_personalization,
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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
)
Expand Down
12 changes: 6 additions & 6 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d741201

Please sign in to comment.