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