aoptk.text_generation_api ========================= .. py:module:: aoptk.text_generation_api Attributes ---------- .. autoapisummary:: aoptk.text_generation_api.topics Exceptions ---------- .. autoapisummary:: aoptk.text_generation_api.LLMFailureError Classes ------- .. autoapisummary:: aoptk.text_generation_api.TextGenerationAPI Module Contents --------------- .. py:data:: topics .. py:exception:: LLMFailureError Bases: :py:obj:`Exception` Base class for capturing LLM failures. .. py:class:: TextGenerationAPI(model: str = 'gpt-oss-120b', url: str = 'https://llm.ai.e-infra.cz/v1', api_key: str = os.environ.get('CERIT_API_KEY')) Bases: :py:obj:`aoptk.find_chemical.FindChemical`, :py:obj:`aoptk.relationships.find_relationship.FindRelationship`, :py:obj:`aoptk.normalization.normalize_chemical.NormalizeChemical`, :py:obj:`aoptk.literature.convert_pdf_scan.ConvertPDFScan`, :py:obj:`aoptk.literature.convert_image.ConvertImage`, :py:obj:`aoptk.literature.find_relevant_publication.FindRelevantPublication` Text generation API using OpenAI. .. py:attribute:: role :type: str :value: 'user' .. py:attribute:: temperature :type: float :value: 0 .. py:attribute:: top_p :type: float :value: 1 .. py:attribute:: client :type: None :value: None .. py:attribute:: prompts_dir :type: pathlib.Path .. py:attribute:: chemical_prompt_template :type: str :value: 'chemical_prompt.txt' .. py:attribute:: relationship_text_prompt_template :type: str :value: 'relationship_text_prompt.txt' .. py:attribute:: relationship_text_images_prompt_template :type: str :value: 'relationship_text_images_prompt.txt' .. py:attribute:: relationships_table_prompt_template :type: str :value: 'relationships_table_prompt.txt' .. py:attribute:: normalization_prompt_template :type: str :value: 'normalization_prompt.txt' .. py:attribute:: convert_pdf_scan_prompt_template :type: str :value: 'convert_pdf_scan_prompt.txt' .. py:attribute:: convert_image_prompt_template :type: str :value: 'convert_image_prompt.txt' .. py:attribute:: find_relevant_publications_prompt_template :type: str :value: 'find_relevant_publications_prompt.txt' .. py:attribute:: specification_relationship_text_prompt :type: str :value: '' .. py:attribute:: model :value: 'gpt-oss-120b' .. py:attribute:: url :value: 'https://llm.ai.e-infra.cz/v1' .. py:attribute:: api_key .. py:method:: find_relationships_in_text(text: str, chemicals: list[aoptk.chemical.Chemical], effects: list[aoptk.effect.Effect], relationship_type: aoptk.relationship_type.RelationshipType) -> list[aoptk.relationships.relationship.Relationship] Find relationships between chemicals and effects. :param text: The input text. :type text: str :param chemicals: List of chemical entities. :type chemicals: list[Chemical] :param effects: List of effect entities. :type effects: list[Effect] :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType .. py:method:: _relationship_prompt(text: str, chemical: aoptk.chemical.Chemical, effect: aoptk.effect.Effect, relationship_type: aoptk.relationship_type.RelationshipType) -> str Classify the relationship between a chemical and an effect. :param text: The input text. :type text: str :param chemical: The chemical entity. :type chemical: Chemical :param effect: The effect entity. :type effect: Effect :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType .. py:method:: _render_prompt(template_name: str, **context: object) -> str .. py:method:: _prompt(content: str) -> str .. py:method:: _select_relationship_type(response: str, relationship_type: aoptk.relationship_type.RelationshipType) -> str | None Select the relationship type based on the response. :param response: The response from the model indicating the relationship type. :type response: str :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType .. py:method:: find_chemicals(text: str) -> list[aoptk.chemical.Chemical] Find chemicals in the given text. :param text: The input text to search for chemicals. :type text: str .. py:method:: _encode_image(image_path: str) -> tuple[str, str] Encode the image at the given path to a base64 string and return MIME type. :param image_path: The path to the image to encode. :type image_path: str :returns: A tuple of (base64_encoded_image, mime_type). :rtype: tuple[str, str] .. py:method:: _process_colon_separated_response(response: str, effect: aoptk.effect.Effect, relationship_type: aoptk.relationship_type.RelationshipType, image_path: str) -> list[aoptk.relationships.relationship.Relationship] Process the response from the model that is colon seperated. :param response: The response from the model. :type response: str :param effect: The effect entity. :type effect: Effect :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType :param context: The path to the image, used for context in the relationship. :type context: str :param image_path: The path to the image, used for context in the relationship. :type image_path: str .. py:method:: find_relationships_in_table(table_df: pandas.DataFrame, effects: list[aoptk.effect.Effect], relationship_type: aoptk.relationship_type.RelationshipType) -> list[aoptk.relationships.relationship.Relationship] Find relationships between chemicals and effects in a table. :param table_df: Pandas DataFrame. :type table_df: pd.DataFrame :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType :param effects: List of effect entities. :type effects: list[Effect] .. py:method:: _classify_relationships_in_table(table_df: pandas.DataFrame, effect: aoptk.effect.Effect, relationship_type: aoptk.relationship_type.RelationshipType) -> list[aoptk.relationships.relationship.Relationship] Classify relationships between chemicals and an effect in a table. :param table_df: Pandas DataFrame. :type table_df: pd.DataFrame :param effect: The effect entity. :type effect: Effect :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType :returns: List of relationships found in the table. :rtype: list[Relationship] .. py:method:: normalize_chemical(chemical: aoptk.chemical.Chemical, chemical_list: list[aoptk.chemical.Chemical]) -> aoptk.chemical.Chemical Normalize the chemical name by finding a matching name in the chemical list. :param chemical: The chemical to normalize. :type chemical: Chemical :param chemical_list: The list of chemicals to match against. :type chemical_list: list[Chemical] :returns: The normalized chemical. :rtype: Chemical .. py:method:: _find_matching_name(chemical: aoptk.chemical.Chemical, chemical_list: list[aoptk.chemical.Chemical]) -> aoptk.chemical.Chemical | None Find a matching chemical name in the chemical list. :param chemical: The chemical to find a match for. :type chemical: Chemical :param chemical_list: The list of chemicals to match against. :type chemical_list: list[Chemical] :returns: The matching chemical name, or None if no match is found. :rtype: Chemical .. py:method:: convert_pdf_scan(img_base64: str, mime_type: str) -> str Extract text from a base64-encoded image. :param img_base64: Base64-encoded image data. :type img_base64: str :param mime_type: MIME type of the image. Defaults to "image/jpeg". :type mime_type: str :returns: Extracted text from the image. :rtype: str .. py:method:: find_relationships_in_text_and_images(text: str, image_paths: list[str], relationship_type: aoptk.relationship_type.RelationshipType, effects: list[aoptk.effect.Effect]) -> list[aoptk.relationships.relationship.Relationship] Find relationships between chemicals and effects in the given text and images combined. :param text: The input text. :type text: str :param image_paths: List of paths to images. :type image_paths: list[str] :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType :param effects: List of effect entities. :type effects: list[Effect] .. py:method:: _classify_relationships_in_text_and_images(text: str, image_paths: list[str], effect: aoptk.effect.Effect, relationship_type: aoptk.relationship_type.RelationshipType) -> list[aoptk.relationships.relationship.Relationship] Classify relationships between chemicals and an effect in the given text and images combined. :param text: The input text. :type text: str :param image_paths: List of paths to images. :type image_paths: list[str] :param effect: The effect entity. :type effect: Effect :param relationship_type: The relationship type to classify. :type relationship_type: RelationshipType .. py:method:: convert_image(image_path: str, text: str) -> str Convert an image to text. :param image_path: Path to the image. :type image_path: str :param text: The full text of the publication for context. :type text: str .. py:method:: find_relevant_publications(question: str, text: str) -> bool | None Answer the question based on a given text. :param question: The question to search for relevant publications. :type question: str :param text: The extracted text of the publication. :type text: str