edsl 0.1.15__py3-none-any.whl → 0.1.40__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 +348 -38
- edsl/BaseDiff.py +260 -0
- edsl/TemplateLoader.py +24 -0
- edsl/__init__.py +45 -10
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +842 -144
- edsl/agents/AgentList.py +521 -25
- edsl/agents/Invigilator.py +250 -374
- edsl/agents/InvigilatorBase.py +257 -0
- edsl/agents/PromptConstructor.py +272 -0
- edsl/agents/QuestionInstructionPromptBuilder.py +128 -0
- edsl/agents/QuestionTemplateReplacementsBuilder.py +137 -0
- edsl/agents/descriptors.py +43 -13
- edsl/agents/prompt_helpers.py +129 -0
- edsl/agents/question_option_processor.py +172 -0
- edsl/auto/AutoStudy.py +130 -0
- edsl/auto/StageBase.py +243 -0
- edsl/auto/StageGenerateSurvey.py +178 -0
- edsl/auto/StageLabelQuestions.py +125 -0
- edsl/auto/StagePersona.py +61 -0
- edsl/auto/StagePersonaDimensionValueRanges.py +88 -0
- edsl/auto/StagePersonaDimensionValues.py +74 -0
- edsl/auto/StagePersonaDimensions.py +69 -0
- edsl/auto/StageQuestions.py +74 -0
- edsl/auto/SurveyCreatorPipeline.py +21 -0
- edsl/auto/utilities.py +218 -0
- edsl/base/Base.py +279 -0
- edsl/config.py +115 -113
- edsl/conversation/Conversation.py +290 -0
- edsl/conversation/car_buying.py +59 -0
- edsl/conversation/chips.py +95 -0
- edsl/conversation/mug_negotiation.py +81 -0
- edsl/conversation/next_speaker_utilities.py +93 -0
- edsl/coop/CoopFunctionsMixin.py +15 -0
- edsl/coop/ExpectedParrotKeyHandler.py +125 -0
- edsl/coop/PriceFetcher.py +54 -0
- edsl/coop/__init__.py +1 -0
- edsl/coop/coop.py +1029 -134
- edsl/coop/utils.py +131 -0
- edsl/data/Cache.py +560 -89
- edsl/data/CacheEntry.py +230 -0
- edsl/data/CacheHandler.py +168 -0
- edsl/data/RemoteCacheSync.py +186 -0
- edsl/data/SQLiteDict.py +292 -0
- edsl/data/__init__.py +5 -3
- edsl/data/orm.py +6 -33
- edsl/data_transfer_models.py +74 -27
- edsl/enums.py +165 -8
- edsl/exceptions/BaseException.py +21 -0
- edsl/exceptions/__init__.py +52 -46
- edsl/exceptions/agents.py +33 -15
- edsl/exceptions/cache.py +5 -0
- edsl/exceptions/coop.py +8 -0
- edsl/exceptions/general.py +34 -0
- edsl/exceptions/inference_services.py +5 -0
- edsl/exceptions/jobs.py +15 -0
- edsl/exceptions/language_models.py +46 -1
- edsl/exceptions/questions.py +80 -5
- edsl/exceptions/results.py +16 -5
- edsl/exceptions/scenarios.py +29 -0
- edsl/exceptions/surveys.py +13 -10
- edsl/inference_services/AnthropicService.py +106 -0
- edsl/inference_services/AvailableModelCacheHandler.py +184 -0
- edsl/inference_services/AvailableModelFetcher.py +215 -0
- edsl/inference_services/AwsBedrock.py +118 -0
- edsl/inference_services/AzureAI.py +215 -0
- edsl/inference_services/DeepInfraService.py +18 -0
- edsl/inference_services/GoogleService.py +143 -0
- edsl/inference_services/GroqService.py +20 -0
- edsl/inference_services/InferenceServiceABC.py +80 -0
- edsl/inference_services/InferenceServicesCollection.py +138 -0
- edsl/inference_services/MistralAIService.py +120 -0
- edsl/inference_services/OllamaService.py +18 -0
- edsl/inference_services/OpenAIService.py +236 -0
- edsl/inference_services/PerplexityService.py +160 -0
- edsl/inference_services/ServiceAvailability.py +135 -0
- edsl/inference_services/TestService.py +90 -0
- edsl/inference_services/TogetherAIService.py +172 -0
- edsl/inference_services/data_structures.py +134 -0
- edsl/inference_services/models_available_cache.py +118 -0
- edsl/inference_services/rate_limits_cache.py +25 -0
- edsl/inference_services/registry.py +41 -0
- edsl/inference_services/write_available.py +10 -0
- edsl/jobs/AnswerQuestionFunctionConstructor.py +223 -0
- edsl/jobs/Answers.py +21 -20
- edsl/jobs/FetchInvigilator.py +47 -0
- edsl/jobs/InterviewTaskManager.py +98 -0
- edsl/jobs/InterviewsConstructor.py +50 -0
- edsl/jobs/Jobs.py +684 -206
- edsl/jobs/JobsChecks.py +172 -0
- edsl/jobs/JobsComponentConstructor.py +189 -0
- edsl/jobs/JobsPrompts.py +270 -0
- edsl/jobs/JobsRemoteInferenceHandler.py +311 -0
- edsl/jobs/JobsRemoteInferenceLogger.py +239 -0
- edsl/jobs/RequestTokenEstimator.py +30 -0
- edsl/jobs/async_interview_runner.py +138 -0
- edsl/jobs/buckets/BucketCollection.py +104 -0
- edsl/jobs/buckets/ModelBuckets.py +65 -0
- edsl/jobs/buckets/TokenBucket.py +283 -0
- edsl/jobs/buckets/TokenBucketAPI.py +211 -0
- edsl/jobs/buckets/TokenBucketClient.py +191 -0
- edsl/jobs/check_survey_scenario_compatibility.py +85 -0
- edsl/jobs/data_structures.py +120 -0
- edsl/jobs/decorators.py +35 -0
- edsl/jobs/interviews/Interview.py +392 -0
- edsl/jobs/interviews/InterviewExceptionCollection.py +99 -0
- edsl/jobs/interviews/InterviewExceptionEntry.py +186 -0
- edsl/jobs/interviews/InterviewStatistic.py +63 -0
- edsl/jobs/interviews/InterviewStatisticsCollection.py +25 -0
- edsl/jobs/interviews/InterviewStatusDictionary.py +78 -0
- edsl/jobs/interviews/InterviewStatusLog.py +92 -0
- edsl/jobs/interviews/ReportErrors.py +66 -0
- edsl/jobs/interviews/interview_status_enum.py +9 -0
- edsl/jobs/jobs_status_enums.py +9 -0
- edsl/jobs/loggers/HTMLTableJobLogger.py +304 -0
- edsl/jobs/results_exceptions_handler.py +98 -0
- edsl/jobs/runners/JobsRunnerAsyncio.py +151 -110
- edsl/jobs/runners/JobsRunnerStatus.py +298 -0
- edsl/jobs/tasks/QuestionTaskCreator.py +244 -0
- edsl/jobs/tasks/TaskCreators.py +64 -0
- edsl/jobs/tasks/TaskHistory.py +470 -0
- edsl/jobs/tasks/TaskStatusLog.py +23 -0
- edsl/jobs/tasks/task_status_enum.py +161 -0
- edsl/jobs/tokens/InterviewTokenUsage.py +27 -0
- edsl/jobs/tokens/TokenUsage.py +34 -0
- edsl/language_models/ComputeCost.py +63 -0
- edsl/language_models/LanguageModel.py +507 -386
- edsl/language_models/ModelList.py +164 -0
- edsl/language_models/PriceManager.py +127 -0
- edsl/language_models/RawResponseHandler.py +106 -0
- edsl/language_models/RegisterLanguageModelsMeta.py +184 -0
- edsl/language_models/__init__.py +1 -8
- edsl/language_models/fake_openai_call.py +15 -0
- edsl/language_models/fake_openai_service.py +61 -0
- edsl/language_models/key_management/KeyLookup.py +63 -0
- edsl/language_models/key_management/KeyLookupBuilder.py +273 -0
- edsl/language_models/key_management/KeyLookupCollection.py +38 -0
- edsl/language_models/key_management/__init__.py +0 -0
- edsl/language_models/key_management/models.py +131 -0
- edsl/language_models/model.py +256 -0
- edsl/language_models/repair.py +109 -41
- edsl/language_models/utilities.py +65 -0
- edsl/notebooks/Notebook.py +263 -0
- edsl/notebooks/NotebookToLaTeX.py +142 -0
- edsl/notebooks/__init__.py +1 -0
- edsl/prompts/Prompt.py +222 -93
- edsl/prompts/__init__.py +1 -1
- edsl/questions/ExceptionExplainer.py +77 -0
- edsl/questions/HTMLQuestion.py +103 -0
- edsl/questions/QuestionBase.py +518 -0
- edsl/questions/QuestionBasePromptsMixin.py +221 -0
- edsl/questions/QuestionBudget.py +164 -67
- edsl/questions/QuestionCheckBox.py +281 -62
- edsl/questions/QuestionDict.py +343 -0
- edsl/questions/QuestionExtract.py +136 -50
- edsl/questions/QuestionFreeText.py +79 -55
- edsl/questions/QuestionFunctional.py +138 -41
- edsl/questions/QuestionList.py +184 -57
- edsl/questions/QuestionMatrix.py +265 -0
- edsl/questions/QuestionMultipleChoice.py +293 -69
- edsl/questions/QuestionNumerical.py +109 -56
- edsl/questions/QuestionRank.py +244 -49
- edsl/questions/Quick.py +41 -0
- edsl/questions/SimpleAskMixin.py +74 -0
- edsl/questions/__init__.py +9 -6
- edsl/questions/{AnswerValidatorMixin.py → answer_validator_mixin.py} +153 -38
- edsl/questions/compose_questions.py +13 -7
- edsl/questions/data_structures.py +20 -0
- edsl/questions/decorators.py +21 -0
- edsl/questions/derived/QuestionLikertFive.py +28 -26
- edsl/questions/derived/QuestionLinearScale.py +41 -28
- edsl/questions/derived/QuestionTopK.py +34 -26
- edsl/questions/derived/QuestionYesNo.py +40 -27
- edsl/questions/descriptors.py +228 -74
- edsl/questions/loop_processor.py +149 -0
- edsl/questions/prompt_templates/question_budget.jinja +13 -0
- edsl/questions/prompt_templates/question_checkbox.jinja +32 -0
- edsl/questions/prompt_templates/question_extract.jinja +11 -0
- edsl/questions/prompt_templates/question_free_text.jinja +3 -0
- edsl/questions/prompt_templates/question_linear_scale.jinja +11 -0
- edsl/questions/prompt_templates/question_list.jinja +17 -0
- edsl/questions/prompt_templates/question_multiple_choice.jinja +33 -0
- edsl/questions/prompt_templates/question_numerical.jinja +37 -0
- edsl/questions/question_base_gen_mixin.py +168 -0
- edsl/questions/question_registry.py +130 -46
- edsl/questions/register_questions_meta.py +71 -0
- edsl/questions/response_validator_abc.py +188 -0
- edsl/questions/response_validator_factory.py +34 -0
- edsl/questions/settings.py +5 -2
- edsl/questions/templates/__init__.py +0 -0
- edsl/questions/templates/budget/__init__.py +0 -0
- edsl/questions/templates/budget/answering_instructions.jinja +7 -0
- edsl/questions/templates/budget/question_presentation.jinja +7 -0
- edsl/questions/templates/checkbox/__init__.py +0 -0
- edsl/questions/templates/checkbox/answering_instructions.jinja +10 -0
- edsl/questions/templates/checkbox/question_presentation.jinja +22 -0
- edsl/questions/templates/dict/__init__.py +0 -0
- edsl/questions/templates/dict/answering_instructions.jinja +21 -0
- edsl/questions/templates/dict/question_presentation.jinja +1 -0
- edsl/questions/templates/extract/__init__.py +0 -0
- edsl/questions/templates/extract/answering_instructions.jinja +7 -0
- edsl/questions/templates/extract/question_presentation.jinja +1 -0
- edsl/questions/templates/free_text/__init__.py +0 -0
- edsl/questions/templates/free_text/answering_instructions.jinja +0 -0
- edsl/questions/templates/free_text/question_presentation.jinja +1 -0
- edsl/questions/templates/likert_five/__init__.py +0 -0
- edsl/questions/templates/likert_five/answering_instructions.jinja +10 -0
- edsl/questions/templates/likert_five/question_presentation.jinja +12 -0
- edsl/questions/templates/linear_scale/__init__.py +0 -0
- edsl/questions/templates/linear_scale/answering_instructions.jinja +5 -0
- edsl/questions/templates/linear_scale/question_presentation.jinja +5 -0
- edsl/questions/templates/list/__init__.py +0 -0
- edsl/questions/templates/list/answering_instructions.jinja +4 -0
- edsl/questions/templates/list/question_presentation.jinja +5 -0
- edsl/questions/templates/matrix/__init__.py +1 -0
- edsl/questions/templates/matrix/answering_instructions.jinja +5 -0
- edsl/questions/templates/matrix/question_presentation.jinja +20 -0
- edsl/questions/templates/multiple_choice/__init__.py +0 -0
- edsl/questions/templates/multiple_choice/answering_instructions.jinja +9 -0
- edsl/questions/templates/multiple_choice/html.jinja +0 -0
- edsl/questions/templates/multiple_choice/question_presentation.jinja +12 -0
- edsl/questions/templates/numerical/__init__.py +0 -0
- edsl/questions/templates/numerical/answering_instructions.jinja +7 -0
- edsl/questions/templates/numerical/question_presentation.jinja +7 -0
- edsl/questions/templates/rank/__init__.py +0 -0
- edsl/questions/templates/rank/answering_instructions.jinja +11 -0
- edsl/questions/templates/rank/question_presentation.jinja +15 -0
- edsl/questions/templates/top_k/__init__.py +0 -0
- edsl/questions/templates/top_k/answering_instructions.jinja +8 -0
- edsl/questions/templates/top_k/question_presentation.jinja +22 -0
- edsl/questions/templates/yes_no/__init__.py +0 -0
- edsl/questions/templates/yes_no/answering_instructions.jinja +6 -0
- edsl/questions/templates/yes_no/question_presentation.jinja +12 -0
- edsl/results/CSSParameterizer.py +108 -0
- edsl/results/Dataset.py +550 -19
- edsl/results/DatasetExportMixin.py +594 -0
- edsl/results/DatasetTree.py +295 -0
- edsl/results/MarkdownToDocx.py +122 -0
- edsl/results/MarkdownToPDF.py +111 -0
- edsl/results/Result.py +477 -173
- edsl/results/Results.py +987 -269
- edsl/results/ResultsExportMixin.py +28 -125
- edsl/results/ResultsGGMixin.py +83 -15
- edsl/results/TableDisplay.py +125 -0
- edsl/results/TextEditor.py +50 -0
- edsl/results/__init__.py +1 -1
- edsl/results/file_exports.py +252 -0
- edsl/results/results_fetch_mixin.py +33 -0
- edsl/results/results_selector.py +145 -0
- edsl/results/results_tools_mixin.py +98 -0
- edsl/results/smart_objects.py +96 -0
- edsl/results/table_data_class.py +12 -0
- edsl/results/table_display.css +78 -0
- edsl/results/table_renderers.py +118 -0
- edsl/results/tree_explore.py +115 -0
- edsl/scenarios/ConstructDownloadLink.py +109 -0
- edsl/scenarios/DocumentChunker.py +102 -0
- edsl/scenarios/DocxScenario.py +16 -0
- edsl/scenarios/FileStore.py +543 -0
- edsl/scenarios/PdfExtractor.py +40 -0
- edsl/scenarios/Scenario.py +431 -62
- edsl/scenarios/ScenarioHtmlMixin.py +65 -0
- edsl/scenarios/ScenarioList.py +1415 -45
- edsl/scenarios/ScenarioListExportMixin.py +45 -0
- edsl/scenarios/ScenarioListPdfMixin.py +239 -0
- edsl/scenarios/__init__.py +2 -0
- edsl/scenarios/directory_scanner.py +96 -0
- edsl/scenarios/file_methods.py +85 -0
- edsl/scenarios/handlers/__init__.py +13 -0
- edsl/scenarios/handlers/csv.py +49 -0
- edsl/scenarios/handlers/docx.py +76 -0
- edsl/scenarios/handlers/html.py +37 -0
- edsl/scenarios/handlers/json.py +111 -0
- edsl/scenarios/handlers/latex.py +5 -0
- edsl/scenarios/handlers/md.py +51 -0
- edsl/scenarios/handlers/pdf.py +68 -0
- edsl/scenarios/handlers/png.py +39 -0
- edsl/scenarios/handlers/pptx.py +105 -0
- edsl/scenarios/handlers/py.py +294 -0
- edsl/scenarios/handlers/sql.py +313 -0
- edsl/scenarios/handlers/sqlite.py +149 -0
- edsl/scenarios/handlers/txt.py +33 -0
- edsl/scenarios/scenario_join.py +131 -0
- edsl/scenarios/scenario_selector.py +156 -0
- edsl/shared.py +1 -0
- edsl/study/ObjectEntry.py +173 -0
- edsl/study/ProofOfWork.py +113 -0
- edsl/study/SnapShot.py +80 -0
- edsl/study/Study.py +521 -0
- edsl/study/__init__.py +4 -0
- edsl/surveys/ConstructDAG.py +92 -0
- edsl/surveys/DAG.py +92 -11
- edsl/surveys/EditSurvey.py +221 -0
- edsl/surveys/InstructionHandler.py +100 -0
- edsl/surveys/Memory.py +9 -4
- edsl/surveys/MemoryManagement.py +72 -0
- edsl/surveys/MemoryPlan.py +156 -35
- edsl/surveys/Rule.py +221 -74
- edsl/surveys/RuleCollection.py +241 -61
- edsl/surveys/RuleManager.py +172 -0
- edsl/surveys/Simulator.py +75 -0
- edsl/surveys/Survey.py +1079 -339
- edsl/surveys/SurveyCSS.py +273 -0
- edsl/surveys/SurveyExportMixin.py +235 -40
- edsl/surveys/SurveyFlowVisualization.py +181 -0
- edsl/surveys/SurveyQualtricsImport.py +284 -0
- edsl/surveys/SurveyToApp.py +141 -0
- edsl/surveys/__init__.py +4 -2
- edsl/surveys/base.py +19 -3
- edsl/surveys/descriptors.py +17 -6
- edsl/surveys/instructions/ChangeInstruction.py +48 -0
- edsl/surveys/instructions/Instruction.py +56 -0
- edsl/surveys/instructions/InstructionCollection.py +82 -0
- edsl/surveys/instructions/__init__.py +0 -0
- edsl/templates/error_reporting/base.html +24 -0
- edsl/templates/error_reporting/exceptions_by_model.html +35 -0
- edsl/templates/error_reporting/exceptions_by_question_name.html +17 -0
- edsl/templates/error_reporting/exceptions_by_type.html +17 -0
- edsl/templates/error_reporting/interview_details.html +116 -0
- edsl/templates/error_reporting/interviews.html +19 -0
- edsl/templates/error_reporting/overview.html +5 -0
- edsl/templates/error_reporting/performance_plot.html +2 -0
- edsl/templates/error_reporting/report.css +74 -0
- edsl/templates/error_reporting/report.html +118 -0
- edsl/templates/error_reporting/report.js +25 -0
- edsl/tools/__init__.py +1 -0
- edsl/tools/clusters.py +192 -0
- edsl/tools/embeddings.py +27 -0
- edsl/tools/embeddings_plotting.py +118 -0
- edsl/tools/plotting.py +112 -0
- edsl/tools/summarize.py +18 -0
- edsl/utilities/PrettyList.py +56 -0
- edsl/utilities/SystemInfo.py +5 -0
- edsl/utilities/__init__.py +21 -20
- edsl/utilities/ast_utilities.py +3 -0
- edsl/utilities/data/Registry.py +2 -0
- edsl/utilities/decorators.py +41 -0
- edsl/utilities/gcp_bucket/__init__.py +0 -0
- edsl/utilities/gcp_bucket/cloud_storage.py +96 -0
- edsl/utilities/interface.py +310 -60
- edsl/utilities/is_notebook.py +18 -0
- edsl/utilities/is_valid_variable_name.py +11 -0
- edsl/utilities/naming_utilities.py +263 -0
- edsl/utilities/remove_edsl_version.py +24 -0
- edsl/utilities/repair_functions.py +28 -0
- edsl/utilities/restricted_python.py +70 -0
- edsl/utilities/utilities.py +203 -13
- edsl-0.1.40.dist-info/METADATA +111 -0
- edsl-0.1.40.dist-info/RECORD +362 -0
- {edsl-0.1.15.dist-info → edsl-0.1.40.dist-info}/WHEEL +1 -1
- edsl/agents/AgentListExportMixin.py +0 -24
- edsl/coop/old.py +0 -31
- edsl/data/Database.py +0 -141
- edsl/data/crud.py +0 -121
- edsl/jobs/Interview.py +0 -435
- edsl/jobs/JobsRunner.py +0 -63
- edsl/jobs/JobsRunnerStatusMixin.py +0 -115
- edsl/jobs/base.py +0 -47
- edsl/jobs/buckets.py +0 -178
- edsl/jobs/runners/JobsRunnerDryRun.py +0 -19
- edsl/jobs/runners/JobsRunnerStreaming.py +0 -54
- edsl/jobs/task_management.py +0 -215
- edsl/jobs/token_tracking.py +0 -78
- edsl/language_models/DeepInfra.py +0 -69
- edsl/language_models/OpenAI.py +0 -98
- edsl/language_models/model_interfaces/GeminiPro.py +0 -66
- edsl/language_models/model_interfaces/LanguageModelOpenAIFour.py +0 -8
- edsl/language_models/model_interfaces/LanguageModelOpenAIThreeFiveTurbo.py +0 -8
- edsl/language_models/model_interfaces/LlamaTwo13B.py +0 -21
- edsl/language_models/model_interfaces/LlamaTwo70B.py +0 -21
- edsl/language_models/model_interfaces/Mixtral8x7B.py +0 -24
- edsl/language_models/registry.py +0 -81
- edsl/language_models/schemas.py +0 -15
- edsl/language_models/unused/ReplicateBase.py +0 -83
- edsl/prompts/QuestionInstructionsBase.py +0 -6
- edsl/prompts/library/agent_instructions.py +0 -29
- edsl/prompts/library/agent_persona.py +0 -17
- edsl/prompts/library/question_budget.py +0 -26
- edsl/prompts/library/question_checkbox.py +0 -32
- edsl/prompts/library/question_extract.py +0 -19
- edsl/prompts/library/question_freetext.py +0 -14
- edsl/prompts/library/question_linear_scale.py +0 -20
- edsl/prompts/library/question_list.py +0 -22
- edsl/prompts/library/question_multiple_choice.py +0 -44
- edsl/prompts/library/question_numerical.py +0 -31
- edsl/prompts/library/question_rank.py +0 -21
- edsl/prompts/prompt_config.py +0 -33
- edsl/prompts/registry.py +0 -185
- edsl/questions/Question.py +0 -240
- edsl/report/InputOutputDataTypes.py +0 -134
- edsl/report/RegressionMixin.py +0 -28
- edsl/report/ReportOutputs.py +0 -1228
- edsl/report/ResultsFetchMixin.py +0 -106
- edsl/report/ResultsOutputMixin.py +0 -14
- edsl/report/demo.ipynb +0 -645
- edsl/results/ResultsDBMixin.py +0 -184
- edsl/surveys/SurveyFlowVisualizationMixin.py +0 -92
- edsl/trackers/Tracker.py +0 -91
- edsl/trackers/TrackerAPI.py +0 -196
- edsl/trackers/TrackerTasks.py +0 -70
- edsl/utilities/pastebin.py +0 -141
- edsl-0.1.15.dist-info/METADATA +0 -69
- edsl-0.1.15.dist-info/RECORD +0 -142
- /edsl/{language_models/model_interfaces → inference_services}/__init__.py +0 -0
- /edsl/{report/__init__.py → jobs/runners/JobsRunnerStatusData.py} +0 -0
- /edsl/{trackers/__init__.py → language_models/ServiceDataSources.py} +0 -0
- {edsl-0.1.15.dist-info → edsl-0.1.40.dist-info}/LICENSE +0 -0
edsl/auto/StageBase.py
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
import json
|
3
|
+
from typing import Dict, List, Any, TypeVar, Generator, Dict, Callable
|
4
|
+
from dataclasses import dataclass, field, KW_ONLY, fields, asdict
|
5
|
+
import textwrap
|
6
|
+
|
7
|
+
|
8
|
+
class ExceptionPipesDoNotFit(Exception):
|
9
|
+
pass
|
10
|
+
|
11
|
+
|
12
|
+
class StageProcessingClosure:
|
13
|
+
def __init__(self, stage_func: Callable, reduction_func=lambda x: x):
|
14
|
+
self.data = []
|
15
|
+
self.stage_func = stage_func
|
16
|
+
# reduction function is applied to self.data when complete
|
17
|
+
# it might just return the list, or it might do something more complicated such as
|
18
|
+
# reduce the list to a dictionary
|
19
|
+
self.reduction_func = reduction_func
|
20
|
+
|
21
|
+
def func(self, obj: "FlowDataBase") -> None:
|
22
|
+
"Function to apply to each stage"
|
23
|
+
self.data.append(self.stage_func(obj))
|
24
|
+
|
25
|
+
def __call__(self):
|
26
|
+
return self.reduction_func(self.data)
|
27
|
+
|
28
|
+
|
29
|
+
@dataclass
|
30
|
+
class FlowDataBase:
|
31
|
+
"""Base class for dataclasses that are passed between stages."""
|
32
|
+
|
33
|
+
_: KW_ONLY
|
34
|
+
# previous_stage: Dict = field(default_factory=dict)
|
35
|
+
previous_stage: Any = None
|
36
|
+
sent_to_stage_name: str = field(default_factory=str)
|
37
|
+
came_from_stage_name: str = field(default_factory=str)
|
38
|
+
|
39
|
+
def to_dict(self):
|
40
|
+
return asdict(self)
|
41
|
+
|
42
|
+
@classmethod
|
43
|
+
def from_dict(cls, data: dict):
|
44
|
+
return cls(**data)
|
45
|
+
|
46
|
+
def __getitem__(self, key):
|
47
|
+
"""Allows dictionary-style getting."""
|
48
|
+
return getattr(self, key)
|
49
|
+
|
50
|
+
def __setitem__(self, key, value):
|
51
|
+
"""Allows dictionary-style setting."""
|
52
|
+
return setattr(self, key, value)
|
53
|
+
|
54
|
+
def current_values(self):
|
55
|
+
"""Returns a dictionary of the current values of the dataclass"""
|
56
|
+
to_exclude = ["sent_to_stage_name", "came_from_stage_name", "previous_stage"]
|
57
|
+
d = asdict(self)
|
58
|
+
[d.pop(key) for key in to_exclude]
|
59
|
+
return d
|
60
|
+
|
61
|
+
def stage_input_output(self):
|
62
|
+
return {
|
63
|
+
"came_from": self.came_from_stage_name,
|
64
|
+
"sent_to": self.sent_to_stage_name,
|
65
|
+
}
|
66
|
+
|
67
|
+
def _align_values_with_padding(
|
68
|
+
self, stages
|
69
|
+
) -> Generator[Dict[str, str], None, None]:
|
70
|
+
"Pads out the the names of the stages so they are aligned when printing"
|
71
|
+
|
72
|
+
def longest_value(stage):
|
73
|
+
return max([len(v) for v in stage.values()])
|
74
|
+
|
75
|
+
max_length = max([longest_value(stage) for stage in stages])
|
76
|
+
for stage in stages:
|
77
|
+
new_stage = {k: v.ljust(max_length) for k, v in stage.items()}
|
78
|
+
yield new_stage
|
79
|
+
|
80
|
+
def _reduce(self, stage_processor: StageProcessingClosure) -> Dict[str, dict]:
|
81
|
+
"""Applies some function defined in stage_processor to each stage in the chain, working from back to front
|
82
|
+
|
83
|
+
The stage_processor will record the results of the function applied to each stage in
|
84
|
+
an instance of the StageProcessingClosure class.
|
85
|
+
The results can be accessed by calling the StageProcessingClosure instance.
|
86
|
+
This somewhat convoluted approach is necessary because the stages are connected in a chain and
|
87
|
+
we want a way to access the results of the function applied to each stage in the chain without
|
88
|
+
writing the while-loop over and over again.
|
89
|
+
"""
|
90
|
+
stage_processor.func(self)
|
91
|
+
current_pipe = self
|
92
|
+
while True:
|
93
|
+
if current_pipe.previous_stage is None:
|
94
|
+
break
|
95
|
+
else:
|
96
|
+
current_pipe = current_pipe.previous_stage
|
97
|
+
stage_processor.func(
|
98
|
+
current_pipe
|
99
|
+
) # the result is getting stored in stage_processor.data
|
100
|
+
|
101
|
+
def combined_results(self) -> Dict[str, dict]:
|
102
|
+
stage_processor = StageProcessingClosure(
|
103
|
+
stage_func=lambda obj: obj.current_values(),
|
104
|
+
reduction_func=lambda x: {k: v for d in x for k, v in d.items()},
|
105
|
+
)
|
106
|
+
self._reduce(stage_processor)
|
107
|
+
return stage_processor()
|
108
|
+
|
109
|
+
def flow_history(self):
|
110
|
+
stage_processor = StageProcessingClosure(
|
111
|
+
stage_func=lambda obj: obj.stage_input_output()
|
112
|
+
)
|
113
|
+
self._reduce(stage_processor)
|
114
|
+
return stage_processor()
|
115
|
+
|
116
|
+
def visualize_flow(self) -> str:
|
117
|
+
"""Visualize the flow of data through the chain"""
|
118
|
+
stages = self.flow_history()
|
119
|
+
new_stages = list(self._align_values_with_padding(stages))
|
120
|
+
new_stages.reverse()
|
121
|
+
return tuple(new_stages)
|
122
|
+
|
123
|
+
|
124
|
+
class StageBase(ABC):
|
125
|
+
input: FlowDataBase = NotImplemented
|
126
|
+
output: FlowDataBase = NotImplemented
|
127
|
+
|
128
|
+
def __init__(self, **kwargs):
|
129
|
+
for key, value in kwargs.items():
|
130
|
+
setattr(self, key, value)
|
131
|
+
|
132
|
+
if hasattr(self, "next_stage"):
|
133
|
+
self._validate_connection(self.next_stage)
|
134
|
+
else:
|
135
|
+
self.next_stage = None
|
136
|
+
|
137
|
+
@classmethod
|
138
|
+
def function_parameters(self):
|
139
|
+
return fields(self.input)
|
140
|
+
|
141
|
+
@classmethod
|
142
|
+
def func(cls, **kwargs):
|
143
|
+
"This provides a shortcut for running a stage by passing keyword arguments to the input function."
|
144
|
+
input_data = cls.input(**kwargs)
|
145
|
+
return cls().process(input_data)
|
146
|
+
|
147
|
+
@abstractmethod
|
148
|
+
def handle_data(self, data):
|
149
|
+
"This implements how the stage actually handles the passed in data"
|
150
|
+
raise NotImplementedError
|
151
|
+
|
152
|
+
def _validate_connection(self, stage):
|
153
|
+
"Checks that the outputs of the first stage match the inputs of the second stage"
|
154
|
+
if not self.output == stage.input:
|
155
|
+
raise ExceptionPipesDoNotFit(
|
156
|
+
textwrap.dedent(
|
157
|
+
f"""\
|
158
|
+
Stage \"{self.__class__.__name__}\" cannot be connected to stage \"{stage.__class__.__name__}\".
|
159
|
+
The outputs of the first stage {self.output} do not match the inputs of the second stage, {stage.input}."""
|
160
|
+
)
|
161
|
+
)
|
162
|
+
|
163
|
+
def __init_subclass__(cls, **kwargs):
|
164
|
+
"Checks that the subclass has the required class variables of input & output"
|
165
|
+
super().__init_subclass__(**kwargs)
|
166
|
+
if cls.input is NotImplemented:
|
167
|
+
raise NotImplementedError(
|
168
|
+
f"Class {cls.__name__} lacks required class variable 'inputs'"
|
169
|
+
)
|
170
|
+
if cls.output is NotImplemented:
|
171
|
+
raise NotImplementedError(
|
172
|
+
f"Class {cls.__name__} lacks required class variable 'outputs'"
|
173
|
+
)
|
174
|
+
|
175
|
+
def process(self, data):
|
176
|
+
print(f"Running stage: {self.__class__.__name__}")
|
177
|
+
data.sent_to_stage_name = self.__class__.__name__
|
178
|
+
processed_data = self.handle_data(data)
|
179
|
+
processed_data.came_from_stage_name = self.__class__.__name__
|
180
|
+
processed_data.previous_stage = data
|
181
|
+
if self.next_stage:
|
182
|
+
return self.next_stage.process(processed_data)
|
183
|
+
else:
|
184
|
+
return processed_data
|
185
|
+
|
186
|
+
|
187
|
+
if __name__ == "__main__":
|
188
|
+
pass
|
189
|
+
# try:
|
190
|
+
|
191
|
+
# class StageMissing(StageBase):
|
192
|
+
# def handle_data(self, data):
|
193
|
+
# return data
|
194
|
+
|
195
|
+
# except NotImplementedError as e:
|
196
|
+
# print(e)
|
197
|
+
# else:
|
198
|
+
# raise Exception("Should have raised NotImplementedError")
|
199
|
+
|
200
|
+
# try:
|
201
|
+
|
202
|
+
# class StageMissingInput(StageBase):
|
203
|
+
# output = FlowDataBase
|
204
|
+
|
205
|
+
# except NotImplementedError as e:
|
206
|
+
# print(e)
|
207
|
+
|
208
|
+
# else:
|
209
|
+
# raise Exception("Should have raised NotImplementedError")
|
210
|
+
|
211
|
+
# @dataclass
|
212
|
+
# class MockInputOutput(FlowDataBase):
|
213
|
+
# text: str
|
214
|
+
|
215
|
+
# class StageTest(StageBase):
|
216
|
+
# input = MockInputOutput
|
217
|
+
# output = MockInputOutput
|
218
|
+
|
219
|
+
# def handle_data(self, data):
|
220
|
+
# return self.output(text=data["text"] + "processed")
|
221
|
+
|
222
|
+
# result = StageTest().process(MockInputOutput(text="Hello world!"))
|
223
|
+
# print(result.text)
|
224
|
+
|
225
|
+
# pipeline = StageTest(next_stage=StageTest(next_stage=StageTest()))
|
226
|
+
# result = pipeline.process(MockInputOutput(text="Hello world!"))
|
227
|
+
# print(result.text)
|
228
|
+
|
229
|
+
# class BadMockInput(FlowDataBase):
|
230
|
+
# text: str
|
231
|
+
# other: str
|
232
|
+
|
233
|
+
# class StageBad(StageBase):
|
234
|
+
# input = BadMockInput
|
235
|
+
# output = BadMockInput
|
236
|
+
|
237
|
+
# def handle_data(self, data):
|
238
|
+
# return self.output(text=data["text"] + "processed")
|
239
|
+
|
240
|
+
# try:
|
241
|
+
# pipeline = StageTest(next_stage=StageBad(next_stage=StageTest()))
|
242
|
+
# except ExceptionPipesDoNotFit as e:
|
243
|
+
# print(e)
|
@@ -0,0 +1,178 @@
|
|
1
|
+
from textwrap import dedent
|
2
|
+
from dataclasses import dataclass
|
3
|
+
from collections import defaultdict
|
4
|
+
|
5
|
+
from typing import List, Dict
|
6
|
+
|
7
|
+
from edsl.auto.StageBase import StageBase
|
8
|
+
from edsl.auto.utilities import gen_pipeline
|
9
|
+
from edsl.auto.StageBase import FlowDataBase
|
10
|
+
|
11
|
+
from edsl.auto.StageQuestions import StageQuestions
|
12
|
+
from edsl.auto.StageLabelQuestions import StageLabelQuestions
|
13
|
+
|
14
|
+
from edsl.questions import QuestionList
|
15
|
+
from edsl.scenarios import Scenario
|
16
|
+
from edsl import Model
|
17
|
+
from edsl.surveys import Survey
|
18
|
+
from edsl.questions import QuestionBase
|
19
|
+
|
20
|
+
from edsl.utilities.utilities import is_valid_variable_name
|
21
|
+
from edsl import Model
|
22
|
+
from edsl.questions import QuestionExtract
|
23
|
+
|
24
|
+
|
25
|
+
m = Model()
|
26
|
+
|
27
|
+
|
28
|
+
def chunker(seq, size):
|
29
|
+
return (seq[pos : pos + size] for pos in range(0, len(seq), size))
|
30
|
+
|
31
|
+
|
32
|
+
def get_short_options(question_options, num_chars=20):
|
33
|
+
"""Gets short names for the options of a question
|
34
|
+
>>> get_short_options(["No, I don't own a scooter", "Yes, I own a scooter"])
|
35
|
+
{'No, I don\'t own a scooter': 'no_scooter', 'Yes, I own a scooter': 'yes_scooter'}
|
36
|
+
"""
|
37
|
+
q = QuestionList(
|
38
|
+
question_text=dedent(
|
39
|
+
f"""\
|
40
|
+
We need short (less than {num_chars} characters) names for the options of a question, with no spaces.
|
41
|
+
E.g., if the options were "No, I don't own a scooter" and "Yes, I own a scooter",
|
42
|
+
you could use "no_scooter" and "yes_scooter".
|
43
|
+
They should be all lower case. Use snake case.
|
44
|
+
The short names have to be unique.
|
45
|
+
The options are: {question_options}
|
46
|
+
The names are {question_options} of them."""
|
47
|
+
),
|
48
|
+
# answer_template={k: None for k in question_options},
|
49
|
+
question_name="short_options",
|
50
|
+
)
|
51
|
+
results = q.by(m).run()
|
52
|
+
return results.select("short_options").first()
|
53
|
+
|
54
|
+
|
55
|
+
def get_short_names_chunk(questions, num_chars=20):
|
56
|
+
q = QuestionList(
|
57
|
+
question_text=dedent(
|
58
|
+
f"""\
|
59
|
+
We need short (less than {num_chars} characters) names for the questions, with no spaces.
|
60
|
+
E.g., if the question was: "What is your first name?", you could use "first_name".
|
61
|
+
The short names have to be unique and not starting with numbers. They should be all lower case.
|
62
|
+
The questions are: {questions}
|
63
|
+
"""
|
64
|
+
),
|
65
|
+
question_name="short_names",
|
66
|
+
)
|
67
|
+
results = q.by(m).run()
|
68
|
+
short_names = results.select("short_names").first()
|
69
|
+
return {k: v for k, v in zip(questions, short_names)}
|
70
|
+
|
71
|
+
|
72
|
+
def get_short_names(questions, max_size=10, num_chars=20):
|
73
|
+
"Gets short names for questions"
|
74
|
+
if len(questions) <= max_size:
|
75
|
+
short_names_dict = get_short_names_chunk(questions, num_chars)
|
76
|
+
else:
|
77
|
+
short_names_dict = {}
|
78
|
+
for chunk in chunker(questions, max_size):
|
79
|
+
results = get_short_names_chunk(chunk, num_chars)
|
80
|
+
short_names_dict.update(results)
|
81
|
+
return short_names_dict
|
82
|
+
|
83
|
+
|
84
|
+
class StageGenerateSurvey(StageBase):
|
85
|
+
input = StageLabelQuestions.output
|
86
|
+
|
87
|
+
@dataclass
|
88
|
+
class Output(FlowDataBase):
|
89
|
+
survey: Survey
|
90
|
+
|
91
|
+
output = Output
|
92
|
+
|
93
|
+
def handle_data(self, data):
|
94
|
+
"""This tage uses the question types to generate a survey
|
95
|
+
It constucts the edsl-specific dictionary needed to create a question
|
96
|
+
"""
|
97
|
+
# survey = Survey(name = {data.overall_question, population = data.population, description)
|
98
|
+
survey = Survey()
|
99
|
+
|
100
|
+
short_names = get_short_names(data.questions)
|
101
|
+
|
102
|
+
question_count = -1
|
103
|
+
for question, question_type, options, option_labels in zip(
|
104
|
+
data.questions, data.types, data.options, data.option_labels
|
105
|
+
):
|
106
|
+
question_count += 1
|
107
|
+
short_names_dict = {}
|
108
|
+
if question in short_names:
|
109
|
+
short_names_dict[question] = short_names[question]
|
110
|
+
data = {
|
111
|
+
"question_text": question,
|
112
|
+
"question_type": question_type,
|
113
|
+
"question_name": short_names.get(question, f"q{question_count}"),
|
114
|
+
}
|
115
|
+
if options is not None:
|
116
|
+
data["question_options"] = options
|
117
|
+
# make sure it's not a linear scale question, in which case we don't want to add short names
|
118
|
+
|
119
|
+
if option_labels is not None:
|
120
|
+
data["option_labels"] = dict(zip(options, option_labels))
|
121
|
+
# print(data["option_labels"])
|
122
|
+
# breakpoint()
|
123
|
+
|
124
|
+
if question_type == "linear_scale":
|
125
|
+
option_keys = option_labels
|
126
|
+
else:
|
127
|
+
option_keys = options
|
128
|
+
|
129
|
+
if options is not None:
|
130
|
+
short_options = get_short_options(option_keys)
|
131
|
+
short_names_dict.update(
|
132
|
+
{k: v for k, v in zip(option_keys, short_options)}
|
133
|
+
)
|
134
|
+
|
135
|
+
if question_type not in ["numerical", "free_text"]:
|
136
|
+
data["short_names_dict"] = short_names_dict
|
137
|
+
_ = data.pop("short_names_dict", None)
|
138
|
+
q = QuestionBase.from_dict(data)
|
139
|
+
survey.add_question(q)
|
140
|
+
|
141
|
+
survey.print()
|
142
|
+
return self.output(survey=survey)
|
143
|
+
|
144
|
+
|
145
|
+
if __name__ == "__main__":
|
146
|
+
# pipeline = gen_pipeline([StageQuestions, StageLabelQuestions, StageGenerateSurvey])
|
147
|
+
|
148
|
+
# results = pipeline.process(
|
149
|
+
# pipeline.input(
|
150
|
+
# overall_question="What are some factors that could determine whether someone likes ice cream?",
|
151
|
+
# population="consumers",
|
152
|
+
# )
|
153
|
+
# )
|
154
|
+
# # print(results)
|
155
|
+
# short_options = get_short_options(
|
156
|
+
# ["No, I don't own a scooter", "Yes, I own a scooter"]
|
157
|
+
# )
|
158
|
+
# print(short_options)
|
159
|
+
|
160
|
+
sample_questions = [
|
161
|
+
"What are the primary goals for your company in sponsoring a research center like the MIT IDE?",
|
162
|
+
"How does your company measure the ROI on sponsorships like this?",
|
163
|
+
"What specific aspects of the MIT IDEs work align with your companys strategic interests?",
|
164
|
+
"Can you describe the decision-making process your company uses to select research initiatives for sponsorship?",
|
165
|
+
"What are the most important factors your company considers when deciding to sponsor a research center?",
|
166
|
+
"How important is the visibility and recognition your company receives from sponsoring a research center like the MIT IDE?",
|
167
|
+
"What kind of collaborative opportunities with the MIT IDE are you looking for?",
|
168
|
+
"What are your companys expectations regarding intellectual property and the commercialization of research outcomes?",
|
169
|
+
"How does your company evaluate the success of the research projects it sponsors?",
|
170
|
+
"Would your company be interested in engaging with students or faculty at the MIT IDE for recruitment or professional development opportunities?",
|
171
|
+
"How does your company plan to leverage the research and insights gained from the MIT IDE?",
|
172
|
+
"What challenges has your company faced in previous sponsorships that you would want to avoid in the future?",
|
173
|
+
"Is there any additional support or involvement your company would like to have in the MIT IDE beyond financial sponsorship?",
|
174
|
+
"How do you see your companys role in shaping the research agenda at the MIT IDE?",
|
175
|
+
"What can the MIT IDE do to make its sponsorship opportunities more attractive to your company?",
|
176
|
+
]
|
177
|
+
|
178
|
+
short_names = get_short_names(sample_questions)
|
@@ -0,0 +1,125 @@
|
|
1
|
+
from textwrap import dedent
|
2
|
+
from dataclasses import dataclass
|
3
|
+
from collections import defaultdict
|
4
|
+
|
5
|
+
from typing import List, Dict, Union
|
6
|
+
|
7
|
+
from edsl.auto.StageBase import StageBase
|
8
|
+
from edsl.auto.StageBase import FlowDataBase
|
9
|
+
|
10
|
+
from edsl.auto.StageQuestions import StageQuestions
|
11
|
+
|
12
|
+
from edsl.questions import QuestionMultipleChoice, QuestionList
|
13
|
+
from edsl.scenarios import Scenario
|
14
|
+
from edsl import Model
|
15
|
+
from edsl.auto.utilities import gen_pipeline
|
16
|
+
|
17
|
+
|
18
|
+
question_purpose = {
|
19
|
+
"multiple_choice": "When options are known and limited",
|
20
|
+
"free_text": "When we are asking an open-ended question",
|
21
|
+
"checkbox": "When multiple options can be selected e.g., have you heard of the following products:",
|
22
|
+
"numerical": "When the answer is a single numerical value e.g., a float",
|
23
|
+
"linear_scale": "When options are text like multiple choice, but can be ordered e.g., daily, weekly, monthly, etc.",
|
24
|
+
"yes_no": "When the question can be fully answered with either a yes or a no",
|
25
|
+
}
|
26
|
+
|
27
|
+
|
28
|
+
class StageLabelQuestions(StageBase):
|
29
|
+
input = StageQuestions.output
|
30
|
+
|
31
|
+
@dataclass
|
32
|
+
class Output(FlowDataBase):
|
33
|
+
questions: List[str]
|
34
|
+
types: List[str]
|
35
|
+
options: Dict[str, List[str]]
|
36
|
+
option_labels: Dict[str, Union[List[str], None]]
|
37
|
+
|
38
|
+
output = Output
|
39
|
+
|
40
|
+
def handle_data(self, data):
|
41
|
+
"""
|
42
|
+
Labels each edsl question type. This is then used later to instantiate the questions
|
43
|
+
"""
|
44
|
+
m = Model()
|
45
|
+
label_questions_scenarios = [
|
46
|
+
Scenario({"question": q, "question_purpose": question_purpose})
|
47
|
+
for q in data.questions
|
48
|
+
]
|
49
|
+
q_type = QuestionMultipleChoice(
|
50
|
+
question_text=dedent(
|
51
|
+
"""\
|
52
|
+
Consider this question: "{{ question }}"
|
53
|
+
The question options and purpose are: {{ question_purpose }}
|
54
|
+
Please avoid free text questions much as possible.
|
55
|
+
If it could be a multiple choice, use that type.
|
56
|
+
What type of question should this be to make for an informative survey?"""
|
57
|
+
),
|
58
|
+
question_options=list(question_purpose.keys()),
|
59
|
+
question_name="question_type",
|
60
|
+
)
|
61
|
+
## If it is a linear scale, multiple choice or checkbox question, we need to know the options
|
62
|
+
option_questions = [
|
63
|
+
"multiple_choice",
|
64
|
+
"linear_scale",
|
65
|
+
"checkbox",
|
66
|
+
]
|
67
|
+
q_options_mc = QuestionList(
|
68
|
+
question_text=dedent(
|
69
|
+
"""\
|
70
|
+
Consider this question: "{{ question }}"
|
71
|
+
What options should this question have?"""
|
72
|
+
),
|
73
|
+
question_name="mc_options",
|
74
|
+
)
|
75
|
+
survey = q_type.add_question(q_options_mc).add_stop_rule(
|
76
|
+
"question_type", f"question_type not in {option_questions}"
|
77
|
+
)
|
78
|
+
type_results = survey.by(label_questions_scenarios).by(m).run()
|
79
|
+
type_results.select("question", "question_type", "mc_options").print()
|
80
|
+
|
81
|
+
# breakpoint()
|
82
|
+
|
83
|
+
question_types = type_results.select("question_type").to_list()
|
84
|
+
options = type_results.select("mc_options").to_list()
|
85
|
+
# question_types, options = type_results.select(
|
86
|
+
# "question_type", "mc_options"
|
87
|
+
# ).to_list()
|
88
|
+
|
89
|
+
type_results.select("question", "question_type", "mc_options").print()
|
90
|
+
|
91
|
+
# if the question is a yes/no question, we need to set the options to be yes/no
|
92
|
+
types_to_questions = defaultdict(list)
|
93
|
+
for question_type, question in zip(question_types, data.questions):
|
94
|
+
types_to_questions[question_type].append(question)
|
95
|
+
|
96
|
+
questions_to_options = dict(zip(data.questions, options))
|
97
|
+
question_to_option_labels = dict(
|
98
|
+
zip(data.questions, len(data.questions) * [None])
|
99
|
+
)
|
100
|
+
for question in types_to_questions.get("yes_no", []):
|
101
|
+
questions_to_options[question] = ["Yes", "No"]
|
102
|
+
|
103
|
+
for question in types_to_questions.get("linear_scale", []):
|
104
|
+
options = questions_to_options[question]
|
105
|
+
questions_to_options[question] = list(range(len(options)))
|
106
|
+
question_to_option_labels[question] = options
|
107
|
+
|
108
|
+
return self.output(
|
109
|
+
questions=data.questions,
|
110
|
+
types=question_types,
|
111
|
+
options=list(questions_to_options.values()),
|
112
|
+
option_labels=list(question_to_option_labels.values()),
|
113
|
+
)
|
114
|
+
|
115
|
+
|
116
|
+
if __name__ == "__main__":
|
117
|
+
pipeline = gen_pipeline([StageQuestions, StageLabelQuestions])
|
118
|
+
|
119
|
+
results = pipeline.process(
|
120
|
+
pipeline.input(
|
121
|
+
overall_question="What are some factors that could determine whether someone likes ice cream?"
|
122
|
+
)
|
123
|
+
)
|
124
|
+
|
125
|
+
print(results.options)
|
@@ -0,0 +1,61 @@
|
|
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
|
+
)
|
@@ -0,0 +1,88 @@
|
|
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
|
+
)
|