diff --git a/trustgraph/model/prompt/generic/service.py b/trustgraph/model/prompt/generic/service.py index 0c6f8095..bc78f664 100755 --- a/trustgraph/model/prompt/generic/service.py +++ b/trustgraph/model/prompt/generic/service.py @@ -55,6 +55,14 @@ class Processor(ConsumerProducer): pulsar_host = self.pulsar_host ) + def parse_json(self, text): + + # Hacky, workaround temperamental JSON markdown + text = text.replace("```json", "") + text = text.replace("```", "") + + return json.loads(text) + def handle(self, msg): v = msg.value() @@ -107,7 +115,7 @@ class Processor(ConsumerProducer): # Silently ignore JSON parse error try: - defs = json.loads(ans) + defs = self.parse_json(ans) except: print("JSON parse error, ignored", flush=True) defs = [] @@ -166,7 +174,7 @@ class Processor(ConsumerProducer): # Silently ignore JSON parse error try: - defs = json.loads(ans) + defs = self.parse_json(ans) except: print("JSON parse error, ignored", flush=True) defs = [] @@ -244,7 +252,7 @@ class Processor(ConsumerProducer): # Silently ignore JSON parse error try: - objs = json.loads(ans) + objs = self.parse_json(ans) except: print("JSON parse error, ignored", flush=True) objs = [] diff --git a/trustgraph/model/prompt/template/service.py b/trustgraph/model/prompt/template/service.py index f54b5071..ce595720 100755 --- a/trustgraph/model/prompt/template/service.py +++ b/trustgraph/model/prompt/template/service.py @@ -67,6 +67,14 @@ class Processor(ConsumerProducer): self.knowledge_query_template = knowledge_query_template self.document_query_template = document_query_template + def parse_json(self, text): + + # Hacky, workaround temperamental JSON markdown + text = text.replace("```json", "") + text = text.replace("```", "") + + return json.loads(text) + def handle(self, msg): v = msg.value() @@ -119,7 +127,7 @@ class Processor(ConsumerProducer): # Silently ignore JSON parse error try: - defs = json.loads(ans) + defs = self.parse_json(ans) except: print("JSON parse error, ignored", flush=True) defs = [] @@ -178,7 +186,7 @@ class Processor(ConsumerProducer): # Silently ignore JSON parse error try: - defs = json.loads(ans) + defs = self.parse_json(ans) except: print("JSON parse error, ignored", flush=True) defs = [] @@ -256,7 +264,7 @@ class Processor(ConsumerProducer): # Silently ignore JSON parse error try: - objs = json.loads(ans) + objs = self.parse_json(ans) except: print("JSON parse error, ignored", flush=True) objs = []