edsl 0.1.37.dev6__py3-none-any.whl → 0.1.38.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. edsl/Base.py +303 -303
  2. edsl/BaseDiff.py +260 -260
  3. edsl/TemplateLoader.py +24 -24
  4. edsl/__init__.py +48 -48
  5. edsl/__version__.py +1 -1
  6. edsl/agents/Agent.py +855 -855
  7. edsl/agents/AgentList.py +350 -350
  8. edsl/agents/Invigilator.py +222 -222
  9. edsl/agents/InvigilatorBase.py +284 -284
  10. edsl/agents/PromptConstructor.py +353 -353
  11. edsl/agents/__init__.py +3 -3
  12. edsl/agents/descriptors.py +99 -99
  13. edsl/agents/prompt_helpers.py +129 -129
  14. edsl/auto/AutoStudy.py +117 -117
  15. edsl/auto/StageBase.py +230 -230
  16. edsl/auto/StageGenerateSurvey.py +178 -178
  17. edsl/auto/StageLabelQuestions.py +125 -125
  18. edsl/auto/StagePersona.py +61 -61
  19. edsl/auto/StagePersonaDimensionValueRanges.py +88 -88
  20. edsl/auto/StagePersonaDimensionValues.py +74 -74
  21. edsl/auto/StagePersonaDimensions.py +69 -69
  22. edsl/auto/StageQuestions.py +73 -73
  23. edsl/auto/SurveyCreatorPipeline.py +21 -21
  24. edsl/auto/utilities.py +224 -224
  25. edsl/base/Base.py +289 -289
  26. edsl/config.py +149 -149
  27. edsl/conjure/AgentConstructionMixin.py +160 -160
  28. edsl/conjure/Conjure.py +62 -62
  29. edsl/conjure/InputData.py +659 -659
  30. edsl/conjure/InputDataCSV.py +48 -48
  31. edsl/conjure/InputDataMixinQuestionStats.py +182 -182
  32. edsl/conjure/InputDataPyRead.py +91 -91
  33. edsl/conjure/InputDataSPSS.py +8 -8
  34. edsl/conjure/InputDataStata.py +8 -8
  35. edsl/conjure/QuestionOptionMixin.py +76 -76
  36. edsl/conjure/QuestionTypeMixin.py +23 -23
  37. edsl/conjure/RawQuestion.py +65 -65
  38. edsl/conjure/SurveyResponses.py +7 -7
  39. edsl/conjure/__init__.py +9 -9
  40. edsl/conjure/naming_utilities.py +263 -263
  41. edsl/conjure/utilities.py +201 -201
  42. edsl/conversation/Conversation.py +290 -290
  43. edsl/conversation/car_buying.py +58 -58
  44. edsl/conversation/chips.py +95 -95
  45. edsl/conversation/mug_negotiation.py +81 -81
  46. edsl/conversation/next_speaker_utilities.py +93 -93
  47. edsl/coop/PriceFetcher.py +54 -54
  48. edsl/coop/__init__.py +2 -2
  49. edsl/coop/coop.py +958 -958
  50. edsl/coop/utils.py +131 -131
  51. edsl/data/Cache.py +527 -527
  52. edsl/data/CacheEntry.py +228 -228
  53. edsl/data/CacheHandler.py +149 -149
  54. edsl/data/RemoteCacheSync.py +97 -97
  55. edsl/data/SQLiteDict.py +292 -292
  56. edsl/data/__init__.py +4 -4
  57. edsl/data/orm.py +10 -10
  58. edsl/data_transfer_models.py +73 -73
  59. edsl/enums.py +173 -173
  60. edsl/exceptions/BaseException.py +21 -21
  61. edsl/exceptions/__init__.py +54 -54
  62. edsl/exceptions/agents.py +38 -38
  63. edsl/exceptions/configuration.py +16 -16
  64. edsl/exceptions/coop.py +10 -10
  65. edsl/exceptions/data.py +14 -14
  66. edsl/exceptions/general.py +34 -34
  67. edsl/exceptions/jobs.py +33 -33
  68. edsl/exceptions/language_models.py +63 -63
  69. edsl/exceptions/prompts.py +15 -15
  70. edsl/exceptions/questions.py +91 -91
  71. edsl/exceptions/results.py +29 -29
  72. edsl/exceptions/scenarios.py +22 -22
  73. edsl/exceptions/surveys.py +37 -37
  74. edsl/inference_services/AnthropicService.py +87 -87
  75. edsl/inference_services/AwsBedrock.py +120 -120
  76. edsl/inference_services/AzureAI.py +217 -217
  77. edsl/inference_services/DeepInfraService.py +18 -18
  78. edsl/inference_services/GoogleService.py +156 -156
  79. edsl/inference_services/GroqService.py +20 -20
  80. edsl/inference_services/InferenceServiceABC.py +147 -147
  81. edsl/inference_services/InferenceServicesCollection.py +97 -97
  82. edsl/inference_services/MistralAIService.py +123 -123
  83. edsl/inference_services/OllamaService.py +18 -18
  84. edsl/inference_services/OpenAIService.py +224 -224
  85. edsl/inference_services/TestService.py +89 -89
  86. edsl/inference_services/TogetherAIService.py +170 -170
  87. edsl/inference_services/models_available_cache.py +118 -118
  88. edsl/inference_services/rate_limits_cache.py +25 -25
  89. edsl/inference_services/registry.py +39 -39
  90. edsl/inference_services/write_available.py +10 -10
  91. edsl/jobs/Answers.py +56 -56
  92. edsl/jobs/Jobs.py +1347 -1347
  93. edsl/jobs/__init__.py +1 -1
  94. edsl/jobs/buckets/BucketCollection.py +63 -63
  95. edsl/jobs/buckets/ModelBuckets.py +65 -65
  96. edsl/jobs/buckets/TokenBucket.py +248 -248
  97. edsl/jobs/interviews/Interview.py +661 -661
  98. edsl/jobs/interviews/InterviewExceptionCollection.py +99 -99
  99. edsl/jobs/interviews/InterviewExceptionEntry.py +186 -186
  100. edsl/jobs/interviews/InterviewStatistic.py +63 -63
  101. edsl/jobs/interviews/InterviewStatisticsCollection.py +25 -25
  102. edsl/jobs/interviews/InterviewStatusDictionary.py +78 -78
  103. edsl/jobs/interviews/InterviewStatusLog.py +92 -92
  104. edsl/jobs/interviews/ReportErrors.py +66 -66
  105. edsl/jobs/interviews/interview_status_enum.py +9 -9
  106. edsl/jobs/runners/JobsRunnerAsyncio.py +338 -338
  107. edsl/jobs/runners/JobsRunnerStatus.py +332 -332
  108. edsl/jobs/tasks/QuestionTaskCreator.py +242 -242
  109. edsl/jobs/tasks/TaskCreators.py +64 -64
  110. edsl/jobs/tasks/TaskHistory.py +442 -442
  111. edsl/jobs/tasks/TaskStatusLog.py +23 -23
  112. edsl/jobs/tasks/task_status_enum.py +163 -163
  113. edsl/jobs/tokens/InterviewTokenUsage.py +27 -27
  114. edsl/jobs/tokens/TokenUsage.py +34 -34
  115. edsl/language_models/KeyLookup.py +30 -30
  116. edsl/language_models/LanguageModel.py +706 -706
  117. edsl/language_models/ModelList.py +102 -102
  118. edsl/language_models/RegisterLanguageModelsMeta.py +184 -184
  119. edsl/language_models/__init__.py +3 -3
  120. edsl/language_models/fake_openai_call.py +15 -15
  121. edsl/language_models/fake_openai_service.py +61 -61
  122. edsl/language_models/registry.py +137 -137
  123. edsl/language_models/repair.py +156 -156
  124. edsl/language_models/unused/ReplicateBase.py +83 -83
  125. edsl/language_models/utilities.py +64 -64
  126. edsl/notebooks/Notebook.py +259 -259
  127. edsl/notebooks/__init__.py +1 -1
  128. edsl/prompts/Prompt.py +357 -357
  129. edsl/prompts/__init__.py +2 -2
  130. edsl/questions/AnswerValidatorMixin.py +289 -289
  131. edsl/questions/QuestionBase.py +656 -656
  132. edsl/questions/QuestionBaseGenMixin.py +161 -161
  133. edsl/questions/QuestionBasePromptsMixin.py +234 -234
  134. edsl/questions/QuestionBudget.py +227 -227
  135. edsl/questions/QuestionCheckBox.py +359 -359
  136. edsl/questions/QuestionExtract.py +183 -183
  137. edsl/questions/QuestionFreeText.py +114 -114
  138. edsl/questions/QuestionFunctional.py +159 -159
  139. edsl/questions/QuestionList.py +231 -231
  140. edsl/questions/QuestionMultipleChoice.py +286 -286
  141. edsl/questions/QuestionNumerical.py +153 -153
  142. edsl/questions/QuestionRank.py +324 -324
  143. edsl/questions/Quick.py +41 -41
  144. edsl/questions/RegisterQuestionsMeta.py +71 -71
  145. edsl/questions/ResponseValidatorABC.py +174 -174
  146. edsl/questions/SimpleAskMixin.py +73 -73
  147. edsl/questions/__init__.py +26 -26
  148. edsl/questions/compose_questions.py +98 -98
  149. edsl/questions/decorators.py +21 -21
  150. edsl/questions/derived/QuestionLikertFive.py +76 -76
  151. edsl/questions/derived/QuestionLinearScale.py +87 -87
  152. edsl/questions/derived/QuestionTopK.py +91 -91
  153. edsl/questions/derived/QuestionYesNo.py +82 -82
  154. edsl/questions/descriptors.py +413 -413
  155. edsl/questions/prompt_templates/question_budget.jinja +13 -13
  156. edsl/questions/prompt_templates/question_checkbox.jinja +32 -32
  157. edsl/questions/prompt_templates/question_extract.jinja +11 -11
  158. edsl/questions/prompt_templates/question_free_text.jinja +3 -3
  159. edsl/questions/prompt_templates/question_linear_scale.jinja +11 -11
  160. edsl/questions/prompt_templates/question_list.jinja +17 -17
  161. edsl/questions/prompt_templates/question_multiple_choice.jinja +33 -33
  162. edsl/questions/prompt_templates/question_numerical.jinja +36 -36
  163. edsl/questions/question_registry.py +147 -147
  164. edsl/questions/settings.py +12 -12
  165. edsl/questions/templates/budget/answering_instructions.jinja +7 -7
  166. edsl/questions/templates/budget/question_presentation.jinja +7 -7
  167. edsl/questions/templates/checkbox/answering_instructions.jinja +10 -10
  168. edsl/questions/templates/checkbox/question_presentation.jinja +22 -22
  169. edsl/questions/templates/extract/answering_instructions.jinja +7 -7
  170. edsl/questions/templates/likert_five/answering_instructions.jinja +10 -10
  171. edsl/questions/templates/likert_five/question_presentation.jinja +11 -11
  172. edsl/questions/templates/linear_scale/answering_instructions.jinja +5 -5
  173. edsl/questions/templates/linear_scale/question_presentation.jinja +5 -5
  174. edsl/questions/templates/list/answering_instructions.jinja +3 -3
  175. edsl/questions/templates/list/question_presentation.jinja +5 -5
  176. edsl/questions/templates/multiple_choice/answering_instructions.jinja +9 -9
  177. edsl/questions/templates/multiple_choice/question_presentation.jinja +11 -11
  178. edsl/questions/templates/numerical/answering_instructions.jinja +6 -6
  179. edsl/questions/templates/numerical/question_presentation.jinja +6 -6
  180. edsl/questions/templates/rank/answering_instructions.jinja +11 -11
  181. edsl/questions/templates/rank/question_presentation.jinja +15 -15
  182. edsl/questions/templates/top_k/answering_instructions.jinja +8 -8
  183. edsl/questions/templates/top_k/question_presentation.jinja +22 -22
  184. edsl/questions/templates/yes_no/answering_instructions.jinja +6 -6
  185. edsl/questions/templates/yes_no/question_presentation.jinja +11 -11
  186. edsl/results/Dataset.py +293 -293
  187. edsl/results/DatasetExportMixin.py +717 -717
  188. edsl/results/DatasetTree.py +145 -145
  189. edsl/results/Result.py +450 -450
  190. edsl/results/Results.py +1071 -1071
  191. edsl/results/ResultsDBMixin.py +238 -238
  192. edsl/results/ResultsExportMixin.py +43 -43
  193. edsl/results/ResultsFetchMixin.py +33 -33
  194. edsl/results/ResultsGGMixin.py +121 -121
  195. edsl/results/ResultsToolsMixin.py +98 -98
  196. edsl/results/Selector.py +135 -135
  197. edsl/results/__init__.py +2 -2
  198. edsl/results/tree_explore.py +115 -115
  199. edsl/scenarios/FileStore.py +458 -458
  200. edsl/scenarios/Scenario.py +546 -546
  201. edsl/scenarios/ScenarioHtmlMixin.py +64 -64
  202. edsl/scenarios/ScenarioList.py +1112 -1112
  203. edsl/scenarios/ScenarioListExportMixin.py +52 -52
  204. edsl/scenarios/ScenarioListPdfMixin.py +261 -261
  205. edsl/scenarios/__init__.py +4 -4
  206. edsl/shared.py +1 -1
  207. edsl/study/ObjectEntry.py +173 -173
  208. edsl/study/ProofOfWork.py +113 -113
  209. edsl/study/SnapShot.py +80 -80
  210. edsl/study/Study.py +528 -528
  211. edsl/study/__init__.py +4 -4
  212. edsl/surveys/DAG.py +148 -148
  213. edsl/surveys/Memory.py +31 -31
  214. edsl/surveys/MemoryPlan.py +244 -244
  215. edsl/surveys/Rule.py +330 -330
  216. edsl/surveys/RuleCollection.py +387 -387
  217. edsl/surveys/Survey.py +1795 -1795
  218. edsl/surveys/SurveyCSS.py +261 -261
  219. edsl/surveys/SurveyExportMixin.py +259 -259
  220. edsl/surveys/SurveyFlowVisualizationMixin.py +121 -121
  221. edsl/surveys/SurveyQualtricsImport.py +284 -284
  222. edsl/surveys/__init__.py +3 -3
  223. edsl/surveys/base.py +53 -53
  224. edsl/surveys/descriptors.py +56 -56
  225. edsl/surveys/instructions/ChangeInstruction.py +47 -47
  226. edsl/surveys/instructions/Instruction.py +51 -51
  227. edsl/surveys/instructions/InstructionCollection.py +77 -77
  228. edsl/templates/error_reporting/base.html +23 -23
  229. edsl/templates/error_reporting/exceptions_by_model.html +34 -34
  230. edsl/templates/error_reporting/exceptions_by_question_name.html +16 -16
  231. edsl/templates/error_reporting/exceptions_by_type.html +16 -16
  232. edsl/templates/error_reporting/interview_details.html +115 -115
  233. edsl/templates/error_reporting/interviews.html +9 -9
  234. edsl/templates/error_reporting/overview.html +4 -4
  235. edsl/templates/error_reporting/performance_plot.html +1 -1
  236. edsl/templates/error_reporting/report.css +73 -73
  237. edsl/templates/error_reporting/report.html +117 -117
  238. edsl/templates/error_reporting/report.js +25 -25
  239. edsl/tools/__init__.py +1 -1
  240. edsl/tools/clusters.py +192 -192
  241. edsl/tools/embeddings.py +27 -27
  242. edsl/tools/embeddings_plotting.py +118 -118
  243. edsl/tools/plotting.py +112 -112
  244. edsl/tools/summarize.py +18 -18
  245. edsl/utilities/SystemInfo.py +28 -28
  246. edsl/utilities/__init__.py +22 -22
  247. edsl/utilities/ast_utilities.py +25 -25
  248. edsl/utilities/data/Registry.py +6 -6
  249. edsl/utilities/data/__init__.py +1 -1
  250. edsl/utilities/data/scooter_results.json +1 -1
  251. edsl/utilities/decorators.py +77 -77
  252. edsl/utilities/gcp_bucket/cloud_storage.py +96 -96
  253. edsl/utilities/interface.py +627 -627
  254. edsl/utilities/repair_functions.py +28 -28
  255. edsl/utilities/restricted_python.py +70 -70
  256. edsl/utilities/utilities.py +409 -409
  257. {edsl-0.1.37.dev6.dist-info → edsl-0.1.38.dev1.dist-info}/LICENSE +21 -21
  258. {edsl-0.1.37.dev6.dist-info → edsl-0.1.38.dev1.dist-info}/METADATA +1 -1
  259. edsl-0.1.38.dev1.dist-info/RECORD +283 -0
  260. edsl-0.1.37.dev6.dist-info/RECORD +0 -283
  261. {edsl-0.1.37.dev6.dist-info → edsl-0.1.38.dev1.dist-info}/WHEEL +0 -0
edsl/coop/coop.py CHANGED
@@ -1,958 +1,958 @@
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(
94
- self, response: requests.Response, check_api_key: bool = True
95
- ) -> None:
96
- """
97
- Check the response from the server and raise errors as appropriate.
98
- """
99
- if response.status_code >= 400:
100
- message = response.json().get("detail")
101
- # print(response.text)
102
- if "The API key you provided is invalid" in message and check_api_key:
103
- import secrets
104
- from edsl.utilities.utilities import write_api_key_to_env
105
-
106
- edsl_auth_token = secrets.token_urlsafe(16)
107
-
108
- print("Your Expected Parrot API key is invalid.")
109
- print(
110
- "\nUse the link below to log in to Expected Parrot so we can automatically update your API key."
111
- )
112
- self._display_login_url(edsl_auth_token=edsl_auth_token)
113
- api_key = self._poll_for_api_key(edsl_auth_token)
114
-
115
- if api_key is None:
116
- print("\nTimed out waiting for login. Please try again.")
117
- return
118
-
119
- write_api_key_to_env(api_key)
120
- print("\n✨ API key retrieved and written to .env file.")
121
- print("Rerun your code to try again with a valid API key.")
122
- return
123
-
124
- elif "Authorization" in message:
125
- print(message)
126
- message = "Please provide an Expected Parrot API key."
127
-
128
- raise CoopServerResponseError(message)
129
-
130
- def _poll_for_api_key(
131
- self, edsl_auth_token: str, timeout: int = 120
132
- ) -> Union[str, None]:
133
- """
134
- Allows the user to retrieve their Expected Parrot API key by logging in with an EDSL auth token.
135
-
136
- :param edsl_auth_token: The EDSL auth token to use for login
137
- :param timeout: Maximum time to wait for login, in seconds (default: 120)
138
- """
139
- import time
140
- from datetime import datetime
141
-
142
- start_poll_time = time.time()
143
- waiting_for_login = True
144
- while waiting_for_login:
145
- elapsed_time = time.time() - start_poll_time
146
- if elapsed_time > timeout:
147
- # Timed out waiting for the user to log in
148
- print("\r" + " " * 80 + "\r", end="")
149
- return None
150
-
151
- api_key = self._get_api_key(edsl_auth_token)
152
- if api_key is not None:
153
- print("\r" + " " * 80 + "\r", end="")
154
- return api_key
155
- else:
156
- duration = 5
157
- time_checked = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
158
- frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
159
- start_time = time.time()
160
- i = 0
161
- while time.time() - start_time < duration:
162
- print(
163
- f"\r{frames[i % len(frames)]} Waiting for login. Last checked: {time_checked}",
164
- end="",
165
- flush=True,
166
- )
167
- time.sleep(0.1)
168
- i += 1
169
-
170
- def _json_handle_none(self, value: Any) -> Any:
171
- """
172
- Handle None values during JSON serialization.
173
- - Return "null" if the value is None. Otherwise, don't return anything.
174
- """
175
- if value is None:
176
- return "null"
177
-
178
- def _resolve_uuid(
179
- self, uuid: Union[str, UUID] = None, url: str = None
180
- ) -> Union[str, UUID]:
181
- """
182
- Resolve the uuid from a uuid or a url.
183
- """
184
- if not url and not uuid:
185
- raise CoopNoUUIDError("No uuid or url provided for the object.")
186
- if not uuid and url:
187
- uuid = url.split("/")[-1]
188
- return uuid
189
-
190
- @property
191
- def edsl_settings(self) -> dict:
192
- """
193
- Retrieve and return the EDSL settings stored on Coop.
194
- If no response is received within 5 seconds, return an empty dict.
195
- """
196
- from requests.exceptions import Timeout
197
-
198
- try:
199
- response = self._send_server_request(
200
- uri="api/v0/edsl-settings", method="GET", timeout=5
201
- )
202
- self._resolve_server_response(response, check_api_key=False)
203
- return response.json()
204
- except Timeout:
205
- return {}
206
-
207
- ################
208
- # Objects
209
- ################
210
- def create(
211
- self,
212
- object: EDSLObject,
213
- description: Optional[str] = None,
214
- visibility: Optional[VisibilityType] = "unlisted",
215
- ) -> dict:
216
- """
217
- Create an EDSL object in the Coop server.
218
- """
219
- object_type = ObjectRegistry.get_object_type_by_edsl_class(object)
220
- response = self._send_server_request(
221
- uri=f"api/v0/object",
222
- method="POST",
223
- payload={
224
- "description": description,
225
- "json_string": json.dumps(
226
- object.to_dict(),
227
- default=self._json_handle_none,
228
- ),
229
- "object_type": object_type,
230
- "visibility": visibility,
231
- "version": self._edsl_version,
232
- },
233
- )
234
- self._resolve_server_response(response)
235
- response_json = response.json()
236
- return {
237
- "description": response_json.get("description"),
238
- "object_type": object_type,
239
- "url": f"{self.url}/content/{response_json.get('uuid')}",
240
- "uuid": response_json.get("uuid"),
241
- "version": self._edsl_version,
242
- "visibility": response_json.get("visibility"),
243
- }
244
-
245
- def get(
246
- self,
247
- uuid: Union[str, UUID] = None,
248
- url: str = None,
249
- expected_object_type: Optional[ObjectType] = None,
250
- ) -> EDSLObject:
251
- """
252
- Retrieve an EDSL object by its uuid or its url.
253
- - If the object's visibility is private, the user must be the owner.
254
- - Optionally, check if the retrieved object is of a certain type.
255
-
256
- :param uuid: the uuid of the object either in str or UUID format.
257
- :param url: the url of the object.
258
- :param expected_object_type: the expected type of the object.
259
-
260
- :return: the object instance.
261
- """
262
- uuid = self._resolve_uuid(uuid, url)
263
- response = self._send_server_request(
264
- uri=f"api/v0/object",
265
- method="GET",
266
- params={"uuid": uuid},
267
- )
268
- self._resolve_server_response(response)
269
- json_string = response.json().get("json_string")
270
- object_type = response.json().get("object_type")
271
- if expected_object_type and object_type != expected_object_type:
272
- raise Exception(f"Expected {expected_object_type=} but got {object_type=}")
273
- edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
274
- object = edsl_class.from_dict(json.loads(json_string))
275
- return object
276
-
277
- def get_all(self, object_type: ObjectType) -> list[dict[str, Any]]:
278
- """
279
- Retrieve all objects of a certain type associated with the user.
280
- """
281
- edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
282
- response = self._send_server_request(
283
- uri=f"api/v0/objects",
284
- method="GET",
285
- params={"type": object_type},
286
- )
287
- self._resolve_server_response(response)
288
- objects = [
289
- {
290
- "object": edsl_class.from_dict(json.loads(o.get("json_string"))),
291
- "uuid": o.get("uuid"),
292
- "version": o.get("version"),
293
- "description": o.get("description"),
294
- "visibility": o.get("visibility"),
295
- "url": f"{self.url}/content/{o.get('uuid')}",
296
- }
297
- for o in response.json()
298
- ]
299
- return objects
300
-
301
- def delete(self, uuid: Union[str, UUID] = None, url: str = None) -> dict:
302
- """
303
- Delete an object from the server.
304
- """
305
- uuid = self._resolve_uuid(uuid, url)
306
- response = self._send_server_request(
307
- uri=f"api/v0/object",
308
- method="DELETE",
309
- params={"uuid": uuid},
310
- )
311
- self._resolve_server_response(response)
312
- return response.json()
313
-
314
- def patch(
315
- self,
316
- uuid: Union[str, UUID] = None,
317
- url: str = None,
318
- description: Optional[str] = None,
319
- value: Optional[EDSLObject] = None,
320
- visibility: Optional[VisibilityType] = None,
321
- ) -> dict:
322
- """
323
- Change the attributes of an uploaded object
324
- - Only supports visibility for now
325
- """
326
- if description is None and visibility is None and value is None:
327
- raise Exception("Nothing to patch.")
328
- uuid = self._resolve_uuid(uuid, url)
329
- response = self._send_server_request(
330
- uri=f"api/v0/object",
331
- method="PATCH",
332
- params={"uuid": uuid},
333
- payload={
334
- "description": description,
335
- "json_string": (
336
- json.dumps(
337
- value.to_dict(),
338
- default=self._json_handle_none,
339
- )
340
- if value
341
- else None
342
- ),
343
- "visibility": visibility,
344
- },
345
- )
346
- self._resolve_server_response(response)
347
- return response.json()
348
-
349
- ################
350
- # Remote Cache
351
- ################
352
- def remote_cache_create(
353
- self,
354
- cache_entry: CacheEntry,
355
- visibility: VisibilityType = "private",
356
- description: Optional[str] = None,
357
- ) -> dict:
358
- """
359
- Create a single remote cache entry.
360
- If an entry with the same key already exists in the database, update it instead.
361
-
362
- :param cache_entry: The cache entry to send to the server.
363
- :param visibility: The visibility of the cache entry.
364
- :param optional description: A description for this entry in the remote cache.
365
-
366
- >>> entry = CacheEntry.example()
367
- >>> coop.remote_cache_create(cache_entry=entry)
368
- {'status': 'success', 'created_entry_count': 1, 'updated_entry_count': 0}
369
- """
370
- response = self._send_server_request(
371
- uri="api/v0/remote-cache",
372
- method="POST",
373
- payload={
374
- "json_string": json.dumps(cache_entry.to_dict()),
375
- "version": self._edsl_version,
376
- "visibility": visibility,
377
- "description": description,
378
- },
379
- )
380
- self._resolve_server_response(response)
381
- response_json = response.json()
382
- created_entry_count = response_json.get("created_entry_count", 0)
383
- if created_entry_count > 0:
384
- self.remote_cache_create_log(
385
- response,
386
- description="Upload new cache entries to server",
387
- cache_entry_count=created_entry_count,
388
- )
389
- return response.json()
390
-
391
- def remote_cache_create_many(
392
- self,
393
- cache_entries: list[CacheEntry],
394
- visibility: VisibilityType = "private",
395
- description: Optional[str] = None,
396
- ) -> dict:
397
- """
398
- Create many remote cache entries.
399
- If an entry with the same key already exists in the database, update it instead.
400
-
401
- :param cache_entries: The list of cache entries to send to the server.
402
- :param visibility: The visibility of the cache entries.
403
- :param optional description: A description for these entries in the remote cache.
404
-
405
- >>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
406
- >>> coop.remote_cache_create_many(cache_entries=entries)
407
- {'status': 'success', 'created_entry_count': 10, 'updated_entry_count': 0}
408
- """
409
- payload = [
410
- {
411
- "json_string": json.dumps(c.to_dict()),
412
- "version": self._edsl_version,
413
- "visibility": visibility,
414
- "description": description,
415
- }
416
- for c in cache_entries
417
- ]
418
- response = self._send_server_request(
419
- uri="api/v0/remote-cache/many",
420
- method="POST",
421
- payload=payload,
422
- )
423
- self._resolve_server_response(response)
424
- response_json = response.json()
425
- created_entry_count = response_json.get("created_entry_count", 0)
426
- if created_entry_count > 0:
427
- self.remote_cache_create_log(
428
- response,
429
- description="Upload new cache entries to server",
430
- cache_entry_count=created_entry_count,
431
- )
432
- return response.json()
433
-
434
- def remote_cache_get(
435
- self,
436
- exclude_keys: Optional[list[str]] = None,
437
- ) -> list[CacheEntry]:
438
- """
439
- Get all remote cache entries.
440
-
441
- :param optional exclude_keys: Exclude CacheEntry objects with these keys.
442
-
443
- >>> coop.remote_cache_get()
444
- [CacheEntry(...), CacheEntry(...), ...]
445
- """
446
- if exclude_keys is None:
447
- exclude_keys = []
448
- response = self._send_server_request(
449
- uri="api/v0/remote-cache/get-many",
450
- method="POST",
451
- payload={"keys": exclude_keys},
452
- )
453
- self._resolve_server_response(response)
454
- return [
455
- CacheEntry.from_dict(json.loads(v.get("json_string")))
456
- for v in response.json()
457
- ]
458
-
459
- def remote_cache_get_diff(
460
- self,
461
- client_cacheentry_keys: list[str],
462
- ) -> dict:
463
- """
464
- Get the difference between local and remote cache entries for a user.
465
- """
466
- response = self._send_server_request(
467
- uri="api/v0/remote-cache/get-diff",
468
- method="POST",
469
- payload={"keys": client_cacheentry_keys},
470
- )
471
- self._resolve_server_response(response)
472
- response_json = response.json()
473
- response_dict = {
474
- "client_missing_cacheentries": [
475
- CacheEntry.from_dict(json.loads(c.get("json_string")))
476
- for c in response_json.get("client_missing_cacheentries", [])
477
- ],
478
- "server_missing_cacheentry_keys": response_json.get(
479
- "server_missing_cacheentry_keys", []
480
- ),
481
- }
482
- downloaded_entry_count = len(response_dict["client_missing_cacheentries"])
483
- if downloaded_entry_count > 0:
484
- self.remote_cache_create_log(
485
- response,
486
- description="Download missing cache entries to client",
487
- cache_entry_count=downloaded_entry_count,
488
- )
489
- return response_dict
490
-
491
- def remote_cache_clear(self) -> dict:
492
- """
493
- Clear all remote cache entries.
494
-
495
- >>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
496
- >>> coop.remote_cache_create_many(cache_entries=entries)
497
- >>> coop.remote_cache_clear()
498
- {'status': 'success', 'deleted_entry_count': 10}
499
- """
500
- response = self._send_server_request(
501
- uri="api/v0/remote-cache/delete-all",
502
- method="DELETE",
503
- )
504
- self._resolve_server_response(response)
505
- response_json = response.json()
506
- deleted_entry_count = response_json.get("deleted_entry_count", 0)
507
- if deleted_entry_count > 0:
508
- self.remote_cache_create_log(
509
- response,
510
- description="Clear cache entries",
511
- cache_entry_count=deleted_entry_count,
512
- )
513
- return response.json()
514
-
515
- def remote_cache_create_log(
516
- self, response: requests.Response, description: str, cache_entry_count: int
517
- ) -> Union[dict, None]:
518
- """
519
- If a remote cache action has been completed successfully,
520
- log the action.
521
- """
522
- if 200 <= response.status_code < 300:
523
- log_response = self._send_server_request(
524
- uri="api/v0/remote-cache-log",
525
- method="POST",
526
- payload={
527
- "description": description,
528
- "cache_entry_count": cache_entry_count,
529
- },
530
- )
531
- self._resolve_server_response(log_response)
532
- return response.json()
533
-
534
- def remote_cache_clear_log(self) -> dict:
535
- """
536
- Clear all remote cache log entries.
537
-
538
- >>> coop.remote_cache_clear_log()
539
- {'status': 'success'}
540
- """
541
- response = self._send_server_request(
542
- uri="api/v0/remote-cache-log/delete-all",
543
- method="DELETE",
544
- )
545
- self._resolve_server_response(response)
546
- return response.json()
547
-
548
- ################
549
- # Remote Inference
550
- ################
551
- def remote_inference_create(
552
- self,
553
- job: Jobs,
554
- description: Optional[str] = None,
555
- status: RemoteJobStatus = "queued",
556
- visibility: Optional[VisibilityType] = "unlisted",
557
- initial_results_visibility: Optional[VisibilityType] = "unlisted",
558
- iterations: Optional[int] = 1,
559
- ) -> dict:
560
- """
561
- Send a remote inference job to the server.
562
-
563
- :param job: The EDSL job to send to the server.
564
- :param optional description: A description for this entry in the remote cache.
565
- :param status: The status of the job. Should be 'queued', unless you are debugging.
566
- :param visibility: The visibility of the cache entry.
567
- :param iterations: The number of times to run each interview.
568
-
569
- >>> job = Jobs.example()
570
- >>> coop.remote_inference_create(job=job, description="My job")
571
- {'uuid': '9f8484ee-b407-40e4-9652-4133a7236c9c', 'description': 'My job', 'status': 'queued', 'visibility': 'unlisted', 'version': '0.1.29.dev4'}
572
- """
573
- response = self._send_server_request(
574
- uri="api/v0/remote-inference",
575
- method="POST",
576
- payload={
577
- "json_string": json.dumps(
578
- job.to_dict(),
579
- default=self._json_handle_none,
580
- ),
581
- "description": description,
582
- "status": status,
583
- "iterations": iterations,
584
- "visibility": visibility,
585
- "version": self._edsl_version,
586
- "initial_results_visibility": initial_results_visibility,
587
- },
588
- )
589
- self._resolve_server_response(response)
590
- response_json = response.json()
591
- return {
592
- "uuid": response_json.get("job_uuid"),
593
- "description": response_json.get("description"),
594
- "status": response_json.get("status"),
595
- "iterations": response_json.get("iterations"),
596
- "visibility": response_json.get("visibility"),
597
- "version": self._edsl_version,
598
- }
599
-
600
- def remote_inference_get(
601
- self, job_uuid: Optional[str] = None, results_uuid: Optional[str] = None
602
- ) -> dict:
603
- """
604
- Get the details of a remote inference job.
605
- You can pass either the job uuid or the results uuid as a parameter.
606
- If you pass both, the job uuid will be prioritized.
607
-
608
- :param job_uuid: The UUID of the EDSL job.
609
- :param results_uuid: The UUID of the results associated with the EDSL job.
610
-
611
- >>> coop.remote_inference_get("9f8484ee-b407-40e4-9652-4133a7236c9c")
612
- {'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'}
613
- """
614
- if job_uuid is None and results_uuid is None:
615
- raise ValueError("Either job_uuid or results_uuid must be provided.")
616
- elif job_uuid is not None:
617
- params = {"job_uuid": job_uuid}
618
- else:
619
- params = {"results_uuid": results_uuid}
620
-
621
- response = self._send_server_request(
622
- uri="api/v0/remote-inference",
623
- method="GET",
624
- params=params,
625
- )
626
- self._resolve_server_response(response)
627
- data = response.json()
628
- return {
629
- "job_uuid": data.get("job_uuid"),
630
- "results_uuid": data.get("results_uuid"),
631
- "results_url": f"{self.url}/content/{data.get('results_uuid')}",
632
- "status": data.get("status"),
633
- "reason": data.get("reason"),
634
- "credits_consumed": data.get("price"),
635
- "version": data.get("version"),
636
- }
637
-
638
- def remote_inference_cost(
639
- self, input: Union[Jobs, Survey], iterations: int = 1
640
- ) -> int:
641
- """
642
- Get the cost of a remote inference job.
643
-
644
- :param input: The EDSL job to send to the server.
645
-
646
- >>> job = Jobs.example()
647
- >>> coop.remote_inference_cost(input=job)
648
- 16
649
- """
650
- if isinstance(input, Jobs):
651
- job = input
652
- elif isinstance(input, Survey):
653
- job = Jobs(survey=input)
654
- else:
655
- raise TypeError("Input must be either a Job or a Survey.")
656
-
657
- response = self._send_server_request(
658
- uri="api/v0/remote-inference/cost",
659
- method="POST",
660
- payload={
661
- "json_string": json.dumps(
662
- job.to_dict(),
663
- default=self._json_handle_none,
664
- ),
665
- "iterations": iterations,
666
- },
667
- )
668
- self._resolve_server_response(response)
669
- response_json = response.json()
670
- return {
671
- "credits": response_json.get("cost_in_credits"),
672
- "usd": response_json.get("cost_in_usd"),
673
- }
674
-
675
- ################
676
- # DUNDER METHODS
677
- ################
678
- def __repr__(self):
679
- """Return a string representation of the client."""
680
- return f"Client(api_key='{self.api_key}', url='{self.url}')"
681
-
682
- ################
683
- # EXPERIMENTAL
684
- ################
685
- async def remote_async_execute_model_call(
686
- self, model_dict: dict, user_prompt: str, system_prompt: str
687
- ) -> dict:
688
- url = self.url + "/inference/"
689
- # print("Now using url: ", url)
690
- data = {
691
- "model_dict": model_dict,
692
- "user_prompt": user_prompt,
693
- "system_prompt": system_prompt,
694
- }
695
- # Use aiohttp to send a POST request asynchronously
696
- async with aiohttp.ClientSession() as session:
697
- async with session.post(url, json=data) as response:
698
- response_data = await response.json()
699
- return response_data
700
-
701
- def web(
702
- self,
703
- survey: dict,
704
- platform: Literal[
705
- "google_forms", "lime_survey", "survey_monkey"
706
- ] = "lime_survey",
707
- email=None,
708
- ):
709
- url = f"{self.url}/api/v0/export_to_{platform}"
710
- if email:
711
- data = {"json_string": json.dumps({"survey": survey, "email": email})}
712
- else:
713
- data = {"json_string": json.dumps({"survey": survey, "email": ""})}
714
-
715
- response_json = requests.post(url, headers=self.headers, data=json.dumps(data))
716
-
717
- return response_json
718
-
719
- def fetch_prices(self) -> dict:
720
- """
721
- Fetch model prices from Coop. If the request fails, return an empty dict.
722
- """
723
-
724
- from edsl.coop.PriceFetcher import PriceFetcher
725
-
726
- from edsl.config import CONFIG
727
-
728
- if bool(CONFIG.get("EDSL_FETCH_TOKEN_PRICES")):
729
- price_fetcher = PriceFetcher()
730
- return price_fetcher.fetch_prices()
731
- else:
732
- return {}
733
-
734
- def fetch_models(self) -> dict:
735
- """
736
- Fetch a dict of available models from Coop.
737
-
738
- Each key in the dict is an inference service, and each value is a list of models from that service.
739
- """
740
- response = self._send_server_request(uri="api/v0/models", method="GET")
741
- self._resolve_server_response(response)
742
- data = response.json()
743
- return data
744
-
745
- def fetch_rate_limit_config_vars(self) -> dict:
746
- """
747
- Fetch a dict of rate limit config vars from Coop.
748
-
749
- The dict keys are RPM and TPM variables like EDSL_SERVICE_RPM_OPENAI.
750
- """
751
- response = self._send_server_request(
752
- uri="api/v0/config-vars",
753
- method="GET",
754
- )
755
- self._resolve_server_response(response)
756
- data = response.json()
757
- return data
758
-
759
- def _display_login_url(self, edsl_auth_token: str):
760
- """
761
- Uses rich.print to display a login URL.
762
-
763
- - We need this function because URL detection with print() does not work alongside animations in VSCode.
764
- """
765
- from rich import print as rich_print
766
-
767
- url = f"{CONFIG.EXPECTED_PARROT_URL}/login?edsl_auth_token={edsl_auth_token}"
768
-
769
- rich_print(f"[#38bdf8][link={url}]{url}[/link][/#38bdf8]")
770
-
771
- def _get_api_key(self, edsl_auth_token: str):
772
- """
773
- Given an EDSL auth token, find the corresponding user's API key.
774
- """
775
-
776
- response = self._send_server_request(
777
- uri="api/v0/get-api-key",
778
- method="POST",
779
- payload={
780
- "edsl_auth_token": edsl_auth_token,
781
- },
782
- )
783
- data = response.json()
784
- api_key = data.get("api_key")
785
- return api_key
786
-
787
- def login(self):
788
- """
789
- Starts the EDSL auth token login flow.
790
- """
791
- import secrets
792
- from dotenv import load_dotenv
793
- from edsl.utilities.utilities import write_api_key_to_env
794
-
795
- edsl_auth_token = secrets.token_urlsafe(16)
796
-
797
- print(
798
- "\nUse the link below to log in to Expected Parrot so we can automatically update your API key."
799
- )
800
- self._display_login_url(edsl_auth_token=edsl_auth_token)
801
- api_key = self._poll_for_api_key(edsl_auth_token)
802
-
803
- if api_key is None:
804
- raise Exception("Timed out waiting for login. Please try again.")
805
-
806
- write_api_key_to_env(api_key)
807
- print("\n✨ API key retrieved and written to .env file.")
808
-
809
- # Add API key to environment
810
- load_dotenv()
811
-
812
-
813
- if __name__ == "__main__":
814
- sheet_data = fetch_sheet_data()
815
- if sheet_data:
816
- print(f"Successfully fetched {len(sheet_data)} rows of data.")
817
- print("First row:", sheet_data[0])
818
- else:
819
- print("Failed to fetch sheet data.")
820
-
821
-
822
- def main():
823
- """
824
- A simple example for the coop client
825
- """
826
- from uuid import uuid4
827
- from edsl import (
828
- Agent,
829
- AgentList,
830
- Cache,
831
- Notebook,
832
- QuestionFreeText,
833
- QuestionMultipleChoice,
834
- Results,
835
- Scenario,
836
- ScenarioList,
837
- Survey,
838
- )
839
- from edsl.coop import Coop
840
- from edsl.data.CacheEntry import CacheEntry
841
- from edsl.jobs import Jobs
842
-
843
- # init & basics
844
- API_KEY = "b"
845
- coop = Coop(api_key=API_KEY)
846
- coop
847
- coop.edsl_settings
848
-
849
- ##############
850
- # A. A simple example
851
- ##############
852
- # .. create and manipulate an object through the Coop client
853
- response = coop.create(QuestionMultipleChoice.example())
854
- coop.get(uuid=response.get("uuid"))
855
- coop.get(uuid=response.get("uuid"), expected_object_type="question")
856
- coop.get(url=response.get("url"))
857
- coop.create(QuestionMultipleChoice.example())
858
- coop.get_all("question")
859
- coop.patch(uuid=response.get("uuid"), visibility="private")
860
- coop.patch(uuid=response.get("uuid"), description="hey")
861
- coop.patch(uuid=response.get("uuid"), value=QuestionFreeText.example())
862
- # coop.patch(uuid=response.get("uuid"), value=Survey.example()) - should throw error
863
- coop.get(uuid=response.get("uuid"))
864
- coop.delete(uuid=response.get("uuid"))
865
-
866
- # .. create and manipulate an object through the class
867
- response = QuestionMultipleChoice.example().push()
868
- QuestionMultipleChoice.pull(uuid=response.get("uuid"))
869
- QuestionMultipleChoice.pull(url=response.get("url"))
870
- QuestionMultipleChoice.patch(uuid=response.get("uuid"), visibility="private")
871
- QuestionMultipleChoice.patch(uuid=response.get("uuid"), description="hey")
872
- QuestionMultipleChoice.patch(
873
- uuid=response.get("uuid"), value=QuestionFreeText.example()
874
- )
875
- QuestionMultipleChoice.pull(response.get("uuid"))
876
- QuestionMultipleChoice.delete(response.get("uuid"))
877
-
878
- ##############
879
- # B. Examples with all objects
880
- ##############
881
- OBJECTS = [
882
- ("agent", Agent),
883
- ("agent_list", AgentList),
884
- ("cache", Cache),
885
- ("notebook", Notebook),
886
- ("question", QuestionMultipleChoice),
887
- ("results", Results),
888
- ("scenario", Scenario),
889
- ("scenario_list", ScenarioList),
890
- ("survey", Survey),
891
- ]
892
- for object_type, cls in OBJECTS:
893
- print(f"Testing {object_type} objects")
894
- # 1. Delete existing objects
895
- existing_objects = coop.get_all(object_type)
896
- for item in existing_objects:
897
- coop.delete(uuid=item.get("uuid"))
898
- # 2. Create new objects
899
- example = cls.example()
900
- response_1 = coop.create(example)
901
- response_2 = coop.create(cls.example(), visibility="private")
902
- response_3 = coop.create(cls.example(), visibility="public")
903
- response_4 = coop.create(
904
- cls.example(), visibility="unlisted", description="hey"
905
- )
906
- # 3. Retrieve all objects
907
- objects = coop.get_all(object_type)
908
- assert len(objects) == 4
909
- # 4. Try to retrieve an item that does not exist
910
- try:
911
- coop.get(uuid=uuid4())
912
- except Exception as e:
913
- print(e)
914
- # 5. Try to retrieve all test objects by their uuids
915
- for response in [response_1, response_2, response_3, response_4]:
916
- coop.get(uuid=response.get("uuid"))
917
- # 6. Change visibility of all objects
918
- for item in objects:
919
- coop.patch(uuid=item.get("uuid"), visibility="private")
920
- # 6. Change description of all objects
921
- for item in objects:
922
- coop.patch(uuid=item.get("uuid"), description="hey")
923
- # 7. Delete all objects
924
- for item in objects:
925
- coop.delete(uuid=item.get("uuid"))
926
- assert len(coop.get_all(object_type)) == 0
927
-
928
- ##############
929
- # C. Remote Cache
930
- ##############
931
- # clear
932
- coop.remote_cache_clear()
933
- assert coop.remote_cache_get() == []
934
- # create one remote cache entry
935
- cache_entry = CacheEntry.example()
936
- cache_entry.to_dict()
937
- coop.remote_cache_create(cache_entry)
938
- # create many remote cache entries
939
- cache_entries = [CacheEntry.example(randomize=True) for _ in range(10)]
940
- coop.remote_cache_create_many(cache_entries)
941
- # get all remote cache entries
942
- coop.remote_cache_get()
943
- coop.remote_cache_get(exclude_keys=[])
944
- coop.remote_cache_get(exclude_keys=["a"])
945
- exclude_keys = [cache_entry.key for cache_entry in cache_entries]
946
- coop.remote_cache_get(exclude_keys)
947
- # clear
948
- coop.remote_cache_clear()
949
- coop.remote_cache_get()
950
-
951
- ##############
952
- # D. Remote Inference
953
- ##############
954
- job = Jobs.example()
955
- coop.remote_inference_cost(job)
956
- job_coop_object = coop.remote_inference_create(job)
957
- job_coop_results = coop.remote_inference_get(job_coop_object.get("uuid"))
958
- 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
+
32
+ self.url = url or CONFIG.EXPECTED_PARROT_URL
33
+ if self.url.endswith("/"):
34
+ self.url = self.url[:-1]
35
+ if "chick.expectedparrot" in self.url:
36
+ self.api_url = "https://chickapi.expectedparrot.com"
37
+ elif "expectedparrot" in self.url:
38
+ self.api_url = "https://api.expectedparrot.com"
39
+ elif "localhost:1234" in self.url:
40
+ self.api_url = "http://localhost:8000"
41
+ else:
42
+ self.api_url = self.url
43
+ self._edsl_version = edsl.__version__
44
+
45
+ ################
46
+ # BASIC METHODS
47
+ ################
48
+ @property
49
+ def headers(self) -> dict:
50
+ """
51
+ Return the headers for the request.
52
+ """
53
+ headers = {}
54
+ if self.api_key:
55
+ headers["Authorization"] = f"Bearer {self.api_key}"
56
+ else:
57
+ headers["Authorization"] = f"Bearer None"
58
+ return headers
59
+
60
+ def _send_server_request(
61
+ self,
62
+ uri: str,
63
+ method: str,
64
+ payload: Optional[dict[str, Any]] = None,
65
+ params: Optional[dict[str, Any]] = None,
66
+ timeout: Optional[float] = 5,
67
+ ) -> requests.Response:
68
+ """
69
+ Send a request to the server and return the response.
70
+ """
71
+ url = f"{self.api_url}/{uri}"
72
+ method = method.upper()
73
+ if payload is None:
74
+ timeout = 20
75
+ elif (
76
+ method.upper() == "POST"
77
+ and "json_string" in payload
78
+ and payload.get("json_string") is not None
79
+ ):
80
+ timeout = max(20, (len(payload.get("json_string", "")) // (1024 * 1024)))
81
+ try:
82
+ if method in ["GET", "DELETE"]:
83
+ response = requests.request(
84
+ method, url, params=params, headers=self.headers, timeout=timeout
85
+ )
86
+ elif method in ["POST", "PATCH"]:
87
+ response = requests.request(
88
+ method,
89
+ url,
90
+ params=params,
91
+ json=payload,
92
+ headers=self.headers,
93
+ timeout=timeout,
94
+ )
95
+ else:
96
+ raise Exception(f"Invalid {method=}.")
97
+ except requests.ConnectionError:
98
+ raise requests.ConnectionError(f"Could not connect to the server at {url}.")
99
+
100
+ return response
101
+
102
+ def _resolve_server_response(
103
+ self, response: requests.Response, check_api_key: bool = True
104
+ ) -> None:
105
+ """
106
+ Check the response from the server and raise errors as appropriate.
107
+ """
108
+ if response.status_code >= 400:
109
+ message = response.json().get("detail")
110
+ # print(response.text)
111
+ if "The API key you provided is invalid" in message and check_api_key:
112
+ import secrets
113
+ from edsl.utilities.utilities import write_api_key_to_env
114
+
115
+ edsl_auth_token = secrets.token_urlsafe(16)
116
+
117
+ print("Your Expected Parrot API key is invalid.")
118
+ print(
119
+ "\nUse the link below to log in to Expected Parrot so we can automatically update your API key."
120
+ )
121
+ self._display_login_url(edsl_auth_token=edsl_auth_token)
122
+ api_key = self._poll_for_api_key(edsl_auth_token)
123
+
124
+ if api_key is None:
125
+ print("\nTimed out waiting for login. Please try again.")
126
+ return
127
+
128
+ write_api_key_to_env(api_key)
129
+ print("\n✨ API key retrieved and written to .env file.")
130
+ print("Rerun your code to try again with a valid API key.")
131
+ return
132
+
133
+ elif "Authorization" in message:
134
+ print(message)
135
+ message = "Please provide an Expected Parrot API key."
136
+
137
+ raise CoopServerResponseError(message)
138
+
139
+ def _poll_for_api_key(
140
+ self, edsl_auth_token: str, timeout: int = 120
141
+ ) -> Union[str, None]:
142
+ """
143
+ Allows the user to retrieve their Expected Parrot API key by logging in with an EDSL auth token.
144
+
145
+ :param edsl_auth_token: The EDSL auth token to use for login
146
+ :param timeout: Maximum time to wait for login, in seconds (default: 120)
147
+ """
148
+ import time
149
+ from datetime import datetime
150
+
151
+ start_poll_time = time.time()
152
+ waiting_for_login = True
153
+ while waiting_for_login:
154
+ elapsed_time = time.time() - start_poll_time
155
+ if elapsed_time > timeout:
156
+ # Timed out waiting for the user to log in
157
+ print("\r" + " " * 80 + "\r", end="")
158
+ return None
159
+
160
+ api_key = self._get_api_key(edsl_auth_token)
161
+ if api_key is not None:
162
+ print("\r" + " " * 80 + "\r", end="")
163
+ return api_key
164
+ else:
165
+ duration = 5
166
+ time_checked = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
167
+ frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
168
+ start_time = time.time()
169
+ i = 0
170
+ while time.time() - start_time < duration:
171
+ print(
172
+ f"\r{frames[i % len(frames)]} Waiting for login. Last checked: {time_checked}",
173
+ end="",
174
+ flush=True,
175
+ )
176
+ time.sleep(0.1)
177
+ i += 1
178
+
179
+ def _json_handle_none(self, value: Any) -> Any:
180
+ """
181
+ Handle None values during JSON serialization.
182
+ - Return "null" if the value is None. Otherwise, don't return anything.
183
+ """
184
+ if value is None:
185
+ return "null"
186
+
187
+ def _resolve_uuid(
188
+ self, uuid: Union[str, UUID] = None, url: str = None
189
+ ) -> Union[str, UUID]:
190
+ """
191
+ Resolve the uuid from a uuid or a url.
192
+ """
193
+ if not url and not uuid:
194
+ raise CoopNoUUIDError("No uuid or url provided for the object.")
195
+ if not uuid and url:
196
+ uuid = url.split("/")[-1]
197
+ return uuid
198
+
199
+ @property
200
+ def edsl_settings(self) -> dict:
201
+ """
202
+ Retrieve and return the EDSL settings stored on Coop.
203
+ If no response is received within 5 seconds, return an empty dict.
204
+ """
205
+ from requests.exceptions import Timeout
206
+
207
+ try:
208
+ response = self._send_server_request(
209
+ uri="api/v0/edsl-settings", method="GET", timeout=5
210
+ )
211
+ self._resolve_server_response(response, check_api_key=False)
212
+ return response.json()
213
+ except Timeout:
214
+ return {}
215
+
216
+ ################
217
+ # Objects
218
+ ################
219
+ def create(
220
+ self,
221
+ object: EDSLObject,
222
+ description: Optional[str] = None,
223
+ visibility: Optional[VisibilityType] = "unlisted",
224
+ ) -> dict:
225
+ """
226
+ Create an EDSL object in the Coop server.
227
+ """
228
+ object_type = ObjectRegistry.get_object_type_by_edsl_class(object)
229
+ response = self._send_server_request(
230
+ uri=f"api/v0/object",
231
+ method="POST",
232
+ payload={
233
+ "description": description,
234
+ "json_string": json.dumps(
235
+ object.to_dict(),
236
+ default=self._json_handle_none,
237
+ ),
238
+ "object_type": object_type,
239
+ "visibility": visibility,
240
+ "version": self._edsl_version,
241
+ },
242
+ )
243
+ self._resolve_server_response(response)
244
+ response_json = response.json()
245
+ return {
246
+ "description": response_json.get("description"),
247
+ "object_type": object_type,
248
+ "url": f"{self.url}/content/{response_json.get('uuid')}",
249
+ "uuid": response_json.get("uuid"),
250
+ "version": self._edsl_version,
251
+ "visibility": response_json.get("visibility"),
252
+ }
253
+
254
+ def get(
255
+ self,
256
+ uuid: Union[str, UUID] = None,
257
+ url: str = None,
258
+ expected_object_type: Optional[ObjectType] = None,
259
+ ) -> EDSLObject:
260
+ """
261
+ Retrieve an EDSL object by its uuid or its url.
262
+ - If the object's visibility is private, the user must be the owner.
263
+ - Optionally, check if the retrieved object is of a certain type.
264
+
265
+ :param uuid: the uuid of the object either in str or UUID format.
266
+ :param url: the url of the object.
267
+ :param expected_object_type: the expected type of the object.
268
+
269
+ :return: the object instance.
270
+ """
271
+ uuid = self._resolve_uuid(uuid, url)
272
+ response = self._send_server_request(
273
+ uri=f"api/v0/object",
274
+ method="GET",
275
+ params={"uuid": uuid},
276
+ )
277
+ self._resolve_server_response(response)
278
+ json_string = response.json().get("json_string")
279
+ object_type = response.json().get("object_type")
280
+ if expected_object_type and object_type != expected_object_type:
281
+ raise Exception(f"Expected {expected_object_type=} but got {object_type=}")
282
+ edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
283
+ object = edsl_class.from_dict(json.loads(json_string))
284
+ return object
285
+
286
+ def get_all(self, object_type: ObjectType) -> list[dict[str, Any]]:
287
+ """
288
+ Retrieve all objects of a certain type associated with the user.
289
+ """
290
+ edsl_class = ObjectRegistry.object_type_to_edsl_class.get(object_type)
291
+ response = self._send_server_request(
292
+ uri=f"api/v0/objects",
293
+ method="GET",
294
+ params={"type": object_type},
295
+ )
296
+ self._resolve_server_response(response)
297
+ objects = [
298
+ {
299
+ "object": edsl_class.from_dict(json.loads(o.get("json_string"))),
300
+ "uuid": o.get("uuid"),
301
+ "version": o.get("version"),
302
+ "description": o.get("description"),
303
+ "visibility": o.get("visibility"),
304
+ "url": f"{self.url}/content/{o.get('uuid')}",
305
+ }
306
+ for o in response.json()
307
+ ]
308
+ return objects
309
+
310
+ def delete(self, uuid: Union[str, UUID] = None, url: str = None) -> dict:
311
+ """
312
+ Delete an object from the server.
313
+ """
314
+ uuid = self._resolve_uuid(uuid, url)
315
+ response = self._send_server_request(
316
+ uri=f"api/v0/object",
317
+ method="DELETE",
318
+ params={"uuid": uuid},
319
+ )
320
+ self._resolve_server_response(response)
321
+ return response.json()
322
+
323
+ def patch(
324
+ self,
325
+ uuid: Union[str, UUID] = None,
326
+ url: str = None,
327
+ description: Optional[str] = None,
328
+ value: Optional[EDSLObject] = None,
329
+ visibility: Optional[VisibilityType] = None,
330
+ ) -> dict:
331
+ """
332
+ Change the attributes of an uploaded object
333
+ - Only supports visibility for now
334
+ """
335
+ if description is None and visibility is None and value is None:
336
+ raise Exception("Nothing to patch.")
337
+ uuid = self._resolve_uuid(uuid, url)
338
+ response = self._send_server_request(
339
+ uri=f"api/v0/object",
340
+ method="PATCH",
341
+ params={"uuid": uuid},
342
+ payload={
343
+ "description": description,
344
+ "json_string": (
345
+ json.dumps(
346
+ value.to_dict(),
347
+ default=self._json_handle_none,
348
+ )
349
+ if value
350
+ else None
351
+ ),
352
+ "visibility": visibility,
353
+ },
354
+ )
355
+ self._resolve_server_response(response)
356
+ return response.json()
357
+
358
+ ################
359
+ # Remote Cache
360
+ ################
361
+ def remote_cache_create(
362
+ self,
363
+ cache_entry: CacheEntry,
364
+ visibility: VisibilityType = "private",
365
+ description: Optional[str] = None,
366
+ ) -> dict:
367
+ """
368
+ Create a single remote cache entry.
369
+ If an entry with the same key already exists in the database, update it instead.
370
+
371
+ :param cache_entry: The cache entry to send to the server.
372
+ :param visibility: The visibility of the cache entry.
373
+ :param optional description: A description for this entry in the remote cache.
374
+
375
+ >>> entry = CacheEntry.example()
376
+ >>> coop.remote_cache_create(cache_entry=entry)
377
+ {'status': 'success', 'created_entry_count': 1, 'updated_entry_count': 0}
378
+ """
379
+ response = self._send_server_request(
380
+ uri="api/v0/remote-cache",
381
+ method="POST",
382
+ payload={
383
+ "json_string": json.dumps(cache_entry.to_dict()),
384
+ "version": self._edsl_version,
385
+ "visibility": visibility,
386
+ "description": description,
387
+ },
388
+ )
389
+ self._resolve_server_response(response)
390
+ response_json = response.json()
391
+ created_entry_count = response_json.get("created_entry_count", 0)
392
+ if created_entry_count > 0:
393
+ self.remote_cache_create_log(
394
+ response,
395
+ description="Upload new cache entries to server",
396
+ cache_entry_count=created_entry_count,
397
+ )
398
+ return response.json()
399
+
400
+ def remote_cache_create_many(
401
+ self,
402
+ cache_entries: list[CacheEntry],
403
+ visibility: VisibilityType = "private",
404
+ description: Optional[str] = None,
405
+ ) -> dict:
406
+ """
407
+ Create many remote cache entries.
408
+ If an entry with the same key already exists in the database, update it instead.
409
+
410
+ :param cache_entries: The list of cache entries to send to the server.
411
+ :param visibility: The visibility of the cache entries.
412
+ :param optional description: A description for these entries in the remote cache.
413
+
414
+ >>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
415
+ >>> coop.remote_cache_create_many(cache_entries=entries)
416
+ {'status': 'success', 'created_entry_count': 10, 'updated_entry_count': 0}
417
+ """
418
+ payload = [
419
+ {
420
+ "json_string": json.dumps(c.to_dict()),
421
+ "version": self._edsl_version,
422
+ "visibility": visibility,
423
+ "description": description,
424
+ }
425
+ for c in cache_entries
426
+ ]
427
+ response = self._send_server_request(
428
+ uri="api/v0/remote-cache/many",
429
+ method="POST",
430
+ payload=payload,
431
+ )
432
+ self._resolve_server_response(response)
433
+ response_json = response.json()
434
+ created_entry_count = response_json.get("created_entry_count", 0)
435
+ if created_entry_count > 0:
436
+ self.remote_cache_create_log(
437
+ response,
438
+ description="Upload new cache entries to server",
439
+ cache_entry_count=created_entry_count,
440
+ )
441
+ return response.json()
442
+
443
+ def remote_cache_get(
444
+ self,
445
+ exclude_keys: Optional[list[str]] = None,
446
+ ) -> list[CacheEntry]:
447
+ """
448
+ Get all remote cache entries.
449
+
450
+ :param optional exclude_keys: Exclude CacheEntry objects with these keys.
451
+
452
+ >>> coop.remote_cache_get()
453
+ [CacheEntry(...), CacheEntry(...), ...]
454
+ """
455
+ if exclude_keys is None:
456
+ exclude_keys = []
457
+ response = self._send_server_request(
458
+ uri="api/v0/remote-cache/get-many",
459
+ method="POST",
460
+ payload={"keys": exclude_keys},
461
+ )
462
+ self._resolve_server_response(response)
463
+ return [
464
+ CacheEntry.from_dict(json.loads(v.get("json_string")))
465
+ for v in response.json()
466
+ ]
467
+
468
+ def remote_cache_get_diff(
469
+ self,
470
+ client_cacheentry_keys: list[str],
471
+ ) -> dict:
472
+ """
473
+ Get the difference between local and remote cache entries for a user.
474
+ """
475
+ response = self._send_server_request(
476
+ uri="api/v0/remote-cache/get-diff",
477
+ method="POST",
478
+ payload={"keys": client_cacheentry_keys},
479
+ )
480
+ self._resolve_server_response(response)
481
+ response_json = response.json()
482
+ response_dict = {
483
+ "client_missing_cacheentries": [
484
+ CacheEntry.from_dict(json.loads(c.get("json_string")))
485
+ for c in response_json.get("client_missing_cacheentries", [])
486
+ ],
487
+ "server_missing_cacheentry_keys": response_json.get(
488
+ "server_missing_cacheentry_keys", []
489
+ ),
490
+ }
491
+ downloaded_entry_count = len(response_dict["client_missing_cacheentries"])
492
+ if downloaded_entry_count > 0:
493
+ self.remote_cache_create_log(
494
+ response,
495
+ description="Download missing cache entries to client",
496
+ cache_entry_count=downloaded_entry_count,
497
+ )
498
+ return response_dict
499
+
500
+ def remote_cache_clear(self) -> dict:
501
+ """
502
+ Clear all remote cache entries.
503
+
504
+ >>> entries = [CacheEntry.example(randomize=True) for _ in range(10)]
505
+ >>> coop.remote_cache_create_many(cache_entries=entries)
506
+ >>> coop.remote_cache_clear()
507
+ {'status': 'success', 'deleted_entry_count': 10}
508
+ """
509
+ response = self._send_server_request(
510
+ uri="api/v0/remote-cache/delete-all",
511
+ method="DELETE",
512
+ )
513
+ self._resolve_server_response(response)
514
+ response_json = response.json()
515
+ deleted_entry_count = response_json.get("deleted_entry_count", 0)
516
+ if deleted_entry_count > 0:
517
+ self.remote_cache_create_log(
518
+ response,
519
+ description="Clear cache entries",
520
+ cache_entry_count=deleted_entry_count,
521
+ )
522
+ return response.json()
523
+
524
+ def remote_cache_create_log(
525
+ self, response: requests.Response, description: str, cache_entry_count: int
526
+ ) -> Union[dict, None]:
527
+ """
528
+ If a remote cache action has been completed successfully,
529
+ log the action.
530
+ """
531
+ if 200 <= response.status_code < 300:
532
+ log_response = self._send_server_request(
533
+ uri="api/v0/remote-cache-log",
534
+ method="POST",
535
+ payload={
536
+ "description": description,
537
+ "cache_entry_count": cache_entry_count,
538
+ },
539
+ )
540
+ self._resolve_server_response(log_response)
541
+ return response.json()
542
+
543
+ def remote_cache_clear_log(self) -> dict:
544
+ """
545
+ Clear all remote cache log entries.
546
+
547
+ >>> coop.remote_cache_clear_log()
548
+ {'status': 'success'}
549
+ """
550
+ response = self._send_server_request(
551
+ uri="api/v0/remote-cache-log/delete-all",
552
+ method="DELETE",
553
+ )
554
+ self._resolve_server_response(response)
555
+ return response.json()
556
+
557
+ ################
558
+ # Remote Inference
559
+ ################
560
+ def remote_inference_create(
561
+ self,
562
+ job: Jobs,
563
+ description: Optional[str] = None,
564
+ status: RemoteJobStatus = "queued",
565
+ visibility: Optional[VisibilityType] = "unlisted",
566
+ initial_results_visibility: Optional[VisibilityType] = "unlisted",
567
+ iterations: Optional[int] = 1,
568
+ ) -> dict:
569
+ """
570
+ Send a remote inference job to the server.
571
+
572
+ :param job: The EDSL job to send to the server.
573
+ :param optional description: A description for this entry in the remote cache.
574
+ :param status: The status of the job. Should be 'queued', unless you are debugging.
575
+ :param visibility: The visibility of the cache entry.
576
+ :param iterations: The number of times to run each interview.
577
+
578
+ >>> job = Jobs.example()
579
+ >>> coop.remote_inference_create(job=job, description="My job")
580
+ {'uuid': '9f8484ee-b407-40e4-9652-4133a7236c9c', 'description': 'My job', 'status': 'queued', 'visibility': 'unlisted', 'version': '0.1.29.dev4'}
581
+ """
582
+ response = self._send_server_request(
583
+ uri="api/v0/remote-inference",
584
+ method="POST",
585
+ payload={
586
+ "json_string": json.dumps(
587
+ job.to_dict(),
588
+ default=self._json_handle_none,
589
+ ),
590
+ "description": description,
591
+ "status": status,
592
+ "iterations": iterations,
593
+ "visibility": visibility,
594
+ "version": self._edsl_version,
595
+ "initial_results_visibility": initial_results_visibility,
596
+ },
597
+ )
598
+ self._resolve_server_response(response)
599
+ response_json = response.json()
600
+ return {
601
+ "uuid": response_json.get("job_uuid"),
602
+ "description": response_json.get("description"),
603
+ "status": response_json.get("status"),
604
+ "iterations": response_json.get("iterations"),
605
+ "visibility": response_json.get("visibility"),
606
+ "version": self._edsl_version,
607
+ }
608
+
609
+ def remote_inference_get(
610
+ self, job_uuid: Optional[str] = None, results_uuid: Optional[str] = None
611
+ ) -> dict:
612
+ """
613
+ Get the details of a remote inference job.
614
+ You can pass either the job uuid or the results uuid as a parameter.
615
+ If you pass both, the job uuid will be prioritized.
616
+
617
+ :param job_uuid: The UUID of the EDSL job.
618
+ :param results_uuid: The UUID of the results associated with the EDSL job.
619
+
620
+ >>> coop.remote_inference_get("9f8484ee-b407-40e4-9652-4133a7236c9c")
621
+ {'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'}
622
+ """
623
+ if job_uuid is None and results_uuid is None:
624
+ raise ValueError("Either job_uuid or results_uuid must be provided.")
625
+ elif job_uuid is not None:
626
+ params = {"job_uuid": job_uuid}
627
+ else:
628
+ params = {"results_uuid": results_uuid}
629
+
630
+ response = self._send_server_request(
631
+ uri="api/v0/remote-inference",
632
+ method="GET",
633
+ params=params,
634
+ )
635
+ self._resolve_server_response(response)
636
+ data = response.json()
637
+ return {
638
+ "job_uuid": data.get("job_uuid"),
639
+ "results_uuid": data.get("results_uuid"),
640
+ "results_url": f"{self.url}/content/{data.get('results_uuid')}",
641
+ "status": data.get("status"),
642
+ "reason": data.get("reason"),
643
+ "credits_consumed": data.get("price"),
644
+ "version": data.get("version"),
645
+ }
646
+
647
+ def remote_inference_cost(
648
+ self, input: Union[Jobs, Survey], iterations: int = 1
649
+ ) -> int:
650
+ """
651
+ Get the cost of a remote inference job.
652
+
653
+ :param input: The EDSL job to send to the server.
654
+
655
+ >>> job = Jobs.example()
656
+ >>> coop.remote_inference_cost(input=job)
657
+ 16
658
+ """
659
+ if isinstance(input, Jobs):
660
+ job = input
661
+ elif isinstance(input, Survey):
662
+ job = Jobs(survey=input)
663
+ else:
664
+ raise TypeError("Input must be either a Job or a Survey.")
665
+
666
+ response = self._send_server_request(
667
+ uri="api/v0/remote-inference/cost",
668
+ method="POST",
669
+ payload={
670
+ "json_string": json.dumps(
671
+ job.to_dict(),
672
+ default=self._json_handle_none,
673
+ ),
674
+ "iterations": iterations,
675
+ },
676
+ )
677
+ self._resolve_server_response(response)
678
+ response_json = response.json()
679
+ return {
680
+ "credits": response_json.get("cost_in_credits"),
681
+ "usd": response_json.get("cost_in_usd"),
682
+ }
683
+
684
+ ################
685
+ # DUNDER METHODS
686
+ ################
687
+ def __repr__(self):
688
+ """Return a string representation of the client."""
689
+ return f"Client(api_key='{self.api_key}', url='{self.url}')"
690
+
691
+ ################
692
+ # EXPERIMENTAL
693
+ ################
694
+ async def remote_async_execute_model_call(
695
+ self, model_dict: dict, user_prompt: str, system_prompt: str
696
+ ) -> dict:
697
+ url = self.api_url + "/inference/"
698
+ # print("Now using url: ", url)
699
+ data = {
700
+ "model_dict": model_dict,
701
+ "user_prompt": user_prompt,
702
+ "system_prompt": system_prompt,
703
+ }
704
+ # Use aiohttp to send a POST request asynchronously
705
+ async with aiohttp.ClientSession() as session:
706
+ async with session.post(url, json=data) as response:
707
+ response_data = await response.json()
708
+ return response_data
709
+
710
+ def web(
711
+ self,
712
+ survey: dict,
713
+ platform: Literal[
714
+ "google_forms", "lime_survey", "survey_monkey"
715
+ ] = "lime_survey",
716
+ email=None,
717
+ ):
718
+ url = f"{self.api_url}/api/v0/export_to_{platform}"
719
+ if email:
720
+ data = {"json_string": json.dumps({"survey": survey, "email": email})}
721
+ else:
722
+ data = {"json_string": json.dumps({"survey": survey, "email": ""})}
723
+
724
+ response_json = requests.post(url, headers=self.headers, data=json.dumps(data))
725
+
726
+ return response_json
727
+
728
+ def fetch_prices(self) -> dict:
729
+ """
730
+ Fetch model prices from Coop. If the request fails, return an empty dict.
731
+ """
732
+
733
+ from edsl.coop.PriceFetcher import PriceFetcher
734
+
735
+ from edsl.config import CONFIG
736
+
737
+ if bool(CONFIG.get("EDSL_FETCH_TOKEN_PRICES")):
738
+ price_fetcher = PriceFetcher()
739
+ return price_fetcher.fetch_prices()
740
+ else:
741
+ return {}
742
+
743
+ def fetch_models(self) -> dict:
744
+ """
745
+ Fetch a dict of available models from Coop.
746
+
747
+ Each key in the dict is an inference service, and each value is a list of models from that service.
748
+ """
749
+ response = self._send_server_request(uri="api/v0/models", method="GET")
750
+ self._resolve_server_response(response)
751
+ data = response.json()
752
+ return data
753
+
754
+ def fetch_rate_limit_config_vars(self) -> dict:
755
+ """
756
+ Fetch a dict of rate limit config vars from Coop.
757
+
758
+ The dict keys are RPM and TPM variables like EDSL_SERVICE_RPM_OPENAI.
759
+ """
760
+ response = self._send_server_request(
761
+ uri="api/v0/config-vars",
762
+ method="GET",
763
+ )
764
+ self._resolve_server_response(response)
765
+ data = response.json()
766
+ return data
767
+
768
+ def _display_login_url(self, edsl_auth_token: str):
769
+ """
770
+ Uses rich.print to display a login URL.
771
+
772
+ - We need this function because URL detection with print() does not work alongside animations in VSCode.
773
+ """
774
+ from rich import print as rich_print
775
+
776
+ url = f"{CONFIG.EXPECTED_PARROT_URL}/login?edsl_auth_token={edsl_auth_token}"
777
+
778
+ rich_print(f"[#38bdf8][link={url}]{url}[/link][/#38bdf8]")
779
+
780
+ def _get_api_key(self, edsl_auth_token: str):
781
+ """
782
+ Given an EDSL auth token, find the corresponding user's API key.
783
+ """
784
+
785
+ response = self._send_server_request(
786
+ uri="api/v0/get-api-key",
787
+ method="POST",
788
+ payload={
789
+ "edsl_auth_token": edsl_auth_token,
790
+ },
791
+ )
792
+ data = response.json()
793
+ api_key = data.get("api_key")
794
+ return api_key
795
+
796
+ def login(self):
797
+ """
798
+ Starts the EDSL auth token login flow.
799
+ """
800
+ import secrets
801
+ from dotenv import load_dotenv
802
+ from edsl.utilities.utilities import write_api_key_to_env
803
+
804
+ edsl_auth_token = secrets.token_urlsafe(16)
805
+
806
+ print(
807
+ "\nUse the link below to log in to Expected Parrot so we can automatically update your API key."
808
+ )
809
+ self._display_login_url(edsl_auth_token=edsl_auth_token)
810
+ api_key = self._poll_for_api_key(edsl_auth_token)
811
+
812
+ if api_key is None:
813
+ raise Exception("Timed out waiting for login. Please try again.")
814
+
815
+ write_api_key_to_env(api_key)
816
+ print("\n✨ API key retrieved and written to .env file.")
817
+
818
+ # Add API key to environment
819
+ load_dotenv()
820
+
821
+
822
+ def main():
823
+ """
824
+ A simple example for the coop client
825
+ """
826
+ from uuid import uuid4
827
+ from edsl import (
828
+ Agent,
829
+ AgentList,
830
+ Cache,
831
+ Notebook,
832
+ QuestionFreeText,
833
+ QuestionMultipleChoice,
834
+ Results,
835
+ Scenario,
836
+ ScenarioList,
837
+ Survey,
838
+ )
839
+ from edsl.coop import Coop
840
+ from edsl.data.CacheEntry import CacheEntry
841
+ from edsl.jobs import Jobs
842
+
843
+ # init & basics
844
+ API_KEY = "b"
845
+ coop = Coop(api_key=API_KEY)
846
+ coop
847
+ coop.edsl_settings
848
+
849
+ ##############
850
+ # A. A simple example
851
+ ##############
852
+ # .. create and manipulate an object through the Coop client
853
+ response = coop.create(QuestionMultipleChoice.example())
854
+ coop.get(uuid=response.get("uuid"))
855
+ coop.get(uuid=response.get("uuid"), expected_object_type="question")
856
+ coop.get(url=response.get("url"))
857
+ coop.create(QuestionMultipleChoice.example())
858
+ coop.get_all("question")
859
+ coop.patch(uuid=response.get("uuid"), visibility="private")
860
+ coop.patch(uuid=response.get("uuid"), description="hey")
861
+ coop.patch(uuid=response.get("uuid"), value=QuestionFreeText.example())
862
+ # coop.patch(uuid=response.get("uuid"), value=Survey.example()) - should throw error
863
+ coop.get(uuid=response.get("uuid"))
864
+ coop.delete(uuid=response.get("uuid"))
865
+
866
+ # .. create and manipulate an object through the class
867
+ response = QuestionMultipleChoice.example().push()
868
+ QuestionMultipleChoice.pull(uuid=response.get("uuid"))
869
+ QuestionMultipleChoice.pull(url=response.get("url"))
870
+ QuestionMultipleChoice.patch(uuid=response.get("uuid"), visibility="private")
871
+ QuestionMultipleChoice.patch(uuid=response.get("uuid"), description="hey")
872
+ QuestionMultipleChoice.patch(
873
+ uuid=response.get("uuid"), value=QuestionFreeText.example()
874
+ )
875
+ QuestionMultipleChoice.pull(response.get("uuid"))
876
+ QuestionMultipleChoice.delete(response.get("uuid"))
877
+
878
+ ##############
879
+ # B. Examples with all objects
880
+ ##############
881
+ OBJECTS = [
882
+ ("agent", Agent),
883
+ ("agent_list", AgentList),
884
+ ("cache", Cache),
885
+ ("notebook", Notebook),
886
+ ("question", QuestionMultipleChoice),
887
+ ("results", Results),
888
+ ("scenario", Scenario),
889
+ ("scenario_list", ScenarioList),
890
+ ("survey", Survey),
891
+ ]
892
+ for object_type, cls in OBJECTS:
893
+ print(f"Testing {object_type} objects")
894
+ # 1. Delete existing objects
895
+ existing_objects = coop.get_all(object_type)
896
+ for item in existing_objects:
897
+ coop.delete(uuid=item.get("uuid"))
898
+ # 2. Create new objects
899
+ example = cls.example()
900
+ response_1 = coop.create(example)
901
+ response_2 = coop.create(cls.example(), visibility="private")
902
+ response_3 = coop.create(cls.example(), visibility="public")
903
+ response_4 = coop.create(
904
+ cls.example(), visibility="unlisted", description="hey"
905
+ )
906
+ # 3. Retrieve all objects
907
+ objects = coop.get_all(object_type)
908
+ assert len(objects) == 4
909
+ # 4. Try to retrieve an item that does not exist
910
+ try:
911
+ coop.get(uuid=uuid4())
912
+ except Exception as e:
913
+ print(e)
914
+ # 5. Try to retrieve all test objects by their uuids
915
+ for response in [response_1, response_2, response_3, response_4]:
916
+ coop.get(uuid=response.get("uuid"))
917
+ # 6. Change visibility of all objects
918
+ for item in objects:
919
+ coop.patch(uuid=item.get("uuid"), visibility="private")
920
+ # 6. Change description of all objects
921
+ for item in objects:
922
+ coop.patch(uuid=item.get("uuid"), description="hey")
923
+ # 7. Delete all objects
924
+ for item in objects:
925
+ coop.delete(uuid=item.get("uuid"))
926
+ assert len(coop.get_all(object_type)) == 0
927
+
928
+ ##############
929
+ # C. Remote Cache
930
+ ##############
931
+ # clear
932
+ coop.remote_cache_clear()
933
+ assert coop.remote_cache_get() == []
934
+ # create one remote cache entry
935
+ cache_entry = CacheEntry.example()
936
+ cache_entry.to_dict()
937
+ coop.remote_cache_create(cache_entry)
938
+ # create many remote cache entries
939
+ cache_entries = [CacheEntry.example(randomize=True) for _ in range(10)]
940
+ coop.remote_cache_create_many(cache_entries)
941
+ # get all remote cache entries
942
+ coop.remote_cache_get()
943
+ coop.remote_cache_get(exclude_keys=[])
944
+ coop.remote_cache_get(exclude_keys=["a"])
945
+ exclude_keys = [cache_entry.key for cache_entry in cache_entries]
946
+ coop.remote_cache_get(exclude_keys)
947
+ # clear
948
+ coop.remote_cache_clear()
949
+ coop.remote_cache_get()
950
+
951
+ ##############
952
+ # D. Remote Inference
953
+ ##############
954
+ job = Jobs.example()
955
+ coop.remote_inference_cost(job)
956
+ job_coop_object = coop.remote_inference_create(job)
957
+ job_coop_results = coop.remote_inference_get(job_coop_object.get("uuid"))
958
+ coop.get(uuid=job_coop_results.get("results_uuid"))