Skip to content

Commit 48ac815

Browse files
authored
Bugfix/Restore Requests Tool (FlowiseAI#2513)
restore requests tool
1 parent 2878af6 commit 48ac815

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { INode, INodeData, INodeParams } from '../../../src/Interface'
2+
import { getBaseClasses } from '../../../src/utils'
3+
import { desc, RequestParameters, RequestsGetTool } from './core'
4+
5+
class RequestsGet_Tools implements INode {
6+
label: string
7+
name: string
8+
version: number
9+
description: string
10+
type: string
11+
icon: string
12+
category: string
13+
baseClasses: string[]
14+
inputs: INodeParams[]
15+
16+
constructor() {
17+
this.label = 'Requests Get'
18+
this.name = 'requestsGet'
19+
this.version = 1.0
20+
this.type = 'RequestsGet'
21+
this.icon = 'requestsget.svg'
22+
this.category = 'Tools'
23+
this.description = 'Execute HTTP GET requests'
24+
this.baseClasses = [this.type, ...getBaseClasses(RequestsGetTool)]
25+
this.inputs = [
26+
{
27+
label: 'URL',
28+
name: 'url',
29+
type: 'string',
30+
description:
31+
'Agent will make call to this exact URL. If not specified, agent will try to figure out itself from AIPlugin if provided',
32+
additionalParams: true,
33+
optional: true
34+
},
35+
{
36+
label: 'Description',
37+
name: 'description',
38+
type: 'string',
39+
rows: 4,
40+
default: desc,
41+
description: 'Acts like a prompt to tell agent when it should use this tool',
42+
additionalParams: true,
43+
optional: true
44+
},
45+
{
46+
label: 'Headers',
47+
name: 'headers',
48+
type: 'json',
49+
additionalParams: true,
50+
optional: true
51+
}
52+
]
53+
}
54+
55+
async init(nodeData: INodeData): Promise<any> {
56+
const headers = nodeData.inputs?.headers as string
57+
const url = nodeData.inputs?.url as string
58+
const description = nodeData.inputs?.description as string
59+
60+
const obj: RequestParameters = {}
61+
if (url) obj.url = url
62+
if (description) obj.description = description
63+
if (headers) {
64+
const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(headers)
65+
obj.headers = parsedHeaders
66+
}
67+
68+
return new RequestsGetTool(obj)
69+
}
70+
}
71+
72+
module.exports = { nodeClass: RequestsGet_Tools }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import fetch from 'node-fetch'
2+
import { Tool } from '@langchain/core/tools'
3+
4+
export const desc = `A portal to the internet. Use this when you need to get specific content from a website.
5+
Input should be a url (i.e. https://www.google.com). The output will be the text response of the GET request.`
6+
7+
export interface Headers {
8+
[key: string]: string
9+
}
10+
11+
export interface RequestParameters {
12+
headers?: Headers
13+
url?: string
14+
description?: string
15+
maxOutputLength?: number
16+
}
17+
18+
export class RequestsGetTool extends Tool {
19+
name = 'requests_get'
20+
url = ''
21+
description = desc
22+
maxOutputLength = 2000
23+
headers = {}
24+
25+
constructor(args?: RequestParameters) {
26+
super()
27+
this.url = args?.url ?? this.url
28+
this.headers = args?.headers ?? this.headers
29+
this.description = args?.description ?? this.description
30+
this.maxOutputLength = args?.maxOutputLength ?? this.maxOutputLength
31+
}
32+
33+
/** @ignore */
34+
async _call(input: string) {
35+
const inputUrl = !this.url ? input : this.url
36+
37+
if (process.env.DEBUG === 'true') console.info(`Making GET API call to ${inputUrl}`)
38+
39+
const res = await fetch(inputUrl, {
40+
headers: this.headers
41+
})
42+
43+
const text = await res.text()
44+
return text.slice(0, this.maxOutputLength)
45+
}
46+
}
Lines changed: 6 additions & 0 deletions
Loading
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { INode, INodeData, INodeParams } from '../../../src/Interface'
2+
import { getBaseClasses } from '../../../src/utils'
3+
import { RequestParameters, desc, RequestsPostTool } from './core'
4+
5+
class RequestsPost_Tools implements INode {
6+
label: string
7+
name: string
8+
version: number
9+
description: string
10+
type: string
11+
icon: string
12+
category: string
13+
baseClasses: string[]
14+
inputs: INodeParams[]
15+
16+
constructor() {
17+
this.label = 'Requests Post'
18+
this.name = 'requestsPost'
19+
this.version = 1.0
20+
this.type = 'RequestsPost'
21+
this.icon = 'requestspost.svg'
22+
this.category = 'Tools'
23+
this.description = 'Execute HTTP POST requests'
24+
this.baseClasses = [this.type, ...getBaseClasses(RequestsPostTool)]
25+
this.inputs = [
26+
{
27+
label: 'URL',
28+
name: 'url',
29+
type: 'string',
30+
description:
31+
'Agent will make call to this exact URL. If not specified, agent will try to figure out itself from AIPlugin if provided',
32+
additionalParams: true,
33+
optional: true
34+
},
35+
{
36+
label: 'Body',
37+
name: 'body',
38+
type: 'json',
39+
description:
40+
'JSON body for the POST request. If not specified, agent will try to figure out itself from AIPlugin if provided',
41+
additionalParams: true,
42+
optional: true
43+
},
44+
{
45+
label: 'Description',
46+
name: 'description',
47+
type: 'string',
48+
rows: 4,
49+
default: desc,
50+
description: 'Acts like a prompt to tell agent when it should use this tool',
51+
additionalParams: true,
52+
optional: true
53+
},
54+
{
55+
label: 'Headers',
56+
name: 'headers',
57+
type: 'json',
58+
additionalParams: true,
59+
optional: true
60+
}
61+
]
62+
}
63+
64+
async init(nodeData: INodeData): Promise<any> {
65+
const headers = nodeData.inputs?.headers as string
66+
const url = nodeData.inputs?.url as string
67+
const description = nodeData.inputs?.description as string
68+
const body = nodeData.inputs?.body as string
69+
70+
const obj: RequestParameters = {}
71+
if (url) obj.url = url
72+
if (description) obj.description = description
73+
if (headers) {
74+
const parsedHeaders = typeof headers === 'object' ? headers : JSON.parse(headers)
75+
obj.headers = parsedHeaders
76+
}
77+
if (body) {
78+
const parsedBody = typeof body === 'object' ? body : JSON.parse(body)
79+
obj.body = parsedBody
80+
}
81+
82+
return new RequestsPostTool(obj)
83+
}
84+
}
85+
86+
module.exports = { nodeClass: RequestsPost_Tools }
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Tool } from '@langchain/core/tools'
2+
import fetch from 'node-fetch'
3+
4+
export const desc = `Use this when you want to POST to a website.
5+
Input should be a json string with two keys: "url" and "data".
6+
The value of "url" should be a string, and the value of "data" should be a dictionary of
7+
key-value pairs you want to POST to the url as a JSON body.
8+
Be careful to always use double quotes for strings in the json string
9+
The output will be the text response of the POST request.`
10+
11+
export interface Headers {
12+
[key: string]: string
13+
}
14+
15+
export interface Body {
16+
[key: string]: any
17+
}
18+
19+
export interface RequestParameters {
20+
headers?: Headers
21+
body?: Body
22+
url?: string
23+
description?: string
24+
maxOutputLength?: number
25+
}
26+
27+
export class RequestsPostTool extends Tool {
28+
name = 'requests_post'
29+
url = ''
30+
description = desc
31+
maxOutputLength = Infinity
32+
headers = {}
33+
body = {}
34+
35+
constructor(args?: RequestParameters) {
36+
super()
37+
this.url = args?.url ?? this.url
38+
this.headers = args?.headers ?? this.headers
39+
this.body = args?.body ?? this.body
40+
this.description = args?.description ?? this.description
41+
this.maxOutputLength = args?.maxOutputLength ?? this.maxOutputLength
42+
}
43+
44+
/** @ignore */
45+
async _call(input: string) {
46+
try {
47+
let inputUrl = ''
48+
let inputBody = {}
49+
if (Object.keys(this.body).length || this.url) {
50+
if (this.url) inputUrl = this.url
51+
if (Object.keys(this.body).length) inputBody = this.body
52+
} else {
53+
const { url, data } = JSON.parse(input)
54+
inputUrl = url
55+
inputBody = data
56+
}
57+
58+
if (process.env.DEBUG === 'true') console.info(`Making POST API call to ${inputUrl} with body ${JSON.stringify(inputBody)}`)
59+
60+
const res = await fetch(inputUrl, {
61+
method: 'POST',
62+
headers: this.headers,
63+
body: JSON.stringify(inputBody)
64+
})
65+
66+
const text = await res.text()
67+
return text.slice(0, this.maxOutputLength)
68+
} catch (error) {
69+
return `${error}`
70+
}
71+
}
72+
}
Lines changed: 7 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)