forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOSTApiChain.ts
128 lines (116 loc) · 5.08 KB
/
POSTApiChain.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { BaseLanguageModel } from '@langchain/core/language_models/base'
import { PromptTemplate } from '@langchain/core/prompts'
import { API_RESPONSE_RAW_PROMPT_TEMPLATE, API_URL_RAW_PROMPT_TEMPLATE, APIChain } from './postCore'
import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from '../../../src/handler'
import { ICommonObject, INode, INodeData, INodeParams, IServerSideEventStreamer } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
class POSTApiChain_Chains implements INode {
label: string
name: string
version: number
type: string
icon: string
category: string
baseClasses: string[]
description: string
inputs: INodeParams[]
constructor() {
this.label = 'POST API Chain'
this.name = 'postApiChain'
this.version = 1.0
this.type = 'POSTApiChain'
this.icon = 'post.svg'
this.category = 'Chains'
this.description = 'Chain to run queries against POST API'
this.baseClasses = [this.type, ...getBaseClasses(APIChain)]
this.inputs = [
{
label: 'Language Model',
name: 'model',
type: 'BaseLanguageModel'
},
{
label: 'API Documentation',
name: 'apiDocs',
type: 'string',
description:
'Description of how API works. Please refer to more <a target="_blank" href="https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/chains/api/open_meteo_docs.py">examples</a>',
rows: 4
},
{
label: 'Headers',
name: 'headers',
type: 'json',
additionalParams: true,
optional: true
},
{
label: 'URL Prompt',
name: 'urlPrompt',
type: 'string',
description: 'Prompt used to tell LLMs how to construct the URL. Must contains {api_docs} and {question}',
default: API_URL_RAW_PROMPT_TEMPLATE,
rows: 4,
additionalParams: true
},
{
label: 'Answer Prompt',
name: 'ansPrompt',
type: 'string',
description:
'Prompt used to tell LLMs how to return the API response. Must contains {api_response}, {api_url}, and {question}',
default: API_RESPONSE_RAW_PROMPT_TEMPLATE,
rows: 4,
additionalParams: true
}
]
}
async init(nodeData: INodeData): Promise<any> {
const model = nodeData.inputs?.model as BaseLanguageModel
const apiDocs = nodeData.inputs?.apiDocs as string
const headers = nodeData.inputs?.headers as string
const urlPrompt = nodeData.inputs?.urlPrompt as string
const ansPrompt = nodeData.inputs?.ansPrompt as string
const chain = await getAPIChain(apiDocs, model, headers, urlPrompt, ansPrompt)
return chain
}
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
const model = nodeData.inputs?.model as BaseLanguageModel
const apiDocs = nodeData.inputs?.apiDocs as string
const headers = nodeData.inputs?.headers as string
const urlPrompt = nodeData.inputs?.urlPrompt as string
const ansPrompt = nodeData.inputs?.ansPrompt as string
const chain = await getAPIChain(apiDocs, model, headers, urlPrompt, ansPrompt)
const loggerHandler = new ConsoleCallbackHandler(options.logger)
const callbacks = await additionalCallbacks(nodeData, options)
const shouldStreamResponse = options.shouldStreamResponse
const sseStreamer: IServerSideEventStreamer = options.sseStreamer as IServerSideEventStreamer
const chatId = options.chatId
if (shouldStreamResponse) {
const handler = new CustomChainHandler(sseStreamer, chatId)
const res = await chain.run(input, [loggerHandler, handler, ...callbacks])
return res
} else {
const res = await chain.run(input, [loggerHandler, ...callbacks])
return res
}
}
}
const getAPIChain = async (documents: string, llm: BaseLanguageModel, headers: string, urlPrompt: string, ansPrompt: string) => {
const apiUrlPrompt = new PromptTemplate({
inputVariables: ['api_docs', 'question'],
template: urlPrompt ? urlPrompt : API_URL_RAW_PROMPT_TEMPLATE
})
const apiResponsePrompt = new PromptTemplate({
inputVariables: ['api_docs', 'question', 'api_url_body', 'api_response'],
template: ansPrompt ? ansPrompt : API_RESPONSE_RAW_PROMPT_TEMPLATE
})
const chain = APIChain.fromLLMAndAPIDocs(llm, documents, {
apiUrlPrompt,
apiResponsePrompt,
verbose: process.env.DEBUG === 'true',
headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {}
})
return chain
}
module.exports = { nodeClass: POSTApiChain_Chains }