Compare commits

...

1 Commits

Author SHA1 Message Date
Henry 7a73e10d06 add openapi agent toolkit 2023-04-28 17:48:51 +01:00
3 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,112 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { AgentExecutor } from 'langchain/agents'
import { getBaseClasses } from '../../../src/utils'
import * as yaml from 'js-yaml'
import { OpenAI } from 'langchain/llms/openai'
import { JsonSpec, JsonObject } from 'langchain/tools'
import { createOpenApiAgent, OpenApiToolkit } from 'langchain/agents'
class OpenAPIAgent_Agents implements INode {
label: string
name: string
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
constructor() {
this.label = 'OpenAPI Agent'
this.name = 'openAPIAgent'
this.type = 'AgentExecutor'
this.category = 'Agents'
this.icon = 'openapi.svg'
this.description = 'Conversational agent for a chat model. It will utilize chat specific prompts'
this.baseClasses = [this.type, ...getBaseClasses(AgentExecutor)]
this.inputs = [
{
label: 'YAML File',
name: 'yamlFile',
type: 'string',
placeholder: 'https://github.com/openai/openai-openapi/blob/master/openapi.yaml'
},
{
label: 'Headers',
name: 'headers',
type: 'string',
rows: 4,
default: `{
"Content-Type": "application/json",
}`,
placeholder: `{
"Content-Type": "application/json",
"Authorization": "Bearer <api-key>",
}`
},
{
label: 'Language Model',
name: 'model',
type: 'BaseLanguageModel'
}
]
}
async init(nodeData: INodeData): Promise<any> {
/*
const model = nodeData.inputs?.model as BaseChatModel
const tools = nodeData.inputs?.tools as Tool[]
const memory = nodeData.inputs?.memory as BaseChatMemory
const humanMessage = nodeData.inputs?.humanMessage as string
const systemMessage = nodeData.inputs?.systemMessage as string
const obj: InitializeAgentExecutorOptions = {
agentType: 'chat-conversational-react-description',
verbose: true
}
const agentArgs: any = {}
if (humanMessage) {
agentArgs.humanMessage = humanMessage
}
if (systemMessage) {
agentArgs.systemMessage = systemMessage
}
if (Object.keys(agentArgs).length) obj.agentArgs = agentArgs
const executor = await initializeAgentExecutorWithOptions(tools, model, obj)
executor.memory = memory
return executor*/
return null
}
async run(nodeData: INodeData, input: string): Promise<string> {
let data: JsonObject
try {
// const yamlFile = fs.readFileSync("openai_openapi.yaml", "utf8");
data = yaml.load('https://github.com/openai/openai-openapi/blob/master/openapi.yaml') as JsonObject
if (!data) {
throw new Error('Failed to load OpenAPI spec')
}
} catch (e) {
console.error(e)
return ''
}
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer `
}
const model = new OpenAI({ temperature: 0 })
const toolkit = new OpenApiToolkit(new JsonSpec(data), model, headers)
const executor = createOpenApiAgent(model, toolkit)
const result = await executor.call({ input })
console.log(`Got output ${result.output}`)
return result?.output
}
}
module.exports = { nodeClass: OpenAPIAgent_Agents }

View File

@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-api" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M4 13h5"></path>
<path d="M12 16v-8h3a2 2 0 0 1 2 2v1a2 2 0 0 1 -2 2h-3"></path>
<path d="M20 8v8"></path>
<path d="M9 16v-5.5a2.5 2.5 0 0 0 -5 0v5.5"></path>
</svg>

After

Width:  |  Height:  |  Size: 470 B

View File

@ -27,6 +27,7 @@
"dotenv": "^16.0.0",
"express": "^4.17.3",
"form-data": "^4.0.0",
"js-yaml": "^4.1.0",
"langchain": "^0.0.63",
"mammoth": "^1.5.1",
"moment": "^2.29.3",
@ -36,6 +37,7 @@
},
"devDependencies": {
"@types/gulp": "4.0.9",
"@types/js-yaml": "^4.0.5",
"@types/ws": "^8.5.3",
"gulp": "^4.0.2",
"typescript": "^4.8.4"