expectedparrot / edsl

Design, conduct and analyze results of AI-powered surveys and experiments. Simulate social science and market research with large numbers of AI agents and LLMs.
https://docs.expectedparrot.com
MIT License
194 stars 19 forks source link

Adapt Results object to hold human responses #1088

Open onmyraedar opened 2 months ago

onmyraedar commented 2 months ago

This would allow us combine human and AI agent responses into the same dataset.

johnjosephhorton commented 2 months ago

Not exactly your question, but I first wanted to show you how you can validate answers against a question 'manually'

from edsl import QuestionMultipleChoice

q = QuestionMultipleChoice(
    question_text="What is the capital of France?",
    question_name="capital",
    question_options=["Paris", "London", "Berlin", "Madrid"],
)

assert q._validate_answer({"answer": "Paris"})  # should return True

assert q._validate_answer({"answer": "Berlin"})  # should return True

# fails because the answer is not in the options
try:
    q._validate_answer({"answer": "New Bedford"})
except Exception as e:
    print(e)  # should print "Answer is not in the options list"

You'll get the actual pydantic exceptions which should could potentially use:

QuestionAnswerValidationError(QuestionAnswerValidationError(1 validation error for ChoiceResponse
answer
  Input should be 'Paris', 'London', 'Berlin' or 'Madrid' [type=literal_error, input_value='New Bedford', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/literal_error)) 
        Data being validated: {'answer': 'New Bedford'} 
        Pydnantic Model: <class 'edsl.questions.QuestionMultipleChoice.create_response_model.<locals>.ChoiceResponse'>.
        Reported error: QuestionAnswerValidationError(1 validation error for ChoiceResponse
answer
  Input should be 'Paris', 'London', 'Berlin' or 'Madrid' [type=literal_error, input_value='New Bedford', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/literal_error) 
        Data being validated: {'answer': 'New Bedford'} 
        Pydnantic Model: <class 'edsl.questions.QuestionMultipleChoice.create_response_model.<locals>.ChoiceResponse'>.
        Reported error: 1 validation error for ChoiceResponse
answer
  Input should be 'Paris', 'London', 'Berlin' or 'Madrid' [type=literal_error, input_value='New Bedford', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/literal_error..