edsl 0.1.33.dev1__py3-none-any.whl → 0.1.33.dev2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. edsl/TemplateLoader.py +24 -0
  2. edsl/__init__.py +8 -4
  3. edsl/agents/Agent.py +46 -14
  4. edsl/agents/AgentList.py +43 -0
  5. edsl/agents/Invigilator.py +125 -212
  6. edsl/agents/InvigilatorBase.py +140 -32
  7. edsl/agents/PromptConstructionMixin.py +43 -66
  8. edsl/agents/__init__.py +1 -0
  9. edsl/auto/AutoStudy.py +117 -0
  10. edsl/auto/StageBase.py +230 -0
  11. edsl/auto/StageGenerateSurvey.py +178 -0
  12. edsl/auto/StageLabelQuestions.py +125 -0
  13. edsl/auto/StagePersona.py +61 -0
  14. edsl/auto/StagePersonaDimensionValueRanges.py +88 -0
  15. edsl/auto/StagePersonaDimensionValues.py +74 -0
  16. edsl/auto/StagePersonaDimensions.py +69 -0
  17. edsl/auto/StageQuestions.py +73 -0
  18. edsl/auto/SurveyCreatorPipeline.py +21 -0
  19. edsl/auto/utilities.py +224 -0
  20. edsl/config.py +38 -39
  21. edsl/coop/PriceFetcher.py +58 -0
  22. edsl/coop/coop.py +39 -5
  23. edsl/data/Cache.py +35 -1
  24. edsl/data_transfer_models.py +120 -38
  25. edsl/enums.py +2 -0
  26. edsl/exceptions/language_models.py +25 -1
  27. edsl/exceptions/questions.py +62 -5
  28. edsl/exceptions/results.py +4 -0
  29. edsl/inference_services/AnthropicService.py +13 -11
  30. edsl/inference_services/AwsBedrock.py +19 -17
  31. edsl/inference_services/AzureAI.py +37 -20
  32. edsl/inference_services/GoogleService.py +16 -12
  33. edsl/inference_services/GroqService.py +2 -0
  34. edsl/inference_services/InferenceServiceABC.py +24 -0
  35. edsl/inference_services/MistralAIService.py +120 -0
  36. edsl/inference_services/OpenAIService.py +41 -50
  37. edsl/inference_services/TestService.py +71 -0
  38. edsl/inference_services/models_available_cache.py +0 -6
  39. edsl/inference_services/registry.py +4 -0
  40. edsl/jobs/Answers.py +10 -12
  41. edsl/jobs/FailedQuestion.py +78 -0
  42. edsl/jobs/Jobs.py +18 -13
  43. edsl/jobs/buckets/TokenBucket.py +39 -14
  44. edsl/jobs/interviews/Interview.py +297 -77
  45. edsl/jobs/interviews/InterviewExceptionEntry.py +83 -19
  46. edsl/jobs/interviews/interview_exception_tracking.py +0 -70
  47. edsl/jobs/interviews/retry_management.py +3 -1
  48. edsl/jobs/runners/JobsRunnerAsyncio.py +116 -70
  49. edsl/jobs/runners/JobsRunnerStatusMixin.py +1 -1
  50. edsl/jobs/tasks/QuestionTaskCreator.py +30 -23
  51. edsl/jobs/tasks/TaskHistory.py +131 -213
  52. edsl/language_models/LanguageModel.py +239 -129
  53. edsl/language_models/ModelList.py +2 -2
  54. edsl/language_models/RegisterLanguageModelsMeta.py +14 -29
  55. edsl/language_models/fake_openai_call.py +15 -0
  56. edsl/language_models/fake_openai_service.py +61 -0
  57. edsl/language_models/registry.py +15 -2
  58. edsl/language_models/repair.py +0 -19
  59. edsl/language_models/utilities.py +61 -0
  60. edsl/prompts/Prompt.py +52 -2
  61. edsl/questions/AnswerValidatorMixin.py +23 -26
  62. edsl/questions/QuestionBase.py +273 -242
  63. edsl/questions/QuestionBaseGenMixin.py +133 -0
  64. edsl/questions/QuestionBasePromptsMixin.py +266 -0
  65. edsl/questions/QuestionBudget.py +6 -0
  66. edsl/questions/QuestionCheckBox.py +227 -35
  67. edsl/questions/QuestionExtract.py +98 -27
  68. edsl/questions/QuestionFreeText.py +46 -29
  69. edsl/questions/QuestionFunctional.py +7 -0
  70. edsl/questions/QuestionList.py +141 -22
  71. edsl/questions/QuestionMultipleChoice.py +173 -64
  72. edsl/questions/QuestionNumerical.py +87 -46
  73. edsl/questions/QuestionRank.py +182 -24
  74. edsl/questions/RegisterQuestionsMeta.py +31 -12
  75. edsl/questions/ResponseValidatorABC.py +169 -0
  76. edsl/questions/__init__.py +3 -4
  77. edsl/questions/decorators.py +21 -0
  78. edsl/questions/derived/QuestionLikertFive.py +10 -5
  79. edsl/questions/derived/QuestionLinearScale.py +11 -1
  80. edsl/questions/derived/QuestionTopK.py +6 -0
  81. edsl/questions/derived/QuestionYesNo.py +16 -1
  82. edsl/questions/descriptors.py +43 -7
  83. edsl/questions/prompt_templates/question_budget.jinja +13 -0
  84. edsl/questions/prompt_templates/question_checkbox.jinja +32 -0
  85. edsl/questions/prompt_templates/question_extract.jinja +11 -0
  86. edsl/questions/prompt_templates/question_free_text.jinja +3 -0
  87. edsl/questions/prompt_templates/question_linear_scale.jinja +11 -0
  88. edsl/questions/prompt_templates/question_list.jinja +17 -0
  89. edsl/questions/prompt_templates/question_multiple_choice.jinja +33 -0
  90. edsl/questions/prompt_templates/question_numerical.jinja +37 -0
  91. edsl/questions/question_registry.py +6 -2
  92. edsl/questions/templates/__init__.py +0 -0
  93. edsl/questions/templates/checkbox/__init__.py +0 -0
  94. edsl/questions/templates/checkbox/answering_instructions.jinja +10 -0
  95. edsl/questions/templates/checkbox/question_presentation.jinja +22 -0
  96. edsl/questions/templates/extract/answering_instructions.jinja +7 -0
  97. edsl/questions/templates/extract/question_presentation.jinja +1 -0
  98. edsl/questions/templates/free_text/__init__.py +0 -0
  99. edsl/questions/templates/free_text/answering_instructions.jinja +0 -0
  100. edsl/questions/templates/free_text/question_presentation.jinja +1 -0
  101. edsl/questions/templates/likert_five/__init__.py +0 -0
  102. edsl/questions/templates/likert_five/answering_instructions.jinja +10 -0
  103. edsl/questions/templates/likert_five/question_presentation.jinja +12 -0
  104. edsl/questions/templates/linear_scale/__init__.py +0 -0
  105. edsl/questions/templates/linear_scale/answering_instructions.jinja +5 -0
  106. edsl/questions/templates/linear_scale/question_presentation.jinja +5 -0
  107. edsl/questions/templates/list/__init__.py +0 -0
  108. edsl/questions/templates/list/answering_instructions.jinja +4 -0
  109. edsl/questions/templates/list/question_presentation.jinja +5 -0
  110. edsl/questions/templates/multiple_choice/__init__.py +0 -0
  111. edsl/questions/templates/multiple_choice/answering_instructions.jinja +9 -0
  112. edsl/questions/templates/multiple_choice/html.jinja +0 -0
  113. edsl/questions/templates/multiple_choice/question_presentation.jinja +12 -0
  114. edsl/questions/templates/numerical/__init__.py +0 -0
  115. edsl/questions/templates/numerical/answering_instructions.jinja +8 -0
  116. edsl/questions/templates/numerical/question_presentation.jinja +7 -0
  117. edsl/questions/templates/rank/answering_instructions.jinja +11 -0
  118. edsl/questions/templates/rank/question_presentation.jinja +15 -0
  119. edsl/questions/templates/top_k/__init__.py +0 -0
  120. edsl/questions/templates/top_k/answering_instructions.jinja +8 -0
  121. edsl/questions/templates/top_k/question_presentation.jinja +22 -0
  122. edsl/questions/templates/yes_no/__init__.py +0 -0
  123. edsl/questions/templates/yes_no/answering_instructions.jinja +6 -0
  124. edsl/questions/templates/yes_no/question_presentation.jinja +12 -0
  125. edsl/results/Dataset.py +20 -0
  126. edsl/results/DatasetExportMixin.py +41 -47
  127. edsl/results/DatasetTree.py +145 -0
  128. edsl/results/Result.py +32 -5
  129. edsl/results/Results.py +131 -45
  130. edsl/results/ResultsDBMixin.py +3 -3
  131. edsl/results/Selector.py +118 -0
  132. edsl/results/tree_explore.py +115 -0
  133. edsl/scenarios/Scenario.py +10 -4
  134. edsl/scenarios/ScenarioList.py +348 -39
  135. edsl/scenarios/ScenarioListExportMixin.py +9 -0
  136. edsl/study/SnapShot.py +8 -1
  137. edsl/surveys/RuleCollection.py +2 -2
  138. edsl/surveys/Survey.py +634 -315
  139. edsl/surveys/SurveyExportMixin.py +71 -9
  140. edsl/surveys/SurveyFlowVisualizationMixin.py +2 -1
  141. edsl/surveys/SurveyQualtricsImport.py +75 -4
  142. edsl/surveys/instructions/ChangeInstruction.py +47 -0
  143. edsl/surveys/instructions/Instruction.py +34 -0
  144. edsl/surveys/instructions/InstructionCollection.py +77 -0
  145. edsl/surveys/instructions/__init__.py +0 -0
  146. edsl/templates/error_reporting/base.html +24 -0
  147. edsl/templates/error_reporting/exceptions_by_model.html +35 -0
  148. edsl/templates/error_reporting/exceptions_by_question_name.html +17 -0
  149. edsl/templates/error_reporting/exceptions_by_type.html +17 -0
  150. edsl/templates/error_reporting/interview_details.html +111 -0
  151. edsl/templates/error_reporting/interviews.html +10 -0
  152. edsl/templates/error_reporting/overview.html +5 -0
  153. edsl/templates/error_reporting/performance_plot.html +2 -0
  154. edsl/templates/error_reporting/report.css +74 -0
  155. edsl/templates/error_reporting/report.html +118 -0
  156. edsl/templates/error_reporting/report.js +25 -0
  157. {edsl-0.1.33.dev1.dist-info → edsl-0.1.33.dev2.dist-info}/METADATA +4 -2
  158. edsl-0.1.33.dev2.dist-info/RECORD +289 -0
  159. edsl/jobs/interviews/InterviewTaskBuildingMixin.py +0 -286
  160. edsl/utilities/gcp_bucket/simple_example.py +0 -9
  161. edsl-0.1.33.dev1.dist-info/RECORD +0 -209
  162. {edsl-0.1.33.dev1.dist-info → edsl-0.1.33.dev2.dist-info}/LICENSE +0 -0
  163. {edsl-0.1.33.dev1.dist-info → edsl-0.1.33.dev2.dist-info}/WHEEL +0 -0
