edsl 0.1.14__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 +46 -10
- edsl/__version__.py +1 -0
- 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 +121 -104
- 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 -204
- 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.14.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 -417
- edsl/jobs/JobsRunner.py +0 -63
- edsl/jobs/JobsRunnerStatusMixin.py +0 -115
- edsl/jobs/base.py +0 -47
- edsl/jobs/buckets.py +0 -166
- edsl/jobs/runners/JobsRunnerDryRun.py +0 -19
- edsl/jobs/runners/JobsRunnerStreaming.py +0 -54
- edsl/jobs/task_management.py +0 -218
- 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.14.dist-info/METADATA +0 -69
- edsl-0.1.14.dist-info/RECORD +0 -141
- /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.14.dist-info → edsl-0.1.40.dist-info}/LICENSE +0 -0
@@ -0,0 +1,311 @@
|
|
1
|
+
from typing import Optional, Union, Literal, TYPE_CHECKING, NewType, Callable, Any
|
2
|
+
|
3
|
+
from dataclasses import dataclass
|
4
|
+
|
5
|
+
|
6
|
+
Seconds = NewType("Seconds", float)
|
7
|
+
JobUUID = NewType("JobUUID", str)
|
8
|
+
|
9
|
+
from edsl.exceptions.coop import CoopServerResponseError
|
10
|
+
|
11
|
+
if TYPE_CHECKING:
|
12
|
+
from edsl.results.Results import Results
|
13
|
+
from edsl.jobs.Jobs import Jobs
|
14
|
+
from edsl.coop.coop import RemoteInferenceResponse, RemoteInferenceCreationInfo
|
15
|
+
from edsl.jobs.JobsRemoteInferenceLogger import JobLogger
|
16
|
+
|
17
|
+
from edsl.coop.coop import RemoteInferenceResponse, RemoteInferenceCreationInfo
|
18
|
+
|
19
|
+
from edsl.jobs.jobs_status_enums import JobsStatus
|
20
|
+
from edsl.coop.utils import VisibilityType
|
21
|
+
from edsl.jobs.JobsRemoteInferenceLogger import JobLogger
|
22
|
+
|
23
|
+
|
24
|
+
class RemoteJobConstants:
|
25
|
+
"""Constants for remote job handling."""
|
26
|
+
|
27
|
+
REMOTE_JOB_POLL_INTERVAL = 1
|
28
|
+
REMOTE_JOB_VERBOSE = False
|
29
|
+
DISCORD_URL = "https://discord.com/invite/mxAYkjfy9m"
|
30
|
+
|
31
|
+
|
32
|
+
@dataclass
|
33
|
+
class RemoteJobInfo:
|
34
|
+
creation_data: RemoteInferenceCreationInfo
|
35
|
+
job_uuid: JobUUID
|
36
|
+
logger: JobLogger
|
37
|
+
|
38
|
+
|
39
|
+
class JobsRemoteInferenceHandler:
|
40
|
+
def __init__(
|
41
|
+
self,
|
42
|
+
jobs: "Jobs",
|
43
|
+
verbose: bool = RemoteJobConstants.REMOTE_JOB_VERBOSE,
|
44
|
+
poll_interval: Seconds = RemoteJobConstants.REMOTE_JOB_POLL_INTERVAL,
|
45
|
+
):
|
46
|
+
"""Handles the creation and running of a remote inference job."""
|
47
|
+
self.jobs = jobs
|
48
|
+
self.verbose = verbose
|
49
|
+
self.poll_interval = poll_interval
|
50
|
+
|
51
|
+
from edsl.config import CONFIG
|
52
|
+
|
53
|
+
self.expected_parrot_url = CONFIG.get("EXPECTED_PARROT_URL")
|
54
|
+
self.remote_inference_url = f"{self.expected_parrot_url}/home/remote-inference"
|
55
|
+
|
56
|
+
def _create_logger(self) -> JobLogger:
|
57
|
+
from edsl.utilities.is_notebook import is_notebook
|
58
|
+
from edsl.jobs.JobsRemoteInferenceLogger import (
|
59
|
+
JupyterJobLogger,
|
60
|
+
StdOutJobLogger,
|
61
|
+
)
|
62
|
+
from edsl.jobs.loggers.HTMLTableJobLogger import HTMLTableJobLogger
|
63
|
+
|
64
|
+
if is_notebook():
|
65
|
+
return HTMLTableJobLogger(verbose=self.verbose)
|
66
|
+
return StdOutJobLogger(verbose=self.verbose)
|
67
|
+
|
68
|
+
def use_remote_inference(self, disable_remote_inference: bool) -> bool:
|
69
|
+
import requests
|
70
|
+
|
71
|
+
if disable_remote_inference:
|
72
|
+
return False
|
73
|
+
if not disable_remote_inference:
|
74
|
+
try:
|
75
|
+
from edsl.coop.coop import Coop
|
76
|
+
|
77
|
+
user_edsl_settings = Coop().edsl_settings
|
78
|
+
return user_edsl_settings.get("remote_inference", False)
|
79
|
+
except requests.ConnectionError:
|
80
|
+
pass
|
81
|
+
except CoopServerResponseError as e:
|
82
|
+
pass
|
83
|
+
|
84
|
+
return False
|
85
|
+
|
86
|
+
def create_remote_inference_job(
|
87
|
+
self,
|
88
|
+
iterations: int = 1,
|
89
|
+
remote_inference_description: Optional[str] = None,
|
90
|
+
remote_inference_results_visibility: Optional[VisibilityType] = "unlisted",
|
91
|
+
) -> RemoteJobInfo:
|
92
|
+
|
93
|
+
from edsl.config import CONFIG
|
94
|
+
from edsl.coop.coop import Coop
|
95
|
+
|
96
|
+
logger = self._create_logger()
|
97
|
+
|
98
|
+
coop = Coop()
|
99
|
+
logger.update(
|
100
|
+
"Remote inference activated. Sending job to server...",
|
101
|
+
status=JobsStatus.QUEUED,
|
102
|
+
)
|
103
|
+
remote_job_creation_data = coop.remote_inference_create(
|
104
|
+
self.jobs,
|
105
|
+
description=remote_inference_description,
|
106
|
+
status="queued",
|
107
|
+
iterations=iterations,
|
108
|
+
initial_results_visibility=remote_inference_results_visibility,
|
109
|
+
)
|
110
|
+
logger.update(
|
111
|
+
"Your survey is running at the Expected Parrot server...",
|
112
|
+
status=JobsStatus.RUNNING,
|
113
|
+
)
|
114
|
+
job_uuid = remote_job_creation_data.get("uuid")
|
115
|
+
logger.update(
|
116
|
+
message=f"Job sent to server. (Job uuid={job_uuid}).",
|
117
|
+
status=JobsStatus.RUNNING,
|
118
|
+
)
|
119
|
+
logger.add_info("job_uuid", job_uuid)
|
120
|
+
|
121
|
+
logger.update(
|
122
|
+
f"Job details are available at your Coop account {self.remote_inference_url}",
|
123
|
+
status=JobsStatus.RUNNING,
|
124
|
+
)
|
125
|
+
progress_bar_url = (
|
126
|
+
f"{self.expected_parrot_url}/home/remote-job-progress/{job_uuid}"
|
127
|
+
)
|
128
|
+
logger.add_info("progress_bar_url", progress_bar_url)
|
129
|
+
logger.update(
|
130
|
+
f"View job progress here: {progress_bar_url}", status=JobsStatus.RUNNING
|
131
|
+
)
|
132
|
+
|
133
|
+
return RemoteJobInfo(
|
134
|
+
creation_data=remote_job_creation_data,
|
135
|
+
job_uuid=job_uuid,
|
136
|
+
logger=logger,
|
137
|
+
)
|
138
|
+
|
139
|
+
@staticmethod
|
140
|
+
def check_status(
|
141
|
+
job_uuid: JobUUID,
|
142
|
+
) -> RemoteInferenceResponse:
|
143
|
+
from edsl.coop.coop import Coop
|
144
|
+
|
145
|
+
coop = Coop()
|
146
|
+
return coop.remote_inference_get(job_uuid)
|
147
|
+
|
148
|
+
def _construct_remote_job_fetcher(
|
149
|
+
self, testing_simulated_response: Optional[Any] = None
|
150
|
+
) -> Callable:
|
151
|
+
if testing_simulated_response is not None:
|
152
|
+
return lambda job_uuid: testing_simulated_response
|
153
|
+
else:
|
154
|
+
from edsl.coop.coop import Coop
|
155
|
+
|
156
|
+
coop = Coop()
|
157
|
+
return coop.remote_inference_get
|
158
|
+
|
159
|
+
def _construct_object_fetcher(
|
160
|
+
self, testing_simulated_response: Optional[Any] = None
|
161
|
+
) -> Callable:
|
162
|
+
"Constructs a function to fetch the results object from Coop."
|
163
|
+
if testing_simulated_response is not None:
|
164
|
+
return lambda results_uuid, expected_object_type: Results.example()
|
165
|
+
else:
|
166
|
+
from edsl.coop.coop import Coop
|
167
|
+
|
168
|
+
coop = Coop()
|
169
|
+
return coop.get
|
170
|
+
|
171
|
+
def _handle_cancelled_job(self, job_info: RemoteJobInfo) -> None:
|
172
|
+
"Handles a cancelled job by logging the cancellation and updating the job status."
|
173
|
+
|
174
|
+
job_info.logger.update(
|
175
|
+
message="Job cancelled by the user.", status=JobsStatus.CANCELLED
|
176
|
+
)
|
177
|
+
job_info.logger.update(
|
178
|
+
f"See {self.expected_parrot_url}/home/remote-inference for more details.",
|
179
|
+
status=JobsStatus.CANCELLED,
|
180
|
+
)
|
181
|
+
|
182
|
+
def _handle_failed_job(
|
183
|
+
self, job_info: RemoteJobInfo, remote_job_data: RemoteInferenceResponse
|
184
|
+
) -> None:
|
185
|
+
"Handles a failed job by logging the error and updating the job status."
|
186
|
+
latest_error_report_url = remote_job_data.get("latest_error_report_url")
|
187
|
+
if latest_error_report_url:
|
188
|
+
job_info.logger.add_info("error_report_url", latest_error_report_url)
|
189
|
+
|
190
|
+
job_info.logger.update("Job failed.", status=JobsStatus.FAILED)
|
191
|
+
job_info.logger.update(
|
192
|
+
f"See {self.expected_parrot_url}/home/remote-inference for more details.",
|
193
|
+
status=JobsStatus.FAILED,
|
194
|
+
)
|
195
|
+
job_info.logger.update(
|
196
|
+
f"Need support? Visit Discord: {RemoteJobConstants.DISCORD_URL}",
|
197
|
+
status=JobsStatus.FAILED,
|
198
|
+
)
|
199
|
+
|
200
|
+
def _sleep_for_a_bit(self, job_info: RemoteJobInfo, status: str) -> None:
|
201
|
+
import time
|
202
|
+
from datetime import datetime
|
203
|
+
|
204
|
+
time_checked = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
|
205
|
+
job_info.logger.update(
|
206
|
+
f"Job status: {status} - last update: {time_checked}",
|
207
|
+
status=JobsStatus.RUNNING,
|
208
|
+
)
|
209
|
+
time.sleep(self.poll_interval)
|
210
|
+
|
211
|
+
def _fetch_results_and_log(
|
212
|
+
self,
|
213
|
+
job_info: RemoteJobInfo,
|
214
|
+
results_uuid: str,
|
215
|
+
remote_job_data: RemoteInferenceResponse,
|
216
|
+
object_fetcher: Callable,
|
217
|
+
) -> "Results":
|
218
|
+
"Fetches the results object and logs the results URL."
|
219
|
+
job_info.logger.add_info("results_uuid", results_uuid)
|
220
|
+
results = object_fetcher(results_uuid, expected_object_type="results")
|
221
|
+
results_url = remote_job_data.get("results_url")
|
222
|
+
job_info.logger.update(
|
223
|
+
f"Job completed and Results stored on Coop: {results_url}",
|
224
|
+
status=JobsStatus.COMPLETED,
|
225
|
+
)
|
226
|
+
results.job_uuid = job_info.job_uuid
|
227
|
+
results.results_uuid = results_uuid
|
228
|
+
return results
|
229
|
+
|
230
|
+
def poll_remote_inference_job(
|
231
|
+
self,
|
232
|
+
job_info: RemoteJobInfo,
|
233
|
+
testing_simulated_response=None,
|
234
|
+
) -> Union[None, "Results"]:
|
235
|
+
"""Polls a remote inference job for completion and returns the results."""
|
236
|
+
|
237
|
+
remote_job_data_fetcher = self._construct_remote_job_fetcher(
|
238
|
+
testing_simulated_response
|
239
|
+
)
|
240
|
+
object_fetcher = self._construct_object_fetcher(testing_simulated_response)
|
241
|
+
|
242
|
+
job_in_queue = True
|
243
|
+
while job_in_queue:
|
244
|
+
remote_job_data = remote_job_data_fetcher(job_info.job_uuid)
|
245
|
+
status = remote_job_data.get("status")
|
246
|
+
|
247
|
+
if status == "cancelled":
|
248
|
+
self._handle_cancelled_job(job_info)
|
249
|
+
return None
|
250
|
+
|
251
|
+
elif status == "failed" or status == "completed":
|
252
|
+
if status == "failed":
|
253
|
+
self._handle_failed_job(job_info, remote_job_data)
|
254
|
+
|
255
|
+
results_uuid = remote_job_data.get("results_uuid")
|
256
|
+
if results_uuid:
|
257
|
+
results = self._fetch_results_and_log(
|
258
|
+
job_info=job_info,
|
259
|
+
results_uuid=results_uuid,
|
260
|
+
remote_job_data=remote_job_data,
|
261
|
+
object_fetcher=object_fetcher,
|
262
|
+
)
|
263
|
+
return results
|
264
|
+
else:
|
265
|
+
return None
|
266
|
+
|
267
|
+
else:
|
268
|
+
self._sleep_for_a_bit(job_info, status)
|
269
|
+
|
270
|
+
async def create_and_poll_remote_job(
|
271
|
+
self,
|
272
|
+
iterations: int = 1,
|
273
|
+
remote_inference_description: Optional[str] = None,
|
274
|
+
remote_inference_results_visibility: Optional[VisibilityType] = "unlisted",
|
275
|
+
) -> Union["Results", None]:
|
276
|
+
"""
|
277
|
+
Creates and polls a remote inference job asynchronously.
|
278
|
+
Reuses existing synchronous methods but runs them in an async context.
|
279
|
+
|
280
|
+
:param iterations: Number of times to run each interview
|
281
|
+
:param remote_inference_description: Optional description for the remote job
|
282
|
+
:param remote_inference_results_visibility: Visibility setting for results
|
283
|
+
:return: Results object if successful, None if job fails or is cancelled
|
284
|
+
"""
|
285
|
+
import asyncio
|
286
|
+
from functools import partial
|
287
|
+
|
288
|
+
# Create job using existing method
|
289
|
+
loop = asyncio.get_event_loop()
|
290
|
+
job_info = await loop.run_in_executor(
|
291
|
+
None,
|
292
|
+
partial(
|
293
|
+
self.create_remote_inference_job,
|
294
|
+
iterations=iterations,
|
295
|
+
remote_inference_description=remote_inference_description,
|
296
|
+
remote_inference_results_visibility=remote_inference_results_visibility,
|
297
|
+
),
|
298
|
+
)
|
299
|
+
if job_info is None:
|
300
|
+
raise ValueError("Remote job creation failed.")
|
301
|
+
|
302
|
+
return await loop.run_in_executor(
|
303
|
+
None,
|
304
|
+
partial(self.poll_remote_inference_job, job_info),
|
305
|
+
)
|
306
|
+
|
307
|
+
|
308
|
+
if __name__ == "__main__":
|
309
|
+
import doctest
|
310
|
+
|
311
|
+
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
@@ -0,0 +1,239 @@
|
|
1
|
+
import re
|
2
|
+
import sys
|
3
|
+
import uuid
|
4
|
+
from abc import ABC, abstractmethod
|
5
|
+
from typing import Optional, Union, Literal, TYPE_CHECKING, List, Dict
|
6
|
+
from datetime import datetime
|
7
|
+
from dataclasses import dataclass
|
8
|
+
from edsl.exceptions.coop import CoopServerResponseError
|
9
|
+
|
10
|
+
from edsl.jobs.jobs_status_enums import JobsStatus
|
11
|
+
|
12
|
+
if TYPE_CHECKING:
|
13
|
+
from edsl.results.Results import Results
|
14
|
+
|
15
|
+
|
16
|
+
@dataclass
|
17
|
+
class LogMessage:
|
18
|
+
text: str
|
19
|
+
status: str
|
20
|
+
timestamp: datetime
|
21
|
+
status: JobsStatus
|
22
|
+
|
23
|
+
|
24
|
+
@dataclass
|
25
|
+
class JobsInfo:
|
26
|
+
job_uuid: str = None
|
27
|
+
progress_bar_url: str = None
|
28
|
+
error_report_url: str = None
|
29
|
+
results_uuid: str = None
|
30
|
+
results_url: str = None
|
31
|
+
|
32
|
+
pretty_names = {
|
33
|
+
"job_uuid": "Job UUID",
|
34
|
+
"progress_bar_url": "Progress Bar URL",
|
35
|
+
"error_report_url": "Error Report URL",
|
36
|
+
"results_uuid": "Results UUID",
|
37
|
+
"results_url": "Results URL",
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
class JobLogger(ABC):
|
42
|
+
def __init__(self, verbose: bool = False):
|
43
|
+
self.verbose = verbose
|
44
|
+
self.jobs_info = JobsInfo()
|
45
|
+
|
46
|
+
def add_info(
|
47
|
+
self,
|
48
|
+
information_type: Literal[
|
49
|
+
"job_uuid",
|
50
|
+
"progress_bar_url",
|
51
|
+
"error_report_url",
|
52
|
+
"results_uuid",
|
53
|
+
"results_url",
|
54
|
+
],
|
55
|
+
value: str,
|
56
|
+
):
|
57
|
+
"""Add information to the logger
|
58
|
+
|
59
|
+
>>> j = StdOutJobLogger()
|
60
|
+
>>> j.add_info("job_uuid", "1234")
|
61
|
+
>>> j.jobs_info.job_uuid
|
62
|
+
'1234'
|
63
|
+
"""
|
64
|
+
if information_type not in self.jobs_info.__annotations__:
|
65
|
+
raise ValueError(f"Information type {information_type} not supported")
|
66
|
+
setattr(self.jobs_info, information_type, value)
|
67
|
+
|
68
|
+
@abstractmethod
|
69
|
+
def update(self, message: str, status: str = "running"):
|
70
|
+
pass
|
71
|
+
|
72
|
+
|
73
|
+
class HTMLTableJobLogger(JobLogger):
|
74
|
+
def __init__(self, verbose=True, **kwargs):
|
75
|
+
from IPython.display import display, HTML
|
76
|
+
|
77
|
+
super().__init__(verbose=verbose)
|
78
|
+
self.display_handle = display(HTML(""), display_id=True)
|
79
|
+
self.current_message = None
|
80
|
+
self.log_id = str(uuid.uuid4())
|
81
|
+
self.is_expanded = True
|
82
|
+
self.spinner_chars = ["◐", "◓", "◑", "◒"] # Rotating spinner characters
|
83
|
+
self.spinner_idx = 0
|
84
|
+
|
85
|
+
def _get_table_row(self, key: str, value: str) -> str:
|
86
|
+
"""Generate a table row with key-value pair"""
|
87
|
+
return f"""
|
88
|
+
<tr>
|
89
|
+
<td style="padding: 8px; border: 1px solid #ddd; font-weight: bold;">{key}</td>
|
90
|
+
<td style="padding: 8px; border: 1px solid #ddd;">{value if value else 'None'}</td>
|
91
|
+
</tr>
|
92
|
+
"""
|
93
|
+
|
94
|
+
def _linkify(self, text: str) -> str:
|
95
|
+
"""Convert URLs in text to clickable links"""
|
96
|
+
url_pattern = r'(https?://[^\s<>"]+|www\.[^\s<>"]+)'
|
97
|
+
return re.sub(
|
98
|
+
url_pattern,
|
99
|
+
r'<a href="\1" target="_blank" style="color: #3b82f6; text-decoration: underline;">\1</a>',
|
100
|
+
text,
|
101
|
+
)
|
102
|
+
|
103
|
+
def _get_spinner(self, status: JobsStatus) -> str:
|
104
|
+
"""Get the current spinner frame if status is running"""
|
105
|
+
if status == JobsStatus.RUNNING:
|
106
|
+
spinner = self.spinner_chars[self.spinner_idx]
|
107
|
+
self.spinner_idx = (self.spinner_idx + 1) % len(self.spinner_chars)
|
108
|
+
return f'<span style="margin-right: 8px;">{spinner}</span>'
|
109
|
+
elif status == JobsStatus.COMPLETED:
|
110
|
+
return '<span style="margin-right: 8px; color: #22c55e;">✓</span>'
|
111
|
+
elif status == JobsStatus.FAILED:
|
112
|
+
return '<span style="margin-right: 8px; color: #ef4444;">✗</span>'
|
113
|
+
return ""
|
114
|
+
|
115
|
+
def _get_html(self, status: JobsStatus = JobsStatus.RUNNING) -> str:
|
116
|
+
"""Generate the complete HTML display"""
|
117
|
+
# Generate table rows for each JobsInfo field
|
118
|
+
info_rows = ""
|
119
|
+
for field, _ in self.jobs_info.__annotations__.items():
|
120
|
+
if field != "pretty_names": # Skip the pretty_names dictionary
|
121
|
+
value = getattr(self.jobs_info, field)
|
122
|
+
value = self._linkify(str(value)) if value else None
|
123
|
+
pretty_name = self.jobs_info.pretty_names.get(
|
124
|
+
field, field.replace("_", " ").title()
|
125
|
+
)
|
126
|
+
info_rows += self._get_table_row(pretty_name, value)
|
127
|
+
|
128
|
+
# Add current message section with spinner
|
129
|
+
message_html = ""
|
130
|
+
if self.current_message:
|
131
|
+
spinner = self._get_spinner(status)
|
132
|
+
message_html = f"""
|
133
|
+
<div style="margin-top: 10px; padding: 8px; background-color: #f8f9fa; border: 1px solid #ddd; border-radius: 4px;">
|
134
|
+
{spinner}<strong>Current Status:</strong> {self._linkify(self.current_message)}
|
135
|
+
</div>
|
136
|
+
"""
|
137
|
+
|
138
|
+
display_style = "block" if self.is_expanded else "none"
|
139
|
+
arrow = "▼" if self.is_expanded else "▶"
|
140
|
+
|
141
|
+
return f"""
|
142
|
+
<div style="font-family: system-ui; max-width: 800px; margin: 10px 0;">
|
143
|
+
<div onclick="document.getElementById('content-{self.log_id}').style.display = document.getElementById('content-{self.log_id}').style.display === 'none' ? 'block' : 'none';
|
144
|
+
document.getElementById('arrow-{self.log_id}').innerHTML = document.getElementById('content-{self.log_id}').style.display === 'none' ? '▶' : '▼';"
|
145
|
+
style="padding: 10px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 4px; cursor: pointer;">
|
146
|
+
<span id="arrow-{self.log_id}">{arrow}</span> Job Status ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})
|
147
|
+
</div>
|
148
|
+
<div id="content-{self.log_id}" style="display: {display_style};">
|
149
|
+
<table style="width: 100%; border-collapse: collapse; background: white; border: 1px solid #ddd;">
|
150
|
+
{info_rows}
|
151
|
+
</table>
|
152
|
+
{message_html}
|
153
|
+
</div>
|
154
|
+
</div>
|
155
|
+
"""
|
156
|
+
|
157
|
+
def update(self, message: str, status: JobsStatus = JobsStatus.RUNNING):
|
158
|
+
"""Update the display with new message and current JobsInfo state"""
|
159
|
+
from IPython.display import HTML
|
160
|
+
|
161
|
+
self.current_message = message
|
162
|
+
if self.verbose:
|
163
|
+
self.display_handle.update(HTML(self._get_html(status)))
|
164
|
+
else:
|
165
|
+
return None
|
166
|
+
|
167
|
+
|
168
|
+
class StdOutJobLogger(JobLogger):
|
169
|
+
def __init__(self, verbose=True, **kwargs):
|
170
|
+
super().__init__(verbose=verbose) # Properly call parent's __init__
|
171
|
+
self.messages: List[LogMessage] = []
|
172
|
+
|
173
|
+
def update(self, message: str, status: JobsStatus = JobsStatus.RUNNING):
|
174
|
+
log_msg = LogMessage(text=message, status=status, timestamp=datetime.now())
|
175
|
+
self.messages.append(log_msg)
|
176
|
+
if self.verbose:
|
177
|
+
sys.stdout.write(f"│ {message}\n")
|
178
|
+
sys.stdout.flush()
|
179
|
+
else:
|
180
|
+
return None
|
181
|
+
|
182
|
+
|
183
|
+
class JupyterJobLogger(JobLogger):
|
184
|
+
def __init__(self, verbose=True, **kwargs):
|
185
|
+
from IPython.display import display, HTML
|
186
|
+
|
187
|
+
super().__init__(verbose=verbose)
|
188
|
+
self.messages = []
|
189
|
+
self.log_id = str(uuid.uuid4())
|
190
|
+
self.is_expanded = True
|
191
|
+
self.display_handle = display(HTML(""), display_id=True)
|
192
|
+
|
193
|
+
def _linkify(self, text):
|
194
|
+
url_pattern = r'(https?://[^\s<>"]+|www\.[^\s<>"]+)'
|
195
|
+
return re.sub(
|
196
|
+
url_pattern,
|
197
|
+
r'<a href="\1" target="_blank" style="color: #3b82f6; text-decoration: underline;">\1</a>',
|
198
|
+
text,
|
199
|
+
)
|
200
|
+
|
201
|
+
def _get_html(self):
|
202
|
+
messages_html = "\n".join(
|
203
|
+
[
|
204
|
+
f'<div style="border-left: 3px solid {msg["color"]}; padding: 5px 10px; margin: 5px 0;">{self._linkify(msg["text"])}</div>'
|
205
|
+
for msg in self.messages
|
206
|
+
]
|
207
|
+
)
|
208
|
+
|
209
|
+
display_style = "block" if self.is_expanded else "none"
|
210
|
+
arrow = "▼" if self.is_expanded else "▶"
|
211
|
+
|
212
|
+
return f"""
|
213
|
+
<div style="border: 1px solid #ccc; margin: 10px 0; max-width: 800px;">
|
214
|
+
<div onclick="document.getElementById('content-{self.log_id}').style.display = document.getElementById('content-{self.log_id}').style.display === 'none' ? 'block' : 'none';
|
215
|
+
document.getElementById('arrow-{self.log_id}').innerHTML = document.getElementById('content-{self.log_id}').style.display === 'none' ? '▶' : '▼';"
|
216
|
+
style="padding: 10px; background: #f5f5f5; cursor: pointer;">
|
217
|
+
<span id="arrow-{self.log_id}">{arrow}</span> Remote Job Log ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})
|
218
|
+
</div>
|
219
|
+
<div id="content-{self.log_id}" style="padding: 10px; display: {display_style};">
|
220
|
+
{messages_html}
|
221
|
+
</div>
|
222
|
+
</div>
|
223
|
+
"""
|
224
|
+
|
225
|
+
def update(self, message, status: JobsStatus = JobsStatus.RUNNING):
|
226
|
+
from IPython.display import HTML
|
227
|
+
|
228
|
+
colors = {"running": "#3b82f6", "completed": "#22c55e", "failed": "#ef4444"}
|
229
|
+
self.messages.append({"text": message, "color": colors.get(status, "#666")})
|
230
|
+
if self.verbose:
|
231
|
+
self.display_handle.update(HTML(self._get_html()))
|
232
|
+
else:
|
233
|
+
return None
|
234
|
+
|
235
|
+
|
236
|
+
if __name__ == "__main__":
|
237
|
+
import doctest
|
238
|
+
|
239
|
+
doctest.testmod()
|
@@ -0,0 +1,30 @@
|
|
1
|
+
from edsl.jobs.FetchInvigilator import FetchInvigilator
|
2
|
+
|
3
|
+
|
4
|
+
class RequestTokenEstimator:
|
5
|
+
"""Estimate the number of tokens that will be required to run the focal task."""
|
6
|
+
|
7
|
+
def __init__(self, interview):
|
8
|
+
self.interview = interview
|
9
|
+
|
10
|
+
def __call__(self, question) -> float:
|
11
|
+
"""Estimate the number of tokens that will be required to run the focal task."""
|
12
|
+
from edsl.scenarios.FileStore import FileStore
|
13
|
+
|
14
|
+
invigilator = FetchInvigilator(self.interview)(question=question)
|
15
|
+
|
16
|
+
# TODO: There should be a way to get a more accurate estimate.
|
17
|
+
combined_text = ""
|
18
|
+
file_tokens = 0
|
19
|
+
for prompt in invigilator.get_prompts().values():
|
20
|
+
if hasattr(prompt, "text"):
|
21
|
+
combined_text += prompt.text
|
22
|
+
elif isinstance(prompt, str):
|
23
|
+
combined_text += prompt
|
24
|
+
elif isinstance(prompt, list):
|
25
|
+
for file in prompt:
|
26
|
+
if isinstance(file, FileStore):
|
27
|
+
file_tokens += file.size * 0.25
|
28
|
+
else:
|
29
|
+
raise ValueError(f"Prompt is of type {type(prompt)}")
|
30
|
+
return len(combined_text) / 4.0 + file_tokens
|