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.
@@ -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 class with given integration and additional options.
10
+ Initializes a new instance of the Notion API app with a specified integration and additional parameters.
11
11
 
12
12
  Args:
13
- integration: An optional Integration instance to configure the connection. Defaults to None.
14
- **kwargs: Additional keyword arguments for configuration.
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 notion_retrieve_auser(self, id, request_body=None) -> dict[str, Any]:
34
+ def retrieve_a_user(self, id, request_body=None) -> dict[str, Any]:
35
35
  """
36
- Retrieves user information from the Notion API by user ID.
36
+ Retrieves user details from the server using the specified user ID.
37
37
 
38
38
  Args:
39
- id: The unique identifier of the user whose information is to be retrieved.
40
- request_body: A dictionary representing the request body, optional, default is None.
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 the user's information from the Notion API.
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 notion_list_all_users(self, ) -> dict[str, Any]:
54
+ def list_all_users(self, ) -> dict[str, Any]:
54
55
  """
55
- Fetches and returns a list of all users from the Notion API.
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 JSON response from the Notion API, representing all users data.
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 notion_retrieve_your_token_sbot_user(self, ) -> dict[str, Any]:
70
+ def retrieve_your_token_sbot_user(self, ) -> dict[str, Any]:
70
71
  """
71
- Retrieves the current user's token data from the Notion API.
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 the necessary configuration and authentication details for accessing the Notion API.
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 as retrieved from the Notion API.
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 notion_retrieve_adatabase(self, id) -> dict[str, Any]:
86
+ def retrieve_a_database(self, id) -> dict[str, Any]:
86
87
  """
87
- Retrieves a Notion database by its unique identifier.
88
+ Retrieves database details from a specified endpoint using the provided database ID.
88
89
 
89
90
  Args:
90
- id: A string representing the unique identifier of the Notion database to be retrieved.
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 retrieved Notion database.
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 notion_update_adatabase(self, id, request_body=None) -> dict[str, Any]:
104
+ def update_a_database(self, id, request_body=None) -> dict[str, Any]:
104
105
  """
105
- Updates a Notion database with the given ID and request body data.
106
+ Updates a database entry with the given ID using a PATCH request.
106
107
 
107
108
  Args:
108
- self: An instance of the class containing configuration and methods for HTTP requests.
109
- id: A string representing the unique identifier of the Notion database to be updated.
110
- request_body: An optional dictionary containing the fields and values to update in the database.
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 Notion API after updating the database.
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={}, params=query_params)
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 notion_query_adatabase(self, id, request_body=None) -> dict[str, Any]:
124
+ def query_a_database(self, id, request_body=None) -> dict[str, Any]:
124
125
  """
125
- Executes a query on a Notion database using the Notion API.
126
+ Executes a query on a specified database using an identifier and an optional request body.
126
127
 
127
128
  Args:
128
- self: Instance of the class which should contain 'base_url' and '_post' method.
129
- id: A string representing the unique identifier of the Notion database to query.
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 JSON response from the Notion API after querying the specified database.
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
- json_body = request_body if request_body is not None else None
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 notion_create_adatabase(self, request_body=None) -> dict[str, Any]:
143
+ def create_a_database(self, request_body=None) -> dict[str, Any]:
145
144
  """
146
- Creates a new database in Notion using the provided request body.
145
+ Creates a new database on the server using the specified request body.
147
146
 
148
147
  Args:
149
- self: Reference to the current instance of the class.
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 representing the JSON response from the Notion API, containing data about the newly created database.
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
- json_body = request_body if request_body is not None else None
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 notion_create_apage(self, request_body=None) -> dict[str, Any]:
159
+ def create_a_page(self, request_body=None) -> dict[str, Any]:
163
160
  """
164
- Creates a new page in Notion by sending a POST request with the specified request body.
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 create a new page in Notion. Defaults to None.
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 containing the JSON response from the Notion API with the details of the newly created page.
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
- json_body = request_body if request_body is not None else None
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 notion_retrieve_apage(self, id) -> dict[str, Any]:
175
+ def retrieve_a_page(self, id) -> dict[str, Any]:
180
176
  """
181
- Retrieves a page from the Notion API using a given page ID.
177
+ Retrieves a page by its unique identifier from a remote server.
182
178
 
183
179
  Args:
184
- id: The unique identifier of the Notion page to be retrieved.
180
+ id: The unique identifier of the page to retrieve.
185
181
 
186
182
  Returns:
187
- A dictionary containing the JSON response from the Notion API, representing the page data.
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 notion_update_page_properties(self, id, request_body=None) -> dict[str, Any]:
193
+ def update_page_properties(self, id, request_body=None) -> dict[str, Any]:
198
194
  """
199
- Updates the properties of a Notion page identified by a given ID.
195
+ Updates the properties of a page identified by its ID using the provided request body.
200
196
 
201
197
  Args:
202
- self: Instance of the class containing the Notion API credentials and methods.
203
- id: The unique identifier of the Notion page to update. Must not be None.
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 JSON response from the Notion API after the page update request.
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={}, params=query_params)
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 notion_retrieve_apage_property_item(self, page_id, property_id) -> dict[str, Any]:
212
+ def retrieve_a_page_property_item(self, page_id, property_id) -> dict[str, Any]:
218
213
  """
219
- Retrieves a specific property item from a page in Notion using the page and property IDs.
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 for the Notion page from which the property item should be retrieved.
223
- property_id: The unique identifier for the property item within the specified page that should be retrieved.
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 containing the JSON response from the Notion API representing the requested property item.
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 notion_retrieve_block_children(self, id, page_size=None) -> dict[str, Any]:
233
+ def retrieve_block_children(self, id, page_size=None) -> dict[str, Any]:
239
234
  """
240
- Retrieves the child blocks of a specified Notion block.
235
+ Retrieves the children of a specified block using its unique identifier.
241
236
 
242
237
  Args:
243
- self: The instance of the class this method belongs to.
244
- id: The unique identifier of the parent block whose children are to be retrieved.
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 JSON response with details about the child blocks.
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 notion_append_block_children(self, id, request_body=None) -> dict[str, Any]:
252
+ def append_block_children(self, id, request_body=None) -> dict[str, Any]:
259
253
  """
260
- Appends child blocks to a block in Notion using its API.
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 Notion API credentials and configuration.
264
- id: The unique identifier of the parent block to which child blocks are to be appended.
265
- request_body: An optional dictionary containing the block data to append. Defaults to None.
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 response from the Notion API, containing the result of the append operation.
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={}, params=query_params)
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 notion_retrieve_ablock(self, id) -> dict[str, Any]:
272
+ def retrieve_a_block(self, id) -> dict[str, Any]:
279
273
  """
280
- Retrieves a block from the Notion API using the specified block ID.
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 of the block to retrieve from the Notion API. Must be a non-null string.
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 block data in JSON format as retrieved from the Notion API.
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 notion_delete_ablock(self, id) -> dict[str, Any]:
290
+ def delete_a_block(self, id) -> dict[str, Any]:
297
291
  """
298
- Deletes a block from the Notion database using the specified block ID.
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 JSON response from the Notion API after attempting to delete the block.
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 notion_update_ablock(self, id, request_body=None) -> dict[str, Any]:
308
+ def update_a_block(self, id, request_body=None) -> dict[str, Any]:
315
309
  """
316
- Updates a block in Notion with the given ID and request body.
310
+ Updates a block by sending a PATCH request to the specified endpoint.
317
311
 
318
312
  Args:
319
- id: The unique identifier of the Notion block to update.
320
- request_body: The request body containing the updates to be made to the block. Defaults to None if not provided.
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 Notion API after the block has been updated.
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={}, params=query_params)
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 notion_search(self, request_body=None) -> dict[str, Any]:
327
+ def search(self, request_body=None) -> dict[str, Any]:
334
328
  """
335
- Executes a search request to the Notion API and returns the response in JSON format.
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 search parameters for the API request. Defaults to None if not provided.
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 Notion API in JSON format.
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
- json_body = request_body if request_body is not None else None
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 notion_retrieve_comments(self, block_id=None, page_size=None, request_body=None) -> dict[str, Any]:
343
+ def retrieve_comments(self, block_id=None, page_size=None, request_body=None) -> dict[str, Any]:
351
344
  """
352
- Retrieves comments from a Notion block using the Notion API.
345
+ Fetches comments from a remote server for a specified block, with optional pagination.
353
346
 
354
347
  Args:
355
- block_id: Optional; The ID of the block for which to retrieve comments. If None, it retrieves comments for all blocks available to the user.
356
- page_size: Optional; The maximum number of comments to retrieve in one request. If None, the default page size will be used.
357
- request_body: Optional; A dictionary to include additional parameters in the request body. If None, no extra parameters are added.
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 JSON response from the Notion API with the comments retrieved.
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 notion_add_comment_to_page(self, request_body=None) -> dict[str, Any]:
361
+ def add_comment_to_page(self, request_body=None) -> dict[str, Any]:
369
362
  """
370
- Adds a comment to a specified Notion page using the provided request body.
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: An optional dictionary containing the details of the comment to be added to the Notion page. If None, no data is sent in the request's 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 containing the response data from the Notion API, parsed from JSON format.
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
- json_body = request_body if request_body is not None else None
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 that interact with Notion's API for various operations.
379
+ Returns a list of functions related to user and database operations.
388
380
 
389
381
  Args:
390
- None: This method does not take any parameters.
382
+ self: The instance of the class to which the function belongs.
391
383
 
392
384
  Returns:
393
- A list of functions that perform specific operations with Notion's API, such as retrieving or updating users, databases, pages, blocks, and comments.
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.notion_retrieve_auser,
397
- self.notion_list_all_users,
398
- self.notion_retrieve_your_token_sbot_user,
399
- self.notion_retrieve_adatabase,
400
- self.notion_update_adatabase,
401
- self.notion_query_adatabase,
402
- self.notion_create_adatabase,
403
- self.notion_create_apage,
404
- self.notion_retrieve_apage,
405
- self.notion_update_page_properties,
406
- self.notion_retrieve_apage_property_item,
407
- self.notion_retrieve_block_children,
408
- self.notion_append_block_children,
409
- self.notion_retrieve_ablock,
410
- self.notion_delete_ablock,
411
- self.notion_update_ablock,
412
- self.notion_search,
413
- self.notion_retrieve_comments,
414
- self.notion_add_comment_to_page
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
+ ]