@@ -195,6 +195,210 @@ Want to learn more about our AI-powered search technology? Visit our [main websi
195
195
196
196
## Advanced Usage
197
197
198
+ ### Request Helper Function
199
+
200
+ The ` getSearchScraperRequest ` function helps create properly formatted request objects for the SearchScraper service:
201
+
202
+ ``` javascript
203
+ import { getSearchScraperRequest } from ' scrapegraph-js' ;
204
+
205
+ const request = getSearchScraperRequest ({
206
+ userPrompt: " What is the latest version of Python and its main features?" ,
207
+ outputSchema: {
208
+ version: { type: " string" },
209
+ release_date: { type: " string" },
210
+ major_features: { type: " array" , items: { type: " string" } }
211
+ }
212
+ });
213
+ ```
214
+
215
+ #### Parameters
216
+
217
+ | Parameter | Type | Required | Description |
218
+ | -----------| ------| ----------| -------------|
219
+ | userPrompt | string | Yes | The search query or question to answer. |
220
+ | headers | object | No | Custom headers for the request. |
221
+ | outputSchema | object | No | Schema defining the structure of the search results. |
222
+
223
+ #### Return Value
224
+
225
+ Returns an object with the following structure:
226
+
227
+ ``` typescript
228
+ {
229
+ request_id : string ;
230
+ status : " queued" | " processing" | " completed" | " failed" ;
231
+ user_prompt : string ;
232
+ result ?: object | null ;
233
+ reference_urls : string [];
234
+ error : string ;
235
+ }
236
+ ```
237
+
238
+ #### Error Handling
239
+
240
+ The function includes comprehensive error handling:
241
+
242
+ ``` javascript
243
+ try {
244
+ const request = getSearchScraperRequest ({
245
+ userPrompt: " What are the latest AI chip developments?" ,
246
+ outputSchema: {
247
+ manufacturers: { type: " array" },
248
+ technologies: { type: " object" }
249
+ }
250
+ });
251
+ } catch (error) {
252
+ if (error .code === ' INVALID_PROMPT' ) {
253
+ console .error (' The search prompt is invalid or empty' );
254
+ } else if (error .code === ' SCHEMA_VALIDATION' ) {
255
+ console .error (' The output schema is invalid:' , error .details );
256
+ } else if (error .code === ' MISSING_REQUIRED' ) {
257
+ console .error (' Required parameters are missing' );
258
+ } else {
259
+ console .error (' An unexpected error occurred:' , error);
260
+ }
261
+ }
262
+ ```
263
+
264
+ #### Advanced Examples
265
+
266
+ ##### Complex Search Queries
267
+
268
+ ``` javascript
269
+ const request = getSearchScraperRequest ({
270
+ userPrompt: " Compare the top 3 cloud providers (AWS, Azure, GCP) focusing on ML services pricing and features" ,
271
+ outputSchema: {
272
+ providers: {
273
+ type: " array" ,
274
+ items: {
275
+ type: " object" ,
276
+ properties: {
277
+ name: { type: " string" },
278
+ ml_services: {
279
+ type: " array" ,
280
+ items: {
281
+ type: " object" ,
282
+ properties: {
283
+ name: { type: " string" },
284
+ pricing: { type: " string" },
285
+ features: { type: " array" , items: { type: " string" } }
286
+ }
287
+ }
288
+ }
289
+ }
290
+ }
291
+ },
292
+ comparison_matrix: { type: " object" },
293
+ recommendation: { type: " string" }
294
+ }
295
+ });
296
+ ```
297
+
298
+ ##### Time-Sensitive Searches
299
+
300
+ ``` javascript
301
+ const request = getSearchScraperRequest ({
302
+ userPrompt: " Latest cryptocurrency market trends in the past 24 hours" ,
303
+ headers: {
304
+ // Headers for real-time data sources
305
+ " Cache-Control" : " no-cache" ,
306
+ " Pragma" : " no-cache"
307
+ },
308
+ outputSchema: {
309
+ timestamp: { type: " string" },
310
+ trends: { type: " array" },
311
+ market_summary: { type: " object" }
312
+ }
313
+ });
314
+ ```
315
+
316
+ #### Best Practices
317
+
318
+ 1 . ** Query Optimization**
319
+ - Be specific and clear in your prompts
320
+ - Include relevant context
321
+ - Use appropriate keywords
322
+
323
+ ``` javascript
324
+ // Good prompt example
325
+ const request = getSearchScraperRequest ({
326
+ userPrompt: " Compare iPhone 15 Pro Max and Samsung S24 Ultra specifications, focusing on camera capabilities, battery life, and performance benchmarks"
327
+ });
328
+
329
+ // Less effective prompt
330
+ const badRequest = getSearchScraperRequest ({
331
+ userPrompt: " Compare phones" // Too vague
332
+ });
333
+ ```
334
+
335
+ 2 . ** Schema Design**
336
+ - Start with essential fields
337
+ - Use appropriate data types
338
+ - Include field descriptions
339
+ - Handle nested data properly
340
+
341
+ ``` javascript
342
+ const schema = {
343
+ comparison: {
344
+ type: " object" ,
345
+ properties: {
346
+ date: { type: " string" , description: " Comparison date" },
347
+ devices: {
348
+ type: " array" ,
349
+ items: {
350
+ type: " object" ,
351
+ properties: {
352
+ name: { type: " string" },
353
+ specs: { type: " object" },
354
+ pros: { type: " array" },
355
+ cons: { type: " array" }
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
361
+ };
362
+ ```
363
+
364
+ 3 . ** Error Recovery**
365
+ - Implement retry logic
366
+ - Handle rate limits
367
+ - Cache results when appropriate
368
+
369
+ ``` javascript
370
+ import { getSearchScraperRequest , retry } from ' scrapegraph-js' ;
371
+
372
+ const searchWithRetry = retry (async (prompt ) => {
373
+ const request = await getSearchScraperRequest ({
374
+ userPrompt: prompt
375
+ });
376
+ return request;
377
+ }, {
378
+ retries: 3 ,
379
+ backoff: {
380
+ initial: 1000 ,
381
+ multiplier: 2 ,
382
+ maxDelay: 10000
383
+ }
384
+ });
385
+ ```
386
+
387
+ 4 . ** Performance Optimization**
388
+ - Use caching for repeated searches
389
+ - Batch related queries
390
+ - Monitor API usage
391
+
392
+ ``` javascript
393
+ import { cache } from ' scrapegraph-js/utils' ;
394
+
395
+ const cachedSearch = cache (getSearchScraperRequest, {
396
+ ttl: 1800 , // Cache for 30 minutes
397
+ maxSize: 50 , // Cache up to 50 requests
398
+ keyGenerator : (params ) => params .userPrompt // Cache key based on prompt
399
+ });
400
+ ```
401
+
198
402
### Custom Schema Example
199
403
200
404
Define exactly what data you want to extract using Pydantic or Zod:
0 commit comments