33import os
44import uuid
55from pathlib import Path
6+ from typing import Literal
67
78import chromadb
89import numpy as np
1819import requests
1920
2021from ChemCoScientist .chemical_utils .openchemie_functions import extract_molecules_from_figure , extract_reactions_from_figure
22+ from ChemCoScientist .paper_analysis .constants import ResearchArea
2123from ChemCoScientist .paper_analysis .prompts import summarisation_prompt
2224from ChemCoScientist .paper_analysis .settings import allowed_providers
2325from ChemCoScientist .paper_analysis .settings import settings as default_settings
@@ -53,10 +55,18 @@ class ExpandedSummary(BaseModel):
5355 description = "Title of the paper. If the title is not explicitly specified, use the default value - 'NO TITLE'"
5456 )
5557 publication_year : int = Field (
56- description = (
57- "Year of publication of the paper. If the publication year is not explicitly specified, use the default"
58+ description = "Year of publication of the paper. If the publication year is not explicitly specified, use the default"
5859 " value - 9999."
59- )
60+ )
61+ paper_authors : str = Field (
62+ description = "Authors of the paper. If the authors are not explicitly specified, use the default value - 'NO AUTHORS'"
63+ )
64+ publication_source : str = Field (
65+ description = "Source where the paper was published. If the source is not explicitly specified, use the default value - 'NO SOURCE'"
66+ )
67+ research_area : ResearchArea = Field (
68+ description = "Area or field of chemistry the paper is about. Must be one of the predefined values."
69+ " If the area has no match in the predefined list or is hard to determine, use the default value - 'OTHER'"
6070 )
6171
6272
@@ -455,7 +465,7 @@ def search_for_papers(self,
455465 return res
456466
457467 def retrieve_context (
458- self , query : str , relevant_papers : dict = None
468+ self , query : str , relevant_papers : dict = None , meta_filter : dict = None
459469 ) -> tuple [list , dict , dict ]:
460470 """
461471 Retrieves relevant information from text and images associated with scientific papers based on a user query.
@@ -468,14 +478,15 @@ def retrieve_context(
468478 query (str): The search query used to identify relevant information.
469479 relevant_papers (list, optional): A list of pre-identified relevant papers. Defaults to None, in which case
470480 a search for relevant papers is initiated.
481+ meta_filter (dict, optional): A dictionary of metadata filters to apply during the search.
471482
472483 Returns:
473484 tuple[list, dict]: A tuple containing the retrieved text and image context.
474485 - text_context (list): A list of text chunks deemed most relevant to the query.
475486 - image_context (dict): A dictionary containing image data associated with the query.
476487 """
477488 if not relevant_papers :
478- relevant_papers = self .search_for_papers (query )
489+ relevant_papers = self .search_for_papers (query , meta_filter = meta_filter )
479490
480491 raw_text_context = self .client .query_chromadb (
481492 self .txt_collection ,
@@ -558,7 +569,24 @@ def search_with_reranker(
558569
559570 return scored_docs [:top_k ]
560571
561- def add_paper_summary_to_db (self , paper_name : str , parsed_paper : str , llm ) -> None :
572+ def _generate_expanded_summary (self , parsed_paper : str , llm ) -> ExpandedSummary :
573+ """
574+ Generates an expanded summary of a paper using a language model.
575+
576+ This method takes parsed paper content and uses an LLM to extract and structure
577+ key information including summary, title, authors, publication year, and source.
578+
579+ Args:
580+ parsed_paper (str): The text content of the parsed paper.
581+ llm: The language model used to generate the summary.
582+
583+ Returns:
584+ ExpandedSummary: An object containing the paper's structured summary information.
585+ """
586+ expanded_summary : ExpandedSummary = llm .invoke ([HumanMessage (content = summarisation_prompt + parsed_paper )])
587+ return expanded_summary
588+
589+ def add_paper_summary_to_db (self , paper_name : str , parsed_paper : str , expanded_summary : ExpandedSummary ) -> None :
562590 """
563591 Adds a paper summary to the document collection for efficient information retrieval.
564592
@@ -574,13 +602,15 @@ def add_paper_summary_to_db(self, paper_name: str, parsed_paper: str, llm) -> No
574602 Returns:
575603 None
576604 """
577- expanded_summary : ExpandedSummary = llm .invoke ([HumanMessage (content = summarisation_prompt + parsed_paper )])
578605 doc = Document (
579606 page_content = expanded_summary .paper_summary ,
580607 metadata = {
581608 "source" : paper_name ,
582609 "paper_title" : expanded_summary .paper_title ,
583- "publication_year" : expanded_summary .publication_year
610+ "publication_year" : expanded_summary .publication_year ,
611+ "paper_authors" : expanded_summary .paper_authors ,
612+ "publication_source" : expanded_summary .publication_source ,
613+ "research_area" : expanded_summary .research_area
584614 }
585615 )
586616 embedding = self .get_embeddings ([doc .page_content ])
@@ -771,13 +801,15 @@ def process_single_document(folder_path: Path, s3_service: S3BucketService, s3_p
771801 else :
772802 parsed_paper , mapping = clean_up_html (folder_path , paper_name , text )
773803 print (f"Finished post-processing paper: { paper_name } " )
774- documents = html_chunking (parsed_paper , paper_name )
775804
776805 llm = create_llm_connector (SUMMARY_LLM_URL , extra_body = {"provider" : {"only" : allowed_providers }})
777806 struct_llm = llm .with_structured_output (schema = ExpandedSummary )
807+ paper_summary = process_local_store ._generate_expanded_summary (parsed_paper , struct_llm )
808+
809+ documents = html_chunking (parsed_paper , paper_name , paper_summary )
778810
779811 print (f"Starting loading paper: { paper_name } " )
780- process_local_store .add_paper_summary_to_db (str (paper_name_to_load ), parsed_paper , struct_llm )
812+ process_local_store .add_paper_summary_to_db (str (paper_name_to_load ), parsed_paper , paper_summary )
781813 process_local_store .store_text_chunks_in_chromadb (documents )
782814 process_local_store .store_images_in_chromadb_txt_format (str (folder_path ), str (paper_name_to_load ), mapping )
783815 print (f"Finished loading paper: { paper_name } " )
0 commit comments