-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated arkitektur merged into branch
- Loading branch information
Rasmus
authored and
Rasmus
committed
Nov 30, 2023
1 parent
62d10c8
commit 263bce5
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
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,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() |
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,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() |