Skip to main content
In EDSL this can be done by including fields for metadata in scenarios that you create for the data/content you are using with a survey. When the scenarios are added to the survey and it is run, columns for the metadata fields are automatically included in the results that are generated.

Example

In the steps below we create and run a simple EDSL survey that uses scenarios to add metadata to the results. The steps consist of:
  • Constructing a survey of questions about some data (mock news stories)
  • Creating a scenario (dictionary) for each news story
  • Adding the scenarios to the survey and running it
  • Inspecting the results

Technical setup

Before running the code below, please ensure that you have installed the EDSL libary and either activated remote inference from your Coop account or stored API keys for the language models that you want to use with EDSL. Please also see our documentation page for tips and tutorials on getting started using EDSL.

Constructing questions

We start by constructing some questions with a {{ placeholder }} for data that we will add to the question texts. EDSL comes with a variety of question types that we can choose from based on the form of the response that we want to get back from the model:
from edsl import QuestionFreeText, QuestionMultipleChoice


q_reference = QuestionFreeText(
    question_name = "reference",
    question_text = "What is this headline referring to: {{ scenario.headline }}",
)

q_section = QuestionMultipleChoice(
    question_name = "section",
    question_text = "Which section of the paper is most likely to include this story: {{ scenario.headline }}",
    question_options = [
        "Front page",
        "Health",
        "Politics",
        "Entertainment",
        "Local",
        "Opinion",
        "Sports",
        "Culture",
        "Housing"
    ]
)

Creating a survey

Next we pass the questions to a survey in order to administer them together:
from edsl import Survey
survey = Survey(questions = [q_reference, q_section])

Parameterizing questions with scenarios

Next we create a ScenarioList with a Scenario consisting of a key/value for each piece of data that we want to add to the questions at the {{ placeholder }}, with additional key/values for metadata that we want to keep with the results that are generated when the survey is run. EDSL comes with a variety of methods for generating scenarios from different data sources (PDFs, CSVs, images, tables, lists, etc.); here we generate scenarios from a dictionary:
from edsl import ScenarioList
data = [
    ["headline", "date", "author"],  # Header row
    ["Armistice Signed, War Over: Celebrations Erupt Across City", "1918-11-11", "John Doe"],
    ["Spanish Flu Pandemic: Hospitals Overwhelmed as Cases Surge", "1918-10-15", "Jane Smith"],
    ["Women Gain Right to Vote: Historic Amendment Passed", "1918-06-05", "Robert Johnson"],
    ["Broadway Theaters Reopen After Flu Shutdown", "1918-12-01", "Mary Lee"],
    ["City Welcomes Returning Soldiers with Parade", "1918-11-12", "James Brown"],
    ["Prohibition Debate Heats Up: Public Opinion Divided", "1918-07-20", "Patricia Green"],
    ["New York Yankees Win First Pennant in Franchise History", "1918-09-30", "William Davis"],
    ["Subway Expansion Project Approved by City Council", "1918-08-18", "Barbara Wilson"],
    ["Harlem Renaissance: New Wave of Cultural Expression", "1918-04-25", "Charles Miller"],
    ["Mayor Announces New Housing Initiative for Veterans", "1918-11-20", "Elizabeth Taylor"]
]

# Writing to CSV file
with open('data.csv', 'w') as file:
    for row in data:
        line = ','.join(str(item) for item in row)
        file.write(line + 'n')
scenarios = ScenarioList.from_csv("data.csv")
We can inspect the scenarios that have been created:
scenarios
ScenarioList scenarios: 10; keys: [‘author’, ‘headline’, ‘date’];
headlinedateauthor
0Armistice SignedWar Over: Celebrations Erupt Across City1918-11-11
1Spanish Flu Pandemic: Hospitals Overwhelmed as Cases Surge1918-10-15Jane Smith
2Women Gain Right to Vote: Historic Amendment Passed1918-06-05Robert Johnson
3Broadway Theaters Reopen After Flu Shutdown1918-12-01Mary Lee
4City Welcomes Returning Soldiers with Parade1918-11-12James Brown
5Prohibition Debate Heats Up: Public Opinion Divided1918-07-20Patricia Green
6New York Yankees Win First Pennant in Franchise History1918-09-30William Davis
7Subway Expansion Project Approved by City Council1918-08-18Barbara Wilson
8Harlem Renaissance: New Wave of Cultural Expression1918-04-25Charles Miller
9Mayor Announces New Housing Initiative for Veterans1918-11-20Elizabeth Taylor

Running a survey

