Fix replace strings for JSON (#57)

* Fix replace strings for JSON
* Add JSON markdown preamble/postamble in prompts
* Remove JSON chicanery in LLM  handling
* Bump version
This commit is contained in:
cybermaggedon 2024-09-05 20:49:32 +01:00 committed by GitHub
parent 3445759598
commit 375b213a54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 277 additions and 276 deletions

View file

@ -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 = []

View file

@ -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 = []

View file

@ -134,10 +134,7 @@ class Processor(ConsumerProducer):
print("Send response...", flush=True)
resp = response.replace("```json", "")
resp = response.replace("```", "")
r = TextCompletionResponse(response=resp, error=None)
r = TextCompletionResponse(response=response, error=None)
self.producer.send(r, properties={"id": id})
except TooManyRequests:

View file

@ -213,13 +213,10 @@ class Processor(ConsumerProducer):
print(outputtext, flush=True)
resp = outputtext.replace("```json", "")
resp = resp.replace("```", "")
print("Send response...", flush=True)
r = TextCompletionResponse(
error=None,
response=resp
response=outputtext
)
self.send(r, properties={"id": id})

View file

@ -93,9 +93,6 @@ class Processor(ConsumerProducer):
resp = output.text
print(resp, flush=True)
resp = resp.replace("```json", "")
resp = resp.replace("```", "")
print("Send response...", flush=True)
r = TextCompletionResponse(response=resp, error=None)
self.send(r, properties={"id": id})

View file

@ -87,10 +87,7 @@ class Processor(ConsumerProducer):
print("Send response...", flush=True)
resp = response.replace("```json", "")
resp = response.replace("```", "")
r = TextCompletionResponse(response=resp, error=None)
r = TextCompletionResponse(response=response, error=None)
self.send(r, properties={"id": id})

View file

@ -148,9 +148,6 @@ class Processor(ConsumerProducer):
resp = resp.text
resp = resp.replace("```json", "")
resp = resp.replace("```", "")
print("Send response...", flush=True)
r = TextCompletionResponse(