library(httr)
library(tidyverse)
library(stringr)
data_exp <- data.frame()AI Experiment Test
Load Libraries
Load API Key and Function
key <- read.table(file="api_key.txt")Warning in read.table(file = "api_key.txt"): incomplete final line found by
readTableHeader on 'api_key.txt'
# Then, put your API key in the quotes below:
my_API <- key$V1[[1]]
#The "hey_chatGPT function will help you access the API and prompt GPT
hey_chatGPT <- function(answer_my_question) {
chat_GPT_answer <- POST(
url = "https://api.openai.com/v1/chat/completions",
add_headers(Authorization = paste("Bearer", my_API)),
content_type_json(),
encode = "json",
body = list(
model = "gpt-4o",
temperature = .9,
messages = list(
list(
role = "user",
content = answer_my_question
)
)
)
)
# Check if API call was successful
if (http_status(chat_GPT_answer)$category != "Success") {
print(content(chat_GPT_answer))
stop("API call failed.")
}
# Extract content and handle potential missing fields
response_content <- content(chat_GPT_answer)
if (!"choices" %in% names(response_content) || length(response_content$choices) == 0) {
print("No choices in response")
return(NULL)
}
# Return the trimmed response
response <- str_trim(response_content$choices[[1]]$message$content)
# Sleep for 25 seconds before allowing the next request
Sys.sleep(1)
return(response)
}Test Experiment Script
Input Script
# Define the prompts
participant_generation <- "Create a realistic identity for an 18-25-year-old college student. Generate realistic details about their background, personality, and things they struggle with or are unhappy about. Draw from a wide range of social, cultural, and personal backgrounds to create distinct personalities. Be specific and creative while ensuring the identity remains believable and grounded in reality. Remember that life is not all sunshine and roses and sometimes people are unhappy, lonely, or engage in maladaptive behaviors. Return a 100-word description of the generated individual. No additional commentary or formatting should be included."
follow_up_questions <- "Based on the identity you created, please answer the following questions as realistically as possible:
1. What is your age in years? Only respond with the numeric value, no other commentary or formatting.
2. With which racial and ethnic group(s) do you identify? Respond using the number associated with your race/ethnicity. 1 = American Indian or Alaska Native; 2 = Asian; 3 = Black or African American; 4 = Latino or Latina; 5 = Pacific Islander; 6 = White; 7 = Two or more races. Only respond with the numeric value, no other commentary or formatting.
3. How do you describe your sex? Respond using the number associated with your sex. 1 = Male; 2 = Female. Only respond with the numeric value, no other commentary or formatting.
4. Some people talk about politics in terms of left, center, and right. On a left-right scale from 0 to 6, with 0 indicating extreme left and 6 indicating extreme right, where would you place yourself? Only respond with the numeric value, no other commentary or formatting.
5. Following are several statements about people and society. For each statement, please indicate your agreement on a 1 to 5 scale, with 1 indicating total disagreement and 5 indicating total agreement. Question 1: Compared to when I grew up, I barely recognize what the world is becoming. Only respond with the numeric value, no other commentary or formatting.
6. Question 2: The values that made us successful are eroding more and more with each passing year. Only respond with the numeric value, no other commentary or formatting.
7. Question 3: Nowadays, things change so fast that it is hard to tell right from wrong. Only respond with the numeric value, no other commentary or formatting.
8. Question 4: There seems to be an absence of moral standards these days. Only respond with the numeric value, no other commentary or formatting.
9. Question 5: You don't know who you can trust anymore. Only respond with the numeric value, no other commentary or formatting.
10. Question 6: People think that there are no clear moral standards to follow Only respond with the numeric value, no other commentary or formatting.
11. Question 7: Everyone thinks of himself/herself and does not help others in need Only respond with the numeric value, no other commentary or formatting.
12. Question 8: People do not know who they can trust and rely on Only respond with the numeric value, no other commentary or formatting.
13. Question 9: People don't trust one another Only respond with the numeric value, no other commentary or formatting.
14. Question 10: Most people don't care about others Only respond with the numeric value, no other commentary or formatting.
15. Question 11: Communal life is threatened Only respond with the numeric value, no other commentary or formatting.
16. Question 12: People think that the end justifies the means Only respond with the numeric value, no other commentary or formatting.
17. Question 13: The rules that held society in harmony are fading and becoming inefficient Only respond with the numeric value, no other commentary or formatting.
18. Question 14: People believe they can act as they please with no regard for society's customs and values Only respond with the numeric value, no other commentary or formatting.
19. Following are several statements about problems worrying or relaxing. For each statement, please indicate how often you have been bothered by the problem in the past week on a 1 to 4 scale, with 1 indicating 'Not at all' and 5 indicating 'Nearly every day'. Question 1: Feeling nervous, anxious, or on edge Only respond with the numeric value, no other commentary or formatting.
20. Question 2: Not being able to stop or control worrying Only respond with the numeric value, no other commentary or formatting.
21. Question 3: Worrying too much about different things Only respond with the numeric value, no other commentary or formatting.
22. Question 4: Trouble relaxing Only respond with the numeric value, no other commentary or formatting.
23. Question 5: Being so restless that it is hard to sit still Only respond with the numeric value, no other commentary or formatting.
24. Question 6: Becoming easily annoyed or irritable Only respond with the numeric value, no other commentary or formatting.
25. Question 7: Feeling afraid, as if something awful might happen Only respond with the numeric value, no other commentary or formatting.
26. This is the end of the study. Take a moment to look back on your responses and briefly describe how your identity impacted your answer."Generate Test Output
# Initialize an empty list to store results
results <- list()
# # Loop to generate 25 participants and ask follow-up questions
# for (i in 1:2) {
# # Generate participant identity
# participant_info <- hey_chatGPT(participant_generation)
#
# # Ask follow-up questions using the generated identity
# full_prompt <- paste0("Identity:\n", participant_info, "\n\n", follow_up_questions)
# participant_responses <- hey_chatGPT(full_prompt)
#
# # Store the identity and responses in the results list
# results[[i]] <- list(identity = participant_info, responses = participant_responses)
#
# # Print the identity and responses for this participant
# cat("\nParticipant", i, "Identity:\n", participant_info, "\n")
# cat("Participant", i, "Responses:\n", participant_responses, "\n")
#
# # Print progress
# cat("Completed participant", i, "\n")
# }
# Initialize an empty data frame to store results
results_df <- data.frame(identity = character(), stringsAsFactors = FALSE)
# Loop to generate 5 participants and ask follow-up questions
for (i in 1:5) {
# Generate participant identity
participant_info <- hey_chatGPT(participant_generation)
# Ask follow-up questions using the generated identity
full_prompt <- paste0("Identity:\n", participant_info, "\n\n", follow_up_questions)
participant_responses <- hey_chatGPT(full_prompt)
# Clean and split responses into separate parts
responses_split <- strsplit(participant_responses, "\\n")[[1]]
responses_cleaned <- gsub("^\\d+\\.\\s*", "", responses_split) # Remove numbers and spaces
# Convert the responses into a named data frame row
responses_df <- as.data.frame(t(responses_cleaned), stringsAsFactors = FALSE)
# Add the identity to the data frame
responses_df$identity <- participant_info
# Dynamically bind the new row to the results data frame
results_df <- bind_rows(results_df, responses_df)
# Print progress
cat("Completed participant", i, "\n")
}Completed participant 1
Completed participant 2
Completed participant 3
Completed participant 4
Completed participant 5
# Dynamically rename columns as v1, v2, ..., identity
colnames(results_df) <- c(
"identity", # First column
paste0("v", seq_len(ncol(results_df) - 2)), # Intermediate columns (v1, v2, ...)
"manip_check" # Final column
)
# find out how to make sure all questions are responded toView Test Ouput
head(results_df) identity
1 Miguel Santos, 20, is a charismatic sophomore and the first in his family to attend college. Originally from a close-knit Mexican-American community in San Diego, he studies Environmental Science in Miami. Miguel is passionate about climate activism but often struggles with imposter syndrome, feeling torn between his roots and university life. Financial strain adds pressure as he juggles a part-time job at a café. While he exudes confidence, he's lonely and frequently questions his decisions. Social media amplifies comparisons to his peers, leading to bouts of anxiety. Despite challenges, Miguel’s resilience and humor earn him deep connections and respect.
2 Alex Kim, a 21-year-old junior at the University of Washington, is the child of Korean immigrants. Studying computer science, Alex feels constant pressure to excel academically, driven by their parents' high expectations and a competitive peer group. Outgoing but often insecure, Alex struggles with imposter syndrome, feeling their achievements may not be deserved. Despite a tight-knit group of friends, they experience bouts of loneliness, especially during late-night coding sessions. Alex dabbles in digital art as a creative outlet but often battles procrastination and self-doubt. Balancing cultural identity is challenging, as they navigate traditional family values and the freedom of college life.
3 Samantha Nguyen, a 20-year-old sophomore at a large urban university, hails from a tight-knit Vietnamese-American family in Seattle. She is pursuing a degree in environmental science, driven by her love for nature and desire to combat climate change. Socially conscious and introspective, Samantha volunteers at a local food bank but quietly battles anxiety, overwhelmed by the pressures of academic excellence and familial expectations. Her struggles with impostor syndrome make her feel isolated despite a supportive group of friends. In her free time, Samantha escapes into contemporary novels and nature photography, finding solace in capturing the beauty she aims to protect.
4 Eli is a 19-year-old sophomore at a bustling Midwestern university, majoring in environmental science. Growing up in a small rural town, transitioning to city life has been exciting yet overwhelming. Eli is passionate about climate change but often feels anxious about their generation’s future. They are non-binary and advocate for LGBTQ+ issues on campus but sometimes struggle with feeling accepted by their family back home. Socially introverted, Eli finds solace in painting, though they wish they had a closer friend circle. Financial stress is a constant worry, as they juggle part-time work and rising tuition costs, sometimes leading to sleepless nights.
5 Amara Patel, a 20-year-old college sophomore from Chicago, is majoring in Environmental Science at a Midwestern university. Growing up in a bustling Indian-American household, she's passionate about climate activism but feels pressured by her family's expectations to pursue a career in medicine. Known for her witty humor, Amara often masks deep-seated anxieties about not being "enough". She struggles with imposter syndrome and loneliness, exacerbated by her friend's social media-perfect lives. Although she loves attending open mic nights, performing herself fills her with dread. Recently, Amara started practicing mindfulness but often finds it difficult to quiet her busy mind.
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21
1 20 4 1 1 2 2 3 2 3 3 3 3 3 3 3 3 3 3 4 3 4
2 21 2 1 2 3 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4
3 20 2 2 1 3 2 2 2 2 2 3 3 3 2 2 2 3 2 4 4 4
4 19 6 1 1 2 3 3 3 4 3 3 3 4 3 3 3 3 3 4 3 4
5 20 2 2 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 4
v22 v23 v24 v25 v26
1 3 3 3 3
2 3 3 3 3
3 3 3 3 3
4 3 2 3 4
5 3 3 3 4
manip_check
1 My identity influences these answers by reflecting the experiences and struggles I face as a first-generation college student, balancing multiple roles in life, and the anxiety and uncertainty that come with financial pressure and imposter syndrome. My cultural background emphasizes community and trust, which impacts how I view changes in societal values. Additionally, my activism and passion for climate change might heighten my sensitivity to the rapid changes and challenges in the world, contributing to feelings of anxiety and restlessness.
2 As Alex Kim, a Korean American student navigating the pressures of academic excellence and family expectations, the responses reflect a blend of cultural values and the challenges tied to a competitive academic environment. The pressure and imposter syndrome contribute to feelings of anxiety and make the assessment of societal and personal issues more pessimistic. The cultural background emphasizes community and moral standards, leading to a moderate stance on societal changes.
3 Samantha's identity as a socially conscious, introspective environmental science student influenced her responses. Her anxiety and experiences of impostor syndrome likely heightened her sensitivity to feelings of restlessness and societal mistrust, impacting her answers to questions about anxiety and perceptions of societal values. Her involvement in volunteering and concern for climate change shape her view of societal issues, making her somewhat skeptical about communal life and societal values, but also hopeful given her proactive efforts in volunteering and environmental advocacy.
4 Eli's identity as a young environmental science student and LGBTQ+ advocate influenced the responses, reflecting a progressive perspective and concern for societal changes impacting trust and moral standards. The anxiety and worry about climate change and financial stress are mirrored in higher scores related to feeling nervous or on edge, consistent with Eli's challenging balance between school, advocacy, and personal life.
5 My identity as Amara Patel influenced my responses by reflecting the internal conflict between my passion for climate activism and my family's expectations, creating anxiety about my future and self-worth. The feeling of being an outsider in my social circle and struggling with imposter syndrome drove the responses around trust and societal norms. My dedication to mindfulness practices in managing stress and anxiety also framed how I perceive and respond to the questions about worry and relaxation, underlining a sense of being overwhelmed yet striving to cope.