To run the survey, we add the scenarios with the by() method and then call the run() method:
results = survey.by(scenarios).run()
This generates a dataset of Results that we can access with built-in methods for analysis. To see a list of all the components of results:
results.columns
0
0agent.agent_index
1agent.agent_instruction
2agent.agent_name
3answer.reference
4answer.section
5cache_keys.reference_cache_key
6cache_keys.section_cache_key
7cache_used.reference_cache_used
8cache_used.section_cache_used
9comment.reference_comment
10comment.section_comment
11generated_tokens.reference_generated_tokens
12generated_tokens.section_generated_tokens
13iteration.iteration
14model.frequency_penalty
15model.inference_service
16model.logprobs
17model.max_tokens
18model.model
19model.model_index
20model.presence_penalty
21model.temperature
22model.top_logprobs
23model.top_p
24prompt.reference_system_prompt
25prompt.reference_user_prompt
26prompt.section_system_prompt
27prompt.section_user_prompt
28question_options.reference_question_options
29question_options.section_question_options
30question_text.reference_question_text
31question_text.section_question_text
32question_type.reference_question_type
33question_type.section_question_type
34raw_model_response.reference_cost
35raw_model_response.reference_one_usd_buys
36raw_model_response.reference_raw_model_response
37raw_model_response.section_cost
38raw_model_response.section_one_usd_buys
39raw_model_response.section_raw_model_response
40scenario.author
41scenario.date
42scenario.headline
43scenario.scenario_index
For example, we can filter, sort, select and print components of results in a table:
(
    results
    .filter("section in ['Sports', 'Health', 'Politics']")
    .sort_by("section", "date")
    .select("headline", "date", "author", "section", "reference")
)
scenario.headlinescenario.datescenario.authoranswer.sectionanswer.reference
0Spanish Flu Pandemic: Hospitals Overwhelmed as Cases Surge1918-10-15Jane SmithHealthThe headline “Spanish Flu Pandemic: Hospitals Overwhelmed as Cases Surge” is referring to the 1918 influenza pandemic, commonly known as the Spanish Flu. This pandemic was caused by the H1N1 influenza A virus and is considered one of the deadliest pandemics in history. It occurred in three waves between 1918 and 1919, infecting about one-third of the world’s population and causing an estimated 50 million deaths globally. The headline likely describes a scenario from that period when hospitals were overwhelmed due to the rapid and widespread increase in cases, leading to significant challenges in medical care and public health responses.
1Women Gain Right to Vote: Historic Amendment Passed1918-06-05Robert JohnsonPoliticsThe headline “Women Gain Right to Vote: Historic Amendment Passed” refers to the passage of the 19th Amendment to the United States Constitution. This amendment, ratified on August 18, 1920, granted American women the legal right to vote, marking a significant victory for the women’s suffrage movement in the United States.
2Prohibition Debate Heats Up: Public Opinion Divided1918-07-20Patricia GreenPoliticsThe headline “Prohibition Debate Heats Up: Public Opinion Divided” likely refers to a renewed discussion or controversy surrounding the topic of prohibition, which historically refers to the legal act of prohibiting the manufacture, transportation, and sale of alcohol. This could be in the context of a historical analysis, a modern-day debate about similar regulatory measures on substances like cannabis, or even discussions about new substances or issues where prohibition is being considered. The headline suggests that there is a significant divide in public opinion on the matter, indicating that it is a contentious issue with strong arguments on both sides.
3New York Yankees Win First Pennant in Franchise History1918-09-30William DavisSportsThe headline “New York Yankees Win First Pennant in Franchise History” is likely referring to a fictional or hypothetical scenario, as the New York Yankees are one of the most successful and storied franchises in Major League Baseball (MLB) history. The Yankees won their first American League pennant in 1921. Since then, they have won numerous pennants and World Series titles. If this headline appears in a real context, it might be part of an alternate history, a satirical piece, or a commemorative article reflecting on the team’s early history.

Posting to the Coop

The Coop is a platform for creating, storing and sharing LLM-based research. It is fully integrated with EDSL and accessible from your workspace or Coop account page. Learn more about creating an account and using the Coop. Here we post the scenarios, survey and results from above, and this notebook:
scenarios.push(
    description = "Scenarios for example survey using metadata",
    alias = "example-scenarios-metadata",
    visibility = "public"
)
{'description': 'Scenarios for example survey using metadata',
 'object_type': 'scenario_list',
 'url': 'https://www.expectedparrot.com/content/3dab0bec-eac2-4e99-8e56-479ceaa4d7a5',
 'uuid': '3dab0bec-eac2-4e99-8e56-479ceaa4d7a5',
 'version': '0.1.47.dev1',
 'visibility': 'public'}
survey.push(
    description = "Example survey using scenarios to add metadata to results",
    alias = "example-survey-scenarios-metadata",
    visibility = "public"
)
{'description': 'Example survey using scenarios to add metadata to results',
 'object_type': 'survey',
 'url': 'https://www.expectedparrot.com/content/dd02126e-fadc-4ce6-bf33-757889764397',
 'uuid': 'dd02126e-fadc-4ce6-bf33-757889764397',
 'version': '0.1.47.dev1',
 'visibility': 'public'}
from edsl import Notebook
n = Notebook(path = "adding_metadata.ipynb")
info = n.push(
    description = "Adding metadata to survey results",
    alias = "adding-metadata-survey-results",
    visibility = "public"
)
info
{'description': 'Adding metadata to survey results',
 'object_type': 'notebook',
 'url': 'https://www.expectedparrot.com/content/f938c278-4c25-4e9c-8eef-36735e83530d',
 'uuid': 'f938c278-4c25-4e9c-8eef-36735e83530d',
 'version': '0.1.47.dev1',
 'visibility': 'public'}
To update an object at the Coop:
n = Notebook("adding_metadata.ipynb") # resave
n.patch("https://www.expectedparrot.com/content/RobinHorton/adding-metadata-survey-results", value = n)
{'status': 'success'}
This is equivalent:
n.patch(info["uuid"], value = n)
{'status': 'success'}
I