2
2
import os
3
3
from typing import Optional , Type
4
4
5
- from aioify import aioify
5
+ from asyncer import asyncify
6
6
7
7
from lagent .actions .base_action import AsyncActionMixin , BaseAction , tool_api
8
8
from lagent .schema import ActionReturn , ActionStatusCode
@@ -31,7 +31,8 @@ def __init__(
31
31
if api_key is None :
32
32
raise ValueError (
33
33
'Please set Serper API key either in the environment '
34
- 'as SERPER_API_KEY or pass it as `api_key` parameter.' )
34
+ 'as SERPER_API_KEY or pass it as `api_key` parameter.'
35
+ )
35
36
self .api_key = api_key
36
37
37
38
@tool_api (explode_return = True )
@@ -78,6 +79,7 @@ def search_google_scholar(
78
79
- pub_info: publication information of selected papers
79
80
"""
80
81
from serpapi import GoogleSearch
82
+
81
83
params = {
82
84
'q' : query ,
83
85
'engine' : 'google_scholar' ,
@@ -94,7 +96,7 @@ def search_google_scholar(
94
96
'as_sdt' : as_sdt ,
95
97
'safe' : safe ,
96
98
'filter' : filter ,
97
- 'as_vis' : as_vis
99
+ 'as_vis' : as_vis ,
98
100
}
99
101
search = GoogleSearch (params )
100
102
try :
@@ -112,27 +114,24 @@ def search_google_scholar(
112
114
cited_by .append (citation ['total' ])
113
115
snippets .append (item ['snippet' ])
114
116
organic_id .append (item ['result_id' ])
115
- return dict (
116
- title = title ,
117
- cited_by = cited_by ,
118
- organic_id = organic_id ,
119
- snippets = snippets )
117
+ return dict (title = title , cited_by = cited_by , organic_id = organic_id , snippets = snippets )
120
118
except Exception as e :
121
- return ActionReturn (
122
- errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
119
+ return ActionReturn (errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
123
120
124
121
@tool_api (explode_return = True )
125
- def get_author_information (self ,
126
- author_id : str ,
127
- hl : Optional [str ] = None ,
128
- view_op : Optional [str ] = None ,
129
- sort : Optional [str ] = None ,
130
- citation_id : Optional [str ] = None ,
131
- start : Optional [int ] = None ,
132
- num : Optional [int ] = None ,
133
- no_cache : Optional [bool ] = None ,
134
- async_req : Optional [bool ] = None ,
135
- output : Optional [str ] = None ) -> dict :
122
+ def get_author_information (
123
+ self ,
124
+ author_id : str ,
125
+ hl : Optional [str ] = None ,
126
+ view_op : Optional [str ] = None ,
127
+ sort : Optional [str ] = None ,
128
+ citation_id : Optional [str ] = None ,
129
+ start : Optional [int ] = None ,
130
+ num : Optional [int ] = None ,
131
+ no_cache : Optional [bool ] = None ,
132
+ async_req : Optional [bool ] = None ,
133
+ output : Optional [str ] = None ,
134
+ ) -> dict :
136
135
"""Search for an author's information by author's id provided by get_author_id.
137
136
138
137
Args:
@@ -155,6 +154,7 @@ def get_author_information(self,
155
154
* website: the author's homepage url
156
155
"""
157
156
from serpapi import GoogleSearch
157
+
158
158
params = {
159
159
'engine' : 'google_scholar_author' ,
160
160
'author_id' : author_id ,
@@ -167,7 +167,7 @@ def get_author_information(self,
167
167
'num' : num ,
168
168
'no_cache' : no_cache ,
169
169
'async' : async_req ,
170
- 'output' : output
170
+ 'output' : output ,
171
171
}
172
172
try :
173
173
search = GoogleSearch (params )
@@ -178,20 +178,19 @@ def get_author_information(self,
178
178
name = author ['name' ],
179
179
affiliations = author .get ('affiliations' , '' ),
180
180
website = author .get ('website' , '' ),
181
- articles = [
182
- dict (title = article ['title' ], authors = article ['authors' ])
183
- for article in articles [:3 ]
184
- ])
181
+ articles = [dict (title = article ['title' ], authors = article ['authors' ]) for article in articles [:3 ]],
182
+ )
185
183
except Exception as e :
186
- return ActionReturn (
187
- errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
184
+ return ActionReturn (errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
188
185
189
186
@tool_api (explode_return = True )
190
- def get_citation_format (self ,
191
- q : str ,
192
- no_cache : Optional [bool ] = None ,
193
- async_ : Optional [bool ] = None ,
194
- output : Optional [str ] = 'json' ) -> dict :
187
+ def get_citation_format (
188
+ self ,
189
+ q : str ,
190
+ no_cache : Optional [bool ] = None ,
191
+ async_ : Optional [bool ] = None ,
192
+ output : Optional [str ] = 'json' ,
193
+ ) -> dict :
195
194
"""Function to get MLA citation format by an identification of organic_result's id provided by search_google_scholar.
196
195
197
196
Args:
@@ -206,13 +205,14 @@ def get_citation_format(self,
206
205
* citation: the citation format of the article
207
206
"""
208
207
from serpapi import GoogleSearch
208
+
209
209
params = {
210
210
'q' : q ,
211
211
'engine' : 'google_scholar_cite' ,
212
212
'api_key' : self .api_key ,
213
213
'no_cache' : no_cache ,
214
214
'async' : async_ ,
215
- 'output' : output
215
+ 'output' : output ,
216
216
}
217
217
try :
218
218
search = GoogleSearch (params )
@@ -221,18 +221,19 @@ def get_citation_format(self,
221
221
citation_info = citation [0 ]['snippet' ]
222
222
return citation_info
223
223
except Exception as e :
224
- return ActionReturn (
225
- errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
224
+ return ActionReturn (errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
226
225
227
226
@tool_api (explode_return = True )
228
- def get_author_id (self ,
229
- mauthors : str ,
230
- hl : Optional [str ] = 'en' ,
231
- after_author : Optional [str ] = None ,
232
- before_author : Optional [str ] = None ,
233
- no_cache : Optional [bool ] = False ,
234
- _async : Optional [bool ] = False ,
235
- output : Optional [str ] = 'json' ) -> dict :
227
+ def get_author_id (
228
+ self ,
229
+ mauthors : str ,
230
+ hl : Optional [str ] = 'en' ,
231
+ after_author : Optional [str ] = None ,
232
+ before_author : Optional [str ] = None ,
233
+ no_cache : Optional [bool ] = False ,
234
+ _async : Optional [bool ] = False ,
235
+ output : Optional [str ] = 'json' ,
236
+ ) -> dict :
236
237
"""The getAuthorId function is used to get the author's id by his or her name.
237
238
238
239
Args:
@@ -249,6 +250,7 @@ def get_author_id(self,
249
250
* author_id: the author_id of the author
250
251
"""
251
252
from serpapi import GoogleSearch
253
+
252
254
params = {
253
255
'mauthors' : mauthors ,
254
256
'engine' : 'google_scholar_profiles' ,
@@ -258,7 +260,7 @@ def get_author_id(self,
258
260
'before_author' : before_author ,
259
261
'no_cache' : no_cache ,
260
262
'async' : _async ,
261
- 'output' : output
263
+ 'output' : output ,
262
264
}
263
265
try :
264
266
search = GoogleSearch (params )
@@ -267,8 +269,7 @@ def get_author_id(self,
267
269
author_info = dict (author_id = profile [0 ]['author_id' ])
268
270
return author_info
269
271
except Exception as e :
270
- return ActionReturn (
271
- errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
272
+ return ActionReturn (errmsg = str (e ), state = ActionStatusCode .HTTP_ERROR )
272
273
273
274
274
275
class AsyncGoogleScholar (AsyncActionMixin , GoogleScholar ):
@@ -283,7 +284,7 @@ class AsyncGoogleScholar(AsyncActionMixin, GoogleScholar):
283
284
"""
284
285
285
286
@tool_api (explode_return = True )
286
- @aioify
287
+ @asyncify
287
288
def search_google_scholar (
288
289
self ,
289
290
query : str ,
@@ -326,23 +327,38 @@ def search_google_scholar(
326
327
- organic_id: a list of the organic results' ids of the three selected papers
327
328
- pub_info: publication information of selected papers
328
329
"""
329
- return super ().search_google_scholar (query , cites , as_ylo , as_yhi ,
330
- scisbd , cluster , hl , lr , start ,
331
- num , as_sdt , safe , filter , as_vis )
330
+ return super ().search_google_scholar (
331
+ query ,
332
+ cites ,
333
+ as_ylo ,
334
+ as_yhi ,
335
+ scisbd ,
336
+ cluster ,
337
+ hl ,
338
+ lr ,
339
+ start ,
340
+ num ,
341
+ as_sdt ,
342
+ safe ,
343
+ filter ,
344
+ as_vis ,
345
+ )
332
346
333
347
@tool_api (explode_return = True )
334
- @aioify
335
- def get_author_information (self ,
336
- author_id : str ,
337
- hl : Optional [str ] = None ,
338
- view_op : Optional [str ] = None ,
339
- sort : Optional [str ] = None ,
340
- citation_id : Optional [str ] = None ,
341
- start : Optional [int ] = None ,
342
- num : Optional [int ] = None ,
343
- no_cache : Optional [bool ] = None ,
344
- async_req : Optional [bool ] = None ,
345
- output : Optional [str ] = None ) -> dict :
348
+ @asyncify
349
+ def get_author_information (
350
+ self ,
351
+ author_id : str ,
352
+ hl : Optional [str ] = None ,
353
+ view_op : Optional [str ] = None ,
354
+ sort : Optional [str ] = None ,
355
+ citation_id : Optional [str ] = None ,
356
+ start : Optional [int ] = None ,
357
+ num : Optional [int ] = None ,
358
+ no_cache : Optional [bool ] = None ,
359
+ async_req : Optional [bool ] = None ,
360
+ output : Optional [str ] = None ,
361
+ ) -> dict :
346
362
"""Search for an author's information by author's id provided by get_author_id.
347
363
348
364
Args:
@@ -364,17 +380,19 @@ def get_author_information(self,
364
380
* articles: at most 3 articles by the author
365
381
* website: the author's homepage url
366
382
"""
367
- return super ().get_author_information (author_id , hl , view_op , sort ,
368
- citation_id , start , num ,
369
- no_cache , async_req , output )
383
+ return super ().get_author_information (
384
+ author_id , hl , view_op , sort , citation_id , start , num , no_cache , async_req , output
385
+ )
370
386
371
387
@tool_api (explode_return = True )
372
- @aioify
373
- def get_citation_format (self ,
374
- q : str ,
375
- no_cache : Optional [bool ] = None ,
376
- async_ : Optional [bool ] = None ,
377
- output : Optional [str ] = 'json' ) -> dict :
388
+ @asyncify
389
+ def get_citation_format (
390
+ self ,
391
+ q : str ,
392
+ no_cache : Optional [bool ] = None ,
393
+ async_ : Optional [bool ] = None ,
394
+ output : Optional [str ] = 'json' ,
395
+ ) -> dict :
378
396
"""Function to get MLA citation format by an identification of organic_result's id provided by search_google_scholar.
379
397
380
398
Args:
@@ -391,15 +409,17 @@ def get_citation_format(self,
391
409
return super ().get_citation_format (q , no_cache , async_ , output )
392
410
393
411
@tool_api (explode_return = True )
394
- @aioify
395
- def get_author_id (self ,
396
- mauthors : str ,
397
- hl : Optional [str ] = 'en' ,
398
- after_author : Optional [str ] = None ,
399
- before_author : Optional [str ] = None ,
400
- no_cache : Optional [bool ] = False ,
401
- _async : Optional [bool ] = False ,
402
- output : Optional [str ] = 'json' ) -> dict :
412
+ @asyncify
413
+ def get_author_id (
414
+ self ,
415
+ mauthors : str ,
416
+ hl : Optional [str ] = 'en' ,
417
+ after_author : Optional [str ] = None ,
418
+ before_author : Optional [str ] = None ,
419
+ no_cache : Optional [bool ] = False ,
420
+ _async : Optional [bool ] = False ,
421
+ output : Optional [str ] = 'json' ,
422
+ ) -> dict :
403
423
"""The getAuthorId function is used to get the author's id by his or her name.
404
424
405
425
Args:
@@ -415,5 +435,4 @@ def get_author_id(self,
415
435
:class:`dict`: author id
416
436
* author_id: the author_id of the author
417
437
"""
418
- return super ().get_author_id (mauthors , hl , after_author , before_author ,
419
- no_cache , _async , output )
438
+ return super ().get_author_id (mauthors , hl , after_author , before_author , no_cache , _async , output )
0 commit comments