@@ -1,5 +1,11 @@
1
1
  import textwrap
2
2
  from random import random
3
+ from edsl.config import CONFIG
4
+
5
+ if "EDSL_DEFAULT_MODEL" not in CONFIG:
6
+ default_model = "test"
7
+ else:
8
+ default_model = CONFIG.get("EDSL_DEFAULT_MODEL")
3
9
 
4
10
 
5
11
  def get_model_class(model_name, registry=None):
@@ -27,7 +33,7 @@ class Meta(type):
27
33
 
28
34
 
29
35
  class Model(metaclass=Meta):
30
- default_model = "gpt-4-1106-preview"
36
+ default_model = default_model
31
37
 
32
38
  def __new__(cls, model_name=None, registry=None, *args, **kwargs):
33
39
  # Map index to the respective subclass
@@ -58,11 +64,18 @@ class Model(metaclass=Meta):
58
64
  return [r._inference_service_ for r in registry.services]
59
65
 
60
66
  @classmethod
61
- def available(cls, search_term=None, name_only=False, registry=None):
67
+ def available(cls, search_term=None, name_only=False, registry=None, service=None):
62
68
  from edsl.inference_services.registry import default
63
69
 
64
70
  registry = registry or default
65
71
  full_list = registry.available()
72
+
73
+ if service is not None:
74
+ if service not in cls.services(registry=registry):
75
+ raise ValueError(f"Service {service} not found in available services.")
76
+
77
+ full_list = [m for m in full_list if m[1] == service]
78
+
66
79
  if search_term is None:
