Skip to content

Commit

Permalink
improve json parsing from OAI output
Browse files Browse the repository at this point in the history
  • Loading branch information
j2whiting committed Feb 20, 2024
1 parent 49dfe47 commit deab5cf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
1 change: 0 additions & 1 deletion core/openai/prompts/model_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@
Use the following research paper as a reference: ---PAPER START---{research_paper}---PAPER END--. Ensure that the output follows the below model card format.\n
TEMPLATE: {model_card_template}\n
Make sure that the following text can be serialized as a JSON object:\n
```json
{{"""
3 changes: 0 additions & 3 deletions core/openai/prompts/petrinet_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Use the following petri net json file as a reference: {petrinet}.
Assume that parameter fields with missing values may have multiple different sets values discussed in the research paper for different conditions.\n
Return the different sets of initial parameters for the petri net model file like so:
```
json
{{"conditions": {{"condition_1": "description of condition_1", "condition_2": "description of condition_2", ...}},
"parameters": [{{
"id": "beta",
Expand All @@ -18,6 +16,5 @@
Only use parameters found in the reference petrinet file provided above.
Ensure that the output follows the above petri net format and can be serialized as a JSON. Specifically populate parameters and initials. Use the following
research paper to answer the user's query: {research_paper}\n\n Answer:
```json
{{
"""
25 changes: 9 additions & 16 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ def remove_references(text: str) -> str:
return new_text.strip()


def extract_json(text: str):
try:
matches = re.findall(r'\{.*\}', text, re.DOTALL)

if matches:
for match in matches:
try:
json_obj = json.loads(match)
return json_obj
except json.JSONDecodeError:
continue
return None
except Exception as e:
print(f"An error occurred while parsing JSON: {e}")
return None
def extract_json(text: str) -> dict:
corrected_text = text.replace('{{', '{').replace('}}', '}')
print(corrected_text)
try:
json_obj = json.loads(corrected_text)
return json_obj
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON: {e}\nfrom text {text}")

def normalize_greek_alphabet(text: str) -> str:
greek_to_english = {
Expand Down Expand Up @@ -62,7 +55,7 @@ def model_config_adapter(model_config: dict) -> dict:
"""

output_json = {'conditions': []}

print(model_config)
for condition_name, description in model_config['conditions'].items():
condition_data = {'name': condition_name, 'description': description, 'parameters': []}
for param_data in model_config['parameters']:
Expand Down

0 comments on commit deab5cf

Please sign in to comment.