Update OpenAPIChain.ts

This commit is contained in:
Henry Heng 2023-07-08 22:34:46 +01:00 committed by GitHub
parent 6166074e07
commit 45792665df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 25 deletions

View File

@ -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<any> {
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<string> {
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 }