Skip to content

Commit 276c30e

Browse files
committed
add js
1 parent 5f3ccf2 commit 276c30e

File tree

7 files changed

+1104
-0
lines changed

7 files changed

+1104
-0
lines changed

scrapegraph-js/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Official JavaScript/TypeScript SDK for the ScrapeGraph AI API - Smart web scrapi
1515
- 🔍 Detailed error handling
1616
- ⚡ Automatic retries and logging
1717
- 🔐 Secure API authentication
18+
- 🔧 AI-powered schema generation
1819

1920
## 📦 Installation
2021

@@ -395,6 +396,108 @@ const feedbackText = 'This is a test feedback message.';
395396
})();
396397
```
397398

399+
### AI-Powered Schema Generation
400+
401+
Generate JSON schemas from natural language prompts using AI. This feature helps you create structured data schemas for web scraping and data extraction.
402+
403+
#### Basic Schema Generation
404+
405+
```javascript
406+
import { generateSchema } from 'scrapegraph-js';
407+
408+
const apiKey = 'your-api-key';
409+
const prompt = 'Find laptops with specifications like brand, processor, RAM, storage, and price';
410+
411+
(async () => {
412+
try {
413+
const response = await generateSchema(prompt, null, { apiKey });
414+
console.log('Generated schema:', response.generated_schema);
415+
console.log('Request ID:', response.request_id);
416+
} catch (error) {
417+
console.error('Error generating schema:', error);
418+
}
419+
})();
420+
```
421+
422+
#### Modifying Existing Schemas
423+
424+
```javascript
425+
import { generateSchema } from 'scrapegraph-js';
426+
427+
const apiKey = 'your-api-key';
428+
const existingSchema = {
429+
type: 'object',
430+
properties: {
431+
name: { type: 'string' },
432+
price: { type: 'number' }
433+
},
434+
required: ['name', 'price']
435+
};
436+
437+
const modificationPrompt = 'Add brand and rating fields to the existing schema';
438+
439+
(async () => {
440+
try {
441+
const response = await generateSchema(modificationPrompt, existingSchema, { apiKey });
442+
console.log('Modified schema:', response.generated_schema);
443+
} catch (error) {
444+
console.error('Error modifying schema:', error);
445+
}
446+
})();
447+
```
448+
449+
#### Checking Schema Generation Status
450+
451+
```javascript
452+
import { getSchemaStatus } from 'scrapegraph-js';
453+
454+
const apiKey = 'your-api-key';
455+
const requestId = '123e4567-e89b-12d3-a456-426614174000';
456+
457+
(async () => {
458+
try {
459+
const response = await getSchemaStatus(requestId, { apiKey });
460+
console.log('Status:', response.status);
461+
if (response.status === 'completed') {
462+
console.log('Generated schema:', response.generated_schema);
463+
}
464+
} catch (error) {
465+
console.error('Error checking status:', error);
466+
}
467+
})();
468+
```
469+
470+
#### Polling for Completion with Progress Tracking
471+
472+
```javascript
473+
import { pollSchemaGeneration } from 'scrapegraph-js';
474+
475+
const apiKey = 'your-api-key';
476+
const requestId = '123e4567-e89b-12d3-a456-426614174000';
477+
478+
(async () => {
479+
try {
480+
const finalResult = await pollSchemaGeneration(requestId, {
481+
apiKey,
482+
maxAttempts: 15,
483+
delay: 3000,
484+
onProgress: ({ attempt, maxAttempts, status, response }) => {
485+
if (status === 'checking') {
486+
console.log(`Checking status... (${attempt}/${maxAttempts})`);
487+
} else {
488+
console.log(`Status: ${status} (${attempt}/${maxAttempts})`);
489+
}
490+
}
491+
});
492+
493+
console.log('Schema generation completed!');
494+
console.log('Final schema:', finalResult.generated_schema);
495+
} catch (error) {
496+
console.error('Error during polling:', error);
497+
}
498+
})();
499+
```
500+
398501
## 📚 Documentation
399502

400503
For detailed documentation, visit [docs.scrapegraphai.com](https://docs.scrapegraphai.com)

0 commit comments

Comments
 (0)