Skip to content

Commit

Permalink
Updated arkitektur merged into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasmus authored and Rasmus committed Nov 30, 2023
1 parent 62d10c8 commit 263bce5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Empty file.
16 changes: 16 additions & 0 deletions relation_extraction/multilingual/llm_messenger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class LLMMessenger:

def process_message(self, response):
print("Recieved response from Llama2...")
print(response.text)


def costruct_prompt_message(data):
for file in data:
for sentence in file["sentences"]:
prompt_message = sentence["sentence"] + " ("
for em in sentence["entityMentions"]:
prompt_message += f"{em['name']}, "
prompt_message = prompt_message[:-2] + ")" #Remove comma and space after last entity mention
print(prompt_message)
#send_request()
66 changes: 66 additions & 0 deletions relation_extraction/multilingual/relation_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
from relation_extraction.ontology_messenger import OntologyMessenger
from relation_extraction.knowledge_graph_messenger import KnowledgeGraphMessenger
from relation_extraction.multilingual.llm_messenger import LLMMessenger

def parse_data(data):
"Parses JSON data and converts it into a dictionary with information on sentence, tokens, and entity mentions"

for file in data:
for i, sentence in enumerate(file["sentences"]):
for i, em in enumerate(sentence["entityMentions"]): #remove all entity mentions with iri=null
if em["iri"] is None:
sentence["entityMentions"].pop(i)
if len(sentence["entityMentions"]) < 2: #skip if less than 2 entity mentions
file["sentences"].pop(i)
return data

def begin_relation_extraction(data):
# try:
# relations = OntologyMessenger.send_request()
# except Exception as E:
# print(f"Exception during retrieval of relations: {str(E)}")
# raise Exception(f"Exception during retrieval of relations")

try:
parsed_data = parse_data(data)
except Exception as E:
print(f"Exception during parse of data {str(E)}")
raise Exception("Incorrectly formatted input. Exception during parsing")

try:
triples = LLMMessenger.costruct_prompt_message(parsed_data)
except Exception as E:
print(f"Exception during prompt to Llama 2: {str(E)}")
raise Exception("Exception during prompt to Llama 2")

# try:
# KnowledgeGraphMessenger.send_request(triples)
# except Exception as E:
# print(f"Exception during request to database. {str(E)}")
# raise Exception("Data was not sent to database due to connection error")


def test():
begin_relation_extraction(data=
[
{
"filename": "path/to/Artikel.txt",
"language": "en",
"sentences": [
{
"sentence": "Barrack Obama is married to Michelle Obama.",
"sentenceStartIndex": 20,
"sentenceEndIndex": 62,
"entityMentions":
[
{ "name": "Barrack Obama", "startIndex": 0, "endIndex": 12, "iri": "knox-kb01.srv.aau.dk/Barack_Obama" },
{ "name": "Michelle Obama", "startIndex": 27, "endIndex": 40, "iri": "knox-kb01.srv.aau.dk/Michele_Obama" }
]
}
]
}
]
)

test()

0 comments on commit 263bce5

Please sign in to comment.