67
80
  if name_only:
68
81
  return [m[0] for m in full_list]
@@ -141,25 +141,6 @@ def repair(
141
141
  return repair_wrapper(bad_json, error_message, user_prompt, system_prompt, cache)
142
142
 
143
143
 
144
- # Example usage:
145
- # result, success = repair_wrapper('{"name": "John Doe", "age": 30,}') # example bad JSON
146
-
147
-
148
- # def repair_wrapper(bad_json, error_message=""):
149
- # loop = asyncio.get_event_loop()
150
- # if loop.is_running():
151
- # # Add repair as a task to the running loop
152
- # task = loop.create_task(repair(bad_json, error_message))
153
- # return task
154
- # else:
155
- # # Run a new event loop for repair
156
- # return loop.run_until_complete(repair(bad_json, error_message))
157
-
158
-
159
- # Example usage:
160
- # result, success = repair_wrapper('{"name": "John Doe", "age": 30,}') # example bad JSON
161
-
162
-
163
144
  if __name__ == "__main__":
164
145
  bad_json = """
165
146
  {
@@ -0,0 +1,61 @@
1
+ import asyncio
2
+ from typing import Any
3
+ from edsl import Survey
4
+ from edsl.config import CONFIG
5
+ from edsl.enums import InferenceServiceType
6
+ from edsl.language_models.LanguageModel import LanguageModel
7
+ from edsl.questions import QuestionFreeText
8
+
9
+
10
+ def create_survey(num_questions: int, chained: bool = True, take_scenario=False):
11
+ survey = Survey()
12
+ for i in range(num_questions):
13
+ if take_scenario:
14
+ q = QuestionFreeText(
15
+ question_text=f"XX{i}XX and {{scenario_value }}",
16
+ question_name=f"question_{i}",
17
+ )
18
+ else:
19
+ q = QuestionFreeText(
20
+ question_text=f"XX{i}XX", question_name=f"question_{i}"
21
+ )
22
+ survey.add_question(q)
23
+ if i > 0 and chained:
24
+ survey.add_targeted_memory(f"question_{i}", f"question_{i-1}")
25
+ return survey
26
+
27
+
28
+ def create_language_model(
29
+ exception: Exception, fail_at_number: int, never_ending=False
30
+ ):
31
+ class LanguageModelFromUtilities(LanguageModel):
32
+ _model_ = "test"
33
+ _parameters_ = {"temperature": 0.5}
34
+ _inference_service_ = InferenceServiceType.TEST.value
35
+ key_sequence = ["message", 0, "text"]
36
+ usage_sequence = ["usage"]
37
+ input_token_name = "prompt_tokens"
38
+ output_token_name = "completion_tokens"
39
+ _rpm = 1000000000000
40
+ _tpm = 1000000000000
41
+
42
+ async def async_execute_model_call(
43
+ self, user_prompt: str, system_prompt: str
44
+ ) -> dict[str, Any]:
45
+ question_number = int(
46
+ user_prompt.split("XX")[1]
47
+ ) ## grabs the question number from the prompt
48
+ await asyncio.sleep(0.1)
49
+ if never_ending: ## you're not going anywhere buddy
50
+ await asyncio.sleep(float("inf"))
51
+ if question_number == fail_at_number:
52
+ if asyncio.iscoroutinefunction(exception):
53
+ await exception()
54
+ else:
55
+ raise exception
56
+ return {
57
+ "message": [{"text": "SPAM!"}],
58
+ "usage": {"prompt_tokens": 1, "completion_tokens": 1},
59
+ }
60
+
61
+ return LanguageModelFromUtilities
edsl/prompts/Prompt.py CHANGED
@@ -3,13 +3,17 @@ from typing import Optional
3
3
  from abc import ABC
4
4
  from typing import Any, List
5
5
 
6
+ from jinja2 import Environment, FileSystemLoader
7
+ from typing import Union, Dict
8
+ from pathlib import Path
9
+
6
10
  from rich.table import Table
7
11
  from jinja2 import Template, Environment, meta, TemplateSyntaxError, Undefined
8
12
 
9
13
 
10
14
  class PreserveUndefined(Undefined):
11
15
  def __str__(self):
12
- return "{{ " + self._undefined_name + " }}"
16
+ return "{{ " + str(self._undefined_name) + " }}"
13
17
 
14
18
 
15
19
  from edsl.exceptions.prompts import TemplateRenderError
@@ -70,6 +74,50 @@ class PromptBase(
70
74
  text = f.read()
71
75
  return cls(text=text)
72
76
 
77
+ @classmethod
78
+ def from_template(
79
+ cls,
80
+ file_name: str,
81
+ path_to_folder: Optional[Union[str, Path]] = None,
82
+ **kwargs: Dict[str, Any],
83
+ ) -> "PromptBase":
84
+ """Create a `PromptBase` from a Jinja template.
85
+
86
+ Args:
87
+ file_name (str): The name of the Jinja template file.
88
+ path_to_folder (Union[str, Path]): The path to the folder containing the template.
89
+ Can be absolute or relative.
90
+ **kwargs: Variables to be passed to the template for rendering.
91
+
92
+ Returns:
93
+ PromptBase: An instance of PromptBase with the rendered template as text.
94
+ """
95
+ # if file_name lacks the .j2 extension, add it
96
+ if not file_name.endswith(".jinja"):
97
+ file_name += ".jinja"
98
+
99
+ # Convert path_to_folder to a Path object if it's a string
100
+ if path_to_folder is None:
101
+ from importlib import resources
102
+ import os
103
+
104
+ path_to_folder = resources.path("edsl.questions", "prompt_templates")
105
+
106
+ try:
107
+ folder_path = Path(path_to_folder)
108
+ except Exception as e:
109
+ raise ValueError(f"Invalid path: {path_to_folder}. Error: {e}")
110
+
111
+ with open(folder_path.joinpath(file_name), "r") as f:
112
+ text = f.read()
113
+ return cls(text=text)
114
+ # Resolve the path to get the absolute path
115
+ # absolute_path = folder_path.resolve()
116
+ # env = Environment(loader=FileSystemLoader(absolute_path))
117
+ # template = env.get_template(file_name)
118
+ # rendered_text = template.render({})
119
+ # return cls(text=rendered_text)
120
+
73
121
  @property
74
122
  def text(self):
75
123
  """Return the `Prompt` text."""
@@ -247,7 +295,9 @@ class PromptBase(
247
295
  "Too much nesting - you created an infinite loop here, pal"
248
296
  )
249
297
  except TemplateSyntaxError as e:
250
- raise TemplateRenderError(f"Template syntax error: {e}")
298
+ raise TemplateRenderError(
299
+ f"Template syntax error: {e}. Bad template: {text}"
300
+ )
251
301
 
252
302
  def to_dict(self) -> dict[str, Any]:
253
303
  """Return the `Prompt` as a dictionary.
@@ -16,19 +16,27 @@ class AnswerValidatorMixin:
16
16
  - Question specific validation: validators for specific question types
17
17
  """
18
18
 
19
+ def failing_job(self):
20
+ from edsl import Agent
21
+
22
+ a = Agent()
23
+
24
+ def f(self, question, scenario):
25
+ return []
26
+
27
+ a.add_direct_question_answering_method(f, validate_response=True)
28
+ from edsl import QuestionNumerical
29
+
30
+ q = QuestionNumerical.example()
31
+ results = q.by(a).run()
32
+ return results
33
+
19
34
  #####################
20
35
  # TEMPLATE VALIDATION
21
36
  #####################
22
37
  def _validate_answer_template_basic(self, answer: Any) -> None:
23
38
  """Check that the answer (i) is a dictionary (ii) has an 'answer' key.
24
39
 
25
- >>> avm = AnswerValidatorMixin()
26
- >>> avm._validate_answer_template_basic({'answer': 1})
27
- >>> avm._validate_answer_template_basic([])
28
- Traceback (most recent call last):
29
- ...
30
- edsl.exceptions.questions.QuestionAnswerValidationError: Answer must be a dictionary (got []).
31
-
32
40
  - E.g., both {'answer': 1} and {'answer': {'a': 1}, 'other_key'=[1,2,3]} are valid
33
41
  """
34
42
  if not isinstance(answer, dict):
@@ -56,14 +64,9 @@ class AnswerValidatorMixin:
56
64
  def _validate_answer_key_value_numeric(
57
65
  self, answer: dict[str, Any], key: str
58
66
  ) -> None:
59
- """Check that the value of a key is numeric (int or float).
60
-
61
- >>> avm = AnswerValidatorMixin()
62
- >>> avm._validate_answer_key_value_numeric({'answer': 1}, 'answer')
63
- >>> avm._validate_answer_key_value_numeric({'answer': 'poo'}, 'answer')
64
- Traceback (most recent call last):
65
- ...
66
- edsl.exceptions.questions.QuestionAnswerValidationError: Answer should be numerical (int or float). Got 'poo'
67
+ """Check that the value is numeric (int or float).
68
+ Can also deal with strings that contain commas and other characters.
69
+
67
70
  """
68
71
  value = answer.get(key)
69
72
  initial_value = value
@@ -128,15 +131,6 @@ class AnswerValidatorMixin:
128
131
 
129
132
  :param answer: Answer to validate
130
133
 
131
- >>> avm = AnswerValidatorMixin()
132
- >>> avm.question_options = ["a", "b", "c"]
133
- >>> avm.min_selections = 1
134
- >>> avm.max_selections = 2
135
- >>> avm._validate_answer_checkbox({"answer": ["0", "1"]})
136
- >>> avm._validate_answer_checkbox({"answer": []})
137
- Traceback (most recent call last):
138
- ...
139
- edsl.exceptions.questions.QuestionAnswerValidationError:...
140
134
 
141
135
  Check that answer["answer"]:
142
136
  - has elements that are strings, bytes-like objects or real numbers evaluating to integers
@@ -287,6 +281,9 @@ class AnswerValidatorMixin:
287
281
 
288
282
 
289
283
  if __name__ == "__main__":
290
- import doctest
284
+ pass
285
+ # import doctest
286
+
287
+ # doctest.testmod(optionflags=doctest.ELLIPSIS)
291
288
 
292
- doctest.testmod(optionflags=doctest.ELLIPSIS)
289
+ # results = AnswerValidatorMixin().failing_job()