From 45792665df1e8971bbfa89f2fb1558c350af9bbb Mon Sep 17 00:00:00 2001 From: Henry Heng Date: Sat, 8 Jul 2023 22:34:46 +0100 Subject: [PATCH] Update OpenAPIChain.ts --- .../nodes/chains/ApiChain/OpenAPIChain.ts | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/packages/components/nodes/chains/ApiChain/OpenAPIChain.ts b/packages/components/nodes/chains/ApiChain/OpenAPIChain.ts index a7e8441a8..ae1ae3c03 100644 --- a/packages/components/nodes/chains/ApiChain/OpenAPIChain.ts +++ b/packages/components/nodes/chains/ApiChain/OpenAPIChain.ts @@ -27,11 +27,19 @@ class OpenApiChain_Chains implements INode { name: 'model', type: 'ChatOpenAI' }, + { + label: 'YAML Link', + name: 'yamlLink', + type: 'string', + placeholder: 'https://api.speak.com/openapi.yaml', + description: 'If YAML link is provided, uploaded YAML File will be ignored and YAML link will be used instead' + }, { label: 'YAML File', name: 'yamlFile', type: 'file', - fileType: '.yaml' + fileType: '.yaml', + description: 'If YAML link is provided, uploaded YAML File will be ignored and YAML link will be used instead' }, { label: 'Headers', @@ -44,34 +52,13 @@ class OpenApiChain_Chains implements INode { } async init(nodeData: INodeData): Promise { - const model = nodeData.inputs?.model as ChatOpenAI - const headers = nodeData.inputs?.headers as string - const yamlFileBase64 = nodeData.inputs?.yamlFile as string - const splitDataURI = yamlFileBase64.split(',') - splitDataURI.pop() - const bf = Buffer.from(splitDataURI.pop() || '', 'base64') - const utf8String = bf.toString('utf-8') - const chain = await createOpenAPIChain(utf8String, { - llm: model, - headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {} - }) - return chain + return await initChain(nodeData) } async run(nodeData: INodeData, input: string, options: ICommonObject): Promise { - const model = nodeData.inputs?.model as ChatOpenAI - const headers = nodeData.inputs?.headers as string - const yamlFileBase64 = nodeData.inputs?.yamlFile as string - const splitDataURI = yamlFileBase64.split(',') - splitDataURI.pop() - const bf = Buffer.from(splitDataURI.pop() || '', 'base64') - const utf8String = bf.toString('utf-8') - const chain = await createOpenAPIChain(utf8String, { - llm: model, - headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {} - }) + const chain = await initChain(nodeData) if (options.socketIO && options.socketIOClientId) { - const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId, 2) + const handler = new CustomChainHandler(options.socketIO, options.socketIOClientId) const res = await chain.run(input, [handler]) return res } else { @@ -81,4 +68,28 @@ class OpenApiChain_Chains implements INode { } } +const initChain = async (nodeData: INodeData) => { + const model = nodeData.inputs?.model as ChatOpenAI + const headers = nodeData.inputs?.headers as string + const yamlLink = nodeData.inputs?.yamlLink as string + const yamlFileBase64 = nodeData.inputs?.yamlFile as string + + let yamlString = '' + + if (yamlLink) { + yamlString = yamlLink + } else { + const splitDataURI = yamlFileBase64.split(',') + splitDataURI.pop() + const bf = Buffer.from(splitDataURI.pop() || '', 'base64') + yamlString = bf.toString('utf-8') + } + + return await createOpenAPIChain(yamlString, { + llm: model, + headers: typeof headers === 'object' ? headers : headers ? JSON.parse(headers) : {}, + verbose: process.env.DEBUG === 'true' ? true : false + }) +} + module.exports = { nodeClass: OpenApiChain_Chains }