thomas commited on
Commit ·
83dc5b3
1
Parent(s): dd2da3e
feature(#32): json parsing issue fixed.
Browse files- src/common/utils.py +21 -0
- src/rising_plugin/risingplugin.py +4 -36
src/common/utils.py
CHANGED
|
@@ -64,3 +64,24 @@ def validateJSON(jsonData):
|
|
| 64 |
except ValueError as err:
|
| 65 |
return False
|
| 66 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except ValueError as err:
|
| 65 |
return False
|
| 66 |
return True
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def parseJsonFromCompletion(data: str) -> json:
|
| 70 |
+
result = data[1:-1]
|
| 71 |
+
# fmt: off
|
| 72 |
+
result = result.replace("{'", '{"')
|
| 73 |
+
result = result.replace("'}", '"}')
|
| 74 |
+
result = result.replace("': '", '": "')
|
| 75 |
+
result = result.replace("': \\\"", '": \"')
|
| 76 |
+
result = result.replace("', '", '", "')
|
| 77 |
+
|
| 78 |
+
substring = '\\"}'
|
| 79 |
+
replacement = '\"}'
|
| 80 |
+
|
| 81 |
+
index = result.rfind(substring)
|
| 82 |
+
|
| 83 |
+
if index == len(result) - 3:
|
| 84 |
+
result = result[:index] + replacement + result[index + len(substring):]
|
| 85 |
+
# fmt: on
|
| 86 |
+
result = json.loads(result.replace("':", '":'))
|
| 87 |
+
return result
|
src/rising_plugin/risingplugin.py
CHANGED
|
@@ -17,6 +17,7 @@ from firebase_admin import storage
|
|
| 17 |
from ..common.utils import (
|
| 18 |
OPENAI_API_KEY,
|
| 19 |
FIREBASE_STORAGE_ROOT,
|
|
|
|
| 20 |
)
|
| 21 |
from .image_embedding import (
|
| 22 |
query_image_text,
|
|
@@ -48,24 +49,8 @@ def processLargeText(app: any, chunks: any):
|
|
| 48 |
]
|
| 49 |
)
|
| 50 |
result = json.dumps(message["content"])
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
result = result.replace("{'", '{"')
|
| 54 |
-
result = result.replace("'}", '"}')
|
| 55 |
-
result = result.replace("': '", '": "')
|
| 56 |
-
result = result.replace("': \\\"", '": \"')
|
| 57 |
-
result = result.replace("', '", '", "')
|
| 58 |
-
|
| 59 |
-
substring = '\\"}'
|
| 60 |
-
replacement = '\"}'
|
| 61 |
-
|
| 62 |
-
index = result.rfind(substring)
|
| 63 |
-
|
| 64 |
-
if index == len(result) - 3:
|
| 65 |
-
result = result[:index] + replacement + result[index + len(substring):]
|
| 66 |
-
# fmt: on
|
| 67 |
-
result = json.loads(result)
|
| 68 |
-
return result
|
| 69 |
else:
|
| 70 |
first_query = "The total length of the content that I want to send you is too large to send in only one piece.\nFor sending you that content, I will follow this rule:\n[START PART 1/10]\nThis is the content of the part 1 out of 10 in total\n[END PART 1/10]\nThen you just answer: 'Received part 1/10'\nAnd when I tell you 'ALL PART SENT', then you can continue processing the data and answering my requests."
|
| 71 |
app.generate(messages=[{"role": "user", "content": first_query}])
|
|
@@ -117,24 +102,7 @@ def processLargeText(app: any, chunks: any):
|
|
| 117 |
messages=[{"role": "user", "content": last_query}]
|
| 118 |
)
|
| 119 |
result = json.dumps(message["content"])
|
| 120 |
-
|
| 121 |
-
# fmt: off
|
| 122 |
-
result = result.replace("{'", '{"')
|
| 123 |
-
result = result.replace("'}", '"}')
|
| 124 |
-
result = result.replace("': '", '": "')
|
| 125 |
-
result = result.replace("': \\\"", '": \"')
|
| 126 |
-
result = result.replace("', '", '", "')
|
| 127 |
-
|
| 128 |
-
substring = '\\"}'
|
| 129 |
-
replacement = '\"}'
|
| 130 |
-
|
| 131 |
-
index = result.rfind(substring)
|
| 132 |
-
|
| 133 |
-
if index == len(result) - 3:
|
| 134 |
-
result = result[:index] + replacement + result[index + len(substring):]
|
| 135 |
-
# fmt: on
|
| 136 |
-
result = json.loads(result)
|
| 137 |
-
return result
|
| 138 |
# out of for-loop
|
| 139 |
|
| 140 |
|
|
|
|
| 17 |
from ..common.utils import (
|
| 18 |
OPENAI_API_KEY,
|
| 19 |
FIREBASE_STORAGE_ROOT,
|
| 20 |
+
parseJsonFromCompletion,
|
| 21 |
)
|
| 22 |
from .image_embedding import (
|
| 23 |
query_image_text,
|
|
|
|
| 49 |
]
|
| 50 |
)
|
| 51 |
result = json.dumps(message["content"])
|
| 52 |
+
|
| 53 |
+
return parseJsonFromCompletion(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
first_query = "The total length of the content that I want to send you is too large to send in only one piece.\nFor sending you that content, I will follow this rule:\n[START PART 1/10]\nThis is the content of the part 1 out of 10 in total\n[END PART 1/10]\nThen you just answer: 'Received part 1/10'\nAnd when I tell you 'ALL PART SENT', then you can continue processing the data and answering my requests."
|
| 56 |
app.generate(messages=[{"role": "user", "content": first_query}])
|
|
|
|
| 102 |
messages=[{"role": "user", "content": last_query}]
|
| 103 |
)
|
| 104 |
result = json.dumps(message["content"])
|
| 105 |
+
return parseJsonFromCompletion(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# out of for-loop
|
| 107 |
|
| 108 |
|