universal-mcp 0.1.2__py3-none-any.whl → 0.1.3__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.
- universal_mcp/applications/firecrawl/app.py +74 -190
- universal_mcp/applications/markitdown/app.py +17 -6
- universal_mcp/applications/notion/README.md +43 -20
- universal_mcp/applications/notion/app.py +122 -130
- universal_mcp/applications/perplexity/app.py +79 -0
- universal_mcp/cli.py +20 -13
- universal_mcp/integrations/integration.py +9 -3
- universal_mcp/logger.py +74 -0
- universal_mcp/servers/server.py +28 -21
- universal_mcp/utils/docgen.py +2 -2
- universal_mcp/utils/installation.py +15 -0
- universal_mcp/utils/openapi.py +22 -29
- universal_mcp-0.1.3.dist-info/METADATA +252 -0
- {universal_mcp-0.1.2.dist-info → universal_mcp-0.1.3.dist-info}/RECORD +16 -14
- universal_mcp-0.1.2.dist-info/METADATA +0 -208
- {universal_mcp-0.1.2.dist-info → universal_mcp-0.1.3.dist-info}/WHEEL +0 -0
- {universal_mcp-0.1.2.dist-info → universal_mcp-0.1.3.dist-info}/entry_points.txt +0 -0
@@ -7,11 +7,11 @@ from universal_mcp.integrations import Integration
|
|
7
7
|
class NotionApp(APIApplication):
|
8
8
|
def __init__(self, integration: Integration = None, **kwargs) -> None:
|
9
9
|
"""
|
10
|
-
Initializes a new instance of the
|
10
|
+
Initializes a new instance of the Notion API app with a specified integration and additional parameters.
|
11
11
|
|
12
12
|
Args:
|
13
|
-
integration:
|
14
|
-
|
13
|
+
integration: Optional; an Integration object containing credentials for authenticating with the Notion API. Defaults to None.
|
14
|
+
kwargs: Additional keyword arguments that are passed to the parent class initializer.
|
15
15
|
|
16
16
|
Returns:
|
17
17
|
None
|
@@ -29,18 +29,19 @@ class NotionApp(APIApplication):
|
|
29
29
|
"Authorization": f"Bearer {credentials['access_token']}",
|
30
30
|
"Accept": "application/json",
|
31
31
|
"Notion-Version": "2022-06-28",
|
32
|
-
}
|
32
|
+
}
|
33
33
|
|
34
|
-
def
|
34
|
+
def retrieve_a_user(self, id, request_body=None) -> dict[str, Any]:
|
35
35
|
"""
|
36
|
-
Retrieves user
|
36
|
+
Retrieves user details from the server using the specified user ID.
|
37
37
|
|
38
38
|
Args:
|
39
|
-
|
40
|
-
|
39
|
+
self: Instance of the class containing the method.
|
40
|
+
id: The unique identifier of the user to retrieve.
|
41
|
+
request_body: Optional request body data, provided when needed. Default is None.
|
41
42
|
|
42
43
|
Returns:
|
43
|
-
A dictionary containing
|
44
|
+
A dictionary containing user details, as retrieved from the server response.
|
44
45
|
"""
|
45
46
|
if id is None:
|
46
47
|
raise ValueError("Missing required parameter 'id'")
|
@@ -50,15 +51,15 @@ class NotionApp(APIApplication):
|
|
50
51
|
response.raise_for_status()
|
51
52
|
return response.json()
|
52
53
|
|
53
|
-
def
|
54
|
+
def list_all_users(self, ) -> dict[str, Any]:
|
54
55
|
"""
|
55
|
-
Fetches
|
56
|
+
Fetches a list of all users from the API endpoint and returns the data as a dictionary.
|
56
57
|
|
57
58
|
Args:
|
58
59
|
None: This method does not take any parameters.
|
59
60
|
|
60
61
|
Returns:
|
61
|
-
A dictionary containing the
|
62
|
+
A dictionary containing the list of users retrieved from the API. The dictionary keys are strings, and the values can be of any type.
|
62
63
|
"""
|
63
64
|
url = f"{self.base_url}/v1/users"
|
64
65
|
query_params = {}
|
@@ -66,15 +67,15 @@ class NotionApp(APIApplication):
|
|
66
67
|
response.raise_for_status()
|
67
68
|
return response.json()
|
68
69
|
|
69
|
-
def
|
70
|
+
def retrieve_your_token_sbot_user(self, ) -> dict[str, Any]:
|
70
71
|
"""
|
71
|
-
Retrieves the current user
|
72
|
+
Retrieves the authentication token for the current user from the SBOT service.
|
72
73
|
|
73
74
|
Args:
|
74
|
-
self: Instance of the class containing
|
75
|
+
self: Instance of the class containing configuration such as 'base_url' and the '_get' method.
|
75
76
|
|
76
77
|
Returns:
|
77
|
-
A dictionary containing the current user's token information
|
78
|
+
A dictionary containing the JSON response of the current user's token information.
|
78
79
|
"""
|
79
80
|
url = f"{self.base_url}/v1/users/me"
|
80
81
|
query_params = {}
|
@@ -82,15 +83,15 @@ class NotionApp(APIApplication):
|
|
82
83
|
response.raise_for_status()
|
83
84
|
return response.json()
|
84
85
|
|
85
|
-
def
|
86
|
+
def retrieve_a_database(self, id) -> dict[str, Any]:
|
86
87
|
"""
|
87
|
-
Retrieves a
|
88
|
+
Retrieves database details from a specified endpoint using the provided database ID.
|
88
89
|
|
89
90
|
Args:
|
90
|
-
id: A
|
91
|
+
id: A unique identifier for the database to be retrieved. Must be a non-null value.
|
91
92
|
|
92
93
|
Returns:
|
93
|
-
A dictionary containing the details of the
|
94
|
+
A dictionary containing the details of the requested database.
|
94
95
|
"""
|
95
96
|
if id is None:
|
96
97
|
raise ValueError("Missing required parameter 'id'")
|
@@ -100,91 +101,86 @@ class NotionApp(APIApplication):
|
|
100
101
|
response.raise_for_status()
|
101
102
|
return response.json()
|
102
103
|
|
103
|
-
def
|
104
|
+
def update_a_database(self, id, request_body=None) -> dict[str, Any]:
|
104
105
|
"""
|
105
|
-
Updates a
|
106
|
+
Updates a database entry with the given ID using a PATCH request.
|
106
107
|
|
107
108
|
Args:
|
108
|
-
self:
|
109
|
-
id:
|
110
|
-
request_body: An optional dictionary containing the
|
109
|
+
self: The instance of the class to which the method belongs.
|
110
|
+
id: The unique identifier of the database entry to be updated.
|
111
|
+
request_body: An optional dictionary containing the data to update the database entry with. Defaults to None.
|
111
112
|
|
112
113
|
Returns:
|
113
|
-
A dictionary representing the JSON response from the
|
114
|
+
A dictionary representing the JSON response from the server after the update operation.
|
114
115
|
"""
|
115
116
|
if id is None:
|
116
117
|
raise ValueError("Missing required parameter 'id'")
|
117
118
|
url = f"{self.base_url}/v1/databases/{id}"
|
118
119
|
query_params = {}
|
119
|
-
response = self._patch(url, data=
|
120
|
+
response = self._patch(url, data=request_body, params=query_params)
|
120
121
|
response.raise_for_status()
|
121
122
|
return response.json()
|
122
123
|
|
123
|
-
def
|
124
|
+
def query_a_database(self, id, request_body=None) -> dict[str, Any]:
|
124
125
|
"""
|
125
|
-
Executes a query on a
|
126
|
+
Executes a query on a specified database using an identifier and an optional request body.
|
126
127
|
|
127
128
|
Args:
|
128
|
-
|
129
|
-
|
130
|
-
request_body: Optional. A dictionary representing the request body for the query. Defaults to None.
|
129
|
+
id: The unique identifier of the database to query.
|
130
|
+
request_body: Optional JSON-compatible dictionary representing the body of the query request; if None, no additional query data is sent.
|
131
131
|
|
132
132
|
Returns:
|
133
|
-
A dictionary containing the
|
133
|
+
A dictionary containing the response data from the database query as parsed from JSON.
|
134
134
|
"""
|
135
135
|
if id is None:
|
136
136
|
raise ValueError("Missing required parameter 'id'")
|
137
137
|
url = f"{self.base_url}/v1/databases/{id}/query"
|
138
138
|
query_params = {}
|
139
|
-
|
140
|
-
response = self._post(url, data=json_body, params=query_params)
|
139
|
+
response = self._post(url, data=request_body, params=query_params)
|
141
140
|
response.raise_for_status()
|
142
141
|
return response.json()
|
143
142
|
|
144
|
-
def
|
143
|
+
def create_a_database(self, request_body=None) -> dict[str, Any]:
|
145
144
|
"""
|
146
|
-
Creates a new database
|
145
|
+
Creates a new database on the server using the specified request body.
|
147
146
|
|
148
147
|
Args:
|
149
|
-
|
150
|
-
request_body: Optional dictionary containing the specifications for creating the Notion database. If None, a default empty request body is used.
|
148
|
+
request_body: A dictionary containing the data to be sent in the request body. Defaults to None if not provided.
|
151
149
|
|
152
150
|
Returns:
|
153
|
-
A dictionary
|
151
|
+
A dictionary containing the server's JSON response from the database creation request.
|
154
152
|
"""
|
155
153
|
url = f"{self.base_url}/v1/databases/"
|
156
154
|
query_params = {}
|
157
|
-
|
158
|
-
response = self._post(url, data=json_body, params=query_params)
|
155
|
+
response = self._post(url, data=request_body, params=query_params)
|
159
156
|
response.raise_for_status()
|
160
157
|
return response.json()
|
161
158
|
|
162
|
-
def
|
159
|
+
def create_a_page(self, request_body=None) -> dict[str, Any]:
|
163
160
|
"""
|
164
|
-
Creates a new page
|
161
|
+
Creates a new page by sending a POST request to the specified endpoint.
|
165
162
|
|
166
163
|
Args:
|
167
|
-
request_body: Optional; A dictionary containing the data to
|
164
|
+
request_body: Optional; A dictionary containing the data to be sent in the body of the POST request. Defaults to None.
|
168
165
|
|
169
166
|
Returns:
|
170
|
-
A dictionary
|
167
|
+
A dictionary representing the JSON response from the server, containing the details of the newly created page.
|
171
168
|
"""
|
172
169
|
url = f"{self.base_url}/v1/pages/"
|
173
170
|
query_params = {}
|
174
|
-
|
175
|
-
response = self._post(url, data=json_body, params=query_params)
|
171
|
+
response = self._post(url, data=request_body, params=query_params)
|
176
172
|
response.raise_for_status()
|
177
173
|
return response.json()
|
178
174
|
|
179
|
-
def
|
175
|
+
def retrieve_a_page(self, id) -> dict[str, Any]:
|
180
176
|
"""
|
181
|
-
Retrieves a page
|
177
|
+
Retrieves a page by its unique identifier from a remote server.
|
182
178
|
|
183
179
|
Args:
|
184
|
-
id: The unique identifier of the
|
180
|
+
id: The unique identifier of the page to retrieve.
|
185
181
|
|
186
182
|
Returns:
|
187
|
-
A dictionary containing the JSON response from the
|
183
|
+
A dictionary containing the JSON response from the server, which represents the page data.
|
188
184
|
"""
|
189
185
|
if id is None:
|
190
186
|
raise ValueError("Missing required parameter 'id'")
|
@@ -194,36 +190,35 @@ class NotionApp(APIApplication):
|
|
194
190
|
response.raise_for_status()
|
195
191
|
return response.json()
|
196
192
|
|
197
|
-
def
|
193
|
+
def update_page_properties(self, id, request_body=None) -> dict[str, Any]:
|
198
194
|
"""
|
199
|
-
Updates the properties of a
|
195
|
+
Updates the properties of a page identified by its ID using the provided request body.
|
200
196
|
|
201
197
|
Args:
|
202
|
-
|
203
|
-
|
204
|
-
request_body: An optional dictionary representing the request body to be sent with the update request. Defaults to None, indicating no additional properties to update.
|
198
|
+
id: The unique identifier of the page whose properties are to be updated. Must not be None.
|
199
|
+
request_body: An optional dictionary representing the request payload containing the properties to be updated. Defaults to None.
|
205
200
|
|
206
201
|
Returns:
|
207
|
-
A dictionary containing the
|
202
|
+
A dictionary containing the updated page properties as returned by the server after the update.
|
208
203
|
"""
|
209
204
|
if id is None:
|
210
205
|
raise ValueError("Missing required parameter 'id'")
|
211
206
|
url = f"{self.base_url}/v1/pages/{id}"
|
212
207
|
query_params = {}
|
213
|
-
response = self._patch(url, data=
|
208
|
+
response = self._patch(url, data=request_body, params=query_params)
|
214
209
|
response.raise_for_status()
|
215
210
|
return response.json()
|
216
211
|
|
217
|
-
def
|
212
|
+
def retrieve_a_page_property_item(self, page_id, property_id) -> dict[str, Any]:
|
218
213
|
"""
|
219
|
-
Retrieves
|
214
|
+
Retrieves the property item of a page using specified page and property identifiers.
|
220
215
|
|
221
216
|
Args:
|
222
|
-
page_id: The unique identifier
|
223
|
-
property_id: The unique identifier
|
217
|
+
page_id: The unique identifier of the page from which the property item is to be retrieved.
|
218
|
+
property_id: The unique identifier of the property associated with the specified page.
|
224
219
|
|
225
220
|
Returns:
|
226
|
-
A dictionary
|
221
|
+
A dictionary representing the JSON response with details of the property item.
|
227
222
|
"""
|
228
223
|
if page_id is None:
|
229
224
|
raise ValueError("Missing required parameter 'page_id'")
|
@@ -235,17 +230,16 @@ class NotionApp(APIApplication):
|
|
235
230
|
response.raise_for_status()
|
236
231
|
return response.json()
|
237
232
|
|
238
|
-
def
|
233
|
+
def retrieve_block_children(self, id, page_size=None) -> dict[str, Any]:
|
239
234
|
"""
|
240
|
-
Retrieves the
|
235
|
+
Retrieves the children of a specified block using its unique identifier.
|
241
236
|
|
242
237
|
Args:
|
243
|
-
|
244
|
-
|
245
|
-
page_size: Optional; the number of child blocks to retrieve per request.
|
238
|
+
id: The unique identifier of the block whose children are to be retrieved.
|
239
|
+
page_size: Optional; The maximum number of children to return per page in the response, if specified.
|
246
240
|
|
247
241
|
Returns:
|
248
|
-
A dictionary containing the
|
242
|
+
A dictionary containing the data representing the children of the specified block.
|
249
243
|
"""
|
250
244
|
if id is None:
|
251
245
|
raise ValueError("Missing required parameter 'id'")
|
@@ -255,35 +249,35 @@ class NotionApp(APIApplication):
|
|
255
249
|
response.raise_for_status()
|
256
250
|
return response.json()
|
257
251
|
|
258
|
-
def
|
252
|
+
def append_block_children(self, id, request_body=None) -> dict[str, Any]:
|
259
253
|
"""
|
260
|
-
Appends child
|
254
|
+
Appends child elements to a block identified by its ID and returns the updated block data.
|
261
255
|
|
262
256
|
Args:
|
263
|
-
self: Instance of the class containing
|
264
|
-
id: The
|
265
|
-
request_body:
|
257
|
+
self: Instance of the class containing this method.
|
258
|
+
id: The identifier of the block to which children will be appended. It must not be None.
|
259
|
+
request_body: Optional dictionary containing the data of the child elements to be appended to the block.
|
266
260
|
|
267
261
|
Returns:
|
268
|
-
A dictionary representing the
|
262
|
+
A dictionary representing the updated block data after appending the child elements.
|
269
263
|
"""
|
270
264
|
if id is None:
|
271
265
|
raise ValueError("Missing required parameter 'id'")
|
272
266
|
url = f"{self.base_url}/v1/blocks/{id}/children"
|
273
267
|
query_params = {}
|
274
|
-
response = self._patch(url, data=
|
268
|
+
response = self._patch(url, data=request_body, params=query_params)
|
275
269
|
response.raise_for_status()
|
276
270
|
return response.json()
|
277
271
|
|
278
|
-
def
|
272
|
+
def retrieve_a_block(self, id) -> dict[str, Any]:
|
279
273
|
"""
|
280
|
-
Retrieves a block from
|
274
|
+
Retrieves a block of data from a given API endpoint using the specified block ID.
|
281
275
|
|
282
276
|
Args:
|
283
|
-
id: The unique identifier
|
277
|
+
id: The unique identifier for the block to be retrieved. It must be a non-None value, otherwise a ValueError will be raised.
|
284
278
|
|
285
279
|
Returns:
|
286
|
-
A dictionary containing the
|
280
|
+
A dictionary containing the JSON response from the API, representing the block data corresponding to the provided ID.
|
287
281
|
"""
|
288
282
|
if id is None:
|
289
283
|
raise ValueError("Missing required parameter 'id'")
|
@@ -293,15 +287,15 @@ class NotionApp(APIApplication):
|
|
293
287
|
response.raise_for_status()
|
294
288
|
return response.json()
|
295
289
|
|
296
|
-
def
|
290
|
+
def delete_a_block(self, id) -> dict[str, Any]:
|
297
291
|
"""
|
298
|
-
Deletes a block
|
292
|
+
Deletes a block by its unique identifier and returns the server's response.
|
299
293
|
|
300
294
|
Args:
|
301
295
|
id: The unique identifier of the block to be deleted. Must not be None.
|
302
296
|
|
303
297
|
Returns:
|
304
|
-
A dictionary containing the
|
298
|
+
A dictionary containing the response data from the server after attempting to delete the block.
|
305
299
|
"""
|
306
300
|
if id is None:
|
307
301
|
raise ValueError("Missing required parameter 'id'")
|
@@ -311,53 +305,52 @@ class NotionApp(APIApplication):
|
|
311
305
|
response.raise_for_status()
|
312
306
|
return response.json()
|
313
307
|
|
314
|
-
def
|
308
|
+
def update_a_block(self, id, request_body=None) -> dict[str, Any]:
|
315
309
|
"""
|
316
|
-
Updates a block
|
310
|
+
Updates a block by sending a PATCH request to the specified endpoint.
|
317
311
|
|
318
312
|
Args:
|
319
|
-
id: The unique identifier
|
320
|
-
request_body:
|
313
|
+
id: The unique identifier for the block that is to be updated.
|
314
|
+
request_body: Optional; A dictionary containing the data to update the block with. Defaults to None.
|
321
315
|
|
322
316
|
Returns:
|
323
|
-
A dictionary containing the JSON response from the
|
317
|
+
A dictionary containing the JSON response from the server after updating the block.
|
324
318
|
"""
|
325
319
|
if id is None:
|
326
320
|
raise ValueError("Missing required parameter 'id'")
|
327
321
|
url = f"{self.base_url}/v1/blocks/{id}"
|
328
322
|
query_params = {}
|
329
|
-
response = self._patch(url, data=
|
323
|
+
response = self._patch(url, data=request_body, params=query_params)
|
330
324
|
response.raise_for_status()
|
331
325
|
return response.json()
|
332
326
|
|
333
|
-
def
|
327
|
+
def search(self, request_body=None) -> dict[str, Any]:
|
334
328
|
"""
|
335
|
-
Executes a search
|
329
|
+
Executes a search query using the specified request body and returns the results.
|
336
330
|
|
337
331
|
Args:
|
338
|
-
request_body: An optional dictionary containing the
|
332
|
+
request_body: An optional dictionary containing the data to be sent in the search request.
|
339
333
|
|
340
334
|
Returns:
|
341
|
-
A dictionary containing the response from the
|
335
|
+
A dictionary containing the JSON-decoded response from the search operation.
|
342
336
|
"""
|
343
337
|
url = f"{self.base_url}/v1/search"
|
344
338
|
query_params = {}
|
345
|
-
|
346
|
-
response = self._post(url, data=json_body, params=query_params)
|
339
|
+
response = self._post(url, data=request_body, params=query_params)
|
347
340
|
response.raise_for_status()
|
348
341
|
return response.json()
|
349
342
|
|
350
|
-
def
|
343
|
+
def retrieve_comments(self, block_id=None, page_size=None, request_body=None) -> dict[str, Any]:
|
351
344
|
"""
|
352
|
-
|
345
|
+
Fetches comments from a remote server for a specified block, with optional pagination.
|
353
346
|
|
354
347
|
Args:
|
355
|
-
block_id: Optional;
|
356
|
-
page_size: Optional;
|
357
|
-
request_body:
|
348
|
+
block_id: Optional; Identifies the block whose comments should be retrieved. If None, retrieves comments without filtering by block.
|
349
|
+
page_size: Optional; Specifies the number of comments to retrieve per page for pagination. If None, the server's default page size is used.
|
350
|
+
request_body: Unused placeholder for future extensibility; this function currently does not utilize this parameter.
|
358
351
|
|
359
352
|
Returns:
|
360
|
-
A dictionary containing the
|
353
|
+
A dictionary containing the response data from the server, parsed from JSON, with keys and values representing comment-related information.
|
361
354
|
"""
|
362
355
|
url = f"{self.base_url}/v1/comments"
|
363
356
|
query_params = {k: v for k, v in [('block_id', block_id), ('page_size', page_size)] if v is not None}
|
@@ -365,51 +358,50 @@ class NotionApp(APIApplication):
|
|
365
358
|
response.raise_for_status()
|
366
359
|
return response.json()
|
367
360
|
|
368
|
-
def
|
361
|
+
def add_comment_to_page(self, request_body=None) -> dict[str, Any]:
|
369
362
|
"""
|
370
|
-
Adds a comment to a
|
363
|
+
Adds a comment to a page by sending a POST request with the provided request body.
|
371
364
|
|
372
365
|
Args:
|
373
|
-
request_body:
|
366
|
+
request_body: Optional; A dictionary containing the data for the comment to be added. Defaults to None.
|
374
367
|
|
375
368
|
Returns:
|
376
|
-
A dictionary
|
369
|
+
A dictionary representing the JSON response from the server, which includes details of the newly added comment.
|
377
370
|
"""
|
378
371
|
url = f"{self.base_url}/v1/comments"
|
379
372
|
query_params = {}
|
380
|
-
|
381
|
-
response = self._post(url, data=json_body, params=query_params)
|
373
|
+
response = self._post(url, data=request_body, params=query_params)
|
382
374
|
response.raise_for_status()
|
383
375
|
return response.json()
|
384
376
|
|
385
377
|
def list_tools(self):
|
386
378
|
"""
|
387
|
-
Returns a list of functions
|
379
|
+
Returns a list of functions related to user and database operations.
|
388
380
|
|
389
381
|
Args:
|
390
|
-
|
382
|
+
self: The instance of the class to which the function belongs.
|
391
383
|
|
392
384
|
Returns:
|
393
|
-
A list of
|
385
|
+
A list of method references that pertain to various operations such as user retrieval, database operations, and page/block management.
|
394
386
|
"""
|
395
387
|
return [
|
396
|
-
self.
|
397
|
-
self.
|
398
|
-
self.
|
399
|
-
self.
|
400
|
-
self.
|
401
|
-
self.
|
402
|
-
self.
|
403
|
-
self.
|
404
|
-
self.
|
405
|
-
self.
|
406
|
-
self.
|
407
|
-
self.
|
408
|
-
self.
|
409
|
-
self.
|
410
|
-
self.
|
411
|
-
self.
|
412
|
-
self.
|
413
|
-
self.
|
414
|
-
self.
|
388
|
+
self.retrieve_a_user,
|
389
|
+
self.list_all_users,
|
390
|
+
self.retrieve_your_token_sbot_user,
|
391
|
+
self.retrieve_a_database,
|
392
|
+
self.update_a_database,
|
393
|
+
self.query_a_database,
|
394
|
+
self.create_a_database,
|
395
|
+
self.create_a_page,
|
396
|
+
self.retrieve_a_page,
|
397
|
+
self.update_page_properties,
|
398
|
+
self.retrieve_a_page_property_item,
|
399
|
+
self.retrieve_block_children,
|
400
|
+
self.append_block_children,
|
401
|
+
self.retrieve_a_block,
|
402
|
+
self.delete_a_block,
|
403
|
+
self.update_a_block,
|
404
|
+
self.search,
|
405
|
+
self.retrieve_comments,
|
406
|
+
self.add_comment_to_page
|
415
407
|
]
|
@@ -0,0 +1,79 @@
|
|
1
|
+
from typing import Any, Literal
|
2
|
+
|
3
|
+
from universal_mcp.applications.application import APIApplication
|
4
|
+
from universal_mcp.integrations import Integration
|
5
|
+
|
6
|
+
|
7
|
+
class PerplexityApp(APIApplication):
|
8
|
+
def __init__(self, integration: Integration | None = None) -> None:
|
9
|
+
super().__init__(name="perplexity", integration=integration)
|
10
|
+
self.api_key: str | None = None
|
11
|
+
self.base_url = "https://api.perplexity.ai"
|
12
|
+
|
13
|
+
def _set_api_key(self):
|
14
|
+
if self.api_key:
|
15
|
+
return
|
16
|
+
|
17
|
+
if not self.integration:
|
18
|
+
raise ValueError("Integration is None. Cannot retrieve Perplexity API Key.")
|
19
|
+
|
20
|
+
credentials = self.integration.get_credentials()
|
21
|
+
if not credentials:
|
22
|
+
raise ValueError(
|
23
|
+
f"Failed to retrieve Perplexity API Key using integration '{self.integration.name}'. "
|
24
|
+
)
|
25
|
+
|
26
|
+
if not isinstance(credentials, str) or not credentials.strip():
|
27
|
+
raise ValueError(
|
28
|
+
f"Invalid credential format received for Perplexity API Key via integration '{self.integration.name}'. "
|
29
|
+
)
|
30
|
+
self.api_key = credentials
|
31
|
+
|
32
|
+
def _get_headers(self) -> dict[str, str]:
|
33
|
+
self._set_api_key()
|
34
|
+
return {
|
35
|
+
"Authorization": f"Bearer {self.api_key}",
|
36
|
+
"Content-Type": "application/json",
|
37
|
+
"Accept": "application/json",
|
38
|
+
}
|
39
|
+
|
40
|
+
def chat(self, query: str, model: Literal["r1-1776","sonar","sonar-pro","sonar-reasoning","sonar-reasoning-pro", "sonar-deep-research"] = "sonar" , temperature: float = 1, system_prompt: str = "Be precise and concise.") -> dict[str, Any] | str:
|
41
|
+
"""
|
42
|
+
Sends a query to a Perplexity Sonar online model and returns the response.
|
43
|
+
|
44
|
+
This uses the chat completions endpoint, suitable for conversational queries
|
45
|
+
and leveraging Perplexity's online capabilities.
|
46
|
+
|
47
|
+
Args:
|
48
|
+
query: The user's query or message.
|
49
|
+
model: The specific Perplexity model to use (e.g., "r1-1776","sonar","sonar-pro","sonar-reasoning","sonar-reasoning-pro", "sonar-deep-research").Defaults to 'sonar'.
|
50
|
+
temperature: Sampling temperature for the response generation (e.g., 0.7).
|
51
|
+
system_prompt: An optional system message to guide the model's behavior.
|
52
|
+
|
53
|
+
Returns:
|
54
|
+
A dictionary containing 'content' (str) and 'citations' (list) on success, or a string containing an error message on failure.
|
55
|
+
"""
|
56
|
+
endpoint = f"{self.base_url}/chat/completions"
|
57
|
+
|
58
|
+
messages = []
|
59
|
+
if system_prompt:
|
60
|
+
messages.append({"role": "system", "content": system_prompt})
|
61
|
+
messages.append({"role": "user", "content": query})
|
62
|
+
|
63
|
+
payload = {
|
64
|
+
"model": model,
|
65
|
+
"messages": messages,
|
66
|
+
"temperature": temperature,
|
67
|
+
# "max_tokens": 512,
|
68
|
+
}
|
69
|
+
|
70
|
+
data = self._post(endpoint, data=payload)
|
71
|
+
response = data.json()
|
72
|
+
content = response['choices'][0]['message']['content']
|
73
|
+
citations = response.get('citations', [])
|
74
|
+
return {"content": content, "citations": citations}
|
75
|
+
|
76
|
+
def list_tools(self):
|
77
|
+
return [
|
78
|
+
self.chat,
|
79
|
+
]
|