promptslab / Promptify

Prompt Engineering | Prompt Versioning | Use GPT or other prompt based models to get structured output. Join our discord for Prompt-Engineering, LLMs and other latest research
https://discord.gg/m88xfYMbK6
Apache License 2.0
3.21k stars 238 forks source link

Parsing issue of single and double quote response from openai #4

Closed monk1337 closed 1 year ago

monk1337 commented 1 year ago

Issue was in Question-Generation prompt, Need to check and fix it.

Issue code:

from promptify import OpenAI
from promptify import Prompter

context = "The patient is a 93-year-old female with a medical history of chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation admitted for evaluation and management of severe nausea and vomiting and urinary tract infection"

result = nlp_prompter.fit('qa_gen.jinja',
                                      text_input=context,
                                      domain="medical",
                                      total_questions=5,
                                      max_QA_tokens=10
                                     )

The output is :

[{'Q': 'What is the patient's age?', 'A': '93 years old'}, 
{'Q': 'What medical conditions does the patient have?', 'A': 'Chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation'}, 
{'Q': 'What is the patient admitted for?', 'A': 'Evaluation and management of severe nausea and vomiting and urinary tract infection'}, 
{'Q': 'What is the patient's gender?', 'A': 'Female'}, 
{'Q': 'What is the patient's primary complaint?', 'A': 'Severe nausea and vomiting'}]

Parsing this output with eval() gives error because of single and double quote issue.

escesare commented 1 year ago

Not sure how far you've gotten on this bug, but I'll throw out a suggestion.

Since the user can specify any kind of output, it would be difficult to build a universal parser/verifier. Maybe we could just settle for a collection of parsers that the users can select from depending on the output format they specified, e.g. something like this?:

result       = nlp_prompter.fit('ner.jinja',
                                parser=promptify.parser.parse_dict_of_str
                                ...
monk1337 commented 1 year ago

@escesare That's an interesting suggestion. I am working on a parser class; if you are using discord, please reach out to me, and let's discuss this in detail.

discord: StoicBatman#6403

monk1337 commented 1 year ago

Temporary solution

def escaped_(data):
    escaped_str = re.sub(r'(\w)(\')(\w)', r'\1\'\3', data)
    obj         = ast.literal_eval(escaped_str)
    return obj

or

def escaped_(data):
    if "'" in data:
        escaped_str = re.sub(r'(\w)(\')(\w)', r'\1\"\3', data)
    else:
        escaped_str = re.sub(r'(\w)(\")(\w)', r'\1\'\3', data)
    return escaped_str
monk1337 commented 1 year ago

Issue solved https://github.com/promptslab/Promptify/blob/2f67481d053a4ea43fd8fefd50e4b453863beb6a/promptify/parser/parser.py#L78