edsl 0.1.38.dev4__py3-none-any.whl → 0.1.39__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 +197 -116
- edsl/__init__.py +15 -7
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +351 -147
- edsl/agents/AgentList.py +211 -73
- edsl/agents/Invigilator.py +101 -50
- edsl/agents/InvigilatorBase.py +62 -70
- edsl/agents/PromptConstructor.py +143 -225
- edsl/agents/QuestionInstructionPromptBuilder.py +128 -0
- edsl/agents/QuestionTemplateReplacementsBuilder.py +137 -0
- edsl/agents/__init__.py +0 -1
- edsl/agents/prompt_helpers.py +3 -3
- edsl/agents/question_option_processor.py +172 -0
- edsl/auto/AutoStudy.py +18 -5
- edsl/auto/StageBase.py +53 -40
- edsl/auto/StageQuestions.py +2 -1
- edsl/auto/utilities.py +0 -6
- edsl/config.py +22 -2
- edsl/conversation/car_buying.py +2 -1
- edsl/coop/CoopFunctionsMixin.py +15 -0
- edsl/coop/ExpectedParrotKeyHandler.py +125 -0
- edsl/coop/PriceFetcher.py +1 -1
- edsl/coop/coop.py +125 -47
- edsl/coop/utils.py +14 -14
- edsl/data/Cache.py +45 -27
- edsl/data/CacheEntry.py +12 -15
- edsl/data/CacheHandler.py +31 -12
- edsl/data/RemoteCacheSync.py +154 -46
- edsl/data/__init__.py +4 -3
- edsl/data_transfer_models.py +2 -1
- edsl/enums.py +27 -0
- edsl/exceptions/__init__.py +50 -50
- edsl/exceptions/agents.py +12 -0
- edsl/exceptions/inference_services.py +5 -0
- edsl/exceptions/questions.py +24 -6
- edsl/exceptions/scenarios.py +7 -0
- edsl/inference_services/AnthropicService.py +38 -19
- edsl/inference_services/AvailableModelCacheHandler.py +184 -0
- edsl/inference_services/AvailableModelFetcher.py +215 -0
- edsl/inference_services/AwsBedrock.py +0 -2
- edsl/inference_services/AzureAI.py +0 -2
- edsl/inference_services/GoogleService.py +7 -12
- edsl/inference_services/InferenceServiceABC.py +18 -85
- edsl/inference_services/InferenceServicesCollection.py +120 -79
- edsl/inference_services/MistralAIService.py +0 -3
- edsl/inference_services/OpenAIService.py +47 -35
- edsl/inference_services/PerplexityService.py +0 -3
- edsl/inference_services/ServiceAvailability.py +135 -0
- edsl/inference_services/TestService.py +11 -10
- edsl/inference_services/TogetherAIService.py +5 -3
- edsl/inference_services/data_structures.py +134 -0
- edsl/jobs/AnswerQuestionFunctionConstructor.py +223 -0
- edsl/jobs/Answers.py +1 -14
- edsl/jobs/FetchInvigilator.py +47 -0
- edsl/jobs/InterviewTaskManager.py +98 -0
- edsl/jobs/InterviewsConstructor.py +50 -0
- edsl/jobs/Jobs.py +356 -431
- edsl/jobs/JobsChecks.py +35 -10
- edsl/jobs/JobsComponentConstructor.py +189 -0
- edsl/jobs/JobsPrompts.py +6 -4
- edsl/jobs/JobsRemoteInferenceHandler.py +205 -133
- 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 +44 -3
- edsl/jobs/buckets/TokenBucket.py +53 -21
- 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 +143 -408
- 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 +88 -403
- edsl/jobs/runners/JobsRunnerStatus.py +133 -165
- edsl/jobs/tasks/QuestionTaskCreator.py +21 -19
- edsl/jobs/tasks/TaskHistory.py +38 -18
- edsl/jobs/tasks/task_status_enum.py +0 -2
- edsl/language_models/ComputeCost.py +63 -0
- edsl/language_models/LanguageModel.py +194 -236
- edsl/language_models/ModelList.py +28 -19
- edsl/language_models/PriceManager.py +127 -0
- edsl/language_models/RawResponseHandler.py +106 -0
- edsl/language_models/ServiceDataSources.py +0 -0
- edsl/language_models/__init__.py +1 -2
- 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 +2 -2
- edsl/language_models/utilities.py +5 -4
- edsl/notebooks/Notebook.py +19 -14
- edsl/notebooks/NotebookToLaTeX.py +142 -0
- edsl/prompts/Prompt.py +29 -39
- edsl/questions/ExceptionExplainer.py +77 -0
- edsl/questions/HTMLQuestion.py +103 -0
- edsl/questions/QuestionBase.py +68 -214
- edsl/questions/QuestionBasePromptsMixin.py +7 -3
- edsl/questions/QuestionBudget.py +1 -1
- edsl/questions/QuestionCheckBox.py +3 -3
- edsl/questions/QuestionExtract.py +5 -7
- edsl/questions/QuestionFreeText.py +2 -3
- edsl/questions/QuestionList.py +10 -18
- edsl/questions/QuestionMatrix.py +265 -0
- edsl/questions/QuestionMultipleChoice.py +67 -23
- edsl/questions/QuestionNumerical.py +2 -4
- edsl/questions/QuestionRank.py +7 -17
- edsl/questions/SimpleAskMixin.py +4 -3
- edsl/questions/__init__.py +2 -1
- edsl/questions/{AnswerValidatorMixin.py → answer_validator_mixin.py} +47 -2
- edsl/questions/data_structures.py +20 -0
- edsl/questions/derived/QuestionLinearScale.py +6 -3
- edsl/questions/derived/QuestionTopK.py +1 -1
- edsl/questions/descriptors.py +17 -3
- edsl/questions/loop_processor.py +149 -0
- edsl/questions/{QuestionBaseGenMixin.py → question_base_gen_mixin.py} +57 -50
- edsl/questions/question_registry.py +1 -1
- edsl/questions/{ResponseValidatorABC.py → response_validator_abc.py} +40 -26
- edsl/questions/response_validator_factory.py +34 -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/results/CSSParameterizer.py +1 -1
- edsl/results/Dataset.py +170 -7
- edsl/results/DatasetExportMixin.py +168 -305
- edsl/results/DatasetTree.py +28 -8
- edsl/results/MarkdownToDocx.py +122 -0
- edsl/results/MarkdownToPDF.py +111 -0
- edsl/results/Result.py +298 -206
- edsl/results/Results.py +149 -131
- edsl/results/ResultsExportMixin.py +2 -0
- edsl/results/TableDisplay.py +98 -171
- edsl/results/TextEditor.py +50 -0
- edsl/results/__init__.py +1 -1
- edsl/results/file_exports.py +252 -0
- edsl/results/{Selector.py → results_selector.py} +23 -13
- edsl/results/smart_objects.py +96 -0
- edsl/results/table_data_class.py +12 -0
- edsl/results/table_renderers.py +118 -0
- edsl/scenarios/ConstructDownloadLink.py +109 -0
- edsl/scenarios/DocumentChunker.py +102 -0
- edsl/scenarios/DocxScenario.py +16 -0
- edsl/scenarios/FileStore.py +150 -239
- edsl/scenarios/PdfExtractor.py +40 -0
- edsl/scenarios/Scenario.py +90 -193
- edsl/scenarios/ScenarioHtmlMixin.py +4 -3
- edsl/scenarios/ScenarioList.py +415 -244
- edsl/scenarios/ScenarioListExportMixin.py +0 -7
- edsl/scenarios/ScenarioListPdfMixin.py +15 -37
- edsl/scenarios/__init__.py +1 -2
- 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/{ScenarioJoin.py → scenario_join.py} +10 -6
- edsl/scenarios/scenario_selector.py +156 -0
- edsl/study/ObjectEntry.py +1 -1
- edsl/study/SnapShot.py +1 -1
- edsl/study/Study.py +5 -12
- edsl/surveys/ConstructDAG.py +92 -0
- edsl/surveys/EditSurvey.py +221 -0
- edsl/surveys/InstructionHandler.py +100 -0
- edsl/surveys/MemoryManagement.py +72 -0
- edsl/surveys/Rule.py +5 -4
- edsl/surveys/RuleCollection.py +25 -27
- edsl/surveys/RuleManager.py +172 -0
- edsl/surveys/Simulator.py +75 -0
- edsl/surveys/Survey.py +270 -791
- edsl/surveys/SurveyCSS.py +20 -8
- edsl/surveys/{SurveyFlowVisualizationMixin.py → SurveyFlowVisualization.py} +11 -9
- edsl/surveys/SurveyToApp.py +141 -0
- edsl/surveys/__init__.py +4 -2
- edsl/surveys/descriptors.py +6 -2
- edsl/surveys/instructions/ChangeInstruction.py +1 -2
- edsl/surveys/instructions/Instruction.py +4 -13
- edsl/surveys/instructions/InstructionCollection.py +11 -6
- edsl/templates/error_reporting/interview_details.html +1 -1
- edsl/templates/error_reporting/report.html +1 -1
- edsl/tools/plotting.py +1 -1
- edsl/utilities/PrettyList.py +56 -0
- edsl/utilities/is_notebook.py +18 -0
- edsl/utilities/is_valid_variable_name.py +11 -0
- edsl/utilities/remove_edsl_version.py +24 -0
- edsl/utilities/utilities.py +35 -23
- {edsl-0.1.38.dev4.dist-info → edsl-0.1.39.dist-info}/METADATA +12 -10
- edsl-0.1.39.dist-info/RECORD +358 -0
- {edsl-0.1.38.dev4.dist-info → edsl-0.1.39.dist-info}/WHEEL +1 -1
- edsl/language_models/KeyLookup.py +0 -30
- edsl/language_models/registry.py +0 -190
- edsl/language_models/unused/ReplicateBase.py +0 -83
- edsl/results/ResultsDBMixin.py +0 -238
- edsl-0.1.38.dev4.dist-info/RECORD +0 -277
- /edsl/questions/{RegisterQuestionsMeta.py → register_questions_meta.py} +0 -0
- /edsl/results/{ResultsFetchMixin.py → results_fetch_mixin.py} +0 -0
- /edsl/results/{ResultsToolsMixin.py → results_tools_mixin.py} +0 -0
- {edsl-0.1.38.dev4.dist-info → edsl-0.1.39.dist-info}/LICENSE +0 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
from collections import UserDict
|
2
|
+
|
3
|
+
from edsl.language_models.key_management.KeyLookupBuilder import KeyLookupBuilder
|
4
|
+
|
5
|
+
|
6
|
+
class KeyLookupCollection(UserDict):
|
7
|
+
"""A singleton class that stores key-lookup objects.
|
8
|
+
|
9
|
+
This is because once a KeyLook is created once, we do not
|
10
|
+
need to keep re-creating it.
|
11
|
+
|
12
|
+
>>> collection = KeyLookupCollection()
|
13
|
+
>>> collection2 = KeyLookupCollection()
|
14
|
+
>>> collection is collection2 # Test singleton pattern
|
15
|
+
True
|
16
|
+
>>> collection.add_key_lookup(("config", "env"))
|
17
|
+
>>> ("config", "env") in collection.data
|
18
|
+
True
|
19
|
+
"""
|
20
|
+
|
21
|
+
_instance = None
|
22
|
+
|
23
|
+
def __new__(cls, *args, **kwargs):
|
24
|
+
if cls._instance is None:
|
25
|
+
cls._instance = super().__new__(cls)
|
26
|
+
return cls._instance
|
27
|
+
|
28
|
+
def __init__(self, *args, **kwargs):
|
29
|
+
if not hasattr(self, "_initialized"):
|
30
|
+
self.data = {}
|
31
|
+
self._initialized = True
|
32
|
+
super().__init__(*args, **kwargs)
|
33
|
+
|
34
|
+
def add_key_lookup(self, fetch_order=None):
|
35
|
+
if fetch_order is None:
|
36
|
+
fetch_order = ("config", "env")
|
37
|
+
if fetch_order not in self.data:
|
38
|
+
self.data[fetch_order] = KeyLookupBuilder(fetch_order=fetch_order).build()
|
File without changes
|
@@ -0,0 +1,131 @@
|
|
1
|
+
from dataclasses import dataclass, asdict
|
2
|
+
from typing import Optional
|
3
|
+
|
4
|
+
|
5
|
+
@dataclass
|
6
|
+
class APIKeyEntry:
|
7
|
+
"""A class representing an API key entry.
|
8
|
+
|
9
|
+
>>> entry = APIKeyEntry.example()
|
10
|
+
>>> entry.service
|
11
|
+
'openai'
|
12
|
+
>>> entry.name
|
13
|
+
'OPENAI_API_KEY'
|
14
|
+
>>> entry.value
|
15
|
+
'sk-abcd1234'
|
16
|
+
>>> entry.source
|
17
|
+
'env'
|
18
|
+
"""
|
19
|
+
|
20
|
+
service: str
|
21
|
+
name: str
|
22
|
+
value: str
|
23
|
+
source: Optional[str] = None
|
24
|
+
|
25
|
+
@classmethod
|
26
|
+
def example(cls):
|
27
|
+
return APIKeyEntry(
|
28
|
+
service="openai", name="OPENAI_API_KEY", value="sk-abcd1234", source="env"
|
29
|
+
)
|
30
|
+
|
31
|
+
|
32
|
+
@dataclass
|
33
|
+
class LimitEntry:
|
34
|
+
"""A class representing rate limit entries for a service.
|
35
|
+
|
36
|
+
>>> limit = LimitEntry.example()
|
37
|
+
>>> limit.service
|
38
|
+
'openai'
|
39
|
+
>>> limit.rpm
|
40
|
+
60
|
41
|
+
>>> limit.tpm
|
42
|
+
100000
|
43
|
+
>>> limit.source
|
44
|
+
'config'
|
45
|
+
"""
|
46
|
+
|
47
|
+
service: str
|
48
|
+
rpm: int
|
49
|
+
tpm: int
|
50
|
+
source: Optional[str] = None
|
51
|
+
|
52
|
+
@classmethod
|
53
|
+
def example(cls):
|
54
|
+
return LimitEntry(service="openai", rpm=60, tpm=100000, source="config")
|
55
|
+
|
56
|
+
|
57
|
+
@dataclass
|
58
|
+
class APIIDEntry:
|
59
|
+
"""A class representing an API ID entry.
|
60
|
+
|
61
|
+
>>> id_entry = APIIDEntry.example()
|
62
|
+
>>> id_entry.service
|
63
|
+
'bedrock'
|
64
|
+
>>> id_entry.name
|
65
|
+
'AWS_ACCESS_KEY_ID'
|
66
|
+
>>> id_entry.value
|
67
|
+
'AKIA1234'
|
68
|
+
>>> id_entry.source
|
69
|
+
'env'
|
70
|
+
"""
|
71
|
+
|
72
|
+
service: str
|
73
|
+
name: str
|
74
|
+
value: str
|
75
|
+
source: Optional[str] = None
|
76
|
+
|
77
|
+
@classmethod
|
78
|
+
def example(cls):
|
79
|
+
return APIIDEntry(
|
80
|
+
service="bedrock", name="AWS_ACCESS_KEY_ID", value="AKIA1234", source="env"
|
81
|
+
)
|
82
|
+
|
83
|
+
|
84
|
+
@dataclass
|
85
|
+
class LanguageModelInput:
|
86
|
+
"""A class representing input configuration for a language model service.
|
87
|
+
|
88
|
+
>>> lm_input = LanguageModelInput.example()
|
89
|
+
>>> lm_input.api_token
|
90
|
+
'sk-abcd123'
|
91
|
+
>>> lm_input.rpm
|
92
|
+
60
|
93
|
+
>>> lm_input.tpm
|
94
|
+
100000
|
95
|
+
>>> lm_input.api_id
|
96
|
+
|
97
|
+
|
98
|
+
Test dictionary conversion:
|
99
|
+
>>> d = lm_input.to_dict()
|
100
|
+
>>> isinstance(d, dict)
|
101
|
+
True
|
102
|
+
>>> LanguageModelInput.from_dict(d).api_token == lm_input.api_token
|
103
|
+
True
|
104
|
+
"""
|
105
|
+
|
106
|
+
api_token: str
|
107
|
+
rpm: int
|
108
|
+
tpm: int
|
109
|
+
api_id: Optional[str] = None
|
110
|
+
token_source: Optional[str] = None
|
111
|
+
limit_source: Optional[str] = None
|
112
|
+
id_source: Optional[str] = None
|
113
|
+
|
114
|
+
def to_dict(self):
|
115
|
+
return asdict(self)
|
116
|
+
|
117
|
+
@classmethod
|
118
|
+
def from_dict(cls, d):
|
119
|
+
return cls(**d)
|
120
|
+
|
121
|
+
@classmethod
|
122
|
+
def example(cls):
|
123
|
+
return LanguageModelInput(
|
124
|
+
api_token="sk-abcd123", tpm=100000, rpm=60, api_id=None
|
125
|
+
)
|
126
|
+
|
127
|
+
|
128
|
+
if __name__ == "__main__":
|
129
|
+
import doctest
|
130
|
+
|
131
|
+
doctest.testmod()
|
@@ -0,0 +1,256 @@
|
|
1
|
+
import textwrap
|
2
|
+
from random import random
|
3
|
+
from typing import Optional, TYPE_CHECKING, List
|
4
|
+
|
5
|
+
from edsl.utilities.PrettyList import PrettyList
|
6
|
+
from edsl.config import CONFIG
|
7
|
+
|
8
|
+
from edsl.inference_services.InferenceServicesCollection import (
|
9
|
+
InferenceServicesCollection,
|
10
|
+
)
|
11
|
+
from edsl.inference_services.data_structures import AvailableModels
|
12
|
+
from edsl.inference_services.InferenceServiceABC import InferenceServiceABC
|
13
|
+
from edsl.enums import InferenceServiceLiteral
|
14
|
+
|
15
|
+
if TYPE_CHECKING:
|
16
|
+
from edsl.results.Dataset import Dataset
|
17
|
+
|
18
|
+
|
19
|
+
def get_model_class(model_name, registry: Optional[InferenceServicesCollection] = None):
|
20
|
+
from edsl.inference_services.registry import default
|
21
|
+
|
22
|
+
registry = registry or default
|
23
|
+
factory = registry.create_model_factory(model_name)
|
24
|
+
return factory
|
25
|
+
|
26
|
+
|
27
|
+
class Meta(type):
|
28
|
+
def __repr__(cls):
|
29
|
+
return textwrap.dedent(
|
30
|
+
f"""\
|
31
|
+
Available models: {cls.available()}
|
32
|
+
|
33
|
+
To create an instance, you can do:
|
34
|
+
>>> m = Model('gpt-4-1106-preview', temperature=0.5, ...)
|
35
|
+
|
36
|
+
To get the default model, you can leave out the model name.
|
37
|
+
To see the available models, you can do:
|
38
|
+
>>> Model.available()
|
39
|
+
"""
|
40
|
+
)
|
41
|
+
|
42
|
+
|
43
|
+
class Model(metaclass=Meta):
|
44
|
+
default_model = CONFIG.get("EDSL_DEFAULT_MODEL")
|
45
|
+
_registry: InferenceServicesCollection = None # Class-level registry storage
|
46
|
+
|
47
|
+
@classmethod
|
48
|
+
def get_registry(cls) -> InferenceServicesCollection:
|
49
|
+
"""Get the current registry or initialize with default if None"""
|
50
|
+
if cls._registry is None:
|
51
|
+
from edsl.inference_services.registry import default
|
52
|
+
|
53
|
+
cls._registry = default
|
54
|
+
return cls._registry
|
55
|
+
|
56
|
+
@classmethod
|
57
|
+
def set_registry(cls, registry: InferenceServicesCollection) -> None:
|
58
|
+
"""Set a new registry"""
|
59
|
+
cls._registry = registry
|
60
|
+
|
61
|
+
def __new__(
|
62
|
+
cls,
|
63
|
+
model_name: Optional[str] = None,
|
64
|
+
service_name: Optional[InferenceServiceLiteral] = None,
|
65
|
+
registry: Optional[InferenceServicesCollection] = None,
|
66
|
+
*args,
|
67
|
+
**kwargs,
|
68
|
+
):
|
69
|
+
"Instantiate a new language model."
|
70
|
+
# Map index to the respective subclass
|
71
|
+
if model_name is None:
|
72
|
+
model_name = (
|
73
|
+
cls.default_model
|
74
|
+
) # when model_name is None, use the default model, set in the config file
|
75
|
+
|
76
|
+
if registry is not None:
|
77
|
+
cls.set_registry(registry)
|
78
|
+
|
79
|
+
if isinstance(model_name, int): # can refer to a model by index
|
80
|
+
model_name = cls.available(name_only=True)[model_name]
|
81
|
+
|
82
|
+
factory = cls.get_registry().create_model_factory(
|
83
|
+
model_name, service_name=service_name
|
84
|
+
)
|
85
|
+
return factory(*args, **kwargs)
|
86
|
+
|
87
|
+
@classmethod
|
88
|
+
def add_model(cls, service_name, model_name) -> None:
|
89
|
+
cls.get_registry().add_model(service_name, model_name)
|
90
|
+
|
91
|
+
@classmethod
|
92
|
+
def service_classes(cls) -> List["InferenceServiceABC"]:
|
93
|
+
"""Returns a list of service classes.
|
94
|
+
|
95
|
+
>>> Model.service_classes()
|
96
|
+
[...]
|
97
|
+
"""
|
98
|
+
return [r for r in cls.services(name_only=True)]
|
99
|
+
|
100
|
+
@classmethod
|
101
|
+
def services(cls, name_only: bool = False) -> List[str]:
|
102
|
+
"""Returns a list of services, annotated with whether the user has local keys for them."""
|
103
|
+
services_with_local_keys = set(cls.key_info().select("service").to_list())
|
104
|
+
f = lambda service_name: (
|
105
|
+
"yes" if service_name in services_with_local_keys else " "
|
106
|
+
)
|
107
|
+
if name_only:
|
108
|
+
return PrettyList(
|
109
|
+
[r._inference_service_ for r in cls.get_registry().services],
|
110
|
+
columns=["Service Name"],
|
111
|
+
)
|
112
|
+
else:
|
113
|
+
return PrettyList(
|
114
|
+
[
|
115
|
+
(r._inference_service_, f(r._inference_service_))
|
116
|
+
for r in cls.get_registry().services
|
117
|
+
],
|
118
|
+
columns=["Service Name", "Local key?"],
|
119
|
+
)
|
120
|
+
|
121
|
+
@classmethod
|
122
|
+
def services_with_local_keys(cls) -> set:
|
123
|
+
"""Returns a list of services for which the user has local keys."""
|
124
|
+
return set(cls.key_info().select("service").to_list())
|
125
|
+
|
126
|
+
@classmethod
|
127
|
+
def key_info(cls, obscure_api_key: bool = True) -> "Dataset":
|
128
|
+
"""Returns a dataset of local key information."""
|
129
|
+
from edsl.language_models.key_management.KeyLookupCollection import (
|
130
|
+
KeyLookupCollection,
|
131
|
+
)
|
132
|
+
from edsl.scenarios import Scenario, ScenarioList
|
133
|
+
|
134
|
+
klc = KeyLookupCollection()
|
135
|
+
klc.add_key_lookup(fetch_order=None)
|
136
|
+
sl = ScenarioList()
|
137
|
+
for service, entry in list(klc.data.values())[0].items():
|
138
|
+
sl.append(Scenario({"service": service} | entry.to_dict()))
|
139
|
+
if obscure_api_key:
|
140
|
+
for service in sl:
|
141
|
+
service["api_token"] = (
|
142
|
+
service["api_token"][:4] + "..." + service["api_token"][-4:]
|
143
|
+
)
|
144
|
+
return sl.to_dataset()
|
145
|
+
|
146
|
+
@classmethod
|
147
|
+
def search_models(cls, search_term: str):
|
148
|
+
return cls.available(search_term=search_term)
|
149
|
+
|
150
|
+
@classmethod
|
151
|
+
def all_known_models(cls) -> "AvailableModels":
|
152
|
+
return cls.get_registry().available()
|
153
|
+
|
154
|
+
@classmethod
|
155
|
+
def available_with_local_keys(cls):
|
156
|
+
services_with_local_keys = set(cls.key_info().select("service").to_list())
|
157
|
+
return [
|
158
|
+
m
|
159
|
+
for m in cls.get_registry().available()
|
160
|
+
if m.service_name in services_with_local_keys
|
161
|
+
]
|
162
|
+
|
163
|
+
@classmethod
|
164
|
+
def available(
|
165
|
+
cls,
|
166
|
+
search_term: str = None,
|
167
|
+
name_only: bool = False,
|
168
|
+
service: Optional[str] = None,
|
169
|
+
):
|
170
|
+
# if search_term is None and service is None:
|
171
|
+
# print("Getting available models...")
|
172
|
+
# print("You have local keys for the following services:")
|
173
|
+
# print(cls.services_with_local_keys())
|
174
|
+
# print("\n")
|
175
|
+
# print("To see models by service, use the 'service' parameter.")
|
176
|
+
# print("E.g., Model.available(service='openai')")
|
177
|
+
# return None
|
178
|
+
|
179
|
+
if service is not None:
|
180
|
+
if service not in cls.services(name_only=True):
|
181
|
+
raise ValueError(
|
182
|
+
f"Service {service} not found in available services.",
|
183
|
+
f"Available services are: {cls.services()}",
|
184
|
+
)
|
185
|
+
|
186
|
+
full_list = cls.get_registry().available(service=service)
|
187
|
+
|
188
|
+
if search_term is None:
|
189
|
+
if name_only:
|
190
|
+
return PrettyList(
|
191
|
+
[m.model_name for m in full_list],
|
192
|
+
columns=["Model Name"],
|
193
|
+
)
|
194
|
+
else:
|
195
|
+
return PrettyList(
|
196
|
+
[[m.model_name, m.service_name] for m in full_list],
|
197
|
+
columns=["Model Name", "Service Name"],
|
198
|
+
)
|
199
|
+
else:
|
200
|
+
filtered_results = [
|
201
|
+
m
|
202
|
+
for m in full_list
|
203
|
+
if search_term in m.model_name or search_term in m.service_name
|
204
|
+
]
|
205
|
+
if name_only:
|
206
|
+
return PrettyList(
|
207
|
+
[m.model_name for m in filtered_results],
|
208
|
+
columns=["Model Name"],
|
209
|
+
)
|
210
|
+
else:
|
211
|
+
return PrettyList(
|
212
|
+
[[m.model_name, m.service_name] for m in full_list],
|
213
|
+
columns=["Model Name", "Service Name"],
|
214
|
+
)
|
215
|
+
|
216
|
+
@classmethod
|
217
|
+
def check_models(cls, verbose=False):
|
218
|
+
print("Checking all available models...\n")
|
219
|
+
for model in cls.available(name_only=True):
|
220
|
+
print(f"Now checking: {model}")
|
221
|
+
try:
|
222
|
+
m = cls(model)
|
223
|
+
except Exception as e:
|
224
|
+
print(f"Error creating instance of {model}: {e}")
|
225
|
+
continue
|
226
|
+
try:
|
227
|
+
results = m.hello(verbose)
|
228
|
+
if verbose:
|
229
|
+
print(f"Results from model call: {results}")
|
230
|
+
except Exception as e:
|
231
|
+
print(f"Error calling 'hello' on {model}: {e}")
|
232
|
+
continue
|
233
|
+
print("OK!")
|
234
|
+
print("\n")
|
235
|
+
|
236
|
+
@classmethod
|
237
|
+
def example(cls, randomize: bool = False) -> "Model":
|
238
|
+
"""
|
239
|
+
Returns an example Model instance.
|
240
|
+
|
241
|
+
:param randomize: If True, the temperature is set to a random decimal between 0 and 1.
|
242
|
+
"""
|
243
|
+
temperature = 0.5 if not randomize else round(random(), 2)
|
244
|
+
model_name = cls.default_model
|
245
|
+
return cls(model_name, temperature=temperature)
|
246
|
+
|
247
|
+
|
248
|
+
if __name__ == "__main__":
|
249
|
+
import doctest
|
250
|
+
|
251
|
+
doctest.testmod(optionflags=doctest.ELLIPSIS)
|
252
|
+
|
253
|
+
available = Model.available()
|
254
|
+
m = Model("gpt-4-1106-preview")
|
255
|
+
results = m.execute_model_call("Hello world")
|
256
|
+
print(results)
|
edsl/language_models/repair.py
CHANGED
@@ -32,11 +32,11 @@ async def async_repair(
|
|
32
32
|
else:
|
33
33
|
return valid_dict, success
|
34
34
|
|
35
|
-
from edsl import Model
|
35
|
+
from edsl.language_models.model import Model
|
36
36
|
|
37
37
|
m = Model()
|
38
38
|
|
39
|
-
from edsl import QuestionExtract
|
39
|
+
from edsl.questions.QuestionExtract import QuestionExtract
|
40
40
|
|
41
41
|
with warnings.catch_warnings():
|
42
42
|
warnings.simplefilter("ignore", UserWarning)
|
@@ -1,13 +1,12 @@
|
|
1
1
|
import asyncio
|
2
2
|
from typing import Any, Optional, List
|
3
|
-
from edsl import Survey
|
4
|
-
from edsl.config import CONFIG
|
5
3
|
from edsl.enums import InferenceServiceType
|
6
|
-
from edsl.language_models.LanguageModel import LanguageModel
|
7
|
-
from edsl.questions import QuestionFreeText
|
8
4
|
|
9
5
|
|
10
6
|
def create_survey(num_questions: int, chained: bool = True, take_scenario=False):
|
7
|
+
from edsl.surveys.Survey import Survey
|
8
|
+
from edsl.questions.QuestionFreeText import QuestionFreeText
|
9
|
+
|
11
10
|
survey = Survey()
|
12
11
|
for i in range(num_questions):
|
13
12
|
if take_scenario:
|
@@ -28,6 +27,8 @@ def create_survey(num_questions: int, chained: bool = True, take_scenario=False)
|
|
28
27
|
def create_language_model(
|
29
28
|
exception: Exception, fail_at_number: int, never_ending=False
|
30
29
|
):
|
30
|
+
from edsl.language_models.LanguageModel import LanguageModel
|
31
|
+
|
31
32
|
class LanguageModelFromUtilities(LanguageModel):
|
32
33
|
_model_ = "test"
|
33
34
|
_parameters_ = {"temperature": 0.5}
|
edsl/notebooks/Notebook.py
CHANGED
@@ -17,8 +17,8 @@ class Notebook(Base):
|
|
17
17
|
|
18
18
|
def __init__(
|
19
19
|
self,
|
20
|
-
data: Optional[Dict] = None,
|
21
20
|
path: Optional[str] = None,
|
21
|
+
data: Optional[Dict] = None,
|
22
22
|
name: Optional[str] = None,
|
23
23
|
):
|
24
24
|
"""
|
@@ -33,12 +33,16 @@ class Notebook(Base):
|
|
33
33
|
import nbformat
|
34
34
|
|
35
35
|
# Load current notebook path as fallback (VS Code only)
|
36
|
-
|
37
|
-
if
|
36
|
+
current_notebook_path = globals().get("__vsc_ipynb_file__")
|
37
|
+
if path is not None:
|
38
|
+
with open(path, mode="r", encoding="utf-8") as f:
|
39
|
+
data = nbformat.read(f, as_version=4)
|
40
|
+
self.data = json.loads(json.dumps(data))
|
41
|
+
elif data is not None:
|
38
42
|
nbformat.validate(data)
|
39
43
|
self.data = data
|
40
|
-
elif
|
41
|
-
with open(
|
44
|
+
elif current_notebook_path is not None:
|
45
|
+
with open(current_notebook_path, mode="r", encoding="utf-8") as f:
|
42
46
|
data = nbformat.read(f, as_version=4)
|
43
47
|
self.data = json.loads(json.dumps(data))
|
44
48
|
else:
|
@@ -130,15 +134,6 @@ class Notebook(Base):
|
|
130
134
|
|
131
135
|
nbformat.write(nbformat.from_dict(self.data), fp=path)
|
132
136
|
|
133
|
-
def print(self):
|
134
|
-
"""
|
135
|
-
Print the notebook.
|
136
|
-
"""
|
137
|
-
from rich import print_json
|
138
|
-
import json
|
139
|
-
|
140
|
-
print_json(json.dumps(self.to_dict()))
|
141
|
-
|
142
137
|
def __repr__(self):
|
143
138
|
"""
|
144
139
|
Return representation of Notebook.
|
@@ -250,6 +245,16 @@ class Notebook(Base):
|
|
250
245
|
lines.append(f'nb = Notebook(data={self.data}, name="""{self.name}""")')
|
251
246
|
return lines
|
252
247
|
|
248
|
+
def to_latex(self, filename: str):
|
249
|
+
"""
|
250
|
+
Convert notebook to LaTeX and create a folder with all necessary components.
|
251
|
+
|
252
|
+
:param filename: Name of the output folder and main tex file (without extension)
|
253
|
+
"""
|
254
|
+
from edsl.notebooks.NotebookToLaTeX import NotebookToLaTeX
|
255
|
+
|
256
|
+
NotebookToLaTeX(self).convert(filename)
|
257
|
+
|
253
258
|
|
254
259
|
if __name__ == "__main__":
|
255
260
|
from edsl import Notebook
|
@@ -0,0 +1,142 @@
|
|
1
|
+
from typing import Optional, Dict
|
2
|
+
import os
|
3
|
+
import nbformat
|
4
|
+
from nbconvert.exporters import LatexExporter
|
5
|
+
from nbconvert.writers import FilesWriter
|
6
|
+
|
7
|
+
|
8
|
+
class NotebookToLaTeX:
|
9
|
+
"""
|
10
|
+
A class for converting Jupyter notebooks to LaTeX with proper directory structure.
|
11
|
+
"""
|
12
|
+
|
13
|
+
def __init__(self, notebook):
|
14
|
+
"""
|
15
|
+
Initialize with a Notebook instance.
|
16
|
+
|
17
|
+
:param notebook: An instance of the Notebook class
|
18
|
+
"""
|
19
|
+
self.notebook = notebook
|
20
|
+
self.latex_exporter = LatexExporter()
|
21
|
+
self._configure_exporter()
|
22
|
+
|
23
|
+
def _configure_exporter(self):
|
24
|
+
"""Configure the LaTeX exporter with default settings."""
|
25
|
+
self.latex_exporter.exclude_input_prompt = True
|
26
|
+
self.latex_exporter.exclude_output_prompt = True
|
27
|
+
self.latex_exporter.template_name = "classic"
|
28
|
+
|
29
|
+
def _create_makefile(self, filename: str, output_dir: str):
|
30
|
+
"""Create a Makefile for the LaTeX project."""
|
31
|
+
makefile_content = f"""# Makefile for {filename}
|
32
|
+
all: pdf
|
33
|
+
|
34
|
+
pdf: {filename}.pdf
|
35
|
+
|
36
|
+
{filename}.pdf: {filename}.tex
|
37
|
+
\tpdflatex {filename}.tex
|
38
|
+
\tpdflatex {filename}.tex # Run twice for references
|
39
|
+
\tbibtex {filename} # Run bibtex if needed
|
40
|
+
\tpdflatex {filename}.tex # Run one more time for bibtex
|
41
|
+
|
42
|
+
clean:
|
43
|
+
\trm -f *.aux *.log *.out *.toc *.pdf *.bbl *.blg
|
44
|
+
"""
|
45
|
+
makefile_path = os.path.join(output_dir, "Makefile")
|
46
|
+
with open(makefile_path, "w") as f:
|
47
|
+
f.write(makefile_content)
|
48
|
+
|
49
|
+
def _create_readme(self, filename: str, output_dir: str):
|
50
|
+
"""Create a README file with usage instructions."""
|
51
|
+
readme_content = f"""# {filename}
|
52
|
+
|
53
|
+
This folder contains the LaTeX version of your Jupyter notebook.
|
54
|
+
|
55
|
+
Files:
|
56
|
+
- {filename}.tex: Main LaTeX file
|
57
|
+
- Makefile: Build automation
|
58
|
+
|
59
|
+
To compile the PDF:
|
60
|
+
1. Make sure you have a LaTeX distribution installed (e.g., TexLive)
|
61
|
+
2. Run `make` in this directory
|
62
|
+
3. The output will be {filename}.pdf
|
63
|
+
|
64
|
+
To clean up build files:
|
65
|
+
- Run `make clean`
|
66
|
+
"""
|
67
|
+
readme_path = os.path.join(output_dir, "README.md")
|
68
|
+
with open(readme_path, "w") as f:
|
69
|
+
f.write(readme_content)
|
70
|
+
|
71
|
+
def convert(self, filename: str, output_dir: Optional[str] = None):
|
72
|
+
"""
|
73
|
+
Convert the notebook to LaTeX and create a project directory.
|
74
|
+
|
75
|
+
:param filename: Name for the output files (without extension)
|
76
|
+
:param output_dir: Optional directory path. If None, uses filename as directory
|
77
|
+
"""
|
78
|
+
# Use filename as directory if no output_dir specified
|
79
|
+
output_dir = output_dir or filename
|
80
|
+
|
81
|
+
# Create output directory
|
82
|
+
os.makedirs(output_dir, exist_ok=True)
|
83
|
+
|
84
|
+
# Convert notebook to nbformat
|
85
|
+
notebook_node = nbformat.from_dict(self.notebook.data)
|
86
|
+
|
87
|
+
# Convert to LaTeX
|
88
|
+
body, resources = self.latex_exporter.from_notebook_node(notebook_node)
|
89
|
+
|
90
|
+
# Write the main tex file
|
91
|
+
output_file_path = os.path.join(output_dir, f"{filename}.tex")
|
92
|
+
with open(output_file_path, "w", encoding="utf-8") as f:
|
93
|
+
f.write(body)
|
94
|
+
|
95
|
+
# Write additional resources (images, etc.)
|
96
|
+
if resources.get("outputs"):
|
97
|
+
for fname, data in resources["outputs"].items():
|
98
|
+
resource_path = os.path.join(output_dir, fname)
|
99
|
+
with open(resource_path, "wb") as f:
|
100
|
+
f.write(data)
|
101
|
+
|
102
|
+
# Create supporting files
|
103
|
+
self._create_makefile(filename, output_dir)
|
104
|
+
self._create_readme(filename, output_dir)
|
105
|
+
|
106
|
+
def set_template(self, template_name: str):
|
107
|
+
"""
|
108
|
+
Set the LaTeX template to use.
|
109
|
+
|
110
|
+
:param template_name: Name of the template (e.g., 'classic', 'article')
|
111
|
+
"""
|
112
|
+
self.latex_exporter.template_name = template_name
|
113
|
+
|
114
|
+
def set_template_options(self, options: Dict):
|
115
|
+
"""
|
116
|
+
Set additional template options.
|
117
|
+
|
118
|
+
:param options: Dictionary of template options
|
119
|
+
"""
|
120
|
+
for key, value in options.items():
|
121
|
+
setattr(self.latex_exporter, key, value)
|
122
|
+
|
123
|
+
|
124
|
+
# Example usage:
|
125
|
+
if __name__ == "__main__":
|
126
|
+
from edsl import Notebook
|
127
|
+
|
128
|
+
# Create or load a notebook
|
129
|
+
notebook = Notebook.example()
|
130
|
+
|
131
|
+
# Create converter and convert
|
132
|
+
converter = NotebookToLaTeX(notebook)
|
133
|
+
converter.convert("example_output")
|
134
|
+
|
135
|
+
# Example with custom template options
|
136
|
+
converter.set_template_options(
|
137
|
+
{
|
138
|
+
"exclude_input": True, # Hide input cells
|
139
|
+
"exclude_output": False, # Show output cells
|
140
|
+
}
|
141
|
+
)
|
142
|
+
converter.convert("example_output_custom")
|