edsl 0.1.39.dev2__py3-none-any.whl → 0.1.39.dev3__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.
- edsl/Base.py +332 -385
- edsl/BaseDiff.py +260 -260
- edsl/TemplateLoader.py +24 -24
- edsl/__init__.py +49 -57
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +867 -1079
- edsl/agents/AgentList.py +413 -551
- edsl/agents/Invigilator.py +233 -285
- edsl/agents/InvigilatorBase.py +270 -254
- edsl/agents/PromptConstructor.py +354 -252
- edsl/agents/__init__.py +3 -2
- edsl/agents/descriptors.py +99 -99
- edsl/agents/prompt_helpers.py +129 -129
- edsl/auto/AutoStudy.py +117 -117
- edsl/auto/StageBase.py +230 -230
- edsl/auto/StageGenerateSurvey.py +178 -178
- edsl/auto/StageLabelQuestions.py +125 -125
- edsl/auto/StagePersona.py +61 -61
- edsl/auto/StagePersonaDimensionValueRanges.py +88 -88
- edsl/auto/StagePersonaDimensionValues.py +74 -74
- edsl/auto/StagePersonaDimensions.py +69 -69
- edsl/auto/StageQuestions.py +73 -73
- edsl/auto/SurveyCreatorPipeline.py +21 -21
- edsl/auto/utilities.py +224 -224
- edsl/base/Base.py +279 -279
- edsl/config.py +157 -177
- edsl/conversation/Conversation.py +290 -290
- edsl/conversation/car_buying.py +58 -59
- edsl/conversation/chips.py +95 -95
- edsl/conversation/mug_negotiation.py +81 -81
- edsl/conversation/next_speaker_utilities.py +93 -93
- edsl/coop/PriceFetcher.py +54 -54
- edsl/coop/__init__.py +2 -2
- edsl/coop/coop.py +1028 -1090
- edsl/coop/utils.py +131 -131
- edsl/data/Cache.py +555 -562
- edsl/data/CacheEntry.py +233 -230
- edsl/data/CacheHandler.py +149 -170
- edsl/data/RemoteCacheSync.py +78 -78
- edsl/data/SQLiteDict.py +292 -292
- edsl/data/__init__.py +4 -5
- edsl/data/orm.py +10 -10
- edsl/data_transfer_models.py +73 -74
- edsl/enums.py +175 -195
- edsl/exceptions/BaseException.py +21 -21
- edsl/exceptions/__init__.py +54 -54
- edsl/exceptions/agents.py +42 -54
- edsl/exceptions/cache.py +5 -5
- edsl/exceptions/configuration.py +16 -16
- edsl/exceptions/coop.py +10 -10
- edsl/exceptions/data.py +14 -14
- edsl/exceptions/general.py +34 -34
- edsl/exceptions/jobs.py +33 -33
- edsl/exceptions/language_models.py +63 -63
- edsl/exceptions/prompts.py +15 -15
- edsl/exceptions/questions.py +91 -109
- edsl/exceptions/results.py +29 -29
- edsl/exceptions/scenarios.py +22 -29
- edsl/exceptions/surveys.py +37 -37
- edsl/inference_services/AnthropicService.py +87 -84
- edsl/inference_services/AwsBedrock.py +120 -118
- edsl/inference_services/AzureAI.py +217 -215
- edsl/inference_services/DeepInfraService.py +18 -18
- edsl/inference_services/GoogleService.py +148 -139
- edsl/inference_services/GroqService.py +20 -20
- edsl/inference_services/InferenceServiceABC.py +147 -80
- edsl/inference_services/InferenceServicesCollection.py +97 -122
- edsl/inference_services/MistralAIService.py +123 -120
- edsl/inference_services/OllamaService.py +18 -18
- edsl/inference_services/OpenAIService.py +224 -221
- edsl/inference_services/PerplexityService.py +163 -160
- edsl/inference_services/TestService.py +89 -92
- edsl/inference_services/TogetherAIService.py +170 -170
- edsl/inference_services/models_available_cache.py +118 -118
- edsl/inference_services/rate_limits_cache.py +25 -25
- edsl/inference_services/registry.py +41 -41
- edsl/inference_services/write_available.py +10 -10
- edsl/jobs/Answers.py +56 -43
- edsl/jobs/Jobs.py +898 -757
- edsl/jobs/JobsChecks.py +147 -172
- edsl/jobs/JobsPrompts.py +268 -270
- edsl/jobs/JobsRemoteInferenceHandler.py +239 -287
- edsl/jobs/__init__.py +1 -1
- edsl/jobs/buckets/BucketCollection.py +63 -104
- edsl/jobs/buckets/ModelBuckets.py +65 -65
- edsl/jobs/buckets/TokenBucket.py +251 -283
- edsl/jobs/interviews/Interview.py +661 -358
- edsl/jobs/interviews/InterviewExceptionCollection.py +99 -99
- edsl/jobs/interviews/InterviewExceptionEntry.py +186 -186
- edsl/jobs/interviews/InterviewStatistic.py +63 -63
- edsl/jobs/interviews/InterviewStatisticsCollection.py +25 -25
- edsl/jobs/interviews/InterviewStatusDictionary.py +78 -78
- edsl/jobs/interviews/InterviewStatusLog.py +92 -92
- edsl/jobs/interviews/ReportErrors.py +66 -66
- edsl/jobs/interviews/interview_status_enum.py +9 -9
- edsl/jobs/runners/JobsRunnerAsyncio.py +466 -421
- edsl/jobs/runners/JobsRunnerStatus.py +330 -330
- edsl/jobs/tasks/QuestionTaskCreator.py +242 -244
- edsl/jobs/tasks/TaskCreators.py +64 -64
- edsl/jobs/tasks/TaskHistory.py +450 -449
- edsl/jobs/tasks/TaskStatusLog.py +23 -23
- edsl/jobs/tasks/task_status_enum.py +163 -161
- edsl/jobs/tokens/InterviewTokenUsage.py +27 -27
- edsl/jobs/tokens/TokenUsage.py +34 -34
- edsl/language_models/KeyLookup.py +30 -0
- edsl/language_models/LanguageModel.py +668 -571
- edsl/language_models/ModelList.py +155 -153
- edsl/language_models/RegisterLanguageModelsMeta.py +184 -184
- edsl/language_models/__init__.py +3 -2
- edsl/language_models/fake_openai_call.py +15 -15
- edsl/language_models/fake_openai_service.py +61 -61
- edsl/language_models/registry.py +190 -180
- edsl/language_models/repair.py +156 -156
- edsl/language_models/unused/ReplicateBase.py +83 -0
- edsl/language_models/utilities.py +64 -65
- edsl/notebooks/Notebook.py +258 -263
- edsl/notebooks/__init__.py +1 -1
- edsl/prompts/Prompt.py +362 -352
- edsl/prompts/__init__.py +2 -2
- edsl/questions/AnswerValidatorMixin.py +289 -334
- edsl/questions/QuestionBase.py +664 -509
- edsl/questions/QuestionBaseGenMixin.py +161 -165
- edsl/questions/QuestionBasePromptsMixin.py +217 -221
- edsl/questions/QuestionBudget.py +227 -227
- edsl/questions/QuestionCheckBox.py +359 -359
- edsl/questions/QuestionExtract.py +182 -182
- edsl/questions/QuestionFreeText.py +114 -113
- edsl/questions/QuestionFunctional.py +166 -166
- edsl/questions/QuestionList.py +231 -229
- edsl/questions/QuestionMultipleChoice.py +286 -330
- edsl/questions/QuestionNumerical.py +153 -151
- edsl/questions/QuestionRank.py +324 -314
- edsl/questions/Quick.py +41 -41
- edsl/questions/RegisterQuestionsMeta.py +71 -71
- edsl/questions/ResponseValidatorABC.py +174 -200
- edsl/questions/SimpleAskMixin.py +73 -74
- edsl/questions/__init__.py +26 -27
- edsl/questions/compose_questions.py +98 -98
- edsl/questions/decorators.py +21 -21
- edsl/questions/derived/QuestionLikertFive.py +76 -76
- edsl/questions/derived/QuestionLinearScale.py +87 -90
- edsl/questions/derived/QuestionTopK.py +93 -93
- edsl/questions/derived/QuestionYesNo.py +82 -82
- edsl/questions/descriptors.py +413 -427
- edsl/questions/prompt_templates/question_budget.jinja +13 -13
- edsl/questions/prompt_templates/question_checkbox.jinja +32 -32
- edsl/questions/prompt_templates/question_extract.jinja +11 -11
- edsl/questions/prompt_templates/question_free_text.jinja +3 -3
- edsl/questions/prompt_templates/question_linear_scale.jinja +11 -11
- edsl/questions/prompt_templates/question_list.jinja +17 -17
- edsl/questions/prompt_templates/question_multiple_choice.jinja +33 -33
- edsl/questions/prompt_templates/question_numerical.jinja +36 -36
- edsl/questions/question_registry.py +177 -177
- edsl/questions/settings.py +12 -12
- edsl/questions/templates/budget/answering_instructions.jinja +7 -7
- edsl/questions/templates/budget/question_presentation.jinja +7 -7
- edsl/questions/templates/checkbox/answering_instructions.jinja +10 -10
- edsl/questions/templates/checkbox/question_presentation.jinja +22 -22
- edsl/questions/templates/extract/answering_instructions.jinja +7 -7
- edsl/questions/templates/likert_five/answering_instructions.jinja +10 -10
- edsl/questions/templates/likert_five/question_presentation.jinja +11 -11
- edsl/questions/templates/linear_scale/answering_instructions.jinja +5 -5
- edsl/questions/templates/linear_scale/question_presentation.jinja +5 -5
- edsl/questions/templates/list/answering_instructions.jinja +3 -3
- edsl/questions/templates/list/question_presentation.jinja +5 -5
- edsl/questions/templates/multiple_choice/answering_instructions.jinja +9 -9
- edsl/questions/templates/multiple_choice/question_presentation.jinja +11 -11
- edsl/questions/templates/numerical/answering_instructions.jinja +6 -6
- edsl/questions/templates/numerical/question_presentation.jinja +6 -6
- edsl/questions/templates/rank/answering_instructions.jinja +11 -11
- edsl/questions/templates/rank/question_presentation.jinja +15 -15
- edsl/questions/templates/top_k/answering_instructions.jinja +8 -8
- edsl/questions/templates/top_k/question_presentation.jinja +22 -22
- edsl/questions/templates/yes_no/answering_instructions.jinja +6 -6
- edsl/questions/templates/yes_no/question_presentation.jinja +11 -11
- edsl/results/CSSParameterizer.py +108 -108
- edsl/results/Dataset.py +424 -587
- edsl/results/DatasetExportMixin.py +731 -653
- edsl/results/DatasetTree.py +275 -295
- edsl/results/Result.py +465 -451
- edsl/results/Results.py +1165 -1172
- edsl/results/ResultsDBMixin.py +238 -0
- edsl/results/ResultsExportMixin.py +43 -45
- edsl/results/ResultsFetchMixin.py +33 -33
- edsl/results/ResultsGGMixin.py +121 -121
- edsl/results/ResultsToolsMixin.py +98 -98
- edsl/results/Selector.py +135 -145
- edsl/results/TableDisplay.py +198 -125
- edsl/results/__init__.py +2 -2
- edsl/results/table_display.css +77 -77
- edsl/results/tree_explore.py +115 -115
- edsl/scenarios/FileStore.py +632 -511
- edsl/scenarios/Scenario.py +601 -498
- edsl/scenarios/ScenarioHtmlMixin.py +64 -65
- edsl/scenarios/ScenarioJoin.py +127 -131
- edsl/scenarios/ScenarioList.py +1287 -1430
- edsl/scenarios/ScenarioListExportMixin.py +52 -45
- edsl/scenarios/ScenarioListPdfMixin.py +261 -239
- edsl/scenarios/__init__.py +4 -3
- edsl/shared.py +1 -1
- edsl/study/ObjectEntry.py +173 -173
- edsl/study/ProofOfWork.py +113 -113
- edsl/study/SnapShot.py +80 -80
- edsl/study/Study.py +528 -521
- edsl/study/__init__.py +4 -4
- edsl/surveys/DAG.py +148 -148
- edsl/surveys/Memory.py +31 -31
- edsl/surveys/MemoryPlan.py +244 -244
- edsl/surveys/Rule.py +326 -327
- edsl/surveys/RuleCollection.py +387 -385
- edsl/surveys/Survey.py +1801 -1229
- edsl/surveys/SurveyCSS.py +261 -273
- edsl/surveys/SurveyExportMixin.py +259 -259
- edsl/surveys/{SurveyFlowVisualization.py → SurveyFlowVisualizationMixin.py} +179 -181
- edsl/surveys/SurveyQualtricsImport.py +284 -284
- edsl/surveys/__init__.py +3 -5
- edsl/surveys/base.py +53 -53
- edsl/surveys/descriptors.py +56 -60
- edsl/surveys/instructions/ChangeInstruction.py +49 -48
- edsl/surveys/instructions/Instruction.py +65 -56
- edsl/surveys/instructions/InstructionCollection.py +77 -82
- edsl/templates/error_reporting/base.html +23 -23
- edsl/templates/error_reporting/exceptions_by_model.html +34 -34
- edsl/templates/error_reporting/exceptions_by_question_name.html +16 -16
- edsl/templates/error_reporting/exceptions_by_type.html +16 -16
- edsl/templates/error_reporting/interview_details.html +115 -115
- edsl/templates/error_reporting/interviews.html +19 -19
- edsl/templates/error_reporting/overview.html +4 -4
- edsl/templates/error_reporting/performance_plot.html +1 -1
- edsl/templates/error_reporting/report.css +73 -73
- edsl/templates/error_reporting/report.html +117 -117
- edsl/templates/error_reporting/report.js +25 -25
- edsl/tools/__init__.py +1 -1
- edsl/tools/clusters.py +192 -192
- edsl/tools/embeddings.py +27 -27
- edsl/tools/embeddings_plotting.py +118 -118
- edsl/tools/plotting.py +112 -112
- edsl/tools/summarize.py +18 -18
- edsl/utilities/SystemInfo.py +28 -28
- edsl/utilities/__init__.py +22 -22
- edsl/utilities/ast_utilities.py +25 -25
- edsl/utilities/data/Registry.py +6 -6
- edsl/utilities/data/__init__.py +1 -1
- edsl/utilities/data/scooter_results.json +1 -1
- edsl/utilities/decorators.py +77 -77
- edsl/utilities/gcp_bucket/cloud_storage.py +96 -96
- edsl/utilities/interface.py +627 -627
- edsl/utilities/naming_utilities.py +263 -263
- edsl/utilities/repair_functions.py +28 -28
- edsl/utilities/restricted_python.py +70 -70
- edsl/utilities/utilities.py +424 -436
- {edsl-0.1.39.dev2.dist-info → edsl-0.1.39.dev3.dist-info}/LICENSE +21 -21
- {edsl-0.1.39.dev2.dist-info → edsl-0.1.39.dev3.dist-info}/METADATA +10 -12
- edsl-0.1.39.dev3.dist-info/RECORD +277 -0
- edsl/agents/QuestionInstructionPromptBuilder.py +0 -128
- edsl/agents/QuestionOptionProcessor.py +0 -172
- edsl/agents/QuestionTemplateReplacementsBuilder.py +0 -137
- edsl/coop/CoopFunctionsMixin.py +0 -15
- edsl/coop/ExpectedParrotKeyHandler.py +0 -125
- edsl/exceptions/inference_services.py +0 -5
- edsl/inference_services/AvailableModelCacheHandler.py +0 -184
- edsl/inference_services/AvailableModelFetcher.py +0 -209
- edsl/inference_services/ServiceAvailability.py +0 -135
- edsl/inference_services/data_structures.py +0 -62
- edsl/jobs/AnswerQuestionFunctionConstructor.py +0 -188
- edsl/jobs/FetchInvigilator.py +0 -40
- edsl/jobs/InterviewTaskManager.py +0 -98
- edsl/jobs/InterviewsConstructor.py +0 -48
- edsl/jobs/JobsComponentConstructor.py +0 -189
- edsl/jobs/JobsRemoteInferenceLogger.py +0 -239
- edsl/jobs/RequestTokenEstimator.py +0 -30
- edsl/jobs/buckets/TokenBucketAPI.py +0 -211
- edsl/jobs/buckets/TokenBucketClient.py +0 -191
- edsl/jobs/decorators.py +0 -35
- edsl/jobs/jobs_status_enums.py +0 -9
- edsl/jobs/loggers/HTMLTableJobLogger.py +0 -304
- edsl/language_models/ComputeCost.py +0 -63
- edsl/language_models/PriceManager.py +0 -127
- edsl/language_models/RawResponseHandler.py +0 -106
- edsl/language_models/ServiceDataSources.py +0 -0
- edsl/language_models/key_management/KeyLookup.py +0 -63
- edsl/language_models/key_management/KeyLookupBuilder.py +0 -273
- edsl/language_models/key_management/KeyLookupCollection.py +0 -38
- edsl/language_models/key_management/__init__.py +0 -0
- edsl/language_models/key_management/models.py +0 -131
- edsl/notebooks/NotebookToLaTeX.py +0 -142
- edsl/questions/ExceptionExplainer.py +0 -77
- edsl/questions/HTMLQuestion.py +0 -103
- edsl/questions/LoopProcessor.py +0 -149
- edsl/questions/QuestionMatrix.py +0 -265
- edsl/questions/ResponseValidatorFactory.py +0 -28
- edsl/questions/templates/matrix/__init__.py +0 -1
- edsl/questions/templates/matrix/answering_instructions.jinja +0 -5
- edsl/questions/templates/matrix/question_presentation.jinja +0 -20
- edsl/results/MarkdownToDocx.py +0 -122
- edsl/results/MarkdownToPDF.py +0 -111
- edsl/results/TextEditor.py +0 -50
- edsl/results/smart_objects.py +0 -96
- edsl/results/table_data_class.py +0 -12
- edsl/results/table_renderers.py +0 -118
- edsl/scenarios/ConstructDownloadLink.py +0 -109
- edsl/scenarios/DirectoryScanner.py +0 -96
- edsl/scenarios/DocumentChunker.py +0 -102
- edsl/scenarios/DocxScenario.py +0 -16
- edsl/scenarios/PdfExtractor.py +0 -40
- edsl/scenarios/ScenarioSelector.py +0 -156
- edsl/scenarios/file_methods.py +0 -85
- edsl/scenarios/handlers/__init__.py +0 -13
- edsl/scenarios/handlers/csv.py +0 -38
- edsl/scenarios/handlers/docx.py +0 -76
- edsl/scenarios/handlers/html.py +0 -37
- edsl/scenarios/handlers/json.py +0 -111
- edsl/scenarios/handlers/latex.py +0 -5
- edsl/scenarios/handlers/md.py +0 -51
- edsl/scenarios/handlers/pdf.py +0 -68
- edsl/scenarios/handlers/png.py +0 -39
- edsl/scenarios/handlers/pptx.py +0 -105
- edsl/scenarios/handlers/py.py +0 -294
- edsl/scenarios/handlers/sql.py +0 -313
- edsl/scenarios/handlers/sqlite.py +0 -149
- edsl/scenarios/handlers/txt.py +0 -33
- edsl/surveys/ConstructDAG.py +0 -92
- edsl/surveys/EditSurvey.py +0 -221
- edsl/surveys/InstructionHandler.py +0 -100
- edsl/surveys/MemoryManagement.py +0 -72
- edsl/surveys/RuleManager.py +0 -172
- edsl/surveys/Simulator.py +0 -75
- edsl/surveys/SurveyToApp.py +0 -141
- edsl/utilities/PrettyList.py +0 -56
- edsl/utilities/is_notebook.py +0 -18
- edsl/utilities/is_valid_variable_name.py +0 -11
- edsl/utilities/remove_edsl_version.py +0 -24
- edsl-0.1.39.dev2.dist-info/RECORD +0 -352
- {edsl-0.1.39.dev2.dist-info → edsl-0.1.39.dev3.dist-info}/WHEEL +0 -0
edsl/jobs/Jobs.py
CHANGED
@@ -1,757 +1,898 @@
|
|
1
|
-
# """The Jobs class is a collection of agents, scenarios and models and one survey."""
|
2
|
-
from __future__ import annotations
|
3
|
-
import warnings
|
4
|
-
|
5
|
-
|
6
|
-
from
|
7
|
-
|
8
|
-
from edsl.
|
9
|
-
|
10
|
-
from edsl.
|
11
|
-
from edsl.
|
12
|
-
|
13
|
-
from edsl.
|
14
|
-
from edsl.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
from edsl.
|
21
|
-
from edsl.
|
22
|
-
from edsl.
|
23
|
-
from edsl.
|
24
|
-
from edsl.
|
25
|
-
from edsl.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
""
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
:param
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
self.
|
53
|
-
|
54
|
-
self.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
self._models = value
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
self._agents = value
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
value
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
self._scenarios = value
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
>>>
|
133
|
-
>>>
|
134
|
-
|
135
|
-
>>>
|
136
|
-
>>> j
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
-
|
145
|
-
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
:
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
def
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
:
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
from edsl.
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
self
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
self.
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
if
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
self.
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
:param
|
661
|
-
:param
|
662
|
-
:param
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
1
|
+
# """The Jobs class is a collection of agents, scenarios and models and one survey."""
|
2
|
+
from __future__ import annotations
|
3
|
+
import warnings
|
4
|
+
import requests
|
5
|
+
from itertools import product
|
6
|
+
from typing import Literal, Optional, Union, Sequence, Generator, TYPE_CHECKING
|
7
|
+
|
8
|
+
from edsl.Base import Base
|
9
|
+
|
10
|
+
from edsl.exceptions import MissingAPIKeyError
|
11
|
+
from edsl.jobs.buckets.BucketCollection import BucketCollection
|
12
|
+
from edsl.jobs.interviews.Interview import Interview
|
13
|
+
from edsl.jobs.runners.JobsRunnerAsyncio import JobsRunnerAsyncio
|
14
|
+
from edsl.utilities.decorators import remove_edsl_version
|
15
|
+
|
16
|
+
from edsl.data.RemoteCacheSync import RemoteCacheSync
|
17
|
+
from edsl.exceptions.coop import CoopServerResponseError
|
18
|
+
|
19
|
+
if TYPE_CHECKING:
|
20
|
+
from edsl.agents.Agent import Agent
|
21
|
+
from edsl.agents.AgentList import AgentList
|
22
|
+
from edsl.language_models.LanguageModel import LanguageModel
|
23
|
+
from edsl.scenarios.Scenario import Scenario
|
24
|
+
from edsl.surveys.Survey import Survey
|
25
|
+
from edsl.results.Results import Results
|
26
|
+
from edsl.results.Dataset import Dataset
|
27
|
+
|
28
|
+
|
29
|
+
class Jobs(Base):
|
30
|
+
"""
|
31
|
+
A collection of agents, scenarios and models and one survey.
|
32
|
+
The actual running of a job is done by a `JobsRunner`, which is a subclass of `JobsRunner`.
|
33
|
+
The `JobsRunner` is chosen by the user, and is stored in the `jobs_runner_name` attribute.
|
34
|
+
"""
|
35
|
+
|
36
|
+
__documentation__ = "https://docs.expectedparrot.com/en/latest/jobs.html"
|
37
|
+
|
38
|
+
def __init__(
|
39
|
+
self,
|
40
|
+
survey: "Survey",
|
41
|
+
agents: Optional[list["Agent"]] = None,
|
42
|
+
models: Optional[list["LanguageModel"]] = None,
|
43
|
+
scenarios: Optional[list["Scenario"]] = None,
|
44
|
+
):
|
45
|
+
"""Initialize a Jobs instance.
|
46
|
+
|
47
|
+
:param survey: the survey to be used in the job
|
48
|
+
:param agents: a list of agents
|
49
|
+
:param models: a list of models
|
50
|
+
:param scenarios: a list of scenarios
|
51
|
+
"""
|
52
|
+
self.survey = survey
|
53
|
+
self.agents: "AgentList" = agents
|
54
|
+
self.scenarios: "ScenarioList" = scenarios
|
55
|
+
self.models = models
|
56
|
+
|
57
|
+
self.__bucket_collection = None
|
58
|
+
|
59
|
+
# these setters and getters are used to ensure that the agents, models, and scenarios are stored as AgentList, ModelList, and ScenarioList objects
|
60
|
+
|
61
|
+
@property
|
62
|
+
def models(self):
|
63
|
+
return self._models
|
64
|
+
|
65
|
+
@models.setter
|
66
|
+
def models(self, value):
|
67
|
+
from edsl import ModelList
|
68
|
+
|
69
|
+
if value:
|
70
|
+
if not isinstance(value, ModelList):
|
71
|
+
self._models = ModelList(value)
|
72
|
+
else:
|
73
|
+
self._models = value
|
74
|
+
else:
|
75
|
+
self._models = ModelList([])
|
76
|
+
|
77
|
+
@property
|
78
|
+
def agents(self):
|
79
|
+
return self._agents
|
80
|
+
|
81
|
+
@agents.setter
|
82
|
+
def agents(self, value):
|
83
|
+
from edsl import AgentList
|
84
|
+
|
85
|
+
if value:
|
86
|
+
if not isinstance(value, AgentList):
|
87
|
+
self._agents = AgentList(value)
|
88
|
+
else:
|
89
|
+
self._agents = value
|
90
|
+
else:
|
91
|
+
self._agents = AgentList([])
|
92
|
+
|
93
|
+
@property
|
94
|
+
def scenarios(self):
|
95
|
+
return self._scenarios
|
96
|
+
|
97
|
+
@scenarios.setter
|
98
|
+
def scenarios(self, value):
|
99
|
+
from edsl import ScenarioList
|
100
|
+
from edsl.results.Dataset import Dataset
|
101
|
+
|
102
|
+
if value:
|
103
|
+
if isinstance(
|
104
|
+
value, Dataset
|
105
|
+
): # if the user passes in a Dataset, convert it to a ScenarioList
|
106
|
+
value = value.to_scenario_list()
|
107
|
+
|
108
|
+
if not isinstance(value, ScenarioList):
|
109
|
+
self._scenarios = ScenarioList(value)
|
110
|
+
else:
|
111
|
+
self._scenarios = value
|
112
|
+
else:
|
113
|
+
self._scenarios = ScenarioList([])
|
114
|
+
|
115
|
+
def by(
|
116
|
+
self,
|
117
|
+
*args: Union[
|
118
|
+
"Agent",
|
119
|
+
"Scenario",
|
120
|
+
"LanguageModel",
|
121
|
+
Sequence[Union["Agent", "Scenario", "LanguageModel"]],
|
122
|
+
],
|
123
|
+
) -> Jobs:
|
124
|
+
"""
|
125
|
+
Add Agents, Scenarios and LanguageModels to a job. If no objects of this type exist in the Jobs instance, it stores the new objects as a list in the corresponding attribute. Otherwise, it combines the new objects with existing objects using the object's `__add__` method.
|
126
|
+
|
127
|
+
This 'by' is intended to create a fluent interface.
|
128
|
+
|
129
|
+
>>> from edsl import Survey
|
130
|
+
>>> from edsl import QuestionFreeText
|
131
|
+
>>> q = QuestionFreeText(question_name="name", question_text="What is your name?")
|
132
|
+
>>> j = Jobs(survey = Survey(questions=[q]))
|
133
|
+
>>> j
|
134
|
+
Jobs(survey=Survey(...), agents=AgentList([]), models=ModelList([]), scenarios=ScenarioList([]))
|
135
|
+
>>> from edsl import Agent; a = Agent(traits = {"status": "Sad"})
|
136
|
+
>>> j.by(a).agents
|
137
|
+
AgentList([Agent(traits = {'status': 'Sad'})])
|
138
|
+
|
139
|
+
:param args: objects or a sequence (list, tuple, ...) of objects of the same type
|
140
|
+
|
141
|
+
Notes:
|
142
|
+
- all objects must implement the 'get_value', 'set_value', and `__add__` methods
|
143
|
+
- agents: traits of new agents are combined with traits of existing agents. New and existing agents should not have overlapping traits, and do not increase the # agents in the instance
|
144
|
+
- scenarios: traits of new scenarios are combined with traits of old existing. New scenarios will overwrite overlapping traits, and do not increase the number of scenarios in the instance
|
145
|
+
- models: new models overwrite old models.
|
146
|
+
"""
|
147
|
+
from edsl.results.Dataset import Dataset
|
148
|
+
|
149
|
+
if isinstance(
|
150
|
+
args[0], Dataset
|
151
|
+
): # let the user user a Dataset as if it were a ScenarioList
|
152
|
+
args = args[0].to_scenario_list()
|
153
|
+
|
154
|
+
passed_objects = self._turn_args_to_list(
|
155
|
+
args
|
156
|
+
) # objects can also be passed comma-separated
|
157
|
+
|
158
|
+
current_objects, objects_key = self._get_current_objects_of_this_type(
|
159
|
+
passed_objects[0]
|
160
|
+
)
|
161
|
+
|
162
|
+
if not current_objects:
|
163
|
+
new_objects = passed_objects
|
164
|
+
else:
|
165
|
+
new_objects = self._merge_objects(passed_objects, current_objects)
|
166
|
+
|
167
|
+
setattr(self, objects_key, new_objects) # update the job
|
168
|
+
return self
|
169
|
+
|
170
|
+
def prompts(self) -> "Dataset":
|
171
|
+
"""Return a Dataset of prompts that will be used.
|
172
|
+
|
173
|
+
|
174
|
+
>>> from edsl.jobs import Jobs
|
175
|
+
>>> Jobs.example().prompts()
|
176
|
+
Dataset(...)
|
177
|
+
"""
|
178
|
+
from edsl.jobs.JobsPrompts import JobsPrompts
|
179
|
+
|
180
|
+
j = JobsPrompts(self)
|
181
|
+
return j.prompts()
|
182
|
+
|
183
|
+
def show_prompts(self, all=False) -> None:
|
184
|
+
"""Print the prompts."""
|
185
|
+
if all:
|
186
|
+
return self.prompts().to_scenario_list().table()
|
187
|
+
else:
|
188
|
+
return (
|
189
|
+
self.prompts().to_scenario_list().table("user_prompt", "system_prompt")
|
190
|
+
)
|
191
|
+
|
192
|
+
@staticmethod
|
193
|
+
def estimate_prompt_cost(
|
194
|
+
system_prompt: str,
|
195
|
+
user_prompt: str,
|
196
|
+
price_lookup: dict,
|
197
|
+
inference_service: str,
|
198
|
+
model: str,
|
199
|
+
) -> dict:
|
200
|
+
"""
|
201
|
+
Estimate the cost of running the prompts.
|
202
|
+
:param iterations: the number of iterations to run
|
203
|
+
"""
|
204
|
+
from edsl.jobs.JobsPrompts import JobsPrompts
|
205
|
+
|
206
|
+
return JobsPrompts.estimate_prompt_cost(
|
207
|
+
system_prompt, user_prompt, price_lookup, inference_service, model
|
208
|
+
)
|
209
|
+
|
210
|
+
def estimate_job_cost(self, iterations: int = 1) -> dict:
|
211
|
+
"""
|
212
|
+
Estimate the cost of running the job.
|
213
|
+
|
214
|
+
:param iterations: the number of iterations to run
|
215
|
+
"""
|
216
|
+
from edsl.jobs.JobsPrompts import JobsPrompts
|
217
|
+
|
218
|
+
j = JobsPrompts(self)
|
219
|
+
return j.estimate_job_cost(iterations)
|
220
|
+
|
221
|
+
def estimate_job_cost_from_external_prices(
|
222
|
+
self, price_lookup: dict, iterations: int = 1
|
223
|
+
) -> dict:
|
224
|
+
from edsl.jobs.JobsPrompts import JobsPrompts
|
225
|
+
|
226
|
+
j = JobsPrompts(self)
|
227
|
+
return j.estimate_job_cost_from_external_prices(price_lookup, iterations)
|
228
|
+
|
229
|
+
@staticmethod
|
230
|
+
def compute_job_cost(job_results: Results) -> float:
|
231
|
+
"""
|
232
|
+
Computes the cost of a completed job in USD.
|
233
|
+
"""
|
234
|
+
return job_results.compute_job_cost()
|
235
|
+
|
236
|
+
@staticmethod
|
237
|
+
def _get_container_class(object):
|
238
|
+
from edsl.agents.AgentList import AgentList
|
239
|
+
from edsl.agents.Agent import Agent
|
240
|
+
from edsl.scenarios.Scenario import Scenario
|
241
|
+
from edsl.scenarios.ScenarioList import ScenarioList
|
242
|
+
from edsl.language_models.ModelList import ModelList
|
243
|
+
|
244
|
+
if isinstance(object, Agent):
|
245
|
+
return AgentList
|
246
|
+
elif isinstance(object, Scenario):
|
247
|
+
return ScenarioList
|
248
|
+
elif isinstance(object, ModelList):
|
249
|
+
return ModelList
|
250
|
+
else:
|
251
|
+
return list
|
252
|
+
|
253
|
+
@staticmethod
|
254
|
+
def _turn_args_to_list(args):
|
255
|
+
"""Return a list of the first argument if it is a sequence, otherwise returns a list of all the arguments.
|
256
|
+
|
257
|
+
Example:
|
258
|
+
|
259
|
+
>>> Jobs._turn_args_to_list([1,2,3])
|
260
|
+
[1, 2, 3]
|
261
|
+
|
262
|
+
"""
|
263
|
+
|
264
|
+
def did_user_pass_a_sequence(args):
|
265
|
+
"""Return True if the user passed a sequence, False otherwise.
|
266
|
+
|
267
|
+
Example:
|
268
|
+
|
269
|
+
>>> did_user_pass_a_sequence([1,2,3])
|
270
|
+
True
|
271
|
+
|
272
|
+
>>> did_user_pass_a_sequence(1)
|
273
|
+
False
|
274
|
+
"""
|
275
|
+
return len(args) == 1 and isinstance(args[0], Sequence)
|
276
|
+
|
277
|
+
if did_user_pass_a_sequence(args):
|
278
|
+
container_class = Jobs._get_container_class(args[0][0])
|
279
|
+
return container_class(args[0])
|
280
|
+
else:
|
281
|
+
container_class = Jobs._get_container_class(args[0])
|
282
|
+
return container_class(args)
|
283
|
+
|
284
|
+
def _get_current_objects_of_this_type(
|
285
|
+
self, object: Union["Agent", "Scenario", "LanguageModel"]
|
286
|
+
) -> tuple[list, str]:
|
287
|
+
from edsl.agents.Agent import Agent
|
288
|
+
from edsl.scenarios.Scenario import Scenario
|
289
|
+
from edsl.language_models.LanguageModel import LanguageModel
|
290
|
+
|
291
|
+
"""Return the current objects of the same type as the first argument.
|
292
|
+
|
293
|
+
>>> from edsl.jobs import Jobs
|
294
|
+
>>> j = Jobs.example()
|
295
|
+
>>> j._get_current_objects_of_this_type(j.agents[0])
|
296
|
+
(AgentList([Agent(traits = {'status': 'Joyful'}), Agent(traits = {'status': 'Sad'})]), 'agents')
|
297
|
+
"""
|
298
|
+
class_to_key = {
|
299
|
+
Agent: "agents",
|
300
|
+
Scenario: "scenarios",
|
301
|
+
LanguageModel: "models",
|
302
|
+
}
|
303
|
+
for class_type in class_to_key:
|
304
|
+
if isinstance(object, class_type) or issubclass(
|
305
|
+
object.__class__, class_type
|
306
|
+
):
|
307
|
+
key = class_to_key[class_type]
|
308
|
+
break
|
309
|
+
else:
|
310
|
+
raise ValueError(
|
311
|
+
f"First argument must be an Agent, Scenario, or LanguageModel, not {object}"
|
312
|
+
)
|
313
|
+
current_objects = getattr(self, key, None)
|
314
|
+
return current_objects, key
|
315
|
+
|
316
|
+
@staticmethod
|
317
|
+
def _get_empty_container_object(object):
|
318
|
+
from edsl.agents.AgentList import AgentList
|
319
|
+
from edsl.scenarios.ScenarioList import ScenarioList
|
320
|
+
|
321
|
+
return {"Agent": AgentList([]), "Scenario": ScenarioList([])}.get(
|
322
|
+
object.__class__.__name__, []
|
323
|
+
)
|
324
|
+
|
325
|
+
@staticmethod
|
326
|
+
def _merge_objects(passed_objects, current_objects) -> list:
|
327
|
+
"""
|
328
|
+
Combine all the existing objects with the new objects.
|
329
|
+
|
330
|
+
For example, if the user passes in 3 agents,
|
331
|
+
and there are 2 existing agents, this will create 6 new agents
|
332
|
+
|
333
|
+
>>> Jobs(survey = [])._merge_objects([1,2,3], [4,5,6])
|
334
|
+
[5, 6, 7, 6, 7, 8, 7, 8, 9]
|
335
|
+
"""
|
336
|
+
new_objects = Jobs._get_empty_container_object(passed_objects[0])
|
337
|
+
for current_object in current_objects:
|
338
|
+
for new_object in passed_objects:
|
339
|
+
new_objects.append(current_object + new_object)
|
340
|
+
return new_objects
|
341
|
+
|
342
|
+
def interviews(self) -> list[Interview]:
|
343
|
+
"""
|
344
|
+
Return a list of :class:`edsl.jobs.interviews.Interview` objects.
|
345
|
+
|
346
|
+
It returns one Interview for each combination of Agent, Scenario, and LanguageModel.
|
347
|
+
If any of Agents, Scenarios, or LanguageModels are missing, it fills in with defaults.
|
348
|
+
|
349
|
+
>>> from edsl.jobs import Jobs
|
350
|
+
>>> j = Jobs.example()
|
351
|
+
>>> len(j.interviews())
|
352
|
+
4
|
353
|
+
>>> j.interviews()[0]
|
354
|
+
Interview(agent = Agent(traits = {'status': 'Joyful'}), survey = Survey(...), scenario = Scenario({'period': 'morning'}), model = Model(...))
|
355
|
+
"""
|
356
|
+
if hasattr(self, "_interviews"):
|
357
|
+
return self._interviews
|
358
|
+
else:
|
359
|
+
return list(self._create_interviews())
|
360
|
+
|
361
|
+
@classmethod
|
362
|
+
def from_interviews(cls, interview_list):
|
363
|
+
"""Return a Jobs instance from a list of interviews.
|
364
|
+
|
365
|
+
This is useful when you have, say, a list of failed interviews and you want to create
|
366
|
+
a new job with only those interviews.
|
367
|
+
"""
|
368
|
+
survey = interview_list[0].survey
|
369
|
+
# get all the models
|
370
|
+
models = list(set([interview.model for interview in interview_list]))
|
371
|
+
jobs = cls(survey)
|
372
|
+
jobs.models = models
|
373
|
+
jobs._interviews = interview_list
|
374
|
+
return jobs
|
375
|
+
|
376
|
+
def _create_interviews(self) -> Generator[Interview, None, None]:
|
377
|
+
"""
|
378
|
+
Generate interviews.
|
379
|
+
|
380
|
+
Note that this sets the agents, model and scenarios if they have not been set. This is a side effect of the method.
|
381
|
+
This is useful because a user can create a job without setting the agents, models, or scenarios, and the job will still run,
|
382
|
+
with us filling in defaults.
|
383
|
+
|
384
|
+
|
385
|
+
"""
|
386
|
+
# if no agents, models, or scenarios are set, set them to defaults
|
387
|
+
from edsl.agents.Agent import Agent
|
388
|
+
from edsl.language_models.registry import Model
|
389
|
+
from edsl.scenarios.Scenario import Scenario
|
390
|
+
|
391
|
+
self.agents = self.agents or [Agent()]
|
392
|
+
self.models = self.models or [Model()]
|
393
|
+
self.scenarios = self.scenarios or [Scenario()]
|
394
|
+
for agent, scenario, model in product(self.agents, self.scenarios, self.models):
|
395
|
+
yield Interview(
|
396
|
+
survey=self.survey,
|
397
|
+
agent=agent,
|
398
|
+
scenario=scenario,
|
399
|
+
model=model,
|
400
|
+
skip_retry=self.skip_retry,
|
401
|
+
raise_validation_errors=self.raise_validation_errors,
|
402
|
+
)
|
403
|
+
|
404
|
+
def create_bucket_collection(self) -> BucketCollection:
|
405
|
+
"""
|
406
|
+
Create a collection of buckets for each model.
|
407
|
+
|
408
|
+
These buckets are used to track API calls and token usage.
|
409
|
+
|
410
|
+
>>> from edsl.jobs import Jobs
|
411
|
+
>>> from edsl import Model
|
412
|
+
>>> j = Jobs.example().by(Model(temperature = 1), Model(temperature = 0.5))
|
413
|
+
>>> bc = j.create_bucket_collection()
|
414
|
+
>>> bc
|
415
|
+
BucketCollection(...)
|
416
|
+
"""
|
417
|
+
bucket_collection = BucketCollection()
|
418
|
+
for model in self.models:
|
419
|
+
bucket_collection.add_model(model)
|
420
|
+
return bucket_collection
|
421
|
+
|
422
|
+
@property
|
423
|
+
def bucket_collection(self) -> BucketCollection:
|
424
|
+
"""Return the bucket collection. If it does not exist, create it."""
|
425
|
+
if self.__bucket_collection is None:
|
426
|
+
self.__bucket_collection = self.create_bucket_collection()
|
427
|
+
return self.__bucket_collection
|
428
|
+
|
429
|
+
def html(self):
|
430
|
+
"""Return the HTML representations for each scenario"""
|
431
|
+
links = []
|
432
|
+
for index, scenario in enumerate(self.scenarios):
|
433
|
+
links.append(
|
434
|
+
self.survey.html(
|
435
|
+
scenario=scenario, return_link=True, cta=f"Scenario {index}"
|
436
|
+
)
|
437
|
+
)
|
438
|
+
return links
|
439
|
+
|
440
|
+
def __hash__(self):
|
441
|
+
"""Allow the model to be used as a key in a dictionary.
|
442
|
+
|
443
|
+
>>> from edsl.jobs import Jobs
|
444
|
+
>>> hash(Jobs.example())
|
445
|
+
846655441787442972
|
446
|
+
|
447
|
+
"""
|
448
|
+
from edsl.utilities.utilities import dict_hash
|
449
|
+
|
450
|
+
return dict_hash(self.to_dict(add_edsl_version=False))
|
451
|
+
|
452
|
+
def _output(self, message) -> None:
|
453
|
+
"""Check if a Job is verbose. If so, print the message."""
|
454
|
+
if hasattr(self, "verbose") and self.verbose:
|
455
|
+
print(message)
|
456
|
+
|
457
|
+
def _check_parameters(self, strict=False, warn=False) -> None:
|
458
|
+
"""Check if the parameters in the survey and scenarios are consistent.
|
459
|
+
|
460
|
+
>>> from edsl import QuestionFreeText
|
461
|
+
>>> from edsl import Survey
|
462
|
+
>>> from edsl import Scenario
|
463
|
+
>>> q = QuestionFreeText(question_text = "{{poo}}", question_name = "ugly_question")
|
464
|
+
>>> j = Jobs(survey = Survey(questions=[q]))
|
465
|
+
>>> with warnings.catch_warnings(record=True) as w:
|
466
|
+
... j._check_parameters(warn = True)
|
467
|
+
... assert len(w) == 1
|
468
|
+
... assert issubclass(w[-1].category, UserWarning)
|
469
|
+
... assert "The following parameters are in the survey but not in the scenarios" in str(w[-1].message)
|
470
|
+
|
471
|
+
>>> q = QuestionFreeText(question_text = "{{poo}}", question_name = "ugly_question")
|
472
|
+
>>> s = Scenario({'plop': "A", 'poo': "B"})
|
473
|
+
>>> j = Jobs(survey = Survey(questions=[q])).by(s)
|
474
|
+
>>> j._check_parameters(strict = True)
|
475
|
+
Traceback (most recent call last):
|
476
|
+
...
|
477
|
+
ValueError: The following parameters are in the scenarios but not in the survey: {'plop'}
|
478
|
+
|
479
|
+
>>> q = QuestionFreeText(question_text = "Hello", question_name = "ugly_question")
|
480
|
+
>>> s = Scenario({'ugly_question': "B"})
|
481
|
+
>>> j = Jobs(survey = Survey(questions=[q])).by(s)
|
482
|
+
>>> j._check_parameters()
|
483
|
+
Traceback (most recent call last):
|
484
|
+
...
|
485
|
+
ValueError: The following names are in both the survey question_names and the scenario keys: {'ugly_question'}. This will create issues.
|
486
|
+
"""
|
487
|
+
survey_parameters: set = self.survey.parameters
|
488
|
+
scenario_parameters: set = self.scenarios.parameters
|
489
|
+
|
490
|
+
msg0, msg1, msg2 = None, None, None
|
491
|
+
|
492
|
+
# look for key issues
|
493
|
+
if intersection := set(self.scenarios.parameters) & set(
|
494
|
+
self.survey.question_names
|
495
|
+
):
|
496
|
+
msg0 = f"The following names are in both the survey question_names and the scenario keys: {intersection}. This will create issues."
|
497
|
+
|
498
|
+
raise ValueError(msg0)
|
499
|
+
|
500
|
+
if in_survey_but_not_in_scenarios := survey_parameters - scenario_parameters:
|
501
|
+
msg1 = f"The following parameters are in the survey but not in the scenarios: {in_survey_but_not_in_scenarios}"
|
502
|
+
if in_scenarios_but_not_in_survey := scenario_parameters - survey_parameters:
|
503
|
+
msg2 = f"The following parameters are in the scenarios but not in the survey: {in_scenarios_but_not_in_survey}"
|
504
|
+
|
505
|
+
if msg1 or msg2:
|
506
|
+
message = "\n".join(filter(None, [msg1, msg2]))
|
507
|
+
if strict:
|
508
|
+
raise ValueError(message)
|
509
|
+
else:
|
510
|
+
if warn:
|
511
|
+
warnings.warn(message)
|
512
|
+
|
513
|
+
if self.scenarios.has_jinja_braces:
|
514
|
+
warnings.warn(
|
515
|
+
"The scenarios have Jinja braces ({{ and }}). Converting to '<<' and '>>'. If you want a different conversion, use the convert_jinja_braces method first to modify the scenario."
|
516
|
+
)
|
517
|
+
self.scenarios = self.scenarios.convert_jinja_braces()
|
518
|
+
|
519
|
+
@property
|
520
|
+
def skip_retry(self):
|
521
|
+
if not hasattr(self, "_skip_retry"):
|
522
|
+
return False
|
523
|
+
return self._skip_retry
|
524
|
+
|
525
|
+
@property
|
526
|
+
def raise_validation_errors(self):
|
527
|
+
if not hasattr(self, "_raise_validation_errors"):
|
528
|
+
return False
|
529
|
+
return self._raise_validation_errors
|
530
|
+
|
531
|
+
def use_remote_cache(self, disable_remote_cache: bool) -> bool:
|
532
|
+
if disable_remote_cache:
|
533
|
+
return False
|
534
|
+
if not disable_remote_cache:
|
535
|
+
try:
|
536
|
+
from edsl import Coop
|
537
|
+
|
538
|
+
user_edsl_settings = Coop().edsl_settings
|
539
|
+
return user_edsl_settings.get("remote_caching", False)
|
540
|
+
except requests.ConnectionError:
|
541
|
+
pass
|
542
|
+
except CoopServerResponseError as e:
|
543
|
+
pass
|
544
|
+
|
545
|
+
return False
|
546
|
+
|
547
|
+
def run(
|
548
|
+
self,
|
549
|
+
n: int = 1,
|
550
|
+
progress_bar: bool = False,
|
551
|
+
stop_on_exception: bool = False,
|
552
|
+
cache: Union[Cache, bool] = None,
|
553
|
+
check_api_keys: bool = False,
|
554
|
+
sidecar_model: Optional[LanguageModel] = None,
|
555
|
+
verbose: bool = True,
|
556
|
+
print_exceptions=True,
|
557
|
+
remote_cache_description: Optional[str] = None,
|
558
|
+
remote_inference_description: Optional[str] = None,
|
559
|
+
remote_inference_results_visibility: Optional[
|
560
|
+
Literal["private", "public", "unlisted"]
|
561
|
+
] = "unlisted",
|
562
|
+
skip_retry: bool = False,
|
563
|
+
raise_validation_errors: bool = False,
|
564
|
+
disable_remote_cache: bool = False,
|
565
|
+
disable_remote_inference: bool = False,
|
566
|
+
) -> Results:
|
567
|
+
"""
|
568
|
+
Runs the Job: conducts Interviews and returns their results.
|
569
|
+
|
570
|
+
:param n: How many times to run each interview
|
571
|
+
:param progress_bar: Whether to show a progress bar
|
572
|
+
:param stop_on_exception: Stops the job if an exception is raised
|
573
|
+
:param cache: A Cache object to store results
|
574
|
+
:param check_api_keys: Raises an error if API keys are invalid
|
575
|
+
:param verbose: Prints extra messages
|
576
|
+
:param remote_cache_description: Specifies a description for this group of entries in the remote cache
|
577
|
+
:param remote_inference_description: Specifies a description for the remote inference job
|
578
|
+
:param remote_inference_results_visibility: The initial visibility of the Results object on Coop. This will only be used for remote jobs!
|
579
|
+
:param disable_remote_cache: If True, the job will not use remote cache. This only works for local jobs!
|
580
|
+
:param disable_remote_inference: If True, the job will not use remote inference
|
581
|
+
"""
|
582
|
+
from edsl.coop.coop import Coop
|
583
|
+
|
584
|
+
self._check_parameters()
|
585
|
+
self._skip_retry = skip_retry
|
586
|
+
self._raise_validation_errors = raise_validation_errors
|
587
|
+
|
588
|
+
self.verbose = verbose
|
589
|
+
|
590
|
+
from edsl.jobs.JobsChecks import JobsChecks
|
591
|
+
|
592
|
+
jc = JobsChecks(self)
|
593
|
+
|
594
|
+
# check if the user has all the keys they need
|
595
|
+
if jc.needs_key_process():
|
596
|
+
jc.key_process()
|
597
|
+
|
598
|
+
from edsl.jobs.JobsRemoteInferenceHandler import JobsRemoteInferenceHandler
|
599
|
+
|
600
|
+
jh = JobsRemoteInferenceHandler(self, verbose=verbose)
|
601
|
+
if jh.use_remote_inference(disable_remote_inference):
|
602
|
+
jh.create_remote_inference_job(
|
603
|
+
iterations=n,
|
604
|
+
remote_inference_description=remote_inference_description,
|
605
|
+
remote_inference_results_visibility=remote_inference_results_visibility,
|
606
|
+
)
|
607
|
+
results = jh.poll_remote_inference_job()
|
608
|
+
return results
|
609
|
+
|
610
|
+
if check_api_keys:
|
611
|
+
jc.check_api_keys()
|
612
|
+
|
613
|
+
# handle cache
|
614
|
+
if cache is None or cache is True:
|
615
|
+
from edsl.data.CacheHandler import CacheHandler
|
616
|
+
|
617
|
+
cache = CacheHandler().get_cache()
|
618
|
+
if cache is False:
|
619
|
+
from edsl.data.Cache import Cache
|
620
|
+
|
621
|
+
cache = Cache()
|
622
|
+
|
623
|
+
remote_cache = self.use_remote_cache(disable_remote_cache)
|
624
|
+
with RemoteCacheSync(
|
625
|
+
coop=Coop(),
|
626
|
+
cache=cache,
|
627
|
+
output_func=self._output,
|
628
|
+
remote_cache=remote_cache,
|
629
|
+
remote_cache_description=remote_cache_description,
|
630
|
+
) as r:
|
631
|
+
results = self._run_local(
|
632
|
+
n=n,
|
633
|
+
progress_bar=progress_bar,
|
634
|
+
cache=cache,
|
635
|
+
stop_on_exception=stop_on_exception,
|
636
|
+
sidecar_model=sidecar_model,
|
637
|
+
print_exceptions=print_exceptions,
|
638
|
+
raise_validation_errors=raise_validation_errors,
|
639
|
+
)
|
640
|
+
|
641
|
+
# results.cache = cache.new_entries_cache()
|
642
|
+
return results
|
643
|
+
|
644
|
+
async def run_async(
|
645
|
+
self,
|
646
|
+
cache=None,
|
647
|
+
n=1,
|
648
|
+
disable_remote_inference: bool = False,
|
649
|
+
remote_inference_description: Optional[str] = None,
|
650
|
+
remote_inference_results_visibility: Optional[
|
651
|
+
Literal["private", "public", "unlisted"]
|
652
|
+
] = "unlisted",
|
653
|
+
**kwargs,
|
654
|
+
):
|
655
|
+
"""Run the job asynchronously, either locally or remotely.
|
656
|
+
|
657
|
+
:param cache: Cache object or boolean
|
658
|
+
:param n: Number of iterations
|
659
|
+
:param disable_remote_inference: If True, forces local execution
|
660
|
+
:param remote_inference_description: Description for remote jobs
|
661
|
+
:param remote_inference_results_visibility: Visibility setting for remote results
|
662
|
+
:param kwargs: Additional arguments passed to local execution
|
663
|
+
:return: Results object
|
664
|
+
"""
|
665
|
+
# Check if we should use remote inference
|
666
|
+
from edsl.jobs.JobsRemoteInferenceHandler import JobsRemoteInferenceHandler
|
667
|
+
|
668
|
+
jh = JobsRemoteInferenceHandler(self, verbose=False)
|
669
|
+
if jh.use_remote_inference(disable_remote_inference):
|
670
|
+
results = await jh.create_and_poll_remote_job(
|
671
|
+
iterations=n,
|
672
|
+
remote_inference_description=remote_inference_description,
|
673
|
+
remote_inference_results_visibility=remote_inference_results_visibility,
|
674
|
+
)
|
675
|
+
return results
|
676
|
+
|
677
|
+
# If not using remote inference, run locally with async
|
678
|
+
return await JobsRunnerAsyncio(self).run_async(cache=cache, n=n, **kwargs)
|
679
|
+
|
680
|
+
def _run_local(self, *args, **kwargs):
|
681
|
+
"""Run the job locally."""
|
682
|
+
|
683
|
+
results = JobsRunnerAsyncio(self).run(*args, **kwargs)
|
684
|
+
return results
|
685
|
+
|
686
|
+
def all_question_parameters(self):
|
687
|
+
"""Return all the fields in the questions in the survey.
|
688
|
+
>>> from edsl.jobs import Jobs
|
689
|
+
>>> Jobs.example().all_question_parameters()
|
690
|
+
{'period'}
|
691
|
+
"""
|
692
|
+
return set.union(*[question.parameters for question in self.survey.questions])
|
693
|
+
|
694
|
+
def __repr__(self) -> str:
|
695
|
+
"""Return an eval-able string representation of the Jobs instance."""
|
696
|
+
return f"Jobs(survey={repr(self.survey)}, agents={repr(self.agents)}, models={repr(self.models)}, scenarios={repr(self.scenarios)})"
|
697
|
+
|
698
|
+
def _summary(self):
|
699
|
+
return {
|
700
|
+
"EDSL Class": "Jobs",
|
701
|
+
"Number of questions": len(self.survey),
|
702
|
+
"Number of agents": len(self.agents),
|
703
|
+
"Number of models": len(self.models),
|
704
|
+
"Number of scenarios": len(self.scenarios),
|
705
|
+
}
|
706
|
+
|
707
|
+
def _repr_html_(self) -> str:
|
708
|
+
footer = f"<a href={self.__documentation__}>(docs)</a>"
|
709
|
+
return str(self.summary(format="html")) + footer
|
710
|
+
|
711
|
+
def __len__(self) -> int:
|
712
|
+
"""Return the maximum number of questions that will be asked while running this job.
|
713
|
+
Note that this is the maximum number of questions, not the actual number of questions that will be asked, as some questions may be skipped.
|
714
|
+
|
715
|
+
>>> from edsl.jobs import Jobs
|
716
|
+
>>> len(Jobs.example())
|
717
|
+
8
|
718
|
+
"""
|
719
|
+
number_of_questions = (
|
720
|
+
len(self.agents or [1])
|
721
|
+
* len(self.scenarios or [1])
|
722
|
+
* len(self.models or [1])
|
723
|
+
* len(self.survey)
|
724
|
+
)
|
725
|
+
return number_of_questions
|
726
|
+
|
727
|
+
#######################
|
728
|
+
# Serialization methods
|
729
|
+
#######################
|
730
|
+
|
731
|
+
def to_dict(self, add_edsl_version=True):
|
732
|
+
d = {
|
733
|
+
"survey": self.survey.to_dict(add_edsl_version=add_edsl_version),
|
734
|
+
"agents": [
|
735
|
+
agent.to_dict(add_edsl_version=add_edsl_version)
|
736
|
+
for agent in self.agents
|
737
|
+
],
|
738
|
+
"models": [
|
739
|
+
model.to_dict(add_edsl_version=add_edsl_version)
|
740
|
+
for model in self.models
|
741
|
+
],
|
742
|
+
"scenarios": [
|
743
|
+
scenario.to_dict(add_edsl_version=add_edsl_version)
|
744
|
+
for scenario in self.scenarios
|
745
|
+
],
|
746
|
+
}
|
747
|
+
if add_edsl_version:
|
748
|
+
from edsl import __version__
|
749
|
+
|
750
|
+
d["edsl_version"] = __version__
|
751
|
+
d["edsl_class_name"] = "Jobs"
|
752
|
+
|
753
|
+
return d
|
754
|
+
|
755
|
+
@classmethod
|
756
|
+
@remove_edsl_version
|
757
|
+
def from_dict(cls, data: dict) -> Jobs:
|
758
|
+
"""Creates a Jobs instance from a dictionary."""
|
759
|
+
from edsl import Survey
|
760
|
+
from edsl.agents.Agent import Agent
|
761
|
+
from edsl.language_models.LanguageModel import LanguageModel
|
762
|
+
from edsl.scenarios.Scenario import Scenario
|
763
|
+
|
764
|
+
return cls(
|
765
|
+
survey=Survey.from_dict(data["survey"]),
|
766
|
+
agents=[Agent.from_dict(agent) for agent in data["agents"]],
|
767
|
+
models=[LanguageModel.from_dict(model) for model in data["models"]],
|
768
|
+
scenarios=[Scenario.from_dict(scenario) for scenario in data["scenarios"]],
|
769
|
+
)
|
770
|
+
|
771
|
+
def __eq__(self, other: Jobs) -> bool:
|
772
|
+
"""Return True if the Jobs instance is equal to another Jobs instance.
|
773
|
+
|
774
|
+
>>> from edsl.jobs import Jobs
|
775
|
+
>>> Jobs.example() == Jobs.example()
|
776
|
+
True
|
777
|
+
|
778
|
+
"""
|
779
|
+
return hash(self) == hash(other)
|
780
|
+
|
781
|
+
#######################
|
782
|
+
# Example methods
|
783
|
+
#######################
|
784
|
+
@classmethod
|
785
|
+
def example(
|
786
|
+
cls,
|
787
|
+
throw_exception_probability: float = 0.0,
|
788
|
+
randomize: bool = False,
|
789
|
+
test_model=False,
|
790
|
+
) -> Jobs:
|
791
|
+
"""Return an example Jobs instance.
|
792
|
+
|
793
|
+
:param throw_exception_probability: the probability that an exception will be thrown when answering a question. This is useful for testing error handling.
|
794
|
+
:param randomize: whether to randomize the job by adding a random string to the period
|
795
|
+
:param test_model: whether to use a test model
|
796
|
+
|
797
|
+
>>> Jobs.example()
|
798
|
+
Jobs(...)
|
799
|
+
|
800
|
+
"""
|
801
|
+
import random
|
802
|
+
from uuid import uuid4
|
803
|
+
from edsl.questions import QuestionMultipleChoice
|
804
|
+
from edsl.agents.Agent import Agent
|
805
|
+
from edsl.scenarios.Scenario import Scenario
|
806
|
+
|
807
|
+
addition = "" if not randomize else str(uuid4())
|
808
|
+
|
809
|
+
if test_model:
|
810
|
+
from edsl.language_models import LanguageModel
|
811
|
+
|
812
|
+
m = LanguageModel.example(test_model=True)
|
813
|
+
|
814
|
+
# (status, question, period)
|
815
|
+
agent_answers = {
|
816
|
+
("Joyful", "how_feeling", "morning"): "OK",
|
817
|
+
("Joyful", "how_feeling", "afternoon"): "Great",
|
818
|
+
("Joyful", "how_feeling_yesterday", "morning"): "Great",
|
819
|
+
("Joyful", "how_feeling_yesterday", "afternoon"): "Good",
|
820
|
+
("Sad", "how_feeling", "morning"): "Terrible",
|
821
|
+
("Sad", "how_feeling", "afternoon"): "OK",
|
822
|
+
("Sad", "how_feeling_yesterday", "morning"): "OK",
|
823
|
+
("Sad", "how_feeling_yesterday", "afternoon"): "Terrible",
|
824
|
+
}
|
825
|
+
|
826
|
+
def answer_question_directly(self, question, scenario):
|
827
|
+
"""Return the answer to a question. This is a method that can be added to an agent."""
|
828
|
+
|
829
|
+
if random.random() < throw_exception_probability:
|
830
|
+
raise Exception("Error!")
|
831
|
+
return agent_answers[
|
832
|
+
(self.traits["status"], question.question_name, scenario["period"])
|
833
|
+
]
|
834
|
+
|
835
|
+
sad_agent = Agent(traits={"status": "Sad"})
|
836
|
+
joy_agent = Agent(traits={"status": "Joyful"})
|
837
|
+
|
838
|
+
sad_agent.add_direct_question_answering_method(answer_question_directly)
|
839
|
+
joy_agent.add_direct_question_answering_method(answer_question_directly)
|
840
|
+
|
841
|
+
q1 = QuestionMultipleChoice(
|
842
|
+
question_text="How are you this {{ period }}?",
|
843
|
+
question_options=["Good", "Great", "OK", "Terrible"],
|
844
|
+
question_name="how_feeling",
|
845
|
+
)
|
846
|
+
q2 = QuestionMultipleChoice(
|
847
|
+
question_text="How were you feeling yesterday {{ period }}?",
|
848
|
+
question_options=["Good", "Great", "OK", "Terrible"],
|
849
|
+
question_name="how_feeling_yesterday",
|
850
|
+
)
|
851
|
+
from edsl import Survey, ScenarioList
|
852
|
+
|
853
|
+
base_survey = Survey(questions=[q1, q2])
|
854
|
+
|
855
|
+
scenario_list = ScenarioList(
|
856
|
+
[
|
857
|
+
Scenario({"period": f"morning{addition}"}),
|
858
|
+
Scenario({"period": "afternoon"}),
|
859
|
+
]
|
860
|
+
)
|
861
|
+
if test_model:
|
862
|
+
job = base_survey.by(m).by(scenario_list).by(joy_agent, sad_agent)
|
863
|
+
else:
|
864
|
+
job = base_survey.by(scenario_list).by(joy_agent, sad_agent)
|
865
|
+
|
866
|
+
return job
|
867
|
+
|
868
|
+
def rich_print(self):
|
869
|
+
"""Print a rich representation of the Jobs instance."""
|
870
|
+
from rich.table import Table
|
871
|
+
|
872
|
+
table = Table(title="Jobs")
|
873
|
+
table.add_column("Jobs")
|
874
|
+
table.add_row(self.survey.rich_print())
|
875
|
+
return table
|
876
|
+
|
877
|
+
def code(self):
|
878
|
+
"""Return the code to create this instance."""
|
879
|
+
raise NotImplementedError
|
880
|
+
|
881
|
+
|
882
|
+
def main():
|
883
|
+
"""Run the module's doctests."""
|
884
|
+
from edsl.jobs import Jobs
|
885
|
+
from edsl.data.Cache import Cache
|
886
|
+
|
887
|
+
job = Jobs.example()
|
888
|
+
len(job) == 8
|
889
|
+
results = job.run(cache=Cache())
|
890
|
+
len(results) == 8
|
891
|
+
results
|
892
|
+
|
893
|
+
|
894
|
+
if __name__ == "__main__":
|
895
|
+
"""Run the module's doctests."""
|
896
|
+
import doctest
|
897
|
+
|
898
|
+
doctest.testmod(optionflags=doctest.ELLIPSIS)
|