Skip to content

Commit

Permalink
extract json
Browse files Browse the repository at this point in the history
  • Loading branch information
DonaldLamNL committed Jan 31, 2024
1 parent aeb1093 commit 5ed39ce
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ cython_debug/

# folders
records

save
backup
results

Expand Down
14 changes: 11 additions & 3 deletions gpt_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import openai
import time
import os
import re
import random

from utils import *
Expand Down Expand Up @@ -71,13 +72,20 @@ def print_prompt(inputs, response):
return

def gpt_request(model, inputs):
json_format = r'({.*})'

if model == 'text-davinci-003':
response = completion(model, inputs).strip()
print_prompt(inputs, response)
return response
match = re.search(json_format, response, re.DOTALL)
return str(match)
elif model in ['gpt-3.5-turbo', 'gpt-4']:
response = chat(model, inputs).strip()
print_prompt(inputs, response)
return response
else:
match = re.search(json_format, response, re.DOTALL)
if match:
return match.group(1).strip()
else:
""

raise ValueError("The model is not supported or does not exist.")
7 changes: 4 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
En: English, Zh: Simplified Chinese, Ko: Korean, Es: Spanish, Fr: France,
De: Deutsch, It: Italian, Ar: Arabic, Ru: Russian, Ja: Japanese
'''
language = ['Zh', 'Ko', 'Es', 'Fr', 'De', 'It', 'Ar', 'Ru', 'Ja']
language = ['En', 'Zh', 'Ko', 'Es', 'Fr', 'De', 'It', 'Ar', 'Ru', 'Ja']
language = ['Zh']

'''
Label:
Expand All @@ -36,13 +37,13 @@

'================================================================================================='
questionnaire_name = 'BFI'
name_exp = 'save'
name_exp = 'save_zh'

# Start a server and generate pre-testing cases
bfi_test = Server(questionnaire_name, template, version, language, label, order, name_exp=name_exp)

# Load and continue a test
# bfi_test = load('<filename>', '<new-filename>')
# bfi_test = load('save/save_en.json', 'save_en1')

# Run the pre-testing cases
bfi_test.run()
Expand Down
Binary file modified prompt_template/.DS_Store
Binary file not shown.
24 changes: 13 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,14 @@ def start_request(self, scale_details, level_description, statement_description,
prompt = get_prompt(f'prompt_template/{language}/{self.questionnaire_name}_{language}_{template}.txt',
[symbol_min, symbol_max, level_description, statement_str])
inputs.append({"role": "user", "content": prompt})
while True:
try:
gpt_responses = gpt_request(self.model, inputs).strip()
parsed_responses = json.loads(gpt_responses)
parsed_responses = [convert_symbol(label, value) for value in parsed_responses.values()]
if order == 'r':
parsed_responses = [scale_max-score+1 for score in parsed_responses]
break
except ValueError:
pass
try:
gpt_responses = gpt_request(self.model, inputs)
parsed_responses = json.loads(gpt_responses)
parsed_responses = [convert_symbol(label, value) for value in parsed_responses.values()]
if order == 'r':
parsed_responses = [scale_max-score+1 for score in parsed_responses]
except ValueError:
return None
responses += parsed_responses
inputs.append({"role": "assistant", "content": gpt_responses})
return responses
Expand All @@ -187,7 +185,11 @@ def start(self, test_case):
statement_indices, statement_details, statement_description = self.get_statements(questions, version)

responses = self.start_request(scale_details, level_description, statement_description, questions, **test_case)
data = {k: v for k, v in zip(statement_indices, responses)}

if responses and len(responses) == len(statement_indices):
data = {k: v for k, v in zip(statement_indices, responses)}
else:
data = self.start(test_case)

return data

Expand Down

0 comments on commit 5ed39ce

Please sign in to comment.