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