promptstudio-sdk 0.1.0__tar.gz
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.
- promptstudio_sdk-0.1.0/PKG-INFO +254 -0
- promptstudio_sdk-0.1.0/README.md +235 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/__init__.py +4 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/base.py +93 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/cache.py +380 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/client.py +27 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/persistent_cache.py +58 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/prompt.py +1958 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/types.py +63 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/utils/__init__.py +27 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/utils/helpers.py +253 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk/utils/logger.py +26 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk.egg-info/PKG-INFO +254 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk.egg-info/SOURCES.txt +25 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk.egg-info/dependency_links.txt +1 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk.egg-info/requires.txt +7 -0
- promptstudio_sdk-0.1.0/promptstudio_sdk.egg-info/top_level.txt +1 -0
- promptstudio_sdk-0.1.0/pyproject.toml +32 -0
- promptstudio_sdk-0.1.0/setup.cfg +4 -0
- promptstudio_sdk-0.1.0/setup.py +29 -0
- promptstudio_sdk-0.1.0/tests/__init__.py +1 -0
- promptstudio_sdk-0.1.0/tests/conftest.py +24 -0
- promptstudio_sdk-0.1.0/tests/test_base.py +99 -0
- promptstudio_sdk-0.1.0/tests/test_client.py +57 -0
- promptstudio_sdk-0.1.0/tests/test_no_bypass.py +102 -0
- promptstudio_sdk-0.1.0/tests/test_prompt.py +292 -0
- promptstudio_sdk-0.1.0/tests/test_summary.py +252 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: promptstudio_sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python SDK for PromptStudio
|
|
5
|
+
Home-page: https://github.com/promptstudio-dev/promptstudio-sdk-python
|
|
6
|
+
Author: PromptStudio
|
|
7
|
+
Author-email: support@promptstudio.dev
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: requests
|
|
14
|
+
Requires-Dist: pymongo
|
|
15
|
+
Requires-Dist: openai
|
|
16
|
+
Provides-Extra: test
|
|
17
|
+
Requires-Dist: pytest; extra == "test"
|
|
18
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
19
|
+
|
|
20
|
+
# PromptStudio Python SDK
|
|
21
|
+
|
|
22
|
+
A Python SDK for interacting with PromptStudio API and AI platforms directly.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
### From PyPI (Coming Soon)
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install promptstudio-sdk
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### From Source
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
git clone https://github.com/your-repo/promptstudio-sdk.git
|
|
36
|
+
cd promptstudio-sdk
|
|
37
|
+
pip install -e .
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Development Setup
|
|
41
|
+
|
|
42
|
+
1. Create a virtual environment:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
python -m venv venv
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
2. Activate the virtual environment:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
# On Windows
|
|
52
|
+
venv\Scripts\activate
|
|
53
|
+
|
|
54
|
+
# On Unix or MacOS
|
|
55
|
+
source venv/bin/activate
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
3. Install dependencies:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install -r requirements.txt
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Usage
|
|
65
|
+
|
|
66
|
+
### Initializing the SDK
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from promptstudio_sdk import PromptStudio
|
|
70
|
+
|
|
71
|
+
client = PromptStudio({
|
|
72
|
+
'api_key': 'YOUR_API_KEY',
|
|
73
|
+
'env': 'test' # Use 'prod' for production environment
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Getting All Prompts
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
# Get all prompts from a specific folder
|
|
81
|
+
prompts = client.get_all_prompts("your_folder_id")
|
|
82
|
+
print(prompts)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Chatting with a Prompt
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
response = client.chat_with_prompt(
|
|
89
|
+
prompt_id="your_prompt_id",
|
|
90
|
+
user_message=[
|
|
91
|
+
{
|
|
92
|
+
"type": "text",
|
|
93
|
+
"text": "Hello, how are you?"
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
memory_type="fullMemory",
|
|
97
|
+
window_size=10,
|
|
98
|
+
session_id="test_session",
|
|
99
|
+
variables={}
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
print(response)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Complete Example
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from promptstudio_sdk import PromptStudio
|
|
109
|
+
|
|
110
|
+
def main():
|
|
111
|
+
# Initialize the client
|
|
112
|
+
client = PromptStudio({
|
|
113
|
+
'api_key': 'YOUR_API_KEY',
|
|
114
|
+
'env': 'test'
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
try:
|
|
118
|
+
# Get all prompts
|
|
119
|
+
prompts = client.get_all_prompts("your_folder_id")
|
|
120
|
+
print("Available prompts:", prompts)
|
|
121
|
+
|
|
122
|
+
# Chat with a specific prompt
|
|
123
|
+
response = client.chat_with_prompt(
|
|
124
|
+
prompt_id="your_prompt_id",
|
|
125
|
+
user_message=[
|
|
126
|
+
{
|
|
127
|
+
"type": "text",
|
|
128
|
+
"text": "Hello, how are you?"
|
|
129
|
+
}
|
|
130
|
+
],
|
|
131
|
+
memory_type="windowMemory",
|
|
132
|
+
window_size=10,
|
|
133
|
+
session_id="test_session",
|
|
134
|
+
variables={}
|
|
135
|
+
)
|
|
136
|
+
print("Chat response:", response)
|
|
137
|
+
|
|
138
|
+
except Exception as e:
|
|
139
|
+
print(f"An error occurred: {e}")
|
|
140
|
+
|
|
141
|
+
if __name__ == "__main__":
|
|
142
|
+
main()
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Testing
|
|
146
|
+
|
|
147
|
+
### Setting Up Tests
|
|
148
|
+
|
|
149
|
+
1. Install test dependencies:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pip install pytest pytest-cov
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
2. Create a `.env` file in the root directory with your test credentials:
|
|
156
|
+
|
|
157
|
+
```env
|
|
158
|
+
PROMPTSTUDIO_API_KEY=your_test_api_key
|
|
159
|
+
PROMPTSTUDIO_ENV=test
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Running Tests
|
|
163
|
+
|
|
164
|
+
Run all tests:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
pytest
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Run tests with coverage:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
pytest --cov=promptstudio_sdk
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Writing Tests
|
|
177
|
+
|
|
178
|
+
Create test files in the `tests` directory. Here's an example test:
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
import pytest
|
|
182
|
+
from promptstudio_sdk import PromptStudio
|
|
183
|
+
|
|
184
|
+
def test_chat_with_prompt():
|
|
185
|
+
client = PromptStudio({
|
|
186
|
+
'api_key': 'test_api_key',
|
|
187
|
+
'env': 'test'
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
response = client.chat_with_prompt(
|
|
191
|
+
prompt_id="test_prompt",
|
|
192
|
+
user_message=[{"type": "text", "text": "Hello"}],
|
|
193
|
+
memory_type="fullMemory",
|
|
194
|
+
window_size=10,
|
|
195
|
+
session_id="test_session",
|
|
196
|
+
variables={}
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
assert isinstance(response, dict)
|
|
200
|
+
assert 'response' in response
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Type Hints
|
|
205
|
+
|
|
206
|
+
The SDK uses Python type hints for better IDE support and code documentation. Here are some key types:
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
from typing import Dict, List, Union, Optional
|
|
210
|
+
|
|
211
|
+
# Message types
|
|
212
|
+
ImageMessage = Dict[str, Union[str, Dict[str, str]]] # {"type": "image_url", "image_url": {"url": "..."}}
|
|
213
|
+
TextMessage = Dict[str, str] # {"type": "text", "text": "..."}
|
|
214
|
+
UserMessage = List[Union[ImageMessage, TextMessage]]
|
|
215
|
+
|
|
216
|
+
# Memory types
|
|
217
|
+
Memory = Literal["fullMemory", "windowMemory", "summarizedMemory"]
|
|
218
|
+
|
|
219
|
+
# Request payload
|
|
220
|
+
RequestPayload = Dict[str, Union[UserMessage, Memory, int, str, Dict[str, str], Optional[int]]]
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Error Handling
|
|
224
|
+
|
|
225
|
+
The SDK raises exceptions for various error cases:
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
from promptstudio_sdk import PromptStudio
|
|
229
|
+
|
|
230
|
+
try:
|
|
231
|
+
client = PromptStudio({
|
|
232
|
+
'api_key': 'YOUR_API_KEY',
|
|
233
|
+
'env': 'test'
|
|
234
|
+
})
|
|
235
|
+
response = client.chat_with_prompt(...)
|
|
236
|
+
except requests.exceptions.HTTPError as e:
|
|
237
|
+
print(f"HTTP error occurred: {e}")
|
|
238
|
+
except requests.exceptions.RequestException as e:
|
|
239
|
+
print(f"Network error occurred: {e}")
|
|
240
|
+
except Exception as e:
|
|
241
|
+
print(f"An error occurred: {e}")
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Contributing
|
|
245
|
+
|
|
246
|
+
1. Fork the repository
|
|
247
|
+
2. Create a new branch for your feature
|
|
248
|
+
3. Make your changes
|
|
249
|
+
4. Run the tests to ensure everything works
|
|
250
|
+
5. Submit a pull request
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
This SDK is released under the MIT License.
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# PromptStudio Python SDK
|
|
2
|
+
|
|
3
|
+
A Python SDK for interacting with PromptStudio API and AI platforms directly.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### From PyPI (Coming Soon)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install promptstudio-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### From Source
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/your-repo/promptstudio-sdk.git
|
|
17
|
+
cd promptstudio-sdk
|
|
18
|
+
pip install -e .
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Development Setup
|
|
22
|
+
|
|
23
|
+
1. Create a virtual environment:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
python -m venv venv
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2. Activate the virtual environment:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# On Windows
|
|
33
|
+
venv\Scripts\activate
|
|
34
|
+
|
|
35
|
+
# On Unix or MacOS
|
|
36
|
+
source venv/bin/activate
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
3. Install dependencies:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install -r requirements.txt
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### Initializing the SDK
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from promptstudio_sdk import PromptStudio
|
|
51
|
+
|
|
52
|
+
client = PromptStudio({
|
|
53
|
+
'api_key': 'YOUR_API_KEY',
|
|
54
|
+
'env': 'test' # Use 'prod' for production environment
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Getting All Prompts
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
# Get all prompts from a specific folder
|
|
62
|
+
prompts = client.get_all_prompts("your_folder_id")
|
|
63
|
+
print(prompts)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Chatting with a Prompt
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
response = client.chat_with_prompt(
|
|
70
|
+
prompt_id="your_prompt_id",
|
|
71
|
+
user_message=[
|
|
72
|
+
{
|
|
73
|
+
"type": "text",
|
|
74
|
+
"text": "Hello, how are you?"
|
|
75
|
+
}
|
|
76
|
+
],
|
|
77
|
+
memory_type="fullMemory",
|
|
78
|
+
window_size=10,
|
|
79
|
+
session_id="test_session",
|
|
80
|
+
variables={}
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
print(response)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Complete Example
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from promptstudio_sdk import PromptStudio
|
|
90
|
+
|
|
91
|
+
def main():
|
|
92
|
+
# Initialize the client
|
|
93
|
+
client = PromptStudio({
|
|
94
|
+
'api_key': 'YOUR_API_KEY',
|
|
95
|
+
'env': 'test'
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
# Get all prompts
|
|
100
|
+
prompts = client.get_all_prompts("your_folder_id")
|
|
101
|
+
print("Available prompts:", prompts)
|
|
102
|
+
|
|
103
|
+
# Chat with a specific prompt
|
|
104
|
+
response = client.chat_with_prompt(
|
|
105
|
+
prompt_id="your_prompt_id",
|
|
106
|
+
user_message=[
|
|
107
|
+
{
|
|
108
|
+
"type": "text",
|
|
109
|
+
"text": "Hello, how are you?"
|
|
110
|
+
}
|
|
111
|
+
],
|
|
112
|
+
memory_type="windowMemory",
|
|
113
|
+
window_size=10,
|
|
114
|
+
session_id="test_session",
|
|
115
|
+
variables={}
|
|
116
|
+
)
|
|
117
|
+
print("Chat response:", response)
|
|
118
|
+
|
|
119
|
+
except Exception as e:
|
|
120
|
+
print(f"An error occurred: {e}")
|
|
121
|
+
|
|
122
|
+
if __name__ == "__main__":
|
|
123
|
+
main()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Testing
|
|
127
|
+
|
|
128
|
+
### Setting Up Tests
|
|
129
|
+
|
|
130
|
+
1. Install test dependencies:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pip install pytest pytest-cov
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
2. Create a `.env` file in the root directory with your test credentials:
|
|
137
|
+
|
|
138
|
+
```env
|
|
139
|
+
PROMPTSTUDIO_API_KEY=your_test_api_key
|
|
140
|
+
PROMPTSTUDIO_ENV=test
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Running Tests
|
|
144
|
+
|
|
145
|
+
Run all tests:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
pytest
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Run tests with coverage:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
pytest --cov=promptstudio_sdk
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Writing Tests
|
|
158
|
+
|
|
159
|
+
Create test files in the `tests` directory. Here's an example test:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
import pytest
|
|
163
|
+
from promptstudio_sdk import PromptStudio
|
|
164
|
+
|
|
165
|
+
def test_chat_with_prompt():
|
|
166
|
+
client = PromptStudio({
|
|
167
|
+
'api_key': 'test_api_key',
|
|
168
|
+
'env': 'test'
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
response = client.chat_with_prompt(
|
|
172
|
+
prompt_id="test_prompt",
|
|
173
|
+
user_message=[{"type": "text", "text": "Hello"}],
|
|
174
|
+
memory_type="fullMemory",
|
|
175
|
+
window_size=10,
|
|
176
|
+
session_id="test_session",
|
|
177
|
+
variables={}
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
assert isinstance(response, dict)
|
|
181
|
+
assert 'response' in response
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Type Hints
|
|
186
|
+
|
|
187
|
+
The SDK uses Python type hints for better IDE support and code documentation. Here are some key types:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from typing import Dict, List, Union, Optional
|
|
191
|
+
|
|
192
|
+
# Message types
|
|
193
|
+
ImageMessage = Dict[str, Union[str, Dict[str, str]]] # {"type": "image_url", "image_url": {"url": "..."}}
|
|
194
|
+
TextMessage = Dict[str, str] # {"type": "text", "text": "..."}
|
|
195
|
+
UserMessage = List[Union[ImageMessage, TextMessage]]
|
|
196
|
+
|
|
197
|
+
# Memory types
|
|
198
|
+
Memory = Literal["fullMemory", "windowMemory", "summarizedMemory"]
|
|
199
|
+
|
|
200
|
+
# Request payload
|
|
201
|
+
RequestPayload = Dict[str, Union[UserMessage, Memory, int, str, Dict[str, str], Optional[int]]]
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Error Handling
|
|
205
|
+
|
|
206
|
+
The SDK raises exceptions for various error cases:
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
from promptstudio_sdk import PromptStudio
|
|
210
|
+
|
|
211
|
+
try:
|
|
212
|
+
client = PromptStudio({
|
|
213
|
+
'api_key': 'YOUR_API_KEY',
|
|
214
|
+
'env': 'test'
|
|
215
|
+
})
|
|
216
|
+
response = client.chat_with_prompt(...)
|
|
217
|
+
except requests.exceptions.HTTPError as e:
|
|
218
|
+
print(f"HTTP error occurred: {e}")
|
|
219
|
+
except requests.exceptions.RequestException as e:
|
|
220
|
+
print(f"Network error occurred: {e}")
|
|
221
|
+
except Exception as e:
|
|
222
|
+
print(f"An error occurred: {e}")
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Contributing
|
|
226
|
+
|
|
227
|
+
1. Fork the repository
|
|
228
|
+
2. Create a new branch for your feature
|
|
229
|
+
3. Make your changes
|
|
230
|
+
4. Run the tests to ensure everything works
|
|
231
|
+
5. Submit a pull request
|
|
232
|
+
|
|
233
|
+
## License
|
|
234
|
+
|
|
235
|
+
This SDK is released under the MIT License.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from typing import Dict, Any, TypedDict, Optional
|
|
2
|
+
import aiohttp
|
|
3
|
+
import logging
|
|
4
|
+
import json
|
|
5
|
+
import ssl
|
|
6
|
+
import certifi
|
|
7
|
+
import os
|
|
8
|
+
|
|
9
|
+
# Configure logging
|
|
10
|
+
logging.basicConfig(
|
|
11
|
+
level=logging.INFO,
|
|
12
|
+
format="\n%(asctime)s - %(name)s - %(levelname)s - %(message)s\n",
|
|
13
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
14
|
+
force=True,
|
|
15
|
+
handlers=[logging.StreamHandler()], # This ensures output goes to console
|
|
16
|
+
)
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ConfigDict(TypedDict):
|
|
21
|
+
api_key: str
|
|
22
|
+
env: str
|
|
23
|
+
bypass: Optional[bool]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Base:
|
|
27
|
+
def __init__(self, config: ConfigDict):
|
|
28
|
+
"""
|
|
29
|
+
Initialize the base class with configuration
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
config: Dictionary containing:
|
|
33
|
+
- 'api_key': API key
|
|
34
|
+
- 'env': Environment ('test' or 'prod')
|
|
35
|
+
- 'bypass': Optional boolean to bypass PromptStudio server
|
|
36
|
+
"""
|
|
37
|
+
self.api_key = config["api_key"]
|
|
38
|
+
self.env = config["env"]
|
|
39
|
+
self.bypass = config.get("bypass", False)
|
|
40
|
+
|
|
41
|
+
self.base_url = (
|
|
42
|
+
"https://api.promptstudio.dev/api/v1"
|
|
43
|
+
if self.env == "prod"
|
|
44
|
+
else "https://api.playground.promptstudio.dev/api/v1"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
async def _request(
|
|
48
|
+
self, endpoint: str, method: str = "GET", **kwargs
|
|
49
|
+
) -> Dict[str, Any]:
|
|
50
|
+
"""
|
|
51
|
+
Make async HTTP requests to the API with proper SSL handling
|
|
52
|
+
"""
|
|
53
|
+
url = f"{self.base_url}{endpoint}"
|
|
54
|
+
headers = {
|
|
55
|
+
"Content-Type": "application/json",
|
|
56
|
+
"x-api-key": "nDBabew4CGIKD8uKnOqOajG8AZgczzgW",
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
# Create SSL context with proper certificate verification
|
|
60
|
+
ssl_context = ssl.create_default_context(cafile=certifi.where())
|
|
61
|
+
|
|
62
|
+
# Log request details
|
|
63
|
+
logger.info(f"Making {method} request to: {url}")
|
|
64
|
+
if "data" in kwargs:
|
|
65
|
+
logger.info(f"Request body: {kwargs['data']}")
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
async with aiohttp.ClientSession(
|
|
69
|
+
connector=aiohttp.TCPConnector(ssl=ssl_context)
|
|
70
|
+
) as session:
|
|
71
|
+
if method.upper() == "POST" and "data" in kwargs:
|
|
72
|
+
if isinstance(kwargs["data"], str):
|
|
73
|
+
json_data = kwargs["data"]
|
|
74
|
+
else:
|
|
75
|
+
json_data = json.dumps(kwargs["data"])
|
|
76
|
+
|
|
77
|
+
async with session.post(
|
|
78
|
+
url, headers=headers, data=json_data
|
|
79
|
+
) as response:
|
|
80
|
+
response.raise_for_status()
|
|
81
|
+
return await response.json()
|
|
82
|
+
else:
|
|
83
|
+
async with session.request(
|
|
84
|
+
method=method, url=url, headers=headers, **kwargs
|
|
85
|
+
) as response:
|
|
86
|
+
response.raise_for_status()
|
|
87
|
+
return await response.json()
|
|
88
|
+
except aiohttp.ClientConnectorCertificateError as e:
|
|
89
|
+
logger.error(f"SSL Certificate Error: {str(e)}")
|
|
90
|
+
raise
|
|
91
|
+
except Exception as e:
|
|
92
|
+
logger.error(f"Request Error: {str(e)}")
|
|
93
|
+
raise
|