edsl 0.1.39.dev3__py3-none-any.whl → 0.1.39.dev5__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 +413 -332
- edsl/BaseDiff.py +260 -260
- edsl/TemplateLoader.py +24 -24
- edsl/__init__.py +57 -49
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +1071 -867
- edsl/agents/AgentList.py +551 -413
- edsl/agents/Invigilator.py +284 -233
- edsl/agents/InvigilatorBase.py +257 -270
- edsl/agents/PromptConstructor.py +272 -354
- edsl/agents/QuestionInstructionPromptBuilder.py +128 -0
- edsl/agents/QuestionTemplateReplacementsBuilder.py +137 -0
- edsl/agents/__init__.py +2 -3
- edsl/agents/descriptors.py +99 -99
- edsl/agents/prompt_helpers.py +129 -129
- edsl/agents/question_option_processor.py +172 -0
- edsl/auto/AutoStudy.py +130 -117
- edsl/auto/StageBase.py +243 -230
- edsl/auto/StageGenerateSurvey.py +178 -178
- edsl/auto/StageLabelQuestions.py +125 -125
- edsl/auto/StagePersona.py +61 -61
- edsl/auto/StagePersonaDimensionValueRanges.py +88 -88
- edsl/auto/StagePersonaDimensionValues.py +74 -74
- edsl/auto/StagePersonaDimensions.py +69 -69
- edsl/auto/StageQuestions.py +74 -73
- edsl/auto/SurveyCreatorPipeline.py +21 -21
- edsl/auto/utilities.py +218 -224
- edsl/base/Base.py +279 -279
- edsl/config.py +177 -157
- edsl/conversation/Conversation.py +290 -290
- edsl/conversation/car_buying.py +59 -58
- edsl/conversation/chips.py +95 -95
- edsl/conversation/mug_negotiation.py +81 -81
- edsl/conversation/next_speaker_utilities.py +93 -93
- edsl/coop/CoopFunctionsMixin.py +15 -0
- edsl/coop/ExpectedParrotKeyHandler.py +125 -0
- edsl/coop/PriceFetcher.py +54 -54
- edsl/coop/__init__.py +2 -2
- edsl/coop/coop.py +1106 -1028
- edsl/coop/utils.py +131 -131
- edsl/data/Cache.py +573 -555
- edsl/data/CacheEntry.py +230 -233
- edsl/data/CacheHandler.py +168 -149
- edsl/data/RemoteCacheSync.py +186 -78
- edsl/data/SQLiteDict.py +292 -292
- edsl/data/__init__.py +5 -4
- edsl/data/orm.py +10 -10
- edsl/data_transfer_models.py +74 -73
- edsl/enums.py +202 -175
- edsl/exceptions/BaseException.py +21 -21
- edsl/exceptions/__init__.py +54 -54
- edsl/exceptions/agents.py +54 -42
- edsl/exceptions/cache.py +5 -5
- edsl/exceptions/configuration.py +16 -16
- edsl/exceptions/coop.py +10 -10
- edsl/exceptions/data.py +14 -14
- edsl/exceptions/general.py +34 -34
- edsl/exceptions/inference_services.py +5 -0
- edsl/exceptions/jobs.py +33 -33
- edsl/exceptions/language_models.py +63 -63
- edsl/exceptions/prompts.py +15 -15
- edsl/exceptions/questions.py +109 -91
- edsl/exceptions/results.py +29 -29
- edsl/exceptions/scenarios.py +29 -22
- edsl/exceptions/surveys.py +37 -37
- edsl/inference_services/AnthropicService.py +106 -87
- edsl/inference_services/AvailableModelCacheHandler.py +184 -0
- edsl/inference_services/AvailableModelFetcher.py +215 -0
- edsl/inference_services/AwsBedrock.py +118 -120
- edsl/inference_services/AzureAI.py +215 -217
- edsl/inference_services/DeepInfraService.py +18 -18
- edsl/inference_services/GoogleService.py +143 -148
- edsl/inference_services/GroqService.py +20 -20
- edsl/inference_services/InferenceServiceABC.py +80 -147
- edsl/inference_services/InferenceServicesCollection.py +138 -97
- edsl/inference_services/MistralAIService.py +120 -123
- edsl/inference_services/OllamaService.py +18 -18
- edsl/inference_services/OpenAIService.py +236 -224
- edsl/inference_services/PerplexityService.py +160 -163
- edsl/inference_services/ServiceAvailability.py +135 -0
- edsl/inference_services/TestService.py +90 -89
- edsl/inference_services/TogetherAIService.py +172 -170
- edsl/inference_services/data_structures.py +134 -0
- edsl/inference_services/models_available_cache.py +118 -118
- edsl/inference_services/rate_limits_cache.py +25 -25
- edsl/inference_services/registry.py +41 -41
- edsl/inference_services/write_available.py +10 -10
- edsl/jobs/AnswerQuestionFunctionConstructor.py +223 -0
- edsl/jobs/Answers.py +43 -56
- edsl/jobs/FetchInvigilator.py +47 -0
- edsl/jobs/InterviewTaskManager.py +98 -0
- edsl/jobs/InterviewsConstructor.py +50 -0
- edsl/jobs/Jobs.py +823 -898
- edsl/jobs/JobsChecks.py +172 -147
- edsl/jobs/JobsComponentConstructor.py +189 -0
- edsl/jobs/JobsPrompts.py +270 -268
- edsl/jobs/JobsRemoteInferenceHandler.py +311 -239
- edsl/jobs/JobsRemoteInferenceLogger.py +239 -0
- edsl/jobs/RequestTokenEstimator.py +30 -0
- edsl/jobs/__init__.py +1 -1
- edsl/jobs/async_interview_runner.py +138 -0
- edsl/jobs/buckets/BucketCollection.py +104 -63
- edsl/jobs/buckets/ModelBuckets.py +65 -65
- edsl/jobs/buckets/TokenBucket.py +283 -251
- 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 +396 -661
- edsl/jobs/interviews/InterviewExceptionCollection.py +99 -99
- edsl/jobs/interviews/InterviewExceptionEntry.py +186 -186
- edsl/jobs/interviews/InterviewStatistic.py +63 -63
- edsl/jobs/interviews/InterviewStatisticsCollection.py +25 -25
- edsl/jobs/interviews/InterviewStatusDictionary.py +78 -78
- edsl/jobs/interviews/InterviewStatusLog.py +92 -92
- edsl/jobs/interviews/ReportErrors.py +66 -66
- edsl/jobs/interviews/interview_status_enum.py +9 -9
- edsl/jobs/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 -466
- edsl/jobs/runners/JobsRunnerStatus.py +297 -330
- edsl/jobs/tasks/QuestionTaskCreator.py +244 -242
- edsl/jobs/tasks/TaskCreators.py +64 -64
- edsl/jobs/tasks/TaskHistory.py +470 -450
- edsl/jobs/tasks/TaskStatusLog.py +23 -23
- edsl/jobs/tasks/task_status_enum.py +161 -163
- edsl/jobs/tokens/InterviewTokenUsage.py +27 -27
- edsl/jobs/tokens/TokenUsage.py +34 -34
- edsl/language_models/ComputeCost.py +63 -0
- edsl/language_models/LanguageModel.py +626 -668
- edsl/language_models/ModelList.py +164 -155
- edsl/language_models/PriceManager.py +127 -0
- edsl/language_models/RawResponseHandler.py +106 -0
- edsl/language_models/RegisterLanguageModelsMeta.py +184 -184
- edsl/language_models/ServiceDataSources.py +0 -0
- edsl/language_models/__init__.py +2 -3
- edsl/language_models/fake_openai_call.py +15 -15
- edsl/language_models/fake_openai_service.py +61 -61
- 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 +156 -156
- edsl/language_models/utilities.py +65 -64
- edsl/notebooks/Notebook.py +263 -258
- edsl/notebooks/NotebookToLaTeX.py +142 -0
- edsl/notebooks/__init__.py +1 -1
- edsl/prompts/Prompt.py +352 -362
- edsl/prompts/__init__.py +2 -2
- edsl/questions/ExceptionExplainer.py +77 -0
- edsl/questions/HTMLQuestion.py +103 -0
- edsl/questions/QuestionBase.py +518 -664
- edsl/questions/QuestionBasePromptsMixin.py +221 -217
- edsl/questions/QuestionBudget.py +227 -227
- edsl/questions/QuestionCheckBox.py +359 -359
- edsl/questions/QuestionExtract.py +180 -182
- edsl/questions/QuestionFreeText.py +113 -114
- edsl/questions/QuestionFunctional.py +166 -166
- edsl/questions/QuestionList.py +223 -231
- edsl/questions/QuestionMatrix.py +265 -0
- edsl/questions/QuestionMultipleChoice.py +330 -286
- edsl/questions/QuestionNumerical.py +151 -153
- edsl/questions/QuestionRank.py +314 -324
- edsl/questions/Quick.py +41 -41
- edsl/questions/SimpleAskMixin.py +74 -73
- edsl/questions/__init__.py +27 -26
- edsl/questions/{AnswerValidatorMixin.py → answer_validator_mixin.py} +334 -289
- edsl/questions/compose_questions.py +98 -98
- edsl/questions/data_structures.py +20 -0
- edsl/questions/decorators.py +21 -21
- edsl/questions/derived/QuestionLikertFive.py +76 -76
- edsl/questions/derived/QuestionLinearScale.py +90 -87
- edsl/questions/derived/QuestionTopK.py +93 -93
- edsl/questions/derived/QuestionYesNo.py +82 -82
- edsl/questions/descriptors.py +427 -413
- edsl/questions/loop_processor.py +149 -0
- edsl/questions/prompt_templates/question_budget.jinja +13 -13
- edsl/questions/prompt_templates/question_checkbox.jinja +32 -32
- edsl/questions/prompt_templates/question_extract.jinja +11 -11
- edsl/questions/prompt_templates/question_free_text.jinja +3 -3
- edsl/questions/prompt_templates/question_linear_scale.jinja +11 -11
- edsl/questions/prompt_templates/question_list.jinja +17 -17
- edsl/questions/prompt_templates/question_multiple_choice.jinja +33 -33
- edsl/questions/prompt_templates/question_numerical.jinja +36 -36
- edsl/questions/{QuestionBaseGenMixin.py → question_base_gen_mixin.py} +168 -161
- edsl/questions/question_registry.py +177 -177
- edsl/questions/{RegisterQuestionsMeta.py → register_questions_meta.py} +71 -71
- edsl/questions/{ResponseValidatorABC.py → response_validator_abc.py} +188 -174
- edsl/questions/response_validator_factory.py +34 -0
- edsl/questions/settings.py +12 -12
- edsl/questions/templates/budget/answering_instructions.jinja +7 -7
- edsl/questions/templates/budget/question_presentation.jinja +7 -7
- edsl/questions/templates/checkbox/answering_instructions.jinja +10 -10
- edsl/questions/templates/checkbox/question_presentation.jinja +22 -22
- edsl/questions/templates/extract/answering_instructions.jinja +7 -7
- edsl/questions/templates/likert_five/answering_instructions.jinja +10 -10
- edsl/questions/templates/likert_five/question_presentation.jinja +11 -11
- edsl/questions/templates/linear_scale/answering_instructions.jinja +5 -5
- edsl/questions/templates/linear_scale/question_presentation.jinja +5 -5
- edsl/questions/templates/list/answering_instructions.jinja +3 -3
- edsl/questions/templates/list/question_presentation.jinja +5 -5
- edsl/questions/templates/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/answering_instructions.jinja +9 -9
- edsl/questions/templates/multiple_choice/question_presentation.jinja +11 -11
- edsl/questions/templates/numerical/answering_instructions.jinja +6 -6
- edsl/questions/templates/numerical/question_presentation.jinja +6 -6
- edsl/questions/templates/rank/answering_instructions.jinja +11 -11
- edsl/questions/templates/rank/question_presentation.jinja +15 -15
- edsl/questions/templates/top_k/answering_instructions.jinja +8 -8
- edsl/questions/templates/top_k/question_presentation.jinja +22 -22
- edsl/questions/templates/yes_no/answering_instructions.jinja +6 -6
- edsl/questions/templates/yes_no/question_presentation.jinja +11 -11
- edsl/results/CSSParameterizer.py +108 -108
- edsl/results/Dataset.py +587 -424
- edsl/results/DatasetExportMixin.py +594 -731
- edsl/results/DatasetTree.py +295 -275
- edsl/results/MarkdownToDocx.py +122 -0
- edsl/results/MarkdownToPDF.py +111 -0
- edsl/results/Result.py +557 -465
- edsl/results/Results.py +1183 -1165
- edsl/results/ResultsExportMixin.py +45 -43
- edsl/results/ResultsGGMixin.py +121 -121
- edsl/results/TableDisplay.py +125 -198
- edsl/results/TextEditor.py +50 -0
- edsl/results/__init__.py +2 -2
- edsl/results/file_exports.py +252 -0
- edsl/results/{ResultsFetchMixin.py → results_fetch_mixin.py} +33 -33
- edsl/results/{Selector.py → results_selector.py} +145 -135
- edsl/results/{ResultsToolsMixin.py → results_tools_mixin.py} +98 -98
- edsl/results/smart_objects.py +96 -0
- edsl/results/table_data_class.py +12 -0
- edsl/results/table_display.css +77 -77
- edsl/results/table_renderers.py +118 -0
- edsl/results/tree_explore.py +115 -115
- edsl/scenarios/ConstructDownloadLink.py +109 -0
- edsl/scenarios/DocumentChunker.py +102 -0
- edsl/scenarios/DocxScenario.py +16 -0
- edsl/scenarios/FileStore.py +511 -632
- edsl/scenarios/PdfExtractor.py +40 -0
- edsl/scenarios/Scenario.py +498 -601
- edsl/scenarios/ScenarioHtmlMixin.py +65 -64
- edsl/scenarios/ScenarioList.py +1458 -1287
- edsl/scenarios/ScenarioListExportMixin.py +45 -52
- edsl/scenarios/ScenarioListPdfMixin.py +239 -261
- edsl/scenarios/__init__.py +3 -4
- 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 +38 -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} +131 -127
- edsl/scenarios/scenario_selector.py +156 -0
- edsl/shared.py +1 -1
- edsl/study/ObjectEntry.py +173 -173
- edsl/study/ProofOfWork.py +113 -113
- edsl/study/SnapShot.py +80 -80
- edsl/study/Study.py +521 -528
- edsl/study/__init__.py +4 -4
- edsl/surveys/ConstructDAG.py +92 -0
- edsl/surveys/DAG.py +148 -148
- edsl/surveys/EditSurvey.py +221 -0
- edsl/surveys/InstructionHandler.py +100 -0
- edsl/surveys/Memory.py +31 -31
- edsl/surveys/MemoryManagement.py +72 -0
- edsl/surveys/MemoryPlan.py +244 -244
- edsl/surveys/Rule.py +327 -326
- edsl/surveys/RuleCollection.py +385 -387
- edsl/surveys/RuleManager.py +172 -0
- edsl/surveys/Simulator.py +75 -0
- edsl/surveys/Survey.py +1280 -1801
- edsl/surveys/SurveyCSS.py +273 -261
- edsl/surveys/SurveyExportMixin.py +259 -259
- edsl/surveys/{SurveyFlowVisualizationMixin.py → SurveyFlowVisualization.py} +181 -179
- edsl/surveys/SurveyQualtricsImport.py +284 -284
- edsl/surveys/SurveyToApp.py +141 -0
- edsl/surveys/__init__.py +5 -3
- edsl/surveys/base.py +53 -53
- edsl/surveys/descriptors.py +60 -56
- edsl/surveys/instructions/ChangeInstruction.py +48 -49
- edsl/surveys/instructions/Instruction.py +56 -65
- edsl/surveys/instructions/InstructionCollection.py +82 -77
- edsl/templates/error_reporting/base.html +23 -23
- edsl/templates/error_reporting/exceptions_by_model.html +34 -34
- edsl/templates/error_reporting/exceptions_by_question_name.html +16 -16
- edsl/templates/error_reporting/exceptions_by_type.html +16 -16
- edsl/templates/error_reporting/interview_details.html +115 -115
- edsl/templates/error_reporting/interviews.html +19 -19
- edsl/templates/error_reporting/overview.html +4 -4
- edsl/templates/error_reporting/performance_plot.html +1 -1
- edsl/templates/error_reporting/report.css +73 -73
- edsl/templates/error_reporting/report.html +117 -117
- edsl/templates/error_reporting/report.js +25 -25
- edsl/tools/__init__.py +1 -1
- edsl/tools/clusters.py +192 -192
- edsl/tools/embeddings.py +27 -27
- edsl/tools/embeddings_plotting.py +118 -118
- edsl/tools/plotting.py +112 -112
- edsl/tools/summarize.py +18 -18
- edsl/utilities/PrettyList.py +56 -0
- edsl/utilities/SystemInfo.py +28 -28
- edsl/utilities/__init__.py +22 -22
- edsl/utilities/ast_utilities.py +25 -25
- edsl/utilities/data/Registry.py +6 -6
- edsl/utilities/data/__init__.py +1 -1
- edsl/utilities/data/scooter_results.json +1 -1
- edsl/utilities/decorators.py +77 -77
- edsl/utilities/gcp_bucket/cloud_storage.py +96 -96
- edsl/utilities/interface.py +627 -627
- edsl/utilities/is_notebook.py +18 -0
- edsl/utilities/is_valid_variable_name.py +11 -0
- edsl/utilities/naming_utilities.py +263 -263
- edsl/utilities/remove_edsl_version.py +24 -0
- edsl/utilities/repair_functions.py +28 -28
- edsl/utilities/restricted_python.py +70 -70
- edsl/utilities/utilities.py +436 -424
- {edsl-0.1.39.dev3.dist-info → edsl-0.1.39.dev5.dist-info}/LICENSE +21 -21
- {edsl-0.1.39.dev3.dist-info → edsl-0.1.39.dev5.dist-info}/METADATA +13 -11
- edsl-0.1.39.dev5.dist-info/RECORD +358 -0
- {edsl-0.1.39.dev3.dist-info → edsl-0.1.39.dev5.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.39.dev3.dist-info/RECORD +0 -277
@@ -0,0 +1,273 @@
|
|
1
|
+
from typing import Optional, List
|
2
|
+
from collections import UserDict
|
3
|
+
import os
|
4
|
+
from functools import lru_cache
|
5
|
+
from dataclasses import dataclass, asdict
|
6
|
+
|
7
|
+
from edsl.enums import service_to_api_keyname
|
8
|
+
from edsl.exceptions.general import MissingAPIKeyError
|
9
|
+
|
10
|
+
from edsl.language_models.key_management.KeyLookup import KeyLookup
|
11
|
+
|
12
|
+
from edsl.language_models.key_management.models import (
|
13
|
+
APIKeyEntry,
|
14
|
+
LimitEntry,
|
15
|
+
APIIDEntry,
|
16
|
+
LanguageModelInput,
|
17
|
+
)
|
18
|
+
|
19
|
+
service_to_api_keyname["bedrock"] = "AWS_SECRET_ACCESS_KEY"
|
20
|
+
service_to_api_id = {"bedrock": "AWS_ACCESS_KEY_ID"}
|
21
|
+
|
22
|
+
api_keyname_to_service = {}
|
23
|
+
|
24
|
+
for service, key in service_to_api_keyname.items():
|
25
|
+
if isinstance(key, list):
|
26
|
+
for k in key:
|
27
|
+
api_keyname_to_service[k] = service
|
28
|
+
else:
|
29
|
+
api_keyname_to_service[key] = service
|
30
|
+
|
31
|
+
api_id_to_service = {"AWS_ACCESS_KEY_ID": "bedrock"}
|
32
|
+
|
33
|
+
|
34
|
+
class KeyLookupBuilder:
|
35
|
+
"""Builds KeyLookup options.
|
36
|
+
|
37
|
+
>>> builder = KeyLookupBuilder(fetch_order=("config", "env"))
|
38
|
+
>>> builder.DEFAULT_RPM
|
39
|
+
10
|
40
|
+
>>> builder.DEFAULT_TPM
|
41
|
+
2000000
|
42
|
+
>>> builder.fetch_order
|
43
|
+
('config', 'env')
|
44
|
+
|
45
|
+
Test invalid fetch_order:
|
46
|
+
>>> try:
|
47
|
+
... KeyLookupBuilder(fetch_order=["config", "env"]) # Should be tuple
|
48
|
+
... except ValueError as e:
|
49
|
+
... str(e)
|
50
|
+
'fetch_order must be a tuple'
|
51
|
+
|
52
|
+
Test service extraction:
|
53
|
+
>>> builder.extract_service("EDSL_SERVICE_RPM_OPENAI")
|
54
|
+
('openai', 'rpm')
|
55
|
+
"""
|
56
|
+
|
57
|
+
DEFAULT_RPM = 10
|
58
|
+
DEFAULT_TPM = 2000000
|
59
|
+
|
60
|
+
def __init__(self, fetch_order: Optional[tuple[str]] = None):
|
61
|
+
if fetch_order is None:
|
62
|
+
self.fetch_order = ("config", "env")
|
63
|
+
else:
|
64
|
+
self.fetch_order = fetch_order
|
65
|
+
|
66
|
+
if not isinstance(self.fetch_order, tuple):
|
67
|
+
raise ValueError("fetch_order must be a tuple")
|
68
|
+
|
69
|
+
self.limit_data = {}
|
70
|
+
self.key_data = {}
|
71
|
+
self.id_data = {}
|
72
|
+
self.process_key_value_pairs()
|
73
|
+
|
74
|
+
@property
|
75
|
+
def known_services(self):
|
76
|
+
"""Get the set of known services.
|
77
|
+
|
78
|
+
>>> builder = KeyLookupBuilder()
|
79
|
+
>>> isinstance(builder.known_services, set)
|
80
|
+
True
|
81
|
+
"""
|
82
|
+
return set(self.key_data.keys()) | set(self.limit_data.keys())
|
83
|
+
|
84
|
+
@lru_cache
|
85
|
+
def build(self) -> "KeyLookup":
|
86
|
+
"""Build a KeyLookup instance.
|
87
|
+
|
88
|
+
>>> builder = KeyLookupBuilder()
|
89
|
+
>>> lookup = builder.build()
|
90
|
+
>>> isinstance(lookup, KeyLookup)
|
91
|
+
True
|
92
|
+
>>> lookup['test'].api_token # Test service should always exist
|
93
|
+
'test'
|
94
|
+
"""
|
95
|
+
d = {}
|
96
|
+
for service in self.known_services:
|
97
|
+
try:
|
98
|
+
d[service] = self.get_language_model_input(service)
|
99
|
+
except MissingAPIKeyError:
|
100
|
+
pass
|
101
|
+
|
102
|
+
d.update({"test": LanguageModelInput(api_token="test", rpm=10, tpm=2000000)})
|
103
|
+
return KeyLookup(d)
|
104
|
+
|
105
|
+
def get_language_model_input(self, service: str) -> LanguageModelInput:
|
106
|
+
"""Get the language model input for a given service.
|
107
|
+
|
108
|
+
>>> builder = KeyLookupBuilder()
|
109
|
+
>>> try:
|
110
|
+
... builder.get_language_model_input("nonexistent_service")
|
111
|
+
... except MissingAPIKeyError as e:
|
112
|
+
... str(e)
|
113
|
+
"No key found for service 'nonexistent_service'"
|
114
|
+
"""
|
115
|
+
if (key_entries := self.key_data.get(service)) is None:
|
116
|
+
raise MissingAPIKeyError(f"No key found for service '{service}'")
|
117
|
+
|
118
|
+
if len(key_entries) == 1:
|
119
|
+
api_key_entry = key_entries[0]
|
120
|
+
|
121
|
+
id_entry = self.id_data.get(service)
|
122
|
+
id_source = id_entry.source if id_entry is not None else None
|
123
|
+
api_id = id_entry.value if id_entry is not None else None
|
124
|
+
|
125
|
+
if (limit_entry := self.limit_data.get(service)) is None:
|
126
|
+
limit_entry = LimitEntry(
|
127
|
+
service=service,
|
128
|
+
rpm=self.DEFAULT_RPM,
|
129
|
+
tpm=self.DEFAULT_TPM,
|
130
|
+
source="default",
|
131
|
+
)
|
132
|
+
|
133
|
+
if limit_entry.rpm is None:
|
134
|
+
limit_entry.rpm = self.DEFAULT_RPM
|
135
|
+
if limit_entry.tpm is None:
|
136
|
+
limit_entry.tpm = self.DEFAULT_TPM
|
137
|
+
|
138
|
+
return LanguageModelInput(
|
139
|
+
api_token=api_key_entry.value,
|
140
|
+
rpm=int(limit_entry.rpm),
|
141
|
+
tpm=int(limit_entry.tpm),
|
142
|
+
api_id=api_id,
|
143
|
+
token_source=api_key_entry.source,
|
144
|
+
limit_source=limit_entry.source,
|
145
|
+
id_source=id_source,
|
146
|
+
)
|
147
|
+
|
148
|
+
def __repr__(self):
|
149
|
+
return f"DataSource(key_data={self.key_data}, limit_data={self.limit_data}, id_data={self.id_data})"
|
150
|
+
|
151
|
+
def _os_env_key_value_pairs(self):
|
152
|
+
return dict(list(os.environ.items()))
|
153
|
+
|
154
|
+
def _coop_key_value_pairs(self):
|
155
|
+
from edsl.coop import Coop
|
156
|
+
|
157
|
+
c = Coop()
|
158
|
+
return dict(list(c.fetch_rate_limit_config_vars().items()))
|
159
|
+
|
160
|
+
def _config_key_value_pairs(self):
|
161
|
+
from edsl.config import CONFIG
|
162
|
+
|
163
|
+
return dict(list(CONFIG.items()))
|
164
|
+
|
165
|
+
@staticmethod
|
166
|
+
def extract_service(key: str) -> str:
|
167
|
+
"""Extract the service and limit type from the key"""
|
168
|
+
limit_type, service_raw = key.replace("EDSL_SERVICE_", "").split("_")
|
169
|
+
return service_raw.lower(), limit_type.lower()
|
170
|
+
|
171
|
+
def get_key_value_pairs(self) -> dict:
|
172
|
+
"""Get key-value pairs from configured sources."""
|
173
|
+
fetching_functions = {
|
174
|
+
"env": self._os_env_key_value_pairs,
|
175
|
+
"coop": self._coop_key_value_pairs,
|
176
|
+
"config": self._config_key_value_pairs,
|
177
|
+
}
|
178
|
+
d = {}
|
179
|
+
for source in self.fetch_order:
|
180
|
+
f = fetching_functions[source]
|
181
|
+
new_data = f()
|
182
|
+
for k, v in new_data.items():
|
183
|
+
d[k] = (v, source)
|
184
|
+
return d
|
185
|
+
|
186
|
+
def _entry_type(self, key, value) -> str:
|
187
|
+
"""Determine the type of entry from a key.
|
188
|
+
|
189
|
+
>>> builder = KeyLookupBuilder()
|
190
|
+
>>> builder._entry_type("EDSL_SERVICE_RPM_OPENAI", "60")
|
191
|
+
'limit'
|
192
|
+
>>> builder._entry_type("OPENAI_API_KEY", "sk-1234")
|
193
|
+
'api_key'
|
194
|
+
>>> builder._entry_type("AWS_ACCESS_KEY_ID", "AKIA1234")
|
195
|
+
'api_id'
|
196
|
+
>>> builder._entry_type("UNKNOWN_KEY", "value")
|
197
|
+
'unknown'
|
198
|
+
"""
|
199
|
+
if key.startswith("EDSL_SERVICE_"):
|
200
|
+
return "limit"
|
201
|
+
elif key in api_keyname_to_service:
|
202
|
+
return "api_key"
|
203
|
+
elif key in api_id_to_service:
|
204
|
+
return "api_id"
|
205
|
+
return "unknown"
|
206
|
+
|
207
|
+
def _add_id(self, key: str, value: str, source: str) -> None:
|
208
|
+
"""Add an API ID to the id_data dictionary.
|
209
|
+
|
210
|
+
>>> builder = KeyLookupBuilder()
|
211
|
+
>>> builder._add_id("AWS_ACCESS_KEY_ID", "AKIA1234", "env")
|
212
|
+
>>> builder.id_data["bedrock"].value
|
213
|
+
'AKIA1234'
|
214
|
+
>>> try:
|
215
|
+
... builder._add_id("AWS_ACCESS_KEY_ID", "AKIA5678", "env")
|
216
|
+
... except ValueError as e:
|
217
|
+
... str(e)
|
218
|
+
'Duplicate ID for service bedrock'
|
219
|
+
"""
|
220
|
+
service = api_id_to_service[key]
|
221
|
+
if service not in self.id_data:
|
222
|
+
self.id_data[service] = APIIDEntry(
|
223
|
+
service=service, name=key, value=value, source=source
|
224
|
+
)
|
225
|
+
else:
|
226
|
+
raise ValueError(f"Duplicate ID for service {service}")
|
227
|
+
|
228
|
+
def _add_limit(self, key: str, value: str, source: str) -> None:
|
229
|
+
"""Add a rate limit entry to the limit_data dictionary.
|
230
|
+
|
231
|
+
>>> builder = KeyLookupBuilder()
|
232
|
+
>>> builder._add_limit("EDSL_SERVICE_RPM_OPENAI", "60", "config")
|
233
|
+
>>> builder.limit_data["openai"].rpm
|
234
|
+
'60'
|
235
|
+
>>> builder._add_limit("EDSL_SERVICE_TPM_OPENAI", "100000", "config")
|
236
|
+
>>> builder.limit_data["openai"].tpm
|
237
|
+
'100000'
|
238
|
+
"""
|
239
|
+
service, limit_type = self.extract_service(key)
|
240
|
+
if service in self.limit_data:
|
241
|
+
setattr(self.limit_data[service], limit_type.lower(), value)
|
242
|
+
else:
|
243
|
+
new_limit_entry = LimitEntry(
|
244
|
+
service=service, rpm=None, tpm=None, source=source
|
245
|
+
)
|
246
|
+
setattr(new_limit_entry, limit_type.lower(), value)
|
247
|
+
self.limit_data[service] = new_limit_entry
|
248
|
+
|
249
|
+
def _add_api_key(self, key: str, value: str, source: str) -> None:
|
250
|
+
"""Add an API key entry to the key_data dictionary.
|
251
|
+
|
252
|
+
>>> builder = KeyLookupBuilder()
|
253
|
+
>>> builder._add_api_key("OPENAI_API_KEY", "sk-1234", "env")
|
254
|
+
>>> 'sk-1234' == builder.key_data["openai"][-1].value
|
255
|
+
True
|
256
|
+
"""
|
257
|
+
service = api_keyname_to_service[key]
|
258
|
+
new_entry = APIKeyEntry(service=service, name=key, value=value, source=source)
|
259
|
+
if service not in self.key_data:
|
260
|
+
self.key_data[service] = [new_entry]
|
261
|
+
else:
|
262
|
+
self.key_data[service].append(new_entry)
|
263
|
+
|
264
|
+
def process_key_value_pairs(self) -> None:
|
265
|
+
"""Process all key-value pairs from the configured sources."""
|
266
|
+
for key, value_pair in self.get_key_value_pairs().items():
|
267
|
+
value, source = value_pair
|
268
|
+
if (entry_type := self._entry_type(key, value)) == "limit":
|
269
|
+
self._add_limit(key, value, source)
|
270
|
+
elif entry_type == "api_key":
|
271
|
+
self._add_api_key(key, value, source)
|
272
|
+
elif entry_type == "api_id":
|
273
|
+
self._add_id(key, value, source)
|
@@ -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)
|