edsl 0.1.45__py3-none-any.whl → 0.1.47__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. edsl/Base.py +87 -16
  2. edsl/__version__.py +1 -1
  3. edsl/agents/PromptConstructor.py +26 -79
  4. edsl/agents/QuestionInstructionPromptBuilder.py +70 -32
  5. edsl/agents/QuestionTemplateReplacementsBuilder.py +12 -2
  6. edsl/coop/coop.py +289 -147
  7. edsl/data/Cache.py +2 -0
  8. edsl/data/CacheEntry.py +10 -2
  9. edsl/data/RemoteCacheSync.py +10 -9
  10. edsl/inference_services/AvailableModelFetcher.py +1 -1
  11. edsl/inference_services/PerplexityService.py +9 -5
  12. edsl/jobs/AnswerQuestionFunctionConstructor.py +12 -1
  13. edsl/jobs/Jobs.py +35 -17
  14. edsl/jobs/JobsComponentConstructor.py +2 -1
  15. edsl/jobs/JobsPrompts.py +49 -26
  16. edsl/jobs/JobsRemoteInferenceHandler.py +4 -5
  17. edsl/jobs/data_structures.py +3 -0
  18. edsl/jobs/interviews/Interview.py +6 -3
  19. edsl/language_models/LanguageModel.py +7 -1
  20. edsl/questions/QuestionBase.py +5 -0
  21. edsl/questions/question_base_gen_mixin.py +2 -0
  22. edsl/questions/question_registry.py +6 -7
  23. edsl/results/DatasetExportMixin.py +124 -6
  24. edsl/results/Results.py +59 -0
  25. edsl/scenarios/FileStore.py +112 -7
  26. edsl/scenarios/ScenarioList.py +283 -21
  27. edsl/study/Study.py +2 -2
  28. edsl/surveys/Survey.py +15 -20
  29. {edsl-0.1.45.dist-info → edsl-0.1.47.dist-info}/METADATA +4 -3
  30. {edsl-0.1.45.dist-info → edsl-0.1.47.dist-info}/RECORD +32 -44
  31. edsl/auto/AutoStudy.py +0 -130
  32. edsl/auto/StageBase.py +0 -243
  33. edsl/auto/StageGenerateSurvey.py +0 -178
  34. edsl/auto/StageLabelQuestions.py +0 -125
  35. edsl/auto/StagePersona.py +0 -61
  36. edsl/auto/StagePersonaDimensionValueRanges.py +0 -88
  37. edsl/auto/StagePersonaDimensionValues.py +0 -74
  38. edsl/auto/StagePersonaDimensions.py +0 -69
  39. edsl/auto/StageQuestions.py +0 -74
  40. edsl/auto/SurveyCreatorPipeline.py +0 -21
  41. edsl/auto/utilities.py +0 -218
  42. edsl/base/Base.py +0 -279
  43. {edsl-0.1.45.dist-info → edsl-0.1.47.dist-info}/LICENSE +0 -0
  44. {edsl-0.1.45.dist-info → edsl-0.1.47.dist-info}/WHEEL +0 -0
edsl/auto/StagePersona.py DELETED
@@ -1,61 +0,0 @@
1
- from textwrap import dedent
2
- from dataclasses import dataclass
3
- from typing import List
4
-
5
- from edsl.auto.StageBase import StageBase
6
- from edsl.auto.StageBase import FlowDataBase
7
- from edsl import Model
8
- from edsl.auto.StageQuestions import StageQuestions
9
-
10
- from edsl.questions import QuestionFreeText
11
- from edsl.scenarios import Scenario
12
-
13
- from edsl.auto.utilities import gen_pipeline
14
-
15
-
16
- class StagePersona(StageBase):
17
- input = StageQuestions.output
18
-
19
- @dataclass
20
- class Output(FlowDataBase):
21
- persona: str
22
- questions: List[str]
23
-
24
- output = Output
25
-
26
- def handle_data(self, data):
27
- m = Model()
28
- q_persona = QuestionFreeText(
29
- question_text=dedent(
30
- """\
31
- Imagine a person from the population {{ population }} responding to these questions: "{{ questions }}"
32
- Make up a 1 paragraph persona for this person who would have answers for these questions.
33
- """
34
- ),
35
- question_name="persona",
36
- )
37
- results = (
38
- q_persona.by(m)
39
- .by(Scenario({"questions": data.questions, "population": data.population}))
40
- .run()
41
- )
42
- print("Constructing a persona that could answer the following questions:")
43
- print(data.questions)
44
- results.select("persona").print(
45
- pretty_labels={
46
- "answer.persona": f"Persona that can answer: {data.questions}"
47
- },
48
- split_at_dot=False,
49
- )
50
- persona = results.select("persona").first()
51
- return self.output(persona=persona, questions=data.questions)
52
-
53
-
54
- if __name__ == "__main__":
55
- pipeline = gen_pipeline([StageQuestions, StagePersona])
56
- pipeline.process(
57
- pipeline.input(
58
- overall_question="What are some factors that could determine whether someone likes ice cream?",
59
- persona="People",
60
- )
61
- )
@@ -1,88 +0,0 @@
1
- from textwrap import dedent
2
- from dataclasses import dataclass
3
-
4
- from typing import List
5
-
6
- from edsl.auto.StageBase import StageBase
7
- from edsl.auto.StageBase import FlowDataBase
8
-
9
- from edsl.auto.StagePersonaDimensionValues import StagePersonaDimensionValues
10
-
11
- from edsl.questions import QuestionList
12
- from edsl.scenarios import Scenario
13
- from edsl import Model
14
- from edsl.auto.utilities import gen_pipeline
15
-
16
-
17
- class StagePersonaDimensionValueRanges(StageBase):
18
- input = StagePersonaDimensionValues.output
19
-
20
- @dataclass
21
- class Output(FlowDataBase):
22
- focal_dimension_values: List[dict]
23
- mapping: dict
24
- persona: str
25
-
26
- output = Output
27
-
28
- def handle_data(self, data):
29
- # breakpoint()
30
- """Goal with this stage is to, for each dimension, get a range of values that the persona might have for that dimension."""
31
- dimension_values = data["dimension_values"]
32
- attribute_results = data["attribute_results"]
33
- persona = data["persona"]
34
- m = Model()
35
- d = dict(zip(attribute_results, dimension_values))
36
- q = QuestionList(
37
- question_text=dedent(
38
- """\
39
- Consider the following persona: {{ persona }}.
40
- They were categorized as having the following attributes: {{ d }}.
41
- For this dimension: {{ focal_dimension }},
42
- What are values that other people might have on this attribute?
43
- """
44
- ),
45
- question_name="focal_dimension_values",
46
- )
47
- s = [
48
- Scenario({"persona": persona, "d": d, "focal_dimension": k})
49
- for k in d.keys()
50
- ]
51
- results = q.by(s).by(m).run()
52
- # breakpoint()
53
- results.select("focal_dimension", "answer.*").print(
54
- pretty_labels={
55
- "scenario.focal_dimension": f"Dimensions of a persona",
56
- "answer.focal_dimension_values": f"Values a person might have for that dimension",
57
- },
58
- split_at_dot=False,
59
- )
60
-
61
- focal_dimension_values = results.select("focal_dimension_values").to_list()
62
- mapping = dict(zip(attribute_results, focal_dimension_values))
63
- return self.output(
64
- focal_dimension_values=focal_dimension_values,
65
- mapping=mapping,
66
- persona=persona,
67
- )
68
-
69
-
70
- if __name__ == "__main__":
71
- from edsl.auto.StageQuestions import StageQuestions
72
- from edsl.auto.StagePersona import StagePersona
73
- from edsl.auto.StagePersonaDimensions import StagePersonaDimensions
74
-
75
- pipeline = gen_pipeline(
76
- [
77
- StageQuestions,
78
- StagePersona,
79
- StagePersonaDimensions,
80
- StagePersonaDimensionValues,
81
- StagePersonaDimensionValueRanges,
82
- ]
83
- )
84
- pipeline.process(
85
- pipeline.input(
86
- overall_question="What are some factors that could determine whether someone likes ice cream?"
87
- )
88
- )
@@ -1,74 +0,0 @@
1
- from textwrap import dedent
2
- from dataclasses import dataclass
3
-
4
- from typing import List, Dict
5
-
6
- from edsl.auto.StageBase import StageBase
7
- from edsl.auto.StageBase import FlowDataBase
8
-
9
- from edsl.auto.StagePersonaDimensions import StagePersonaDimensions
10
- from edsl import Model
11
- from edsl.questions import QuestionList, QuestionExtract
12
- from edsl.scenarios import Scenario
13
-
14
- from edsl.auto.utilities import gen_pipeline
15
-
16
-
17
- class StagePersonaDimensionValues(StageBase):
18
- input = StagePersonaDimensions.output
19
-
20
- @dataclass
21
- class Output(FlowDataBase):
22
- attribute_results: List[str]
23
- dimension_values: Dict[str, str]
24
- persona: str
25
-
26
- output = Output
27
-
28
- def handle_data(self, data):
29
- attribute_results = data.attribute_results
30
- persona = data.persona
31
- m = Model()
32
- q = QuestionExtract(
33
- question_text=dedent(
34
- """\
35
- This is a persona: "{{ persona }}"
36
- They vary on the following dimensions: "{{ attribute_results }}"
37
- For each dimenion, what are some values that this persona might have for that dimension?
38
- Please keep answers very short, ideally one word.
39
- """
40
- ),
41
- answer_template={k: None for k in attribute_results},
42
- question_name="dimension_values",
43
- )
44
- results = (
45
- q.by(Scenario({"attribute_results": attribute_results, "persona": persona}))
46
- .by(m)
47
- .run()
48
- )
49
- results.select("attribute_results", "dimension_values").print()
50
- return self.output(
51
- dimension_values=results.select("dimension_values").first(),
52
- attribute_results=attribute_results,
53
- persona=persona,
54
- )
55
-
56
-
57
- if __name__ == "__main__":
58
- from edsl.auto.StageQuestions import StageQuestions
59
- from edsl.auto.StagePersona import StagePersona
60
- from edsl.auto.StagePersonaDimensions import StagePersonaDimensions
61
-
62
- pipeline = gen_pipeline(
63
- [
64
- StageQuestions,
65
- StagePersona,
66
- StagePersonaDimensions,
67
- StagePersonaDimensionValues,
68
- ]
69
- )
70
- pipeline.process(
71
- pipeline.input(
72
- overall_question="What are some factors that could determine whether someone likes ice cream?"
73
- )
74
- )
@@ -1,69 +0,0 @@
1
- from textwrap import dedent
2
- from dataclasses import dataclass
3
-
4
- from typing import List
5
-
6
- from edsl.auto.StageBase import StageBase
7
- from edsl.auto.StageBase import FlowDataBase
8
-
9
- from edsl.auto.StagePersona import StagePersona
10
-
11
- from edsl.questions import QuestionList
12
- from edsl.scenarios import Scenario
13
- from edsl import Model
14
-
15
- from edsl.auto.utilities import gen_pipeline
16
-
17
-
18
- class StagePersonaDimensions(StageBase):
19
- input = StagePersona.output
20
-
21
- @dataclass
22
- class Output(FlowDataBase):
23
- attribute_results: List[str]
24
- persona: str
25
-
26
- output = Output
27
-
28
- def handle_data(self, data):
29
- q_attributes = QuestionList(
30
- question_text=dedent(
31
- """\
32
- Here is a persona: "{{ persona }}"
33
- It was construced to be someone who could answer these questions: "{{ questions }}"
34
-
35
- We want to identify the general dimensions that make up this persona.
36
- E.g., if the person is desribed as 'happy' then a dimenion would be 'mood'
37
- """
38
- ),
39
- question_name="find_attributes",
40
- )
41
- m = Model()
42
- results = (
43
- q_attributes.by(
44
- Scenario({"persona": data.persona, "questions": data.questions})
45
- )
46
- .by(m)
47
- .run()
48
- )
49
- (
50
- results.select("find_attributes").print(
51
- pretty_labels={
52
- "answer.find_attributes": f'Persona dimensions for: "{data.persona}"'
53
- },
54
- split_at_dot=False,
55
- )
56
- )
57
- attribute_results = results.select("find_attributes").first()
58
- return self.output(attribute_results=attribute_results, persona=data.persona)
59
-
60
-
61
- if __name__ == "__main__":
62
- from edsl.auto.StageQuestions import StageQuestions
63
-
64
- pipeline = gen_pipeline([StageQuestions, StagePersona, StagePersonaDimensions])
65
- pipeline.process(
66
- pipeline.input(
67
- overall_question="What are some factors that could determine whether someone likes ice cream?"
68
- )
69
- )
@@ -1,74 +0,0 @@
1
- from dataclasses import dataclass
2
- from typing import List
3
- from textwrap import dedent
4
-
5
-
6
- from edsl import Scenario
7
- from edsl import Model
8
- from edsl.questions.QuestionList import QuestionList
9
-
10
- from edsl.auto.StageBase import StageBase
11
- from edsl.auto.StageBase import FlowDataBase
12
-
13
- from edsl.auto.utilities import gen_pipeline
14
-
15
-
16
- class StageQuestions(StageBase):
17
- "This stages takes as input an overall question and returns a list of questions"
18
-
19
- @dataclass
20
- class Input(FlowDataBase):
21
- overall_question: str
22
- population: str
23
-
24
- @dataclass
25
- class Output(FlowDataBase):
26
- questions: List[str]
27
- population: str
28
-
29
- input = Input
30
- output = Output
31
-
32
- def handle_data(self, data):
33
- m = Model()
34
- overall_question = data.overall_question
35
- population = data.population
36
- s = Scenario({"overall_question": overall_question, "population": population})
37
- q = QuestionList(
38
- question_text=dedent(
39
- """\
40
- Suppose I am interested in the question:
41
- "{{ overall_question }}"
42
- What would be some survey questions I could ask to {{ population }} that might shed light on this question?
43
- """
44
- ),
45
- question_name="questions",
46
- )
47
- results = q.by(s).by(m).run()
48
- (
49
- results.select("questions").print(
50
- pretty_labels={
51
- "answer.questions": f'Questions for overall question: "{overall_question }"'
52
- },
53
- split_at_dot=False,
54
- )
55
- )
56
-
57
- raw_questions = results.select("questions").first()
58
- questions = [q.replace("'", "").replace(":", "") for q in raw_questions]
59
- return self.Output(questions=questions, population=population)
60
-
61
-
62
- if __name__ == "__main__":
63
- pipeline = gen_pipeline([StageQuestions])
64
-
65
- pipeline.process(
66
- pipeline.input(
67
- overall_question="What are some factors that could determine whether someone likes ice cream?",
68
- population="Consumers",
69
- )
70
- )
71
-
72
- results = StageQuestions.func(
73
- overall_question="Why aren't my students studying more?", population="Tech"
74
- )
@@ -1,21 +0,0 @@
1
- import random
2
- from typing import Dict, List, Any, TypeVar, Generator, Optional
3
-
4
- from textwrap import dedent
5
-
6
- # from edsl.language_models.model_interfaces.LanguageModelOpenAIFour import LanguageModelOpenAIFour
7
- from edsl import Model
8
- from edsl.agents.AgentList import AgentList
9
- from edsl.results.Results import Results
10
- from edsl import Agent
11
-
12
- from edsl import Scenario
13
- from edsl.surveys.Survey import Survey
14
-
15
- from edsl.questions.QuestionMultipleChoice import QuestionMultipleChoice
16
- from edsl.questions.QuestionFreeText import QuestionFreeText
17
- from edsl.auto.utilities import gen_pipeline
18
- from edsl.utilities.naming_utilities import sanitize_string
19
-
20
-
21
- m = Model()
edsl/auto/utilities.py DELETED
@@ -1,218 +0,0 @@
1
- from textwrap import dedent
2
- import random
3
- from typing import List, TypeVar, Generator, Optional
4
- from edsl.auto.StageBase import StageBase
5
- from edsl.utilities.naming_utilities import sanitize_string
6
- from edsl import Agent, Survey, Model, Cache, AgentList
7
- from edsl import QuestionFreeText, Scenario
8
- from edsl import QuestionMultipleChoice, Scenario, Agent, ScenarioList
9
-
10
- StageClassType = TypeVar("StageClassType", bound=StageBase)
11
-
12
-
13
- def gen_pipeline(stages_list: List[StageClassType]) -> StageBase:
14
- """Takes as input a list of Stage classes & returns a pipeline of instantiated stages.
15
- A pipeline is a linked list of stages where each stage has a next_stage attribute.
16
-
17
- """
18
- pipeline = stages_list[0]()
19
- last_stage = pipeline
20
- for stage in stages_list[1:]:
21
- while last_stage.next_stage is not None: # find the end of the pipeline
22
- last_stage = last_stage.next_stage
23
- stage_to_add = stage()
24
- last_stage.next_stage = stage_to_add
25
- return pipeline
26
-
27
-
28
- q_eligibility = QuestionMultipleChoice(
29
- question_text=dedent(
30
- """\
31
- Consider this set of question: '{{ questions }}'.
32
- Consider this persona: '{{ persona }}'.
33
- Would this persona be able to answer all of these questions?
34
- """
35
- ),
36
- question_options=["No", "Yes"],
37
- question_name="eligibility",
38
- )
39
-
40
-
41
- def agent_list_eligibility(
42
- agent_list: AgentList,
43
- survey: Optional[Survey] = None,
44
- model: Optional[Model] = None,
45
- cache: Optional[Cache] = None,
46
- ) -> List[bool]:
47
- """
48
- Returns whether each agent in a list is elgible for a survey i.e., can answer every question.
49
-
50
- >>> from edsl.language_models import LanguageModel
51
- >>> m = LanguageModel.example(canned_response = "1", test_model = True)
52
- >>> agent_list_eligibility(AgentList.example())
53
- [True, True]
54
- >>> agent_list_eligibility(AgentList.example().add_trait('persona', 2*["Cool dude"]), survey = Survey.example(), model = m)
55
- [True, True]
56
- """
57
- if survey is None:
58
- return [True] * len(agent_list)
59
- if "persona" not in agent_list.all_traits:
60
- raise ValueError(
61
- f"Each agent needs to have a persona attribute; traits are {agent_list.all_traits}"
62
- )
63
- sl = agent_list.select("persona").to_scenario_list()
64
- sl.add_value("questions", [q.question_text for q in survey._questions])
65
- results = q_eligibility.by(sl).by(model).run(cache=cache)
66
- return [r == "Yes" for r in results.select("eligibility").to_list()]
67
-
68
-
69
- def agent_eligibility(
70
- agent: Agent,
71
- survey: Survey,
72
- model: Optional[Model] = None,
73
- cache: Optional[Cache] = None,
74
- ) -> bool:
75
- """NB: This could be parallelized.
76
-
77
- >>> from edsl.language_models import LanguageModel
78
- >>> m = LanguageModel.example(canned_response = "1", test_model = True)
79
- >>> agent_eligibility(agent = Agent.example().add_trait({'persona': "Persona"}), survey = Survey.example(), model = m)
80
- True
81
-
82
- """
83
- model = model or Model()
84
-
85
- questions = [q.question_text for q in survey._questions]
86
- persona = agent.traits["persona"]
87
- return (
88
- q_eligibility(model=model, questions=questions, persona=persona, cache=cache)
89
- == "Yes"
90
- )
91
-
92
-
93
- def gen_agent_traits(dimension_dict: dict, seed_value: Optional[str] = None):
94
- """
95
- >>> dimension_dict = {'attitude':['positive', 'negative']}
96
- >>> ag = gen_agent_traits(dimension_dict)
97
- >>> a = next(ag)
98
- >>> a == {'attitude': 'positive'} or a == {'attitude': 'negative'}
99
- True
100
- >>> len([next(ag) for _ in range(100)])
101
- 100
102
- """
103
- if seed_value is None:
104
- seed_value = "edsl"
105
-
106
- random.seed(seed_value)
107
-
108
- while True:
109
- new_agent_traits = {}
110
- for key, list_of_values in dimension_dict.items():
111
- new_agent_traits[key] = random.choice(list_of_values)
112
- yield new_agent_traits
113
-
114
-
115
- def agent_generator(
116
- persona: str,
117
- dimension_dict: dict,
118
- model: Optional[Model] = None,
119
- cache: Optional["Cache"] = None,
120
- ) -> Generator["Results", None, None]:
121
- """
122
- >>> from edsl.language_models import LanguageModel
123
- >>> m = LanguageModel.example(canned_response = "This is a cool dude.", test_model = True)
124
- >>> ag = agent_generator(persona = "Base person", dimension_dict = {'attitude':['Positive', 'Negative']}, model = m)
125
- >>> next(ag).select('new_agent_persona').first()
126
- 'This is a cool dude.'
127
- >>> next(ag).select('new_agent_persona').first()
128
- 'This is a cool dude.'
129
- """
130
-
131
- if model is None:
132
- model = Model()
133
-
134
- q = QuestionFreeText(
135
- question_text=dedent(
136
- """\
137
- Consider this persona: '{{ persona }}'.
138
- Now imagine writing a new persona with these traits:
139
- '{{ new_agent_traits }}'
140
- Please write this persona as a narrative.
141
- """
142
- ),
143
- question_name="new_agent_persona",
144
- )
145
- agent_trait_generator = gen_agent_traits(dimension_dict)
146
- codebook = {sanitize_string(k): k for k in dimension_dict.keys()}
147
- while True:
148
- new_agent_traits = next(agent_trait_generator)
149
- yield q(
150
- persona=persona,
151
- new_agent_traits=new_agent_traits,
152
- codebook=codebook,
153
- just_answer=False,
154
- cache=cache,
155
- model=model,
156
- )
157
-
158
-
159
- def create_agents(
160
- agent_generator: Generator["Results", None, None],
161
- survey: Optional[Survey] = None,
162
- num_agents=11,
163
- ) -> AgentList:
164
- """
165
- >>> from edsl.language_models import LanguageModel
166
- >>> m = LanguageModel.example(canned_response = "This is a cool dude.", test_model = True)
167
- >>> ag = agent_generator(persona = "Base person", dimension_dict = {'attitude':['Positive', 'Negative']}, model = m)
168
- >>> new_agent_list = create_agents(agent_generator = ag)
169
- >>> new_agent_list
170
-
171
- """
172
- agent_list = AgentList([])
173
-
174
- MAX_ITERATIONS_MULTIPLIER = 2
175
- iterations = 0
176
-
177
- while len(agent_list) < num_agents:
178
- iterations += 1
179
- candidate_agent = next(agent_generator)
180
- codebook = candidate_agent.select("codebook").to_list()[0]
181
-
182
- koobedoc = {v: k for k, v in codebook.items()}
183
- persona = candidate_agent.select("new_agent_persona").to_list()[0]
184
- traits = candidate_agent.select("new_agent_traits").to_list()[0]
185
- new_traits = {koobedoc[key]: value for key, value in traits.items()} | {
186
- "persona": persona
187
- }
188
- agent = Agent(traits=new_traits, codebook=codebook)
189
- if survey is not None:
190
- if agent_eligibility(agent, survey):
191
- agent_list.append(agent)
192
- else:
193
- print("Agent not eligible")
194
- else:
195
- agent_list.append(agent)
196
-
197
- if iterations > MAX_ITERATIONS_MULTIPLIER * num_agents:
198
- raise Exception("Too many failures")
199
-
200
- return agent_list
201
-
202
-
203
- if __name__ == "__main__":
204
- import doctest
205
-
206
- doctest.testmod()
207
- # from edsl.language_models import LanguageModel
208
-
209
- # m = LanguageModel.example(canned_response="This is a cool dude.", test_model=True)
210
- # ag = agent_generator(
211
- # persona="Base person",
212
- # dimension_dict={"attitude": ["Positive", "Negative"]},
213
- # model=m,
214
- # )
215
- # example = [next(ag).select("new_agent_persona").first() for _ in range(10)]
216
- # dimension_dict = {"attitude": ["positive", "negative"]}
217
- # ag = gen_agent_traits(dimension_dict)
218
- # example = [next(ag) for _ in range(100)]