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
184 stars 19 forks source link

name space conflict with the following code creates the error in the attached pci #842

Open benjamin-manning opened 2 months ago

benjamin-manning commented 2 months ago

image (2)

import textwrap
import json
import pandas as pd
import numpy as np
import random
from edsl import (
    Model,
    ModelList,
    QuestionMultipleChoice,
    Study,
    Agent,
    Scenario,
    ScenarioList,
    AgentList,
)

lottery_df = pd.read_csv("data/c13k_selections_no_amb.csv")
with open("data/c13k_problems.json") as f:
    lottery_choices = json.load(f)

# only keep the lottery choices that are in the lottery_df
lottery_choices = {
    int(k): v
    for k, v in lottery_choices.items()
    if int(k) in lottery_df["json_key"].values
}

## randomly sample 100 lotteries
random.seed(42)
n = 100
sampled_keys = random.sample(list(lottery_choices.keys()), n)
lottery_choices_subset = {key: lottery_choices[key] for key in sampled_keys}

def lottery_to_text(lottery):
    """
    Convert a lottery dictionary to a string representation to easily pass to the LLM

    Args:
        lottery (dict): a dictionary of the form {A: [(probability, value), ...], B: [(probability, value), ...]}

    Returns:
        dict: a dictionary of the form {A: "outcome1 with probability1\noutcome2 with probability2", B: ...}
    """
    descriptions = {}
    for option, outcomes in lottery.items():
        outcome_descriptions = [
            (
                f"-${abs(value)} with probability {probability}"
                if value < 0
                else f"${value} with probability {probability}"
            )
            for probability, value in outcomes
        ]

        option_desc = "\n".join(outcome_descriptions)
        descriptions[option] = option_desc

    return descriptions

with Study(
    name="lotteries",
    filename="data/lotteries",
    description="lotteries replication",
) as study:

    # presenting gambles as per appendices A, C: https://psycnet.apa.org/record/2017-10520-001
    q = QuestionMultipleChoice(
        question_name="lotteries",
        question_text="""
Please choose which of the two lotteries you most prefer. Your chosen lottery will be played once, and you'll receive (or pay if negative) the resulting amount.
Lottery A: {{lottery_A}}
Lottery B: {{lottery_B}}
What do you choose, with one letter [A, B]?
""",
        question_options=["A", "B"],
    )

    backgrounds = ["You are a human."]
    preferences = [
        "You are risk averse. You generally prefer outcomes with more certainty.",
        "You are risk seeking. You generally prefer outcomes with more uncertainty.",
        "You are loss averse. You really dislike losing money.",
        "You are maximizing. You want to maximize your expected pay-off.",
        "You like extremes. You prefer getting the highest possible pay-off.",
        "You are rational.",
        "You are complexity averse. You prefer simple lotteries where you can better comprehend all potential outcomes.",
    ]

    agents = AgentList(
        [
            Agent(traits={"background": background, "preferences": preference})
            for background in backgrounds
            for preference in preferences
        ]
    )

    scenarios = ScenarioList(
        [
            Scenario(
                {
                    "lottery_num": lottery_num,
                    "lottery_A": lottery_to_text(lottery_choices[lottery_num])["A"],
                    "lottery_B": lottery_to_text(lottery_choices[lottery_num])["B"],
                }
            )
            for lottery_num in lottery_choices_subset.keys()
        ]
    )

    model_list = [
        # "gpt-3.5-turbo",
        # "gpt-4-1106-preview",
        "gpt-4o",
        # "claude-3-5-sonnet-20240620",
        # "meta-llama/Meta-Llama-3-70B-Instruct",
    ]
    models = ModelList([Model(model, temperature=1) for model in model_list])
    jobs = q.by(scenarios).by(models).by(agents)
    responses = jobs.run(5, progress_bar=False, stop_on_exception=True)
benjamin-manning commented 2 months ago

@johnjosephhorton, won't let me assign it, so tagging you here.

johnjosephhorton commented 2 months ago

It looks like there are some files I need to reproduce?

benjamin-manning commented 2 months ago

oops, you're right @johnjosephhorton. Here's the data: https://www.dropbox.com/scl/fi/rcx6dv0xct1m54tvznhc6/c13k_selections_no_amb.csv?rlkey=n3f6pdh0bor1kqs033bvncc0c&st=fv43mk36&dl=0

https://www.dropbox.com/scl/fi/y4s4h1n2fmzaeo8sewi8t/c13k_problems.json?rlkey=cpm4l3qwovkcf3hm251f7bysz&st=ikirnv8c&dl=0

Lmk if you still have problems.

benjamin-manning commented 2 months ago

@johnjosephhorton, any luck with this? I still want to try something out