mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
add get and post api chain
This commit is contained in:
@@ -1,88 +0,0 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { APIChain } from 'langchain/chains'
|
||||
import { CustomChainHandler, getBaseClasses } from '../../../src/utils'
|
||||
import { BaseLanguageModel } from 'langchain/base_language'
|
||||
import { Document } from 'langchain/document'
|
||||
import { PromptTemplate } from 'langchain/prompts'
|
||||
|
||||
class ApiChain_Chains implements INode {
|
||||
label: string
|
||||
name: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
description: string
|
||||
inputs: INodeParams[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'API Chain'
|
||||
this.name = 'apiChain'
|
||||
this.type = 'ApiChain'
|
||||
this.icon = 'apichain.svg'
|
||||
this.category = 'Chains'
|
||||
this.description = 'Chain to run queries against API'
|
||||
this.baseClasses = [this.type, ...getBaseClasses(APIChain)]
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Language Model',
|
||||
name: 'model',
|
||||
type: 'BaseLanguageModel'
|
||||
},
|
||||
{
|
||||
label: 'Document',
|
||||
name: 'document',
|
||||
type: 'Document'
|
||||
},
|
||||
{
|
||||
label: 'Headers',
|
||||
name: 'headers',
|
||||
type: 'json',
|
||||
additionalParams: true,
|
||||
optional: true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
const model = nodeData.inputs?.model as BaseLanguageModel
|
||||
const docs = nodeData.inputs?.document as Document[]
|
||||
const headers = nodeData.inputs?.headers as string
|
||||
|
||||
const chain = await getAPIChain(docs, model, headers)
|
||||
return chain
|
||||
}
|
||||
|
||||
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
|
||||
const model = nodeData.inputs?.model as BaseLanguageModel
|
||||
const docs = nodeData.inputs?.document as Document[]
|
||||
const headers = nodeData.inputs?.headers as string
|
||||
|
||||
const chain = await getAPIChain(docs, model, headers)
|
||||
if (options.socketIO && options.socketIOClientId) {
|
||||
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId)
|
||||
const res = await chain.run(input, [handler])
|
||||
return res
|
||||
} else {
|
||||
const res = await chain.run(input)
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getAPIChain = async (documents: Document[], llm: BaseLanguageModel, headers: any) => {
|
||||
const texts = documents.map(({ pageContent }) => pageContent)
|
||||
const apiResponsePrompt = new PromptTemplate({
|
||||
inputVariables: ['api_docs', 'question', 'api_url', 'api_response'],
|
||||
template: 'Given this {api_response} response for {api_url}. use the given response to answer this {question}'
|
||||
})
|
||||
|
||||
const chain = APIChain.fromLLMAndAPIDocs(llm, texts.toString(), {
|
||||
apiResponsePrompt,
|
||||
verbose: process.env.DEBUG === 'true' ? true : false,
|
||||
headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {}
|
||||
})
|
||||
return chain
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: ApiChain_Chains }
|
||||
@@ -0,0 +1,129 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { APIChain } from 'langchain/chains'
|
||||
import { CustomChainHandler, getBaseClasses } from '../../../src/utils'
|
||||
import { BaseLanguageModel } from 'langchain/base_language'
|
||||
import { PromptTemplate } from 'langchain/prompts'
|
||||
|
||||
export const API_URL_RAW_PROMPT_TEMPLATE = `You are given the below API Documentation:
|
||||
{api_docs}
|
||||
Using this documentation, generate the full API url to call for answering the user question.
|
||||
You should build the API url in order to get a response that is as short as possible, while still getting the necessary information to answer the question. Pay attention to deliberately exclude any unnecessary pieces of data in the API call.
|
||||
|
||||
Question:{question}
|
||||
API url:`
|
||||
|
||||
export const API_RESPONSE_RAW_PROMPT_TEMPLATE =
|
||||
'Given this {api_response} response for {api_url}. use the given response to answer this {question}'
|
||||
|
||||
class GETApiChain_Chains implements INode {
|
||||
label: string
|
||||
name: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
description: string
|
||||
inputs: INodeParams[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'GET API Chain'
|
||||
this.name = 'getApiChain'
|
||||
this.type = 'GETApiChain'
|
||||
this.icon = 'apichain.svg'
|
||||
this.category = 'Chains'
|
||||
this.description = 'Chain to run queries against GET 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/hwchase17/langchain/blob/master/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)
|
||||
if (options.socketIO && options.socketIOClientId) {
|
||||
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2)
|
||||
const res = await chain.run(input, [handler])
|
||||
return res
|
||||
} else {
|
||||
const res = await chain.run(input)
|
||||
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', 'api_response'],
|
||||
template: ansPrompt ? ansPrompt : API_RESPONSE_RAW_PROMPT_TEMPLATE
|
||||
})
|
||||
|
||||
const chain = APIChain.fromLLMAndAPIDocs(llm, documents, {
|
||||
apiUrlPrompt,
|
||||
apiResponsePrompt,
|
||||
verbose: process.env.DEBUG === 'true' ? true : false,
|
||||
headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {}
|
||||
})
|
||||
return chain
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: GETApiChain_Chains }
|
||||
@@ -0,0 +1,118 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { CustomChainHandler, getBaseClasses } from '../../../src/utils'
|
||||
import { BaseLanguageModel } from 'langchain/base_language'
|
||||
import { PromptTemplate } from 'langchain/prompts'
|
||||
import { API_RESPONSE_RAW_PROMPT_TEMPLATE, API_URL_RAW_PROMPT_TEMPLATE, APIChain } from './postCore'
|
||||
|
||||
class POSTApiChain_Chains implements INode {
|
||||
label: string
|
||||
name: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
description: string
|
||||
inputs: INodeParams[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'POST API Chain'
|
||||
this.name = 'postApiChain'
|
||||
this.type = 'POSTApiChain'
|
||||
this.icon = 'apichain.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/hwchase17/langchain/blob/master/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)
|
||||
if (options.socketIO && options.socketIOClientId) {
|
||||
const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2)
|
||||
const res = await chain.run(input, [handler])
|
||||
return res
|
||||
} else {
|
||||
const res = await chain.run(input)
|
||||
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' ? true : false,
|
||||
headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {}
|
||||
})
|
||||
return chain
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: POSTApiChain_Chains }
|
||||
@@ -0,0 +1,162 @@
|
||||
import { BaseLanguageModel } from 'langchain/base_language'
|
||||
import { CallbackManagerForChainRun } from 'langchain/callbacks'
|
||||
import { BaseChain, ChainInputs, LLMChain, SerializedAPIChain } from 'langchain/chains'
|
||||
import { BasePromptTemplate, PromptTemplate } from 'langchain/prompts'
|
||||
import { ChainValues } from 'langchain/schema'
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
export const API_URL_RAW_PROMPT_TEMPLATE = `You are given the below API Documentation:
|
||||
{api_docs}
|
||||
Using this documentation, generate a json string with two keys: "url" and "data".
|
||||
The value of "url" should be a string, which is the API url to call for answering the user question.
|
||||
The value of "data" should be a dictionary of key-value pairs you want to POST to the url as a JSON body.
|
||||
Be careful to always use double quotes for strings in the json string.
|
||||
You should build the json string in order to get a response that is as short as possible, while still getting the necessary information to answer the question. Pay attention to deliberately exclude any unnecessary pieces of data in the API call.
|
||||
|
||||
Question:{question}
|
||||
json string:`
|
||||
|
||||
export const API_RESPONSE_RAW_PROMPT_TEMPLATE = `${API_URL_RAW_PROMPT_TEMPLATE} {api_url_body}
|
||||
|
||||
Here is the response from the API:
|
||||
|
||||
{api_response}
|
||||
|
||||
Summarize this response to answer the original question.
|
||||
|
||||
Summary:`
|
||||
|
||||
const defaultApiUrlPrompt = new PromptTemplate({
|
||||
inputVariables: ['api_docs', 'question'],
|
||||
template: API_URL_RAW_PROMPT_TEMPLATE
|
||||
})
|
||||
|
||||
const defaultApiResponsePrompt = new PromptTemplate({
|
||||
inputVariables: ['api_docs', 'question', 'api_url_body', 'api_response'],
|
||||
template: API_RESPONSE_RAW_PROMPT_TEMPLATE
|
||||
})
|
||||
|
||||
export interface APIChainInput extends Omit<ChainInputs, 'memory'> {
|
||||
apiAnswerChain: LLMChain
|
||||
apiRequestChain: LLMChain
|
||||
apiDocs: string
|
||||
inputKey?: string
|
||||
headers?: Record<string, string>
|
||||
/** Key to use for output, defaults to `output` */
|
||||
outputKey?: string
|
||||
}
|
||||
|
||||
export type APIChainOptions = {
|
||||
headers?: Record<string, string>
|
||||
apiUrlPrompt?: BasePromptTemplate
|
||||
apiResponsePrompt?: BasePromptTemplate
|
||||
}
|
||||
|
||||
export class APIChain extends BaseChain implements APIChainInput {
|
||||
apiAnswerChain: LLMChain
|
||||
|
||||
apiRequestChain: LLMChain
|
||||
|
||||
apiDocs: string
|
||||
|
||||
headers = {}
|
||||
|
||||
inputKey = 'question'
|
||||
|
||||
outputKey = 'output'
|
||||
|
||||
get inputKeys() {
|
||||
return [this.inputKey]
|
||||
}
|
||||
|
||||
get outputKeys() {
|
||||
return [this.outputKey]
|
||||
}
|
||||
|
||||
constructor(fields: APIChainInput) {
|
||||
super(fields)
|
||||
this.apiRequestChain = fields.apiRequestChain
|
||||
this.apiAnswerChain = fields.apiAnswerChain
|
||||
this.apiDocs = fields.apiDocs
|
||||
this.inputKey = fields.inputKey ?? this.inputKey
|
||||
this.outputKey = fields.outputKey ?? this.outputKey
|
||||
this.headers = fields.headers ?? this.headers
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
async _call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues> {
|
||||
try {
|
||||
const question: string = values[this.inputKey]
|
||||
|
||||
const api_url_body = await this.apiRequestChain.predict({ question, api_docs: this.apiDocs }, runManager?.getChild())
|
||||
|
||||
const { url, data } = JSON.parse(api_url_body)
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
|
||||
const api_response = await res.text()
|
||||
|
||||
const answer = await this.apiAnswerChain.predict(
|
||||
{ question, api_docs: this.apiDocs, api_url_body, api_response },
|
||||
runManager?.getChild()
|
||||
)
|
||||
|
||||
return { [this.outputKey]: answer }
|
||||
} catch (error) {
|
||||
return { [this.outputKey]: error }
|
||||
}
|
||||
}
|
||||
|
||||
_chainType() {
|
||||
return 'api_chain' as const
|
||||
}
|
||||
|
||||
static async deserialize(data: SerializedAPIChain) {
|
||||
const { api_request_chain, api_answer_chain, api_docs } = data
|
||||
|
||||
if (!api_request_chain) {
|
||||
throw new Error('LLMChain must have api_request_chain')
|
||||
}
|
||||
if (!api_answer_chain) {
|
||||
throw new Error('LLMChain must have api_answer_chain')
|
||||
}
|
||||
if (!api_docs) {
|
||||
throw new Error('LLMChain must have api_docs')
|
||||
}
|
||||
|
||||
return new APIChain({
|
||||
apiAnswerChain: await LLMChain.deserialize(api_answer_chain),
|
||||
apiRequestChain: await LLMChain.deserialize(api_request_chain),
|
||||
apiDocs: api_docs
|
||||
})
|
||||
}
|
||||
|
||||
serialize(): SerializedAPIChain {
|
||||
return {
|
||||
_type: this._chainType(),
|
||||
api_answer_chain: this.apiAnswerChain.serialize(),
|
||||
api_request_chain: this.apiRequestChain.serialize(),
|
||||
api_docs: this.apiDocs
|
||||
}
|
||||
}
|
||||
|
||||
static fromLLMAndAPIDocs(
|
||||
llm: BaseLanguageModel,
|
||||
apiDocs: string,
|
||||
options: APIChainOptions & Omit<APIChainInput, 'apiAnswerChain' | 'apiRequestChain' | 'apiDocs'> = {}
|
||||
): APIChain {
|
||||
const { apiUrlPrompt = defaultApiUrlPrompt, apiResponsePrompt = defaultApiResponsePrompt } = options
|
||||
const apiRequestChain = new LLMChain({ prompt: apiUrlPrompt, llm })
|
||||
const apiAnswerChain = new LLMChain({ prompt: apiResponsePrompt, llm })
|
||||
return new this({
|
||||
apiAnswerChain,
|
||||
apiRequestChain,
|
||||
apiDocs,
|
||||
...options
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user