edsl 0.1.37.dev2__py3-none-any.whl → 0.1.37.dev3__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 +303 -303
- edsl/BaseDiff.py +260 -260
- edsl/TemplateLoader.py +24 -24
- edsl/__init__.py +48 -48
- edsl/__version__.py +1 -1
- edsl/agents/Agent.py +804 -804
- edsl/agents/AgentList.py +345 -345
- edsl/agents/Invigilator.py +222 -222
- edsl/agents/InvigilatorBase.py +305 -305
- edsl/agents/PromptConstructor.py +312 -312
- edsl/agents/__init__.py +3 -3
- edsl/agents/descriptors.py +86 -86
- edsl/agents/prompt_helpers.py +129 -129
- edsl/auto/AutoStudy.py +117 -117
- edsl/auto/StageBase.py +230 -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 +73 -73
- edsl/auto/SurveyCreatorPipeline.py +21 -21
- edsl/auto/utilities.py +224 -224
- edsl/base/Base.py +289 -289
- edsl/config.py +149 -149
- edsl/conjure/AgentConstructionMixin.py +152 -152
- edsl/conjure/Conjure.py +62 -62
- edsl/conjure/InputData.py +659 -659
- edsl/conjure/InputDataCSV.py +48 -48
- edsl/conjure/InputDataMixinQuestionStats.py +182 -182
- edsl/conjure/InputDataPyRead.py +91 -91
- edsl/conjure/InputDataSPSS.py +8 -8
- edsl/conjure/InputDataStata.py +8 -8
- edsl/conjure/QuestionOptionMixin.py +76 -76
- edsl/conjure/QuestionTypeMixin.py +23 -23
- edsl/conjure/RawQuestion.py +65 -65
- edsl/conjure/SurveyResponses.py +7 -7
- edsl/conjure/__init__.py +9 -9
- edsl/conjure/naming_utilities.py +263 -263
- edsl/conjure/utilities.py +201 -201
- edsl/conversation/Conversation.py +238 -238
- edsl/conversation/car_buying.py +58 -58
- edsl/conversation/mug_negotiation.py +81 -81
- edsl/conversation/next_speaker_utilities.py +93 -93
- edsl/coop/PriceFetcher.py +54 -54
- edsl/coop/__init__.py +2 -2
- edsl/coop/coop.py +824 -824
- edsl/coop/utils.py +131 -131
- edsl/data/Cache.py +527 -527
- edsl/data/CacheEntry.py +228 -228
- edsl/data/CacheHandler.py +149 -149
- edsl/data/RemoteCacheSync.py +97 -97
- edsl/data/SQLiteDict.py +292 -292
- edsl/data/__init__.py +4 -4
- edsl/data/orm.py +10 -10
- edsl/data_transfer_models.py +73 -73
- edsl/enums.py +173 -173
- edsl/exceptions/__init__.py +50 -50
- edsl/exceptions/agents.py +40 -40
- 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/jobs.py +33 -33
- edsl/exceptions/language_models.py +63 -63
- edsl/exceptions/prompts.py +15 -15
- edsl/exceptions/questions.py +91 -91
- edsl/exceptions/results.py +26 -26
- edsl/exceptions/surveys.py +34 -34
- edsl/inference_services/AnthropicService.py +87 -87
- edsl/inference_services/AwsBedrock.py +115 -115
- edsl/inference_services/AzureAI.py +217 -217
- edsl/inference_services/DeepInfraService.py +18 -18
- edsl/inference_services/GoogleService.py +156 -156
- edsl/inference_services/GroqService.py +20 -20
- edsl/inference_services/InferenceServiceABC.py +147 -147
- edsl/inference_services/InferenceServicesCollection.py +74 -74
- edsl/inference_services/MistralAIService.py +123 -123
- edsl/inference_services/OllamaService.py +18 -18
- edsl/inference_services/OpenAIService.py +224 -224
- edsl/inference_services/TestService.py +89 -89
- edsl/inference_services/TogetherAIService.py +170 -170
- edsl/inference_services/models_available_cache.py +118 -118
- edsl/inference_services/rate_limits_cache.py +25 -25
- edsl/inference_services/registry.py +39 -39
- edsl/inference_services/write_available.py +10 -10
- edsl/jobs/Answers.py +56 -56
- edsl/jobs/Jobs.py +1121 -1112
- edsl/jobs/__init__.py +1 -1
- edsl/jobs/buckets/BucketCollection.py +63 -63
- edsl/jobs/buckets/ModelBuckets.py +65 -65
- edsl/jobs/buckets/TokenBucket.py +248 -248
- edsl/jobs/interviews/Interview.py +661 -661
- edsl/jobs/interviews/InterviewExceptionCollection.py +99 -99
- edsl/jobs/interviews/InterviewExceptionEntry.py +182 -182
- 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/runners/JobsRunnerAsyncio.py +338 -338
- edsl/jobs/runners/JobsRunnerStatus.py +332 -332
- edsl/jobs/tasks/QuestionTaskCreator.py +242 -242
- edsl/jobs/tasks/TaskCreators.py +64 -64
- edsl/jobs/tasks/TaskHistory.py +441 -441
- edsl/jobs/tasks/TaskStatusLog.py +23 -23
- edsl/jobs/tasks/task_status_enum.py +163 -163
- edsl/jobs/tokens/InterviewTokenUsage.py +27 -27
- edsl/jobs/tokens/TokenUsage.py +34 -34
- edsl/language_models/LanguageModel.py +718 -718
- edsl/language_models/ModelList.py +102 -102
- edsl/language_models/RegisterLanguageModelsMeta.py +184 -184
- edsl/language_models/__init__.py +2 -2
- edsl/language_models/fake_openai_call.py +15 -15
- edsl/language_models/fake_openai_service.py +61 -61
- edsl/language_models/registry.py +137 -137
- edsl/language_models/repair.py +156 -156
- edsl/language_models/unused/ReplicateBase.py +83 -83
- edsl/language_models/utilities.py +64 -64
- edsl/notebooks/Notebook.py +259 -259
- edsl/notebooks/__init__.py +1 -1
- edsl/prompts/Prompt.py +353 -353
- edsl/prompts/__init__.py +2 -2
- edsl/questions/AnswerValidatorMixin.py +289 -289
- edsl/questions/QuestionBase.py +616 -616
- edsl/questions/QuestionBaseGenMixin.py +161 -161
- edsl/questions/QuestionBasePromptsMixin.py +266 -266
- edsl/questions/QuestionBudget.py +227 -227
- edsl/questions/QuestionCheckBox.py +359 -359
- edsl/questions/QuestionExtract.py +183 -183
- edsl/questions/QuestionFreeText.py +114 -114
- edsl/questions/QuestionFunctional.py +159 -159
- edsl/questions/QuestionList.py +231 -231
- edsl/questions/QuestionMultipleChoice.py +286 -286
- edsl/questions/QuestionNumerical.py +153 -153
- edsl/questions/QuestionRank.py +324 -324
- edsl/questions/Quick.py +41 -41
- edsl/questions/RegisterQuestionsMeta.py +71 -71
- edsl/questions/ResponseValidatorABC.py +174 -174
- edsl/questions/SimpleAskMixin.py +73 -73
- edsl/questions/__init__.py +26 -26
- edsl/questions/compose_questions.py +98 -98
- edsl/questions/decorators.py +21 -21
- edsl/questions/derived/QuestionLikertFive.py +76 -76
- edsl/questions/derived/QuestionLinearScale.py +87 -87
- edsl/questions/derived/QuestionTopK.py +91 -91
- edsl/questions/derived/QuestionYesNo.py +82 -82
- edsl/questions/descriptors.py +418 -418
- 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/question_registry.py +147 -147
- 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/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/Dataset.py +293 -293
- edsl/results/DatasetExportMixin.py +693 -693
- edsl/results/DatasetTree.py +145 -145
- edsl/results/Result.py +435 -435
- edsl/results/Results.py +1160 -1160
- edsl/results/ResultsDBMixin.py +238 -238
- edsl/results/ResultsExportMixin.py +43 -43
- edsl/results/ResultsFetchMixin.py +33 -33
- edsl/results/ResultsGGMixin.py +121 -121
- edsl/results/ResultsToolsMixin.py +98 -98
- edsl/results/Selector.py +118 -118
- edsl/results/__init__.py +2 -2
- edsl/results/tree_explore.py +115 -115
- edsl/scenarios/FileStore.py +458 -458
- edsl/scenarios/Scenario.py +510 -510
- edsl/scenarios/ScenarioHtmlMixin.py +59 -59
- edsl/scenarios/ScenarioList.py +1101 -1101
- edsl/scenarios/ScenarioListExportMixin.py +52 -52
- edsl/scenarios/ScenarioListPdfMixin.py +261 -261
- edsl/scenarios/__init__.py +4 -4
- 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 +528 -528
- edsl/study/__init__.py +4 -4
- edsl/surveys/DAG.py +148 -148
- edsl/surveys/Memory.py +31 -31
- edsl/surveys/MemoryPlan.py +244 -244
- edsl/surveys/Rule.py +324 -324
- edsl/surveys/RuleCollection.py +387 -387
- edsl/surveys/Survey.py +1772 -1772
- edsl/surveys/SurveyCSS.py +261 -261
- edsl/surveys/SurveyExportMixin.py +259 -259
- edsl/surveys/SurveyFlowVisualizationMixin.py +121 -121
- edsl/surveys/SurveyQualtricsImport.py +284 -284
- edsl/surveys/__init__.py +3 -3
- edsl/surveys/base.py +53 -53
- edsl/surveys/descriptors.py +56 -56
- edsl/surveys/instructions/ChangeInstruction.py +47 -47
- edsl/surveys/instructions/Instruction.py +51 -51
- edsl/surveys/instructions/InstructionCollection.py +77 -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 +9 -9
- 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/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/repair_functions.py +28 -28
- edsl/utilities/restricted_python.py +70 -70
- edsl/utilities/utilities.py +391 -391
- {edsl-0.1.37.dev2.dist-info → edsl-0.1.37.dev3.dist-info}/LICENSE +21 -21
- {edsl-0.1.37.dev2.dist-info → edsl-0.1.37.dev3.dist-info}/METADATA +1 -1
- edsl-0.1.37.dev3.dist-info/RECORD +279 -0
- edsl-0.1.37.dev2.dist-info/RECORD +0 -279
- {edsl-0.1.37.dev2.dist-info → edsl-0.1.37.dev3.dist-info}/WHEEL +0 -0
edsl/coop/coop.py
CHANGED
@@ -1,824 +1,824 @@
|
|
1
|
-
import aiohttp
|
2
|
-
import json
|
3
|
-
import os
|
4
|
-
import requests
|
5
|
-
from typing import Any, Optional, Union, Literal
|
6
|
-
from uuid import UUID
|
7
|
-
import edsl
|
8
|
-
from edsl import CONFIG, CacheEntry, Jobs, Survey
|
9
|
-
from edsl.exceptions.coop import CoopNoUUIDError, CoopServerResponseError
|
10
|
-
from edsl.coop.utils import (
|
11
|
-
EDSLObject,
|
12
|
-
ObjectRegistry,
|
13
|
-
ObjectType,
|
14
|
-
RemoteJobStatus,
|
15
|
-
VisibilityType,
|
16
|
-
)
|
17
|
-
|
18
|
-
|
19
|
-
class Coop:
|
20
|
-
"""
|
21
|
-
Client for the Expected Parrot API.
|
22
|
-
"""
|
23
|
-
|
24
|
-
def __init__(self, api_key: str = None, url: str = None) -> None:
|
25
|
-
"""
|
26
|
-
Initialize the client.
|
27
|
-
- Provide an API key directly, or through an env variable.
|
28
|
-
- Provide a URL directly, or use the default one.
|
29
|
-
"""
|
30
|
-
self.api_key = api_key or os.getenv("EXPECTED_PARROT_API_KEY")
|
31
|
-
self.url = url or CONFIG.EXPECTED_PARROT_URL
|
32
|
-
if self.url.endswith("/"):
|
33
|
-
self.url = self.url[:-1]
|
34
|
-
self._edsl_version = edsl.__version__
|
35
|
-
|
36
|
-
################
|
37
|
-
# BASIC METHODS
|
38
|
-
################
|
39
|
-
@property
|
40
|
-
def headers(self) -> dict:
|
41
|
-
"""
|
42
|
-
Return the headers for the request.
|
43
|
-
"""
|
44
|
-
headers = {}
|
45
|
-
if self.api_key:
|
46
|
-
headers["Authorization"] = f"Bearer {self.api_key}"
|
47
|
-
else:
|
48
|
-
headers["Authorization"] = f"Bearer None"
|
49
|
-
return headers
|
50
|
-
|
51
|
-
def _send_server_request(
|
52
|
-
self,
|
53
|
-
uri: str,
|
54
|
-
method: str,
|
55
|
-
payload: Optional[dict[str, Any]] = None,
|
56
|
-
params: Optional[dict[str, Any]] = None,
|
57
|
-
timeout: Optional[float] = 5,
|
58
|
-
) -> requests.Response:
|
59
|
-
"""
|
60
|
-
Send a request to the server and return the response.
|
61
|
-
"""
|
62
|
-
url = f"{self.url}/{uri}"
|
63
|
-
method = method.upper()
|
64
|
-
if payload is None:
|
65
|
-
timeout = 20
|
66
|
-
elif (
|
67
|
-
method.upper() == "POST"
|
68
|
-
and "json_string" in payload
|
69
|
-
and payload.get("json_string") is not None
|
70
|
-
):
|
71
|
-
timeout = max(20, (len(payload.get("json_string", "")) // (1024 * 1024)))
|
72
|
-
try:
|
73
|
-
if method in ["GET", "DELETE"]:
|
74
|
-
response = requests.request(
|
75
|
-
method, url, params=params, headers=self.headers, timeout=timeout
|
76
|
-
)
|
77
|
-
elif method in ["POST", "PATCH"]:
|
78
|
-
response = requests.request(
|
79
|
-
method,
|
80
|
-
url,
|
81
|
-
params=params,
|
82
|
-
json=payload,
|
83
|
-
headers=self.headers,
|
84
|
-
timeout=timeout,
|
85
|
-
)
|
86
|
-
else:
|
87
|
-
raise Exception(f"Invalid {method=}.")
|
88
|
-
except requests.ConnectionError:
|
89
|
-
raise requests.ConnectionError(f"Could not connect to the server at {url}.")
|
90
|
-
|
91
|
-
return response
|
92
|
-
|
93
|
-
def _resolve_server_response(self, response: requests.Response) -> None:
|
94
|
-
"""
|
95
|
-
Check the response from the server and raise errors as appropriate.
|
96
|
-
"""
|
97
|
-
if response.status_code >= 400:
|
98
|
-
message = response.json().get("detail")
|
99
|
-
# print(response.text)
|
100
|
-
if "Authorization" in message:
|
101
|
-
print(message)
|
102
|
-
message = "Please provide an Expected Parrot API key."
|
103
|
-
raise CoopServerResponseError(message)
|
104
|
-
|
105
|
-
def _json_handle_none(self, value: Any) -> Any:
|
106
|
-
"""
|
107
|
-
Handle None values during JSON serialization.
|
108
|
-
- Return "null" if the value is None. Otherwise, don't return anything.
|
109
|
-
"""
|
110
|
-
if value is None:
|
111
|
-
return "null"
|
112
|
-
|
113
|
-
def _resolve_uuid(
|
114
|
-
self, uuid: Union[str, UUID] = None, url: str = None
|
115
|
-
) -> Union[str, UUID]:
|
116
|
-
"""
|
117
|
-
Resolve the uuid from a uuid or a url.
|
118
|
-
"""
|
119
|
-
if not url and not uuid:
|
120
|
-
raise CoopNoUUIDError("No uuid or url provided for the object.")
|
121
|
-
if not uuid and url:
|
122
|
-
uuid = url.split("/")[-1]
|
123
|
-
return uuid
|
124
|
-
|
125
|
-
@property
|
126
|
-
def edsl_settings(self) -> dict:
|
127
|
-
"""
|
128
|
-
Retrieve and return the EDSL settings stored on Coop.
|
129
|
-
If no response is received within 5 seconds, return an empty dict.
|
130
|
-
"""
|
131
|
-
from requests.exceptions import Timeout
|
132
|
-
|
133
|
-
try:
|
134
|
-
response = self._send_server_request(
|
135
|
-
uri="api/v0/edsl-settings", method="GET", timeout=5
|
136
|
-
)
|
137
|
-
self._resolve_server_response(response)
|
138
|
-
return response.json()
|
139
|
-
except Timeout:
|
140
|
-
return {}
|
141
|
-
|
142
|
-
################
|
143
|
-
# Objects
|
144
|
-
################
|
145
|
-
def create(
|
146
|
-
self,
|
147
|
-
object: EDSLObject,
|
148
|
-
description: Optional[str] = None,
|
149
|
-
visibility: Optional[VisibilityType] = "unlisted",
|
150
|
-
) -> dict:
|
151
|
-
"""
|
152
|
-
Create an EDSL object in the Coop server.
|
153
|
-
"""
|
154
|
-
object_type = ObjectRegistry.get_object_type_by_edsl_class(object)
|
155
|
-
response = self._send_server_request(
|
156
|
-
uri=f"api/v0/object",
|
157
|
-
method="POST",
|
158
|
-
payload={
|
159
|
-
"description": description,
|
160
|
-
"json_string": json.dumps(
|
161
|
-
object.to_dict(),
|
162
|
-
default=self._json_handle_none,
|
163
|
-
),
|
164
|
-
"object_type": object_type,
|
165
|
-
"visibility": visibility,
|
166
|
-
"version": self._edsl_version,
|
167
|
-
},
|
168
|
-
)
|
169
|
-
self._resolve_server_response(response)
|
170
|
-
response_json = response.json()
|
171
|
-
return {
|
172
|
-
"description": response_json.get("description"),
|
173
|
-
"object_type": object_type,
|
174
|
-
"url": f"{self.url}/content/{response_json.get('uuid')}",
|
175
|
-
"uuid": response_json.get("uuid"),
|
176
|
-
"version": self._edsl_version,
|
177
|
-
"visibility": response_json.get("visibility"),
|
178
|
-
}
|
179
|
-
|
180
|
-
def get(
|
181
|
-
self,
|
182
|
-
uuid: Union[str, UUID] = None,
|
183
|
-
url: str = None,
|
184
|
-
expected_object_type: Optional[ObjectType] = None,
|
185
|
-
) -> EDSLObject:
|
186
|
-
"""
|
187
|
-
Retrieve an EDSL object by its uuid or its url.
|
188
|
-
- If the object's visibility is private, the user must be the owner.
|
189
|
-
- Optionally, check if the retrieved object is of a certain type.
|
190
|
-
|
191
|
-
:param uuid: the uuid of the object either in str or UUID format.
|
192
|
-
:param url: the url of the object.
|
193
|
-
:param expected_object_type: the expected type of the object.
|
194
|
-
|
195
|
-
:return: the object instance.
|
196
|
-
"""
|
197
|
-
uuid = self._resolve_uuid(uuid, url)
|
198
|
-
response = self._send_server_request(
|
199
|
-
uri=f"api/v0/object",
|
200
|
-
method="GET",
|
201
|
-
params={"uuid": uuid},
|
202
|
-
)
|
203
|
-
self._resolve_server_response(response)
|
204
|
-
json_string = response.json().get("json_string")
|
205
|
-
object_type = response.json().get("object_type")
|
206
|
-
if expected_object_type and object_type != expected_object_type:
|
207
|
-
raise Exception(f"Expected {expected_object_type=} but got {object_type=}")
|
208
|
-
edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
|
209
|
-
object = edsl_class.from_dict(json.loads(json_string))
|
210
|
-
return object
|
211
|
-
|
212
|
-
def get_all(self, object_type: ObjectType) -> list[dict[str, Any]]:
|
213
|
-
"""
|
214
|
-
Retrieve all objects of a certain type associated with the user.
|
215
|
-
"""
|
216
|
-
edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
|
217
|
-
response = self._send_server_request(
|
218
|
-
uri=f"api/v0/objects",
|
219
|
-
method="GET",
|
220
|
-
params={"type": object_type},
|
221
|
-
)
|
222
|
-
self._resolve_server_response(response)
|
223
|
-
objects = [
|
224
|
-
{
|
225
|
-
"object": edsl_class.from_dict(json.loads(o.get("json_string"))),
|
226
|
-
"uuid": o.get("uuid"),
|
227
|
-
"version": o.get("version"),
|
228
|
-
"description": o.get("description"),
|
229
|
-
"visibility": o.get("visibility"),
|
230
|
-
"url": f"{self.url}/content/{o.get('uuid')}",
|
231
|
-
}
|
232
|
-
for o in response.json()
|
233
|
-
]
|
234
|
-
return objects
|
235
|
-
|
236
|
-
def delete(self, uuid: Union[str, UUID] = None, url: str = None) -> dict:
|
237
|
-
"""
|
238
|
-
Delete an object from the server.
|
239
|
-
"""
|
240
|
-
uuid = self._resolve_uuid(uuid, url)
|
241
|
-
response = self._send_server_request(
|
242
|
-
uri=f"api/v0/object",
|
243
|
-
method="DELETE",
|
244
|
-
params={"uuid": uuid},
|
245
|
-
)
|
246
|
-
self._resolve_server_response(response)
|
247
|
-
return response.json()
|
248
|
-
|
249
|
-
def patch(
|
250
|
-
self,
|
251
|
-
uuid: Union[str, UUID] = None,
|
252
|
-
url: str = None,
|
253
|
-
description: Optional[str] = None,
|
254
|
-
value: Optional[EDSLObject] = None,
|
255
|
-
visibility: Optional[VisibilityType] = None,
|
256
|
-
) -> dict:
|
257
|
-
"""
|
258
|
-
Change the attributes of an uploaded object
|
259
|
-
- Only supports visibility for now
|
260
|
-
"""
|
261
|
-
if description is None and visibility is None and value is None:
|
262
|
-
raise Exception("Nothing to patch.")
|
263
|
-
uuid = self._resolve_uuid(uuid, url)
|
264
|
-
response = self._send_server_request(
|
265
|
-
uri=f"api/v0/object",
|
266
|
-
method="PATCH",
|
267
|
-
params={"uuid": uuid},
|
268
|
-
payload={
|
269
|
-
"description": description,
|
270
|
-
"json_string": (
|
271
|
-
json.dumps(
|
272
|
-
value.to_dict(),
|
273
|
-
default=self._json_handle_none,
|
274
|
-
)
|
275
|
-
if value
|
276
|
-
else None
|
277
|
-
),
|
278
|
-
"visibility": visibility,
|
279
|
-
},
|
280
|
-
)
|
281
|
-
self._resolve_server_response(response)
|
282
|
-
return response.json()
|
283
|
-
|
284
|
-
################
|
285
|
-
# Remote Cache
|
286
|
-
################
|
287
|
-
def remote_cache_create(
|
288
|
-
self,
|
289
|
-
cache_entry: CacheEntry,
|
290
|
-
visibility: VisibilityType = "private",
|
291
|
-
description: Optional[str] = None,
|
292
|
-
) -> dict:
|
293
|
-
"""
|
294
|
-
Create a single remote cache entry.
|
295
|
-
If an entry with the same key already exists in the database, update it instead.
|
296
|
-
|
297
|
-
:param cache_entry: The cache entry to send to the server.
|
298
|
-
:param visibility: The visibility of the cache entry.
|
299
|
-
:param optional description: A description for this entry in the remote cache.
|
300
|
-
|
301
|
-
>>> entry = CacheEntry.example()
|
302
|
-
>>> coop.remote_cache_create(cache_entry=entry)
|
303
|
-
{'status': 'success', 'created_entry_count': 1, 'updated_entry_count': 0}
|
304
|
-
"""
|
305
|
-
response = self._send_server_request(
|
306
|
-
uri="api/v0/remote-cache",
|
307
|
-
method="POST",
|
308
|
-
payload={
|
309
|
-
"json_string": json.dumps(cache_entry.to_dict()),
|
310
|
-
"version": self._edsl_version,
|
311
|
-
"visibility": visibility,
|
312
|
-
"description": description,
|
313
|
-
},
|
314
|
-
)
|
315
|
-
self._resolve_server_response(response)
|
316
|
-
response_json = response.json()
|
317
|
-
created_entry_count = response_json.get("created_entry_count", 0)
|
318
|
-
if created_entry_count > 0:
|
319
|
-
self.remote_cache_create_log(
|
320
|
-
response,
|
321
|
-
description="Upload new cache entries to server",
|
322
|
-
cache_entry_count=created_entry_count,
|
323
|
-
)
|
324
|
-
return response.json()
|
325
|
-
|
326
|
-
def remote_cache_create_many(
|
327
|
-
self,
|
328
|
-
cache_entries: list[CacheEntry],
|
329
|
-
visibility: VisibilityType = "private",
|
330
|
-
description: Optional[str] = None,
|
331
|
-
) -> dict:
|
332
|
-
"""
|
333
|
-
Create many remote cache entries.
|
334
|
-
If an entry with the same key already exists in the database, update it instead.
|
335
|
-
|
336
|
-
:param cache_entries: The list of cache entries to send to the server.
|
337
|
-
:param visibility: The visibility of the cache entries.
|
338
|
-
:param optional description: A description for these entries in the remote cache.
|
339
|
-
|
340
|
-
>>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
|
341
|
-
>>> coop.remote_cache_create_many(cache_entries=entries)
|
342
|
-
{'status': 'success', 'created_entry_count': 10, 'updated_entry_count': 0}
|
343
|
-
"""
|
344
|
-
payload = [
|
345
|
-
{
|
346
|
-
"json_string": json.dumps(c.to_dict()),
|
347
|
-
"version": self._edsl_version,
|
348
|
-
"visibility": visibility,
|
349
|
-
"description": description,
|
350
|
-
}
|
351
|
-
for c in cache_entries
|
352
|
-
]
|
353
|
-
response = self._send_server_request(
|
354
|
-
uri="api/v0/remote-cache/many",
|
355
|
-
method="POST",
|
356
|
-
payload=payload,
|
357
|
-
)
|
358
|
-
self._resolve_server_response(response)
|
359
|
-
response_json = response.json()
|
360
|
-
created_entry_count = response_json.get("created_entry_count", 0)
|
361
|
-
if created_entry_count > 0:
|
362
|
-
self.remote_cache_create_log(
|
363
|
-
response,
|
364
|
-
description="Upload new cache entries to server",
|
365
|
-
cache_entry_count=created_entry_count,
|
366
|
-
)
|
367
|
-
return response.json()
|
368
|
-
|
369
|
-
def remote_cache_get(
|
370
|
-
self,
|
371
|
-
exclude_keys: Optional[list[str]] = None,
|
372
|
-
) -> list[CacheEntry]:
|
373
|
-
"""
|
374
|
-
Get all remote cache entries.
|
375
|
-
|
376
|
-
:param optional exclude_keys: Exclude CacheEntry objects with these keys.
|
377
|
-
|
378
|
-
>>> coop.remote_cache_get()
|
379
|
-
[CacheEntry(...), CacheEntry(...), ...]
|
380
|
-
"""
|
381
|
-
if exclude_keys is None:
|
382
|
-
exclude_keys = []
|
383
|
-
response = self._send_server_request(
|
384
|
-
uri="api/v0/remote-cache/get-many",
|
385
|
-
method="POST",
|
386
|
-
payload={"keys": exclude_keys},
|
387
|
-
)
|
388
|
-
self._resolve_server_response(response)
|
389
|
-
return [
|
390
|
-
CacheEntry.from_dict(json.loads(v.get("json_string")))
|
391
|
-
for v in response.json()
|
392
|
-
]
|
393
|
-
|
394
|
-
def remote_cache_get_diff(
|
395
|
-
self,
|
396
|
-
client_cacheentry_keys: list[str],
|
397
|
-
) -> dict:
|
398
|
-
"""
|
399
|
-
Get the difference between local and remote cache entries for a user.
|
400
|
-
"""
|
401
|
-
response = self._send_server_request(
|
402
|
-
uri="api/v0/remote-cache/get-diff",
|
403
|
-
method="POST",
|
404
|
-
payload={"keys": client_cacheentry_keys},
|
405
|
-
)
|
406
|
-
self._resolve_server_response(response)
|
407
|
-
response_json = response.json()
|
408
|
-
response_dict = {
|
409
|
-
"client_missing_cacheentries": [
|
410
|
-
CacheEntry.from_dict(json.loads(c.get("json_string")))
|
411
|
-
for c in response_json.get("client_missing_cacheentries", [])
|
412
|
-
],
|
413
|
-
"server_missing_cacheentry_keys": response_json.get(
|
414
|
-
"server_missing_cacheentry_keys", []
|
415
|
-
),
|
416
|
-
}
|
417
|
-
downloaded_entry_count = len(response_dict["client_missing_cacheentries"])
|
418
|
-
if downloaded_entry_count > 0:
|
419
|
-
self.remote_cache_create_log(
|
420
|
-
response,
|
421
|
-
description="Download missing cache entries to client",
|
422
|
-
cache_entry_count=downloaded_entry_count,
|
423
|
-
)
|
424
|
-
return response_dict
|
425
|
-
|
426
|
-
def remote_cache_clear(self) -> dict:
|
427
|
-
"""
|
428
|
-
Clear all remote cache entries.
|
429
|
-
|
430
|
-
>>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
|
431
|
-
>>> coop.remote_cache_create_many(cache_entries=entries)
|
432
|
-
>>> coop.remote_cache_clear()
|
433
|
-
{'status': 'success', 'deleted_entry_count': 10}
|
434
|
-
"""
|
435
|
-
response = self._send_server_request(
|
436
|
-
uri="api/v0/remote-cache/delete-all",
|
437
|
-
method="DELETE",
|
438
|
-
)
|
439
|
-
self._resolve_server_response(response)
|
440
|
-
response_json = response.json()
|
441
|
-
deleted_entry_count = response_json.get("deleted_entry_count", 0)
|
442
|
-
if deleted_entry_count > 0:
|
443
|
-
self.remote_cache_create_log(
|
444
|
-
response,
|
445
|
-
description="Clear cache entries",
|
446
|
-
cache_entry_count=deleted_entry_count,
|
447
|
-
)
|
448
|
-
return response.json()
|
449
|
-
|
450
|
-
def remote_cache_create_log(
|
451
|
-
self, response: requests.Response, description: str, cache_entry_count: int
|
452
|
-
) -> Union[dict, None]:
|
453
|
-
"""
|
454
|
-
If a remote cache action has been completed successfully,
|
455
|
-
log the action.
|
456
|
-
"""
|
457
|
-
if 200 <= response.status_code < 300:
|
458
|
-
log_response = self._send_server_request(
|
459
|
-
uri="api/v0/remote-cache-log",
|
460
|
-
method="POST",
|
461
|
-
payload={
|
462
|
-
"description": description,
|
463
|
-
"cache_entry_count": cache_entry_count,
|
464
|
-
},
|
465
|
-
)
|
466
|
-
self._resolve_server_response(log_response)
|
467
|
-
return response.json()
|
468
|
-
|
469
|
-
def remote_cache_clear_log(self) -> dict:
|
470
|
-
"""
|
471
|
-
Clear all remote cache log entries.
|
472
|
-
|
473
|
-
>>> coop.remote_cache_clear_log()
|
474
|
-
{'status': 'success'}
|
475
|
-
"""
|
476
|
-
response = self._send_server_request(
|
477
|
-
uri="api/v0/remote-cache-log/delete-all",
|
478
|
-
method="DELETE",
|
479
|
-
)
|
480
|
-
self._resolve_server_response(response)
|
481
|
-
return response.json()
|
482
|
-
|
483
|
-
################
|
484
|
-
# Remote Inference
|
485
|
-
################
|
486
|
-
def remote_inference_create(
|
487
|
-
self,
|
488
|
-
job: Jobs,
|
489
|
-
description: Optional[str] = None,
|
490
|
-
status: RemoteJobStatus = "queued",
|
491
|
-
visibility: Optional[VisibilityType] = "unlisted",
|
492
|
-
iterations: Optional[int] = 1,
|
493
|
-
) -> dict:
|
494
|
-
"""
|
495
|
-
Send a remote inference job to the server.
|
496
|
-
|
497
|
-
:param job: The EDSL job to send to the server.
|
498
|
-
:param optional description: A description for this entry in the remote cache.
|
499
|
-
:param status: The status of the job. Should be 'queued', unless you are debugging.
|
500
|
-
:param visibility: The visibility of the cache entry.
|
501
|
-
:param iterations: The number of times to run each interview.
|
502
|
-
|
503
|
-
>>> job = Jobs.example()
|
504
|
-
>>> coop.remote_inference_create(job=job, description="My job")
|
505
|
-
{'uuid': '9f8484ee-b407-40e4-9652-4133a7236c9c', 'description': 'My job', 'status': 'queued', 'visibility': 'unlisted', 'version': '0.1.29.dev4'}
|
506
|
-
"""
|
507
|
-
response = self._send_server_request(
|
508
|
-
uri="api/v0/remote-inference",
|
509
|
-
method="POST",
|
510
|
-
payload={
|
511
|
-
"json_string": json.dumps(
|
512
|
-
job.to_dict(),
|
513
|
-
default=self._json_handle_none,
|
514
|
-
),
|
515
|
-
"description": description,
|
516
|
-
"status": status,
|
517
|
-
"iterations": iterations,
|
518
|
-
"visibility": visibility,
|
519
|
-
"version": self._edsl_version,
|
520
|
-
},
|
521
|
-
)
|
522
|
-
self._resolve_server_response(response)
|
523
|
-
response_json = response.json()
|
524
|
-
return {
|
525
|
-
"uuid": response_json.get("job_uuid"),
|
526
|
-
"description": response_json.get("description"),
|
527
|
-
"status": response_json.get("status"),
|
528
|
-
"iterations": response_json.get("iterations"),
|
529
|
-
"visibility": response_json.get("visibility"),
|
530
|
-
"version": self._edsl_version,
|
531
|
-
}
|
532
|
-
|
533
|
-
def remote_inference_get(
|
534
|
-
self, job_uuid: Optional[str] = None, results_uuid: Optional[str] = None
|
535
|
-
) -> dict:
|
536
|
-
"""
|
537
|
-
Get the details of a remote inference job.
|
538
|
-
You can pass either the job uuid or the results uuid as a parameter.
|
539
|
-
If you pass both, the job uuid will be prioritized.
|
540
|
-
|
541
|
-
:param job_uuid: The UUID of the EDSL job.
|
542
|
-
:param results_uuid: The UUID of the results associated with the EDSL job.
|
543
|
-
|
544
|
-
>>> coop.remote_inference_get("9f8484ee-b407-40e4-9652-4133a7236c9c")
|
545
|
-
{'jobs_uuid': '9f8484ee-b407-40e4-9652-4133a7236c9c', 'results_uuid': 'dd708234-31bf-4fe1-8747-6e232625e026', 'results_url': 'https://www.expectedparrot.com/content/dd708234-31bf-4fe1-8747-6e232625e026', 'status': 'completed', 'reason': None, 'price': 16, 'version': '0.1.29.dev4'}
|
546
|
-
"""
|
547
|
-
if job_uuid is None and results_uuid is None:
|
548
|
-
raise ValueError("Either job_uuid or results_uuid must be provided.")
|
549
|
-
elif job_uuid is not None:
|
550
|
-
params = {"job_uuid": job_uuid}
|
551
|
-
else:
|
552
|
-
params = {"results_uuid": results_uuid}
|
553
|
-
|
554
|
-
response = self._send_server_request(
|
555
|
-
uri="api/v0/remote-inference",
|
556
|
-
method="GET",
|
557
|
-
params=params,
|
558
|
-
)
|
559
|
-
self._resolve_server_response(response)
|
560
|
-
data = response.json()
|
561
|
-
return {
|
562
|
-
"job_uuid": data.get("job_uuid"),
|
563
|
-
"results_uuid": data.get("results_uuid"),
|
564
|
-
"results_url": f"{self.url}/content/{data.get('results_uuid')}",
|
565
|
-
"status": data.get("status"),
|
566
|
-
"reason": data.get("reason"),
|
567
|
-
"credits_consumed": data.get("price"),
|
568
|
-
"version": data.get("version"),
|
569
|
-
}
|
570
|
-
|
571
|
-
def remote_inference_cost(self, input: Union[Jobs, Survey]) -> int:
|
572
|
-
"""
|
573
|
-
Get the cost of a remote inference job.
|
574
|
-
|
575
|
-
:param input: The EDSL job to send to the server.
|
576
|
-
|
577
|
-
>>> job = Jobs.example()
|
578
|
-
>>> coop.remote_inference_cost(input=job)
|
579
|
-
16
|
580
|
-
"""
|
581
|
-
if isinstance(input, Jobs):
|
582
|
-
job = input
|
583
|
-
elif isinstance(input, Survey):
|
584
|
-
job = Jobs(survey=input)
|
585
|
-
else:
|
586
|
-
raise TypeError("Input must be either a Job or a Survey.")
|
587
|
-
|
588
|
-
response = self._send_server_request(
|
589
|
-
uri="api/v0/remote-inference/cost",
|
590
|
-
method="POST",
|
591
|
-
payload={
|
592
|
-
"json_string": json.dumps(
|
593
|
-
job.to_dict(),
|
594
|
-
default=self._json_handle_none,
|
595
|
-
),
|
596
|
-
},
|
597
|
-
)
|
598
|
-
self._resolve_server_response(response)
|
599
|
-
response_json = response.json()
|
600
|
-
return {
|
601
|
-
"credits": response_json.get("cost_in_credits"),
|
602
|
-
"usd": response_json.get("cost_in_usd"),
|
603
|
-
}
|
604
|
-
|
605
|
-
################
|
606
|
-
# DUNDER METHODS
|
607
|
-
################
|
608
|
-
def __repr__(self):
|
609
|
-
"""Return a string representation of the client."""
|
610
|
-
return f"Client(api_key='{self.api_key}', url='{self.url}')"
|
611
|
-
|
612
|
-
################
|
613
|
-
# EXPERIMENTAL
|
614
|
-
################
|
615
|
-
async def remote_async_execute_model_call(
|
616
|
-
self, model_dict: dict, user_prompt: str, system_prompt: str
|
617
|
-
) -> dict:
|
618
|
-
url = self.url + "/inference/"
|
619
|
-
# print("Now using url: ", url)
|
620
|
-
data = {
|
621
|
-
"model_dict": model_dict,
|
622
|
-
"user_prompt": user_prompt,
|
623
|
-
"system_prompt": system_prompt,
|
624
|
-
}
|
625
|
-
# Use aiohttp to send a POST request asynchronously
|
626
|
-
async with aiohttp.ClientSession() as session:
|
627
|
-
async with session.post(url, json=data) as response:
|
628
|
-
response_data = await response.json()
|
629
|
-
return response_data
|
630
|
-
|
631
|
-
def web(
|
632
|
-
self,
|
633
|
-
survey: dict,
|
634
|
-
platform: Literal[
|
635
|
-
"google_forms", "lime_survey", "survey_monkey"
|
636
|
-
] = "lime_survey",
|
637
|
-
email=None,
|
638
|
-
):
|
639
|
-
url = f"{self.url}/api/v0/export_to_{platform}"
|
640
|
-
if email:
|
641
|
-
data = {"json_string": json.dumps({"survey": survey, "email": email})}
|
642
|
-
else:
|
643
|
-
data = {"json_string": json.dumps({"survey": survey, "email": ""})}
|
644
|
-
|
645
|
-
response_json = requests.post(url, headers=self.headers, data=json.dumps(data))
|
646
|
-
|
647
|
-
return response_json
|
648
|
-
|
649
|
-
def fetch_prices(self) -> dict:
|
650
|
-
"""
|
651
|
-
Fetch model prices from Coop. If the request fails, return an empty dict.
|
652
|
-
"""
|
653
|
-
|
654
|
-
from edsl.coop.PriceFetcher import PriceFetcher
|
655
|
-
|
656
|
-
from edsl.config import CONFIG
|
657
|
-
|
658
|
-
if bool(CONFIG.get("EDSL_FETCH_TOKEN_PRICES")):
|
659
|
-
price_fetcher = PriceFetcher()
|
660
|
-
return price_fetcher.fetch_prices()
|
661
|
-
else:
|
662
|
-
return {}
|
663
|
-
|
664
|
-
def fetch_rate_limit_config_vars(self) -> dict:
|
665
|
-
"""
|
666
|
-
Fetch a dict of rate limit config vars from Coop.
|
667
|
-
|
668
|
-
The dict keys are RPM and TPM variables like EDSL_SERVICE_RPM_OPENAI.
|
669
|
-
"""
|
670
|
-
response = self._send_server_request(
|
671
|
-
uri="api/v0/config-vars",
|
672
|
-
method="GET",
|
673
|
-
)
|
674
|
-
self._resolve_server_response(response)
|
675
|
-
data = response.json()
|
676
|
-
return data
|
677
|
-
|
678
|
-
|
679
|
-
if __name__ == "__main__":
|
680
|
-
sheet_data = fetch_sheet_data()
|
681
|
-
if sheet_data:
|
682
|
-
print(f"Successfully fetched {len(sheet_data)} rows of data.")
|
683
|
-
print("First row:", sheet_data[0])
|
684
|
-
else:
|
685
|
-
print("Failed to fetch sheet data.")
|
686
|
-
|
687
|
-
|
688
|
-
def main():
|
689
|
-
"""
|
690
|
-
A simple example for the coop client
|
691
|
-
"""
|
692
|
-
from uuid import uuid4
|
693
|
-
from edsl import (
|
694
|
-
Agent,
|
695
|
-
AgentList,
|
696
|
-
Cache,
|
697
|
-
Notebook,
|
698
|
-
QuestionFreeText,
|
699
|
-
QuestionMultipleChoice,
|
700
|
-
Results,
|
701
|
-
Scenario,
|
702
|
-
ScenarioList,
|
703
|
-
Survey,
|
704
|
-
)
|
705
|
-
from edsl.coop import Coop
|
706
|
-
from edsl.data.CacheEntry import CacheEntry
|
707
|
-
from edsl.jobs import Jobs
|
708
|
-
|
709
|
-
# init & basics
|
710
|
-
API_KEY = "b"
|
711
|
-
coop = Coop(api_key=API_KEY)
|
712
|
-
coop
|
713
|
-
coop.edsl_settings
|
714
|
-
|
715
|
-
##############
|
716
|
-
# A. A simple example
|
717
|
-
##############
|
718
|
-
# .. create and manipulate an object through the Coop client
|
719
|
-
response = coop.create(QuestionMultipleChoice.example())
|
720
|
-
coop.get(uuid=response.get("uuid"))
|
721
|
-
coop.get(uuid=response.get("uuid"), expected_object_type="question")
|
722
|
-
coop.get(url=response.get("url"))
|
723
|
-
coop.create(QuestionMultipleChoice.example())
|
724
|
-
coop.get_all("question")
|
725
|
-
coop.patch(uuid=response.get("uuid"), visibility="private")
|
726
|
-
coop.patch(uuid=response.get("uuid"), description="hey")
|
727
|
-
coop.patch(uuid=response.get("uuid"), value=QuestionFreeText.example())
|
728
|
-
# coop.patch(uuid=response.get("uuid"), value=Survey.example()) - should throw error
|
729
|
-
coop.get(uuid=response.get("uuid"))
|
730
|
-
coop.delete(uuid=response.get("uuid"))
|
731
|
-
|
732
|
-
# .. create and manipulate an object through the class
|
733
|
-
response = QuestionMultipleChoice.example().push()
|
734
|
-
QuestionMultipleChoice.pull(uuid=response.get("uuid"))
|
735
|
-
QuestionMultipleChoice.pull(url=response.get("url"))
|
736
|
-
QuestionMultipleChoice.patch(uuid=response.get("uuid"), visibility="private")
|
737
|
-
QuestionMultipleChoice.patch(uuid=response.get("uuid"), description="hey")
|
738
|
-
QuestionMultipleChoice.patch(
|
739
|
-
uuid=response.get("uuid"), value=QuestionFreeText.example()
|
740
|
-
)
|
741
|
-
QuestionMultipleChoice.pull(response.get("uuid"))
|
742
|
-
QuestionMultipleChoice.delete(response.get("uuid"))
|
743
|
-
|
744
|
-
##############
|
745
|
-
# B. Examples with all objects
|
746
|
-
##############
|
747
|
-
OBJECTS = [
|
748
|
-
("agent", Agent),
|
749
|
-
("agent_list", AgentList),
|
750
|
-
("cache", Cache),
|
751
|
-
("notebook", Notebook),
|
752
|
-
("question", QuestionMultipleChoice),
|
753
|
-
("results", Results),
|
754
|
-
("scenario", Scenario),
|
755
|
-
("scenario_list", ScenarioList),
|
756
|
-
("survey", Survey),
|
757
|
-
]
|
758
|
-
for object_type, cls in OBJECTS:
|
759
|
-
print(f"Testing {object_type} objects")
|
760
|
-
# 1. Delete existing objects
|
761
|
-
existing_objects = coop.get_all(object_type)
|
762
|
-
for item in existing_objects:
|
763
|
-
coop.delete(uuid=item.get("uuid"))
|
764
|
-
# 2. Create new objects
|
765
|
-
example = cls.example()
|
766
|
-
response_1 = coop.create(example)
|
767
|
-
response_2 = coop.create(cls.example(), visibility="private")
|
768
|
-
response_3 = coop.create(cls.example(), visibility="public")
|
769
|
-
response_4 = coop.create(
|
770
|
-
cls.example(), visibility="unlisted", description="hey"
|
771
|
-
)
|
772
|
-
# 3. Retrieve all objects
|
773
|
-
objects = coop.get_all(object_type)
|
774
|
-
assert len(objects) == 4
|
775
|
-
# 4. Try to retrieve an item that does not exist
|
776
|
-
try:
|
777
|
-
coop.get(uuid=uuid4())
|
778
|
-
except Exception as e:
|
779
|
-
print(e)
|
780
|
-
# 5. Try to retrieve all test objects by their uuids
|
781
|
-
for response in [response_1, response_2, response_3, response_4]:
|
782
|
-
coop.get(uuid=response.get("uuid"))
|
783
|
-
# 6. Change visibility of all objects
|
784
|
-
for item in objects:
|
785
|
-
coop.patch(uuid=item.get("uuid"), visibility="private")
|
786
|
-
# 6. Change description of all objects
|
787
|
-
for item in objects:
|
788
|
-
coop.patch(uuid=item.get("uuid"), description="hey")
|
789
|
-
# 7. Delete all objects
|
790
|
-
for item in objects:
|
791
|
-
coop.delete(uuid=item.get("uuid"))
|
792
|
-
assert len(coop.get_all(object_type)) == 0
|
793
|
-
|
794
|
-
##############
|
795
|
-
# C. Remote Cache
|
796
|
-
##############
|
797
|
-
# clear
|
798
|
-
coop.remote_cache_clear()
|
799
|
-
assert coop.remote_cache_get() == []
|
800
|
-
# create one remote cache entry
|
801
|
-
cache_entry = CacheEntry.example()
|
802
|
-
cache_entry.to_dict()
|
803
|
-
coop.remote_cache_create(cache_entry)
|
804
|
-
# create many remote cache entries
|
805
|
-
cache_entries = [CacheEntry.example(randomize=True) for _ in range(10)]
|
806
|
-
coop.remote_cache_create_many(cache_entries)
|
807
|
-
# get all remote cache entries
|
808
|
-
coop.remote_cache_get()
|
809
|
-
coop.remote_cache_get(exclude_keys=[])
|
810
|
-
coop.remote_cache_get(exclude_keys=["a"])
|
811
|
-
exclude_keys = [cache_entry.key for cache_entry in cache_entries]
|
812
|
-
coop.remote_cache_get(exclude_keys)
|
813
|
-
# clear
|
814
|
-
coop.remote_cache_clear()
|
815
|
-
coop.remote_cache_get()
|
816
|
-
|
817
|
-
##############
|
818
|
-
# D. Remote Inference
|
819
|
-
##############
|
820
|
-
job = Jobs.example()
|
821
|
-
coop.remote_inference_cost(job)
|
822
|
-
job_coop_object = coop.remote_inference_create(job)
|
823
|
-
job_coop_results = coop.remote_inference_get(job_coop_object.get("uuid"))
|
824
|
-
coop.get(uuid=job_coop_results.get("results_uuid"))
|
1
|
+
import aiohttp
|
2
|
+
import json
|
3
|
+
import os
|
4
|
+
import requests
|
5
|
+
from typing import Any, Optional, Union, Literal
|
6
|
+
from uuid import UUID
|
7
|
+
import edsl
|
8
|
+
from edsl import CONFIG, CacheEntry, Jobs, Survey
|
9
|
+
from edsl.exceptions.coop import CoopNoUUIDError, CoopServerResponseError
|
10
|
+
from edsl.coop.utils import (
|
11
|
+
EDSLObject,
|
12
|
+
ObjectRegistry,
|
13
|
+
ObjectType,
|
14
|
+
RemoteJobStatus,
|
15
|
+
VisibilityType,
|
16
|
+
)
|
17
|
+
|
18
|
+
|
19
|
+
class Coop:
|
20
|
+
"""
|
21
|
+
Client for the Expected Parrot API.
|
22
|
+
"""
|
23
|
+
|
24
|
+
def __init__(self, api_key: str = None, url: str = None) -> None:
|
25
|
+
"""
|
26
|
+
Initialize the client.
|
27
|
+
- Provide an API key directly, or through an env variable.
|
28
|
+
- Provide a URL directly, or use the default one.
|
29
|
+
"""
|
30
|
+
self.api_key = api_key or os.getenv("EXPECTED_PARROT_API_KEY")
|
31
|
+
self.url = url or CONFIG.EXPECTED_PARROT_URL
|
32
|
+
if self.url.endswith("/"):
|
33
|
+
self.url = self.url[:-1]
|
34
|
+
self._edsl_version = edsl.__version__
|
35
|
+
|
36
|
+
################
|
37
|
+
# BASIC METHODS
|
38
|
+
################
|
39
|
+
@property
|
40
|
+
def headers(self) -> dict:
|
41
|
+
"""
|
42
|
+
Return the headers for the request.
|
43
|
+
"""
|
44
|
+
headers = {}
|
45
|
+
if self.api_key:
|
46
|
+
headers["Authorization"] = f"Bearer {self.api_key}"
|
47
|
+
else:
|
48
|
+
headers["Authorization"] = f"Bearer None"
|
49
|
+
return headers
|
50
|
+
|
51
|
+
def _send_server_request(
|
52
|
+
self,
|
53
|
+
uri: str,
|
54
|
+
method: str,
|
55
|
+
payload: Optional[dict[str, Any]] = None,
|
56
|
+
params: Optional[dict[str, Any]] = None,
|
57
|
+
timeout: Optional[float] = 5,
|
58
|
+
) -> requests.Response:
|
59
|
+
"""
|
60
|
+
Send a request to the server and return the response.
|
61
|
+
"""
|
62
|
+
url = f"{self.url}/{uri}"
|
63
|
+
method = method.upper()
|
64
|
+
if payload is None:
|
65
|
+
timeout = 20
|
66
|
+
elif (
|
67
|
+
method.upper() == "POST"
|
68
|
+
and "json_string" in payload
|
69
|
+
and payload.get("json_string") is not None
|
70
|
+
):
|
71
|
+
timeout = max(20, (len(payload.get("json_string", "")) // (1024 * 1024)))
|
72
|
+
try:
|
73
|
+
if method in ["GET", "DELETE"]:
|
74
|
+
response = requests.request(
|
75
|
+
method, url, params=params, headers=self.headers, timeout=timeout
|
76
|
+
)
|
77
|
+
elif method in ["POST", "PATCH"]:
|
78
|
+
response = requests.request(
|
79
|
+
method,
|
80
|
+
url,
|
81
|
+
params=params,
|
82
|
+
json=payload,
|
83
|
+
headers=self.headers,
|
84
|
+
timeout=timeout,
|
85
|
+
)
|
86
|
+
else:
|
87
|
+
raise Exception(f"Invalid {method=}.")
|
88
|
+
except requests.ConnectionError:
|
89
|
+
raise requests.ConnectionError(f"Could not connect to the server at {url}.")
|
90
|
+
|
91
|
+
return response
|
92
|
+
|
93
|
+
def _resolve_server_response(self, response: requests.Response) -> None:
|
94
|
+
"""
|
95
|
+
Check the response from the server and raise errors as appropriate.
|
96
|
+
"""
|
97
|
+
if response.status_code >= 400:
|
98
|
+
message = response.json().get("detail")
|
99
|
+
# print(response.text)
|
100
|
+
if "Authorization" in message:
|
101
|
+
print(message)
|
102
|
+
message = "Please provide an Expected Parrot API key."
|
103
|
+
raise CoopServerResponseError(message)
|
104
|
+
|
105
|
+
def _json_handle_none(self, value: Any) -> Any:
|
106
|
+
"""
|
107
|
+
Handle None values during JSON serialization.
|
108
|
+
- Return "null" if the value is None. Otherwise, don't return anything.
|
109
|
+
"""
|
110
|
+
if value is None:
|
111
|
+
return "null"
|
112
|
+
|
113
|
+
def _resolve_uuid(
|
114
|
+
self, uuid: Union[str, UUID] = None, url: str = None
|
115
|
+
) -> Union[str, UUID]:
|
116
|
+
"""
|
117
|
+
Resolve the uuid from a uuid or a url.
|
118
|
+
"""
|
119
|
+
if not url and not uuid:
|
120
|
+
raise CoopNoUUIDError("No uuid or url provided for the object.")
|
121
|
+
if not uuid and url:
|
122
|
+
uuid = url.split("/")[-1]
|
123
|
+
return uuid
|
124
|
+
|
125
|
+
@property
|
126
|
+
def edsl_settings(self) -> dict:
|
127
|
+
"""
|
128
|
+
Retrieve and return the EDSL settings stored on Coop.
|
129
|
+
If no response is received within 5 seconds, return an empty dict.
|
130
|
+
"""
|
131
|
+
from requests.exceptions import Timeout
|
132
|
+
|
133
|
+
try:
|
134
|
+
response = self._send_server_request(
|
135
|
+
uri="api/v0/edsl-settings", method="GET", timeout=5
|
136
|
+
)
|
137
|
+
self._resolve_server_response(response)
|
138
|
+
return response.json()
|
139
|
+
except Timeout:
|
140
|
+
return {}
|
141
|
+
|
142
|
+
################
|
143
|
+
# Objects
|
144
|
+
################
|
145
|
+
def create(
|
146
|
+
self,
|
147
|
+
object: EDSLObject,
|
148
|
+
description: Optional[str] = None,
|
149
|
+
visibility: Optional[VisibilityType] = "unlisted",
|
150
|
+
) -> dict:
|
151
|
+
"""
|
152
|
+
Create an EDSL object in the Coop server.
|
153
|
+
"""
|
154
|
+
object_type = ObjectRegistry.get_object_type_by_edsl_class(object)
|
155
|
+
response = self._send_server_request(
|
156
|
+
uri=f"api/v0/object",
|
157
|
+
method="POST",
|
158
|
+
payload={
|
159
|
+
"description": description,
|
160
|
+
"json_string": json.dumps(
|
161
|
+
object.to_dict(),
|
162
|
+
default=self._json_handle_none,
|
163
|
+
),
|
164
|
+
"object_type": object_type,
|
165
|
+
"visibility": visibility,
|
166
|
+
"version": self._edsl_version,
|
167
|
+
},
|
168
|
+
)
|
169
|
+
self._resolve_server_response(response)
|
170
|
+
response_json = response.json()
|
171
|
+
return {
|
172
|
+
"description": response_json.get("description"),
|
173
|
+
"object_type": object_type,
|
174
|
+
"url": f"{self.url}/content/{response_json.get('uuid')}",
|
175
|
+
"uuid": response_json.get("uuid"),
|
176
|
+
"version": self._edsl_version,
|
177
|
+
"visibility": response_json.get("visibility"),
|
178
|
+
}
|
179
|
+
|
180
|
+
def get(
|
181
|
+
self,
|
182
|
+
uuid: Union[str, UUID] = None,
|
183
|
+
url: str = None,
|
184
|
+
expected_object_type: Optional[ObjectType] = None,
|
185
|
+
) -> EDSLObject:
|
186
|
+
"""
|
187
|
+
Retrieve an EDSL object by its uuid or its url.
|
188
|
+
- If the object's visibility is private, the user must be the owner.
|
189
|
+
- Optionally, check if the retrieved object is of a certain type.
|
190
|
+
|
191
|
+
:param uuid: the uuid of the object either in str or UUID format.
|
192
|
+
:param url: the url of the object.
|
193
|
+
:param expected_object_type: the expected type of the object.
|
194
|
+
|
195
|
+
:return: the object instance.
|
196
|
+
"""
|
197
|
+
uuid = self._resolve_uuid(uuid, url)
|
198
|
+
response = self._send_server_request(
|
199
|
+
uri=f"api/v0/object",
|
200
|
+
method="GET",
|
201
|
+
params={"uuid": uuid},
|
202
|
+
)
|
203
|
+
self._resolve_server_response(response)
|
204
|
+
json_string = response.json().get("json_string")
|
205
|
+
object_type = response.json().get("object_type")
|
206
|
+
if expected_object_type and object_type != expected_object_type:
|
207
|
+
raise Exception(f"Expected {expected_object_type=} but got {object_type=}")
|
208
|
+
edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
|
209
|
+
object = edsl_class.from_dict(json.loads(json_string))
|
210
|
+
return object
|
211
|
+
|
212
|
+
def get_all(self, object_type: ObjectType) -> list[dict[str, Any]]:
|
213
|
+
"""
|
214
|
+
Retrieve all objects of a certain type associated with the user.
|
215
|
+
"""
|
216
|
+
edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
|
217
|
+
response = self._send_server_request(
|
218
|
+
uri=f"api/v0/objects",
|
219
|
+
method="GET",
|
220
|
+
params={"type": object_type},
|
221
|
+
)
|
222
|
+
self._resolve_server_response(response)
|
223
|
+
objects = [
|
224
|
+
{
|
225
|
+
"object": edsl_class.from_dict(json.loads(o.get("json_string"))),
|
226
|
+
"uuid": o.get("uuid"),
|
227
|
+
"version": o.get("version"),
|
228
|
+
"description": o.get("description"),
|
229
|
+
"visibility": o.get("visibility"),
|
230
|
+
"url": f"{self.url}/content/{o.get('uuid')}",
|
231
|
+
}
|
232
|
+
for o in response.json()
|
233
|
+
]
|
234
|
+
return objects
|
235
|
+
|
236
|
+
def delete(self, uuid: Union[str, UUID] = None, url: str = None) -> dict:
|
237
|
+
"""
|
238
|
+
Delete an object from the server.
|
239
|
+
"""
|
240
|
+
uuid = self._resolve_uuid(uuid, url)
|
241
|
+
response = self._send_server_request(
|
242
|
+
uri=f"api/v0/object",
|
243
|
+
method="DELETE",
|
244
|
+
params={"uuid": uuid},
|
245
|
+
)
|
246
|
+
self._resolve_server_response(response)
|
247
|
+
return response.json()
|
248
|
+
|
249
|
+
def patch(
|
250
|
+
self,
|
251
|
+
uuid: Union[str, UUID] = None,
|
252
|
+
url: str = None,
|
253
|
+
description: Optional[str] = None,
|
254
|
+
value: Optional[EDSLObject] = None,
|
255
|
+
visibility: Optional[VisibilityType] = None,
|
256
|
+
) -> dict:
|
257
|
+
"""
|
258
|
+
Change the attributes of an uploaded object
|
259
|
+
- Only supports visibility for now
|
260
|
+
"""
|
261
|
+
if description is None and visibility is None and value is None:
|
262
|
+
raise Exception("Nothing to patch.")
|
263
|
+
uuid = self._resolve_uuid(uuid, url)
|
264
|
+
response = self._send_server_request(
|
265
|
+
uri=f"api/v0/object",
|
266
|
+
method="PATCH",
|
267
|
+
params={"uuid": uuid},
|
268
|
+
payload={
|
269
|
+
"description": description,
|
270
|
+
"json_string": (
|
271
|
+
json.dumps(
|
272
|
+
value.to_dict(),
|
273
|
+
default=self._json_handle_none,
|
274
|
+
)
|
275
|
+
if value
|
276
|
+
else None
|
277
|
+
),
|
278
|
+
"visibility": visibility,
|
279
|
+
},
|
280
|
+
)
|
281
|
+
self._resolve_server_response(response)
|
282
|
+
return response.json()
|
283
|
+
|
284
|
+
################
|
285
|
+
# Remote Cache
|
286
|
+
################
|
287
|
+
def remote_cache_create(
|
288
|
+
self,
|
289
|
+
cache_entry: CacheEntry,
|
290
|
+
visibility: VisibilityType = "private",
|
291
|
+
description: Optional[str] = None,
|
292
|
+
) -> dict:
|
293
|
+
"""
|
294
|
+
Create a single remote cache entry.
|
295
|
+
If an entry with the same key already exists in the database, update it instead.
|
296
|
+
|
297
|
+
:param cache_entry: The cache entry to send to the server.
|
298
|
+
:param visibility: The visibility of the cache entry.
|
299
|
+
:param optional description: A description for this entry in the remote cache.
|
300
|
+
|
301
|
+
>>> entry = CacheEntry.example()
|
302
|
+
>>> coop.remote_cache_create(cache_entry=entry)
|
303
|
+
{'status': 'success', 'created_entry_count': 1, 'updated_entry_count': 0}
|
304
|
+
"""
|
305
|
+
response = self._send_server_request(
|
306
|
+
uri="api/v0/remote-cache",
|
307
|
+
method="POST",
|
308
|
+
payload={
|
309
|
+
"json_string": json.dumps(cache_entry.to_dict()),
|
310
|
+
"version": self._edsl_version,
|
311
|
+
"visibility": visibility,
|
312
|
+
"description": description,
|
313
|
+
},
|
314
|
+
)
|
315
|
+
self._resolve_server_response(response)
|
316
|
+
response_json = response.json()
|
317
|
+
created_entry_count = response_json.get("created_entry_count", 0)
|
318
|
+
if created_entry_count > 0:
|
319
|
+
self.remote_cache_create_log(
|
320
|
+
response,
|
321
|
+
description="Upload new cache entries to server",
|
322
|
+
cache_entry_count=created_entry_count,
|
323
|
+
)
|
324
|
+
return response.json()
|
325
|
+
|
326
|
+
def remote_cache_create_many(
|
327
|
+
self,
|
328
|
+
cache_entries: list[CacheEntry],
|
329
|
+
visibility: VisibilityType = "private",
|
330
|
+
description: Optional[str] = None,
|
331
|
+
) -> dict:
|
332
|
+
"""
|
333
|
+
Create many remote cache entries.
|
334
|
+
If an entry with the same key already exists in the database, update it instead.
|
335
|
+
|
336
|
+
:param cache_entries: The list of cache entries to send to the server.
|
337
|
+
:param visibility: The visibility of the cache entries.
|
338
|
+
:param optional description: A description for these entries in the remote cache.
|
339
|
+
|
340
|
+
>>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
|
341
|
+
>>> coop.remote_cache_create_many(cache_entries=entries)
|
342
|
+
{'status': 'success', 'created_entry_count': 10, 'updated_entry_count': 0}
|
343
|
+
"""
|
344
|
+
payload = [
|
345
|
+
{
|
346
|
+
"json_string": json.dumps(c.to_dict()),
|
347
|
+
"version": self._edsl_version,
|
348
|
+
"visibility": visibility,
|
349
|
+
"description": description,
|
350
|
+
}
|
351
|
+
for c in cache_entries
|
352
|
+
]
|
353
|
+
response = self._send_server_request(
|
354
|
+
uri="api/v0/remote-cache/many",
|
355
|
+
method="POST",
|
356
|
+
payload=payload,
|
357
|
+
)
|
358
|
+
self._resolve_server_response(response)
|
359
|
+
response_json = response.json()
|
360
|
+
created_entry_count = response_json.get("created_entry_count", 0)
|
361
|
+
if created_entry_count > 0:
|
362
|
+
self.remote_cache_create_log(
|
363
|
+
response,
|
364
|
+
description="Upload new cache entries to server",
|
365
|
+
cache_entry_count=created_entry_count,
|
366
|
+
)
|
367
|
+
return response.json()
|
368
|
+
|
369
|
+
def remote_cache_get(
|
370
|
+
self,
|
371
|
+
exclude_keys: Optional[list[str]] = None,
|
372
|
+
) -> list[CacheEntry]:
|
373
|
+
"""
|
374
|
+
Get all remote cache entries.
|
375
|
+
|
376
|
+
:param optional exclude_keys: Exclude CacheEntry objects with these keys.
|
377
|
+
|
378
|
+
>>> coop.remote_cache_get()
|
379
|
+
[CacheEntry(...), CacheEntry(...), ...]
|
380
|
+
"""
|
381
|
+
if exclude_keys is None:
|
382
|
+
exclude_keys = []
|
383
|
+
response = self._send_server_request(
|
384
|
+
uri="api/v0/remote-cache/get-many",
|
385
|
+
method="POST",
|
386
|
+
payload={"keys": exclude_keys},
|
387
|
+
)
|
388
|
+
self._resolve_server_response(response)
|
389
|
+
return [
|
390
|
+
CacheEntry.from_dict(json.loads(v.get("json_string")))
|
391
|
+
for v in response.json()
|
392
|
+
]
|
393
|
+
|
394
|
+
def remote_cache_get_diff(
|
395
|
+
self,
|
396
|
+
client_cacheentry_keys: list[str],
|
397
|
+
) -> dict:
|
398
|
+
"""
|
399
|
+
Get the difference between local and remote cache entries for a user.
|
400
|
+
"""
|
401
|
+
response = self._send_server_request(
|
402
|
+
uri="api/v0/remote-cache/get-diff",
|
403
|
+
method="POST",
|
404
|
+
payload={"keys": client_cacheentry_keys},
|
405
|
+
)
|
406
|
+
self._resolve_server_response(response)
|
407
|
+
response_json = response.json()
|
408
|
+
response_dict = {
|
409
|
+
"client_missing_cacheentries": [
|
410
|
+
CacheEntry.from_dict(json.loads(c.get("json_string")))
|
411
|
+
for c in response_json.get("client_missing_cacheentries", [])
|
412
|
+
],
|
413
|
+
"server_missing_cacheentry_keys": response_json.get(
|
414
|
+
"server_missing_cacheentry_keys", []
|
415
|
+
),
|
416
|
+
}
|
417
|
+
downloaded_entry_count = len(response_dict["client_missing_cacheentries"])
|
418
|
+
if downloaded_entry_count > 0:
|
419
|
+
self.remote_cache_create_log(
|
420
|
+
response,
|
421
|
+
description="Download missing cache entries to client",
|
422
|
+
cache_entry_count=downloaded_entry_count,
|
423
|
+
)
|
424
|
+
return response_dict
|
425
|
+
|
426
|
+
def remote_cache_clear(self) -> dict:
|
427
|
+
"""
|
428
|
+
Clear all remote cache entries.
|
429
|
+
|
430
|
+
>>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
|
431
|
+
>>> coop.remote_cache_create_many(cache_entries=entries)
|
432
|
+
>>> coop.remote_cache_clear()
|
433
|
+
{'status': 'success', 'deleted_entry_count': 10}
|
434
|
+
"""
|
435
|
+
response = self._send_server_request(
|
436
|
+
uri="api/v0/remote-cache/delete-all",
|
437
|
+
method="DELETE",
|
438
|
+
)
|
439
|
+
self._resolve_server_response(response)
|
440
|
+
response_json = response.json()
|
441
|
+
deleted_entry_count = response_json.get("deleted_entry_count", 0)
|
442
|
+
if deleted_entry_count > 0:
|
443
|
+
self.remote_cache_create_log(
|
444
|
+
response,
|
445
|
+
description="Clear cache entries",
|
446
|
+
cache_entry_count=deleted_entry_count,
|
447
|
+
)
|
448
|
+
return response.json()
|
449
|
+
|
450
|
+
def remote_cache_create_log(
|
451
|
+
self, response: requests.Response, description: str, cache_entry_count: int
|
452
|
+
) -> Union[dict, None]:
|
453
|
+
"""
|
454
|
+
If a remote cache action has been completed successfully,
|
455
|
+
log the action.
|
456
|
+
"""
|
457
|
+
if 200 <= response.status_code < 300:
|
458
|
+
log_response = self._send_server_request(
|
459
|
+
uri="api/v0/remote-cache-log",
|
460
|
+
method="POST",
|
461
|
+
payload={
|
462
|
+
"description": description,
|
463
|
+
"cache_entry_count": cache_entry_count,
|
464
|
+
},
|
465
|
+
)
|
466
|
+
self._resolve_server_response(log_response)
|
467
|
+
return response.json()
|
468
|
+
|
469
|
+
def remote_cache_clear_log(self) -> dict:
|
470
|
+
"""
|
471
|
+
Clear all remote cache log entries.
|
472
|
+
|
473
|
+
>>> coop.remote_cache_clear_log()
|
474
|
+
{'status': 'success'}
|
475
|
+
"""
|
476
|
+
response = self._send_server_request(
|
477
|
+
uri="api/v0/remote-cache-log/delete-all",
|
478
|
+
method="DELETE",
|
479
|
+
)
|
480
|
+
self._resolve_server_response(response)
|
481
|
+
return response.json()
|
482
|
+
|
483
|
+
################
|
484
|
+
# Remote Inference
|
485
|
+
################
|
486
|
+
def remote_inference_create(
|
487
|
+
self,
|
488
|
+
job: Jobs,
|
489
|
+
description: Optional[str] = None,
|
490
|
+
status: RemoteJobStatus = "queued",
|
491
|
+
visibility: Optional[VisibilityType] = "unlisted",
|
492
|
+
iterations: Optional[int] = 1,
|
493
|
+
) -> dict:
|
494
|
+
"""
|
495
|
+
Send a remote inference job to the server.
|
496
|
+
|
497
|
+
:param job: The EDSL job to send to the server.
|
498
|
+
:param optional description: A description for this entry in the remote cache.
|
499
|
+
:param status: The status of the job. Should be 'queued', unless you are debugging.
|
500
|
+
:param visibility: The visibility of the cache entry.
|
501
|
+
:param iterations: The number of times to run each interview.
|
502
|
+
|
503
|
+
>>> job = Jobs.example()
|
504
|
+
>>> coop.remote_inference_create(job=job, description="My job")
|
505
|
+
{'uuid': '9f8484ee-b407-40e4-9652-4133a7236c9c', 'description': 'My job', 'status': 'queued', 'visibility': 'unlisted', 'version': '0.1.29.dev4'}
|
506
|
+
"""
|
507
|
+
response = self._send_server_request(
|
508
|
+
uri="api/v0/remote-inference",
|
509
|
+
method="POST",
|
510
|
+
payload={
|
511
|
+
"json_string": json.dumps(
|
512
|
+
job.to_dict(),
|
513
|
+
default=self._json_handle_none,
|
514
|
+
),
|
515
|
+
"description": description,
|
516
|
+
"status": status,
|
517
|
+
"iterations": iterations,
|
518
|
+
"visibility": visibility,
|
519
|
+
"version": self._edsl_version,
|
520
|
+
},
|
521
|
+
)
|
522
|
+
self._resolve_server_response(response)
|
523
|
+
response_json = response.json()
|
524
|
+
return {
|
525
|
+
"uuid": response_json.get("job_uuid"),
|
526
|
+
"description": response_json.get("description"),
|
527
|
+
"status": response_json.get("status"),
|
528
|
+
"iterations": response_json.get("iterations"),
|
529
|
+
"visibility": response_json.get("visibility"),
|
530
|
+
"version": self._edsl_version,
|
531
|
+
}
|
532
|
+
|
533
|
+
def remote_inference_get(
|
534
|
+
self, job_uuid: Optional[str] = None, results_uuid: Optional[str] = None
|
535
|
+
) -> dict:
|
536
|
+
"""
|
537
|
+
Get the details of a remote inference job.
|
538
|
+
You can pass either the job uuid or the results uuid as a parameter.
|
539
|
+
If you pass both, the job uuid will be prioritized.
|
540
|
+
|
541
|
+
:param job_uuid: The UUID of the EDSL job.
|
542
|
+
:param results_uuid: The UUID of the results associated with the EDSL job.
|
543
|
+
|
544
|
+
>>> coop.remote_inference_get("9f8484ee-b407-40e4-9652-4133a7236c9c")
|
545
|
+
{'jobs_uuid': '9f8484ee-b407-40e4-9652-4133a7236c9c', 'results_uuid': 'dd708234-31bf-4fe1-8747-6e232625e026', 'results_url': 'https://www.expectedparrot.com/content/dd708234-31bf-4fe1-8747-6e232625e026', 'status': 'completed', 'reason': None, 'price': 16, 'version': '0.1.29.dev4'}
|
546
|
+
"""
|
547
|
+
if job_uuid is None and results_uuid is None:
|
548
|
+
raise ValueError("Either job_uuid or results_uuid must be provided.")
|
549
|
+
elif job_uuid is not None:
|
550
|
+
params = {"job_uuid": job_uuid}
|
551
|
+
else:
|
552
|
+
params = {"results_uuid": results_uuid}
|
553
|
+
|
554
|
+
response = self._send_server_request(
|
555
|
+
uri="api/v0/remote-inference",
|
556
|
+
method="GET",
|
557
|
+
params=params,
|
558
|
+
)
|
559
|
+
self._resolve_server_response(response)
|
560
|
+
data = response.json()
|
561
|
+
return {
|
562
|
+
"job_uuid": data.get("job_uuid"),
|
563
|
+
"results_uuid": data.get("results_uuid"),
|
564
|
+
"results_url": f"{self.url}/content/{data.get('results_uuid')}",
|
565
|
+
"status": data.get("status"),
|
566
|
+
"reason": data.get("reason"),
|
567
|
+
"credits_consumed": data.get("price"),
|
568
|
+
"version": data.get("version"),
|
569
|
+
}
|
570
|
+
|
571
|
+
def remote_inference_cost(self, input: Union[Jobs, Survey]) -> int:
|
572
|
+
"""
|
573
|
+
Get the cost of a remote inference job.
|
574
|
+
|
575
|
+
:param input: The EDSL job to send to the server.
|
576
|
+
|
577
|
+
>>> job = Jobs.example()
|
578
|
+
>>> coop.remote_inference_cost(input=job)
|
579
|
+
16
|
580
|
+
"""
|
581
|
+
if isinstance(input, Jobs):
|
582
|
+
job = input
|
583
|
+
elif isinstance(input, Survey):
|
584
|
+
job = Jobs(survey=input)
|
585
|
+
else:
|
586
|
+
raise TypeError("Input must be either a Job or a Survey.")
|
587
|
+
|
588
|
+
response = self._send_server_request(
|
589
|
+
uri="api/v0/remote-inference/cost",
|
590
|
+
method="POST",
|
591
|
+
payload={
|
592
|
+
"json_string": json.dumps(
|
593
|
+
job.to_dict(),
|
594
|
+
default=self._json_handle_none,
|
595
|
+
),
|
596
|
+
},
|
597
|
+
)
|
598
|
+
self._resolve_server_response(response)
|
599
|
+
response_json = response.json()
|
600
|
+
return {
|
601
|
+
"credits": response_json.get("cost_in_credits"),
|
602
|
+
"usd": response_json.get("cost_in_usd"),
|
603
|
+
}
|
604
|
+
|
605
|
+
################
|
606
|
+
# DUNDER METHODS
|
607
|
+
################
|
608
|
+
def __repr__(self):
|
609
|
+
"""Return a string representation of the client."""
|
610
|
+
return f"Client(api_key='{self.api_key}', url='{self.url}')"
|
611
|
+
|
612
|
+
################
|
613
|
+
# EXPERIMENTAL
|
614
|
+
################
|
615
|
+
async def remote_async_execute_model_call(
|
616
|
+
self, model_dict: dict, user_prompt: str, system_prompt: str
|
617
|
+
) -> dict:
|
618
|
+
url = self.url + "/inference/"
|
619
|
+
# print("Now using url: ", url)
|
620
|
+
data = {
|
621
|
+
"model_dict": model_dict,
|
622
|
+
"user_prompt": user_prompt,
|
623
|
+
"system_prompt": system_prompt,
|
624
|
+
}
|
625
|
+
# Use aiohttp to send a POST request asynchronously
|
626
|
+
async with aiohttp.ClientSession() as session:
|
627
|
+
async with session.post(url, json=data) as response:
|
628
|
+
response_data = await response.json()
|
629
|
+
return response_data
|
630
|
+
|
631
|
+
def web(
|
632
|
+
self,
|
633
|
+
survey: dict,
|
634
|
+
platform: Literal[
|
635
|
+
"google_forms", "lime_survey", "survey_monkey"
|
636
|
+
] = "lime_survey",
|
637
|
+
email=None,
|
638
|
+
):
|
639
|
+
url = f"{self.url}/api/v0/export_to_{platform}"
|
640
|
+
if email:
|
641
|
+
data = {"json_string": json.dumps({"survey": survey, "email": email})}
|
642
|
+
else:
|
643
|
+
data = {"json_string": json.dumps({"survey": survey, "email": ""})}
|
644
|
+
|
645
|
+
response_json = requests.post(url, headers=self.headers, data=json.dumps(data))
|
646
|
+
|
647
|
+
return response_json
|
648
|
+
|
649
|
+
def fetch_prices(self) -> dict:
|
650
|
+
"""
|
651
|
+
Fetch model prices from Coop. If the request fails, return an empty dict.
|
652
|
+
"""
|
653
|
+
|
654
|
+
from edsl.coop.PriceFetcher import PriceFetcher
|
655
|
+
|
656
|
+
from edsl.config import CONFIG
|
657
|
+
|
658
|
+
if bool(CONFIG.get("EDSL_FETCH_TOKEN_PRICES")):
|
659
|
+
price_fetcher = PriceFetcher()
|
660
|
+
return price_fetcher.fetch_prices()
|
661
|
+
else:
|
662
|
+
return {}
|
663
|
+
|
664
|
+
def fetch_rate_limit_config_vars(self) -> dict:
|
665
|
+
"""
|
666
|
+
Fetch a dict of rate limit config vars from Coop.
|
667
|
+
|
668
|
+
The dict keys are RPM and TPM variables like EDSL_SERVICE_RPM_OPENAI.
|
669
|
+
"""
|
670
|
+
response = self._send_server_request(
|
671
|
+
uri="api/v0/config-vars",
|
672
|
+
method="GET",
|
673
|
+
)
|
674
|
+
self._resolve_server_response(response)
|
675
|
+
data = response.json()
|
676
|
+
return data
|
677
|
+
|
678
|
+
|
679
|
+
if __name__ == "__main__":
|
680
|
+
sheet_data = fetch_sheet_data()
|
681
|
+
if sheet_data:
|
682
|
+
print(f"Successfully fetched {len(sheet_data)} rows of data.")
|
683
|
+
print("First row:", sheet_data[0])
|
684
|
+
else:
|
685
|
+
print("Failed to fetch sheet data.")
|
686
|
+
|
687
|
+
|
688
|
+
def main():
|
689
|
+
"""
|
690
|
+
A simple example for the coop client
|
691
|
+
"""
|
692
|
+
from uuid import uuid4
|
693
|
+
from edsl import (
|
694
|
+
Agent,
|
695
|
+
AgentList,
|
696
|
+
Cache,
|
697
|
+
Notebook,
|
698
|
+
QuestionFreeText,
|
699
|
+
QuestionMultipleChoice,
|
700
|
+
Results,
|
701
|
+
Scenario,
|
702
|
+
ScenarioList,
|
703
|
+
Survey,
|
704
|
+
)
|
705
|
+
from edsl.coop import Coop
|
706
|
+
from edsl.data.CacheEntry import CacheEntry
|
707
|
+
from edsl.jobs import Jobs
|
708
|
+
|
709
|
+
# init & basics
|
710
|
+
API_KEY = "b"
|
711
|
+
coop = Coop(api_key=API_KEY)
|
712
|
+
coop
|
713
|
+
coop.edsl_settings
|
714
|
+
|
715
|
+
##############
|
716
|
+
# A. A simple example
|
717
|
+
##############
|
718
|
+
# .. create and manipulate an object through the Coop client
|
719
|
+
response = coop.create(QuestionMultipleChoice.example())
|
720
|
+
coop.get(uuid=response.get("uuid"))
|
721
|
+
coop.get(uuid=response.get("uuid"), expected_object_type="question")
|
722
|
+
coop.get(url=response.get("url"))
|
723
|
+
coop.create(QuestionMultipleChoice.example())
|
724
|
+
coop.get_all("question")
|
725
|
+
coop.patch(uuid=response.get("uuid"), visibility="private")
|
726
|
+
coop.patch(uuid=response.get("uuid"), description="hey")
|
727
|
+
coop.patch(uuid=response.get("uuid"), value=QuestionFreeText.example())
|
728
|
+
# coop.patch(uuid=response.get("uuid"), value=Survey.example()) - should throw error
|
729
|
+
coop.get(uuid=response.get("uuid"))
|
730
|
+
coop.delete(uuid=response.get("uuid"))
|
731
|
+
|
732
|
+
# .. create and manipulate an object through the class
|
733
|
+
response = QuestionMultipleChoice.example().push()
|
734
|
+
QuestionMultipleChoice.pull(uuid=response.get("uuid"))
|
735
|
+
QuestionMultipleChoice.pull(url=response.get("url"))
|
736
|
+
QuestionMultipleChoice.patch(uuid=response.get("uuid"), visibility="private")
|
737
|
+
QuestionMultipleChoice.patch(uuid=response.get("uuid"), description="hey")
|
738
|
+
QuestionMultipleChoice.patch(
|
739
|
+
uuid=response.get("uuid"), value=QuestionFreeText.example()
|
740
|
+
)
|
741
|
+
QuestionMultipleChoice.pull(response.get("uuid"))
|
742
|
+
QuestionMultipleChoice.delete(response.get("uuid"))
|
743
|
+
|
744
|
+
##############
|
745
|
+
# B. Examples with all objects
|
746
|
+
##############
|
747
|
+
OBJECTS = [
|
748
|
+
("agent", Agent),
|
749
|
+
("agent_list", AgentList),
|
750
|
+
("cache", Cache),
|
751
|
+
("notebook", Notebook),
|
752
|
+
("question", QuestionMultipleChoice),
|
753
|
+
("results", Results),
|
754
|
+
("scenario", Scenario),
|
755
|
+
("scenario_list", ScenarioList),
|
756
|
+
("survey", Survey),
|
757
|
+
]
|
758
|
+
for object_type, cls in OBJECTS:
|
759
|
+
print(f"Testing {object_type} objects")
|
760
|
+
# 1. Delete existing objects
|
761
|
+
existing_objects = coop.get_all(object_type)
|
762
|
+
for item in existing_objects:
|
763
|
+
coop.delete(uuid=item.get("uuid"))
|
764
|
+
# 2. Create new objects
|
765
|
+
example = cls.example()
|
766
|
+
response_1 = coop.create(example)
|
767
|
+
response_2 = coop.create(cls.example(), visibility="private")
|
768
|
+
response_3 = coop.create(cls.example(), visibility="public")
|
769
|
+
response_4 = coop.create(
|
770
|
+
cls.example(), visibility="unlisted", description="hey"
|
771
|
+
)
|
772
|
+
# 3. Retrieve all objects
|
773
|
+
objects = coop.get_all(object_type)
|
774
|
+
assert len(objects) == 4
|
775
|
+
# 4. Try to retrieve an item that does not exist
|
776
|
+
try:
|
777
|
+
coop.get(uuid=uuid4())
|
778
|
+
except Exception as e:
|
779
|
+
print(e)
|
780
|
+
# 5. Try to retrieve all test objects by their uuids
|
781
|
+
for response in [response_1, response_2, response_3, response_4]:
|
782
|
+
coop.get(uuid=response.get("uuid"))
|
783
|
+
# 6. Change visibility of all objects
|
784
|
+
for item in objects:
|
785
|
+
coop.patch(uuid=item.get("uuid"), visibility="private")
|
786
|
+
# 6. Change description of all objects
|
787
|
+
for item in objects:
|
788
|
+
coop.patch(uuid=item.get("uuid"), description="hey")
|
789
|
+
# 7. Delete all objects
|
790
|
+
for item in objects:
|
791
|
+
coop.delete(uuid=item.get("uuid"))
|
792
|
+
assert len(coop.get_all(object_type)) == 0
|
793
|
+
|
794
|
+
##############
|
795
|
+
# C. Remote Cache
|
796
|
+
##############
|
797
|
+
# clear
|
798
|
+
coop.remote_cache_clear()
|
799
|
+
assert coop.remote_cache_get() == []
|
800
|
+
# create one remote cache entry
|
801
|
+
cache_entry = CacheEntry.example()
|
802
|
+
cache_entry.to_dict()
|
803
|
+
coop.remote_cache_create(cache_entry)
|
804
|
+
# create many remote cache entries
|
805
|
+
cache_entries = [CacheEntry.example(randomize=True) for _ in range(10)]
|
806
|
+
coop.remote_cache_create_many(cache_entries)
|
807
|
+
# get all remote cache entries
|
808
|
+
coop.remote_cache_get()
|
809
|
+
coop.remote_cache_get(exclude_keys=[])
|
810
|
+
coop.remote_cache_get(exclude_keys=["a"])
|
811
|
+
exclude_keys = [cache_entry.key for cache_entry in cache_entries]
|
812
|
+
coop.remote_cache_get(exclude_keys)
|
813
|
+
# clear
|
814
|
+
coop.remote_cache_clear()
|
815
|
+
coop.remote_cache_get()
|
816
|
+
|
817
|
+
##############
|
818
|
+
# D. Remote Inference
|
819
|
+
##############
|
820
|
+
job = Jobs.example()
|
821
|
+
coop.remote_inference_cost(job)
|
822
|
+
job_coop_object = coop.remote_inference_create(job)
|
823
|
+
job_coop_results = coop.remote_inference_get(job_coop_object.get("uuid"))
|
824
|
+
coop.get(uuid=job_coop_results.get("results_uuid"))
|