-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Openai summaries #7
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1049949
openai_summaries: organizing endpoints and openai endpoint for summar…
dgbaenar 9efd419
Generation of summaries from open ai and upload them to elasticsearch
dgbaenar c900020
Generation of summaries from open ai and upload them to elasticsearch
dgbaenar fbca494
Evaluation retrieval integrated as evaluation endpoint. Openai summar…
dgbaenar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from fastapi import FastAPI | ||
from app.config.elasticsearch_config import create_index_if_not_exists | ||
from app.models.sentence_transformer import get_sentence_transformer | ||
from app.config.settings import settings | ||
|
||
|
||
embedding_model = get_sentence_transformer() | ||
es_client = create_index_if_not_exists(settings.elasticsearch.index_name) | ||
|
||
|
||
def create_app(): | ||
app = FastAPI() | ||
|
||
from app.routes.database_endpoints import router as database_router | ||
from app.routes.llm_endpoints import router as llm_router | ||
from app.routes.openai_endpoints import router as openai_router | ||
from app.routes.evaluation_endpoints import router as evaluation_router | ||
|
||
app.include_router(database_router, prefix="/database") | ||
app.include_router(llm_router, prefix="/generation") | ||
app.include_router(openai_router, prefix="/openai") | ||
app.include_router(evaluation_router, prefix="/evaluation") | ||
|
||
return app | ||
|
||
|
||
app = create_app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
You will receive a single FHIR resource. Summarize the key information | ||
from the resource in a clear, concise paragraph of plain text, | ||
ideally up to 800 characters. The output should be human-readable and | ||
understandable, not in JSON or other structured formats. Focus on the most | ||
relevant attributes and omit unnecessary details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import json | ||
import random | ||
|
||
from tqdm import tqdm | ||
|
||
from app.services.search_documents import search_query | ||
|
||
|
||
def evaluate_resources_summaries_retrieval( | ||
es_client: str, | ||
embedding_model: str, | ||
resource_chunk_counts: dict, | ||
qa_references: list[dict], | ||
search_text_boost: int = 1, | ||
search_embedding_boost: int = 1, | ||
k: int = 5 | ||
) -> dict: | ||
# Initialize counters and sums for metrics | ||
total_questions = 0 | ||
total_contexts_found = 0 | ||
position_sum = 0 | ||
reciprocal_rank_sum = 0 | ||
precision_sum = 0 | ||
recall_sum = 0 | ||
|
||
# Iterate over the OpenAI responses | ||
for response in tqdm(qa_references, total=len(qa_references), desc="Calculating retrieval metrics"): | ||
# Get content and id of openai responses | ||
reference_resource_id = response["custom_id"] | ||
content = response["response"]["body"]["choices"][0]["message"]["content"] | ||
|
||
questions_and_answers = json.loads( | ||
content)["questions_and_answers"] | ||
|
||
if len(questions_and_answers) > 0: | ||
# Sample one random question per resource_id to evaluate | ||
questions_and_answers = [random.choice(questions_and_answers)] | ||
|
||
for qa in questions_and_answers: | ||
if isinstance(qa, dict) and "question" in qa: | ||
question = qa["question"] | ||
total_questions += 1 | ||
|
||
# Query question | ||
search_results = search_query(question, | ||
embedding_model, | ||
es_client, | ||
k=k, | ||
text_boost=search_text_boost, | ||
embedding_boost=search_embedding_boost) | ||
|
||
# Evaluate if any returned chunk belongs to the correct resource_id | ||
found = False | ||
rank = 0 | ||
retrieved_relevant_chunks = 0 | ||
|
||
# Get the total number of relevant chunks for this resource_id | ||
relevant_chunks = resource_chunk_counts[reference_resource_id] | ||
|
||
if search_results != {"detail": "Not Found"}: | ||
for i, result in enumerate(search_results): | ||
if result["metadata"]["resource_id"] == reference_resource_id: | ||
if not found: | ||
total_contexts_found += 1 | ||
rank = i + 1 | ||
reciprocal_rank_sum += 1 / rank | ||
found = True | ||
retrieved_relevant_chunks += 1 | ||
elif search_results == {"detail": "Not Found"}: | ||
search_results = {} | ||
|
||
# Calculate precision and recall for this specific question | ||
precision = retrieved_relevant_chunks / \ | ||
len(search_results) if len(search_results) > 0 else 0 | ||
recall = retrieved_relevant_chunks / relevant_chunks if relevant_chunks > 0 else 0 | ||
|
||
precision_sum += precision | ||
recall_sum += recall | ||
|
||
if found: | ||
position_sum += rank | ||
|
||
# Calculate final metrics | ||
retrieval_accuracy = round( | ||
total_contexts_found / total_questions, 3) if total_questions > 0 else 0 | ||
average_position = round( | ||
position_sum / total_contexts_found, 3) if total_contexts_found > 0 else 0 | ||
mrr = round(reciprocal_rank_sum / total_questions, | ||
3) if total_questions > 0 else 0 | ||
average_precision = round( | ||
precision_sum / total_questions, 3) if total_questions > 0 else 0 | ||
average_recall = round(recall_sum / total_questions, | ||
3) if total_questions > 0 else 0 | ||
|
||
return { | ||
# The percentage of questions for which the system successfully retrieved at least one relevant chunk. | ||
"Retrieval Accuracy": retrieval_accuracy, | ||
"Average Position": average_position, | ||
"MRR": mrr, | ||
# Precision = Number of relevant chunks returned / Total number of chunks returned | ||
"Average Precision": average_precision, | ||
# Recall = Number of relevant chunks returned / Total number of relevant chunks that exist | ||
"Average Recall": average_recall, | ||
# Others | ||
"Total Questions": total_questions, | ||
"Total contexts found": total_contexts_found, | ||
"Total positions sum": position_sum, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a point in having these lines when you later replace all line breaks (\n)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove them, I thought I had removed all of them