mojakob commited on
Commit
ef117d6
·
verified ·
1 Parent(s): 728a5e2

Update Chatbot_new.py

Browse files
Files changed (1) hide show
  1. Chatbot_new.py +40 -53
Chatbot_new.py CHANGED
@@ -199,33 +199,36 @@ outcomes_reveals = {
199
 
200
 
201
  def interpret_response(user_input, options_text):
 
202
  prompt = f"""
203
- You are an assistant that interprets user responses for multiple-choice questions.
204
-
205
- The options are:
206
  {options_text}
207
-
208
- The user responded: "{user_input}"
209
-
210
- Based on the provided options, determine which option number (e.g., '1', '2', etc.) the user is choosing.
211
- Respond with the option number only.
212
-
213
- If the user's response is ambiguous or does not match any of the options, respond with "Please clarify.".
214
-
215
- Some examples for reference:
216
- - If the options are "1. Go left, 2. Go right" and the user says "I think I'll go left", respond with "1".
217
- - If the options are "1. Yes, 2. No" and the user says "Nope, I don't think so", respond with "2".
218
  """
219
 
220
  response = openai.chat.completions.create(
221
  model="gpt-4o-mini",
222
  messages=[{"role": "user", "content": prompt}],
223
  temperature=0.3,
224
- max_tokens=5 # Limit the tokens to minimize usage
225
  )
226
 
227
  assistant_response = response.choices[0].message.content.strip()
228
- return assistant_response
 
 
 
 
 
 
 
 
 
 
229
  # Additional check: Confirm the response is a valid option number
230
  # option_numbers = list(range(1, len(options_text.split('\n')) + 1))
231
  # if assistant_response.isdigit() and int(assistant_response) in option_numbers:
@@ -238,58 +241,42 @@ def interpret_response(user_input, options_text):
238
 
239
 
240
  def standardize_response(question, response):
 
241
  prompt = f"""
242
- You are tasked with standardizing user responses for data analysis in a psychological assessment.
243
-
244
  Question: "{question}"
245
  User Response: "{response}"
246
-
247
- For the above user response, classify it into one of the predefined categories relevant to the question.
248
-
249
  Examples:
250
- - If the question is about age and the response is "I'm 25 years old", extract: "25".
251
- - If the question is about profession and the response is "I'm currently working as a school teacher", extract: "school teacher".
252
- - If the question is about attribution and the response is "I think it's because of something I did", classify it as "Internal".
253
-
254
- Provide the standardized response in the following format:
255
- Standardized Response: [Your standardized response]
256
-
257
- Studying and other forms of institutional education should be considered as a profession.
258
- Do not include any additional text or explanation.
259
  """
260
 
261
- messages = [
262
- {"role": "system", "content": "You are an assistant that standardizes user responses for data analysis."},
263
- {"role": "user", "content": prompt}
264
- ]
265
-
266
  response = openai.chat.completions.create(
267
  model="gpt-4o-mini",
268
- messages=messages,
269
- temperature=0.1 # Set temperature to 0 for deterministic output
 
 
 
270
  )
271
 
272
-
273
  standardized_response = response.choices[0].message.content.strip()
 
 
274
 
275
- # Extract the standardized response
276
- if "Standardized Response:" in standardized_response:
277
- standardized_response = standardized_response.split("Standardized Response:")[1].strip()
278
- else:
279
- # Handle unexpected output
280
- standardized_response = standardized_response.strip()
281
-
282
- # Type checking to ensure the response matches expected data type
283
- # For example, if Age is expected to be an integer, confirm it
284
- if question == "Please extract the user's age from the following input." and standardized_response.isdigit():
285
- return int(standardized_response)
286
- elif question == "Please extract the user's profession from the following input.":
287
  return standardized_response
288
- elif question == "Please extract the user's interests from the following input.":
289
  return standardized_response
290
  else:
291
- # Handle other questions accordingly
292
- return standardized_response
293
 
294
  # Function to import vignettes from other participants
295
  selected_user_id = None
 
199
 
200
 
201
  def interpret_response(user_input, options_text):
202
+ # Prepare the prompt
203
  prompt = f"""
204
+ You are tasked with interpreting user responses to multiple-choice questions.
205
+ Options provided:
 
206
  {options_text}
207
+ User's response: "{user_input}"
208
+ Identify the matching option number (e.g., '1', '2', etc.) based on the options.
209
+ Respond with the option number only.
210
+ If the user's response is unclear or doesn't match any option, respond with "Please clarify.".
 
 
 
 
 
 
 
211
  """
212
 
213
  response = openai.chat.completions.create(
214
  model="gpt-4o-mini",
215
  messages=[{"role": "user", "content": prompt}],
216
  temperature=0.3,
217
+ max_tokens=5
218
  )
219
 
220
  assistant_response = response.choices[0].message.content.strip()
221
+
222
+ # Validate the response
223
+ option_numbers = [str(i) for i in range(1, len(options_text.split('\n')) + 1)]
224
+ if assistant_response in option_numbers:
225
+ return assistant_response # Valid option number
226
+ elif assistant_response.lower() == "please clarify.":
227
+ return "Please clarify." # Ask user for clarification
228
+ else:
229
+ return "Invalid response." # Fallback for unexpected output
230
+
231
+
232
  # Additional check: Confirm the response is a valid option number
233
  # option_numbers = list(range(1, len(options_text.split('\n')) + 1))
234
  # if assistant_response.isdigit() and int(assistant_response) in option_numbers:
 
241
 
242
 
243
  def standardize_response(question, response):
244
+ # Prepare the prompt
245
  prompt = f"""
246
+ Standardize the following user response for analysis:
 
247
  Question: "{question}"
248
  User Response: "{response}"
249
+ Extract the key information relevant to the question and provide it as a standardized response.
 
 
250
  Examples:
251
+ - Age: "I'm 25 years old" → "25"
252
+ - Profession: "I work as a teacher" → "teacher"
253
+ - Interests: "I enjoy hiking and painting" → "hiking, painting"
254
+ Respond with only the standardized response. Do not include any additional text or explanations.
 
 
 
 
 
255
  """
256
 
 
 
 
 
 
257
  response = openai.chat.completions.create(
258
  model="gpt-4o-mini",
259
+ messages=[
260
+ {"role": "system", "content": "You are an assistant standardizing user responses for data analysis."},
261
+ {"role": "user", "content": prompt}
262
+ ],
263
+ temperature=0.1
264
  )
265
 
266
+ # Extract and validate the standardized response
267
  standardized_response = response.choices[0].message.content.strip()
268
+ if standardized_response.lower().startswith("standardized response:"):
269
+ standardized_response = standardized_response.split(":")[1].strip()
270
 
271
+ # Validate the format based on the question type
272
+ if "age" in question.lower() and standardized_response.isdigit():
273
+ return int(standardized_response) # Return age as integer
274
+ elif "profession" in question.lower():
 
 
 
 
 
 
 
 
275
  return standardized_response
276
+ elif "interests" in question.lower():
277
  return standardized_response
278
  else:
279
+ return standardized_response # Fallback for other questions
 
280
 
281
  # Function to import vignettes from other participants
282
  selected_user_id = None