From 99067cb6c07772c8a60669e41cecc4beebf96a46 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Sat, 26 Oct 2024 14:25:17 +0100 Subject: [PATCH] Remove cruft --- .../model/prompt/template/prompts.py | 47 --- .../model/prompt/template/service.py | 320 +----------------- 2 files changed, 1 insertion(+), 366 deletions(-) delete mode 100644 trustgraph-flow/trustgraph/model/prompt/template/prompts.py diff --git a/trustgraph-flow/trustgraph/model/prompt/template/prompts.py b/trustgraph-flow/trustgraph/model/prompt/template/prompts.py deleted file mode 100644 index e3148157..00000000 --- a/trustgraph-flow/trustgraph/model/prompt/template/prompts.py +++ /dev/null @@ -1,47 +0,0 @@ - -def to_relationships(template, text): - return template.format(text=text) - -def to_definitions(template, text): - return template.format(text=text) - -def to_topics(template, text): - return template.format(text=text) - -def to_rows(template, schema, text): - - field_schema = [ - f"- Name: {f.name}\n Type: {f.type}\n Definition: {f.description}" - for f in schema.fields - ] - - field_schema = "\n".join(field_schema) - - return template.format(schema=schema, text=text) - - schema = f"""Object name: {schema.name} -Description: {schema.description} - -Fields: -{schema}""" - - prompt = f"""""" - - return prompt - -def get_cypher(kg): - sg2 = [] - for f in kg: - sg2.append(f"({f.s})-[{f.p}]->({f.o})") - kg = "\n".join(sg2) - kg = kg.replace("\\", "-") - return kg - -def to_kg_query(template, query, kg): - cypher = get_cypher(kg) - return template.format(query=query, graph=cypher) - -def to_document_query(template, query, docs): - docs = "\n\n".join(docs) - return template.format(query=query, documents=docs) - diff --git a/trustgraph-flow/trustgraph/model/prompt/template/service.py b/trustgraph-flow/trustgraph/model/prompt/template/service.py index b8610ee9..f3ffe0c2 100755 --- a/trustgraph-flow/trustgraph/model/prompt/template/service.py +++ b/trustgraph-flow/trustgraph/model/prompt/template/service.py @@ -15,10 +15,8 @@ from .... schema import text_completion_response_queue from .... schema import prompt_request_queue, prompt_response_queue from .... base import ConsumerProducer from .... clients.llm_client import LlmClient -from . prompt_manager import PromptConfiguration, Prompt, PromptManager -from . prompts import to_definitions, to_relationships, to_rows -from . prompts import to_kg_query, to_document_query, to_topics +from . prompt_manager import PromptConfiguration, Prompt, PromptManager module = ".".join(__name__.split(".")[1:-1]) @@ -235,49 +233,6 @@ class Processor(ConsumerProducer): self.producer.send(r, properties={"id": id}) - def handle_extract_definitions(self, id, v): - - try: - - prompt = to_definitions(self.definition_template, v.chunk) - - ans = self.llm.request(prompt) - - # Silently ignore JSON parse error - try: - defs = self.parse_json(ans) - except: - print("JSON parse error, ignored", flush=True) - defs = [] - - output = [] - - for defn in defs: - - try: - e = defn["entity"] - d = defn["definition"] - - if e == "": continue - if e is None: continue - if d == "": continue - if d is None: continue - - output.append( - Definition( - name=e, definition=d - ) - ) - - except: - print("definition fields missing, ignored", flush=True) - - print("Send response...", flush=True) - r = PromptResponse(definitions=output, error=None) - self.producer.send(r, properties={"id": id}) - - print("Done.", flush=True) - except Exception as e: print(f"Exception: {e}") @@ -294,279 +249,6 @@ class Processor(ConsumerProducer): self.producer.send(r, properties={"id": id}) - def handle_extract_topics(self, id, v): - - try: - - prompt = to_topics(self.topic_template, v.chunk) - - ans = self.llm.request(prompt) - - # Silently ignore JSON parse error - try: - defs = self.parse_json(ans) - except: - print("JSON parse error, ignored", flush=True) - defs = [] - - output = [] - - for defn in defs: - - try: - e = defn["topic"] - d = defn["definition"] - - if e == "": continue - if e is None: continue - if d == "": continue - if d is None: continue - - output.append( - Topic( - name=e, definition=d - ) - ) - - except: - print("definition fields missing, ignored", flush=True) - - print("Send response...", flush=True) - r = PromptResponse(topics=output, error=None) - self.producer.send(r, properties={"id": id}) - - print("Done.", flush=True) - - except Exception as e: - - print(f"Exception: {e}") - - print("Send error response...", flush=True) - - r = PromptResponse( - error=Error( - type = "llm-error", - message = str(e), - ), - response=None, - ) - - self.producer.send(r, properties={"id": id}) - - - def handle_extract_relationships(self, id, v): - - try: - - prompt = to_relationships(self.relationship_template, v.chunk) - - ans = self.llm.request(prompt) - - # Silently ignore JSON parse error - try: - defs = self.parse_json(ans) - except: - print("JSON parse error, ignored", flush=True) - defs = [] - - output = [] - - for defn in defs: - - try: - - s = defn["subject"] - p = defn["predicate"] - o = defn["object"] - o_entity = defn["object-entity"] - - if s == "": continue - if s is None: continue - - if p == "": continue - if p is None: continue - - if o == "": continue - if o is None: continue - - if o_entity == "" or o_entity is None: - o_entity = False - - output.append( - Relationship( - s = s, - p = p, - o = o, - o_entity = o_entity, - ) - ) - - except Exception as e: - print("relationship fields missing, ignored", flush=True) - - print("Send response...", flush=True) - r = PromptResponse(relationships=output, error=None) - self.producer.send(r, properties={"id": id}) - - print("Done.", flush=True) - - except Exception as e: - - print(f"Exception: {e}") - - print("Send error response...", flush=True) - - r = PromptResponse( - error=Error( - type = "llm-error", - message = str(e), - ), - response=None, - ) - - self.producer.send(r, properties={"id": id}) - - def handle_extract_rows(self, id, v): - - try: - - fields = v.row_schema.fields - - prompt = to_rows(self.rows_template, v.row_schema, v.chunk) - - print(prompt) - - ans = self.llm.request(prompt) - - print(ans) - - # Silently ignore JSON parse error - try: - objs = self.parse_json(ans) - except: - print("JSON parse error, ignored", flush=True) - objs = [] - - output = [] - - for obj in objs: - - try: - - row = {} - - for f in fields: - - if f.name not in obj: - print(f"Object ignored, missing field {f.name}") - row = {} - break - - row[f.name] = obj[f.name] - - if row == {}: - continue - - output.append(row) - - except Exception as e: - print("row fields missing, ignored", flush=True) - - for row in output: - print(row) - - print("Send response...", flush=True) - r = PromptResponse(rows=output, error=None) - self.producer.send(r, properties={"id": id}) - - print("Done.", flush=True) - - except Exception as e: - - print(f"Exception: {e}") - - print("Send error response...", flush=True) - - r = PromptResponse( - error=Error( - type = "llm-error", - message = str(e), - ), - response=None, - ) - - self.producer.send(r, properties={"id": id}) - - def handle_kg_prompt(self, id, v): - - try: - - prompt = to_kg_query(self.knowledge_query_template, v.query, v.kg) - - print(prompt) - - ans = self.llm.request(prompt) - - print(ans) - - print("Send response...", flush=True) - r = PromptResponse(answer=ans, error=None) - self.producer.send(r, properties={"id": id}) - - print("Done.", flush=True) - - except Exception as e: - - print(f"Exception: {e}") - - print("Send error response...", flush=True) - - r = PromptResponse( - error=Error( - type = "llm-error", - message = str(e), - ), - response=None, - ) - - self.producer.send(r, properties={"id": id}) - - def handle_document_prompt(self, id, v): - - try: - - prompt = to_document_query( - self.document_query_template, v.query, v.documents - ) - - print(prompt) - - ans = self.llm.request(prompt) - - print(ans) - - print("Send response...", flush=True) - r = PromptResponse(answer=ans, error=None) - self.producer.send(r, properties={"id": id}) - - print("Done.", flush=True) - - except Exception as e: - - print(f"Exception: {e}") - - print("Send error response...", flush=True) - - r = PromptResponse( - error=Error( - type = "llm-error", - message = str(e), - ), - response=None, - ) - - self.producer.send(r, properties={"id": id}) - @staticmethod def add_args(parser):