Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OpenAPIChain #491

Merged
merged 7 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions packages/components/nodes/chains/ApiChain/OpenAPIChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { APIChain, createOpenAPIChain } from 'langchain/chains'
import { CustomChainHandler, getBaseClasses } from '../../../src/utils'
import { ChatOpenAI } from 'langchain/chat_models/openai'

class OpenApiChain_Chains implements INode {
label: string
name: string
type: string
icon: string
category: string
baseClasses: string[]
description: string
inputs: INodeParams[]

constructor() {
this.label = 'OpenAPI Chain'
this.name = 'openApiChain'
this.type = 'openApiChain'
this.icon = 'openapi.png'
this.category = 'Chains'
this.description = 'Chain to run queries against OpenAPI'
this.baseClasses = [this.type, ...getBaseClasses(APIChain)]
this.inputs = [
{
label: 'ChatOpenAI Model',
name: 'model',
type: 'ChatOpenAI'
},
{
label: 'YAML File',
name: 'yamlFile',
type: 'file',
fileType: '.yaml'
},
{
label: 'Headers',
name: 'headers',
type: 'json',
additionalParams: true,
optional: true
}
]
}

async init(nodeData: INodeData): Promise<any> {
const model = nodeData.inputs?.model as ChatOpenAI
const headers = nodeData.inputs?.headers as Record<string, 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
})
return chain
}

async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
const model = nodeData.inputs?.model as ChatOpenAI
const headers = nodeData.inputs?.headers as Record<string, 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
})
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
}
}
}

module.exports = { nodeClass: OpenApiChain_Chains }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { SqlDatabaseChain, SqlDatabaseChainInput } from 'langchain/chains'
import { SqlDatabaseChain, SqlDatabaseChainInput } from 'langchain/chains/sql_db'
Copy link
Contributor Author

@vjsai vjsai Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to make this change because of the newer langchainjs version.

Refer PR: https://github.com/hwchase17/langchainjs/pull/1836/files

import { CustomChainHandler, getBaseClasses } from '../../../src/utils'
import { DataSource } from 'typeorm'
import { SqlDatabase } from 'langchain/sql_db'
Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"form-data": "^4.0.0",
"graphql": "^16.6.0",
"html-to-text": "^9.0.5",
"langchain": "^0.0.96",
"langchain": "^0.0.103",
"linkifyjs": "^4.1.1",
"mammoth": "^1.5.1",
"moment": "^2.29.3",
Expand Down