-
Notifications
You must be signed in to change notification settings - Fork 0
/
firecrawl.tool.js
51 lines (44 loc) · 1.81 KB
/
firecrawl.tool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Tool } from '@langchain/core/tools';
import { z } from 'zod';
export class FirecrawlTool extends Tool {
constructor(fields) {
super(fields);
this.apiKey = fields.apiKey;
this.name = 'firecrawl';
this.description =
'Fetches web content from a specified URL using the Firecrawl API. Input should be a JSON object with a "url".';
// Define the input schema using Zod
this.schema = z.object({
url: z.string().describe('The URL to scrape and retrieve content from.'),
});
}
async _call(input) {
try {
// Fetch content from the Firecrawl API. The API requires an API key for authorization.
// We send a POST request with the URL to scrape and the desired output format, which is markdown in this case.
const response = await fetch('https://api.firecrawl.dev/v1/scrape', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify({
url: input.url,
formats: ['markdown'],
}),
});
if (!response.ok) {
throw new Error(`Error fetching data from Firecrawl: ${response.statusText}`);
}
// Parse the JSON response
const jsonData = await response.json();
// Extract the markdown content from the response
const markdownContent = jsonData?.data?.markdown;
// Return the markdown content directly
return markdownContent;
} catch (error) {
console.error('Error running the FirecrawlTool:', error);
throw error;
}
}
}