Chore/Upgrade llamaindex (#2017)

upgrade llamaindex
This commit is contained in:
Henry Heng
2024-03-25 16:10:28 +08:00
committed by GitHub
parent 414b9f125c
commit 3a7c2fd4db
5 changed files with 320 additions and 133 deletions
@@ -0,0 +1,139 @@
import { flatten } from 'lodash'
import { ChatMessage, OpenAI, OpenAIAgent } from 'llamaindex'
import { getBaseClasses } from '../../../src/utils'
import { FlowiseMemory, ICommonObject, IMessage, INode, INodeData, INodeParams, IUsedTool } from '../../../src/Interface'
class OpenAIFunctionAgent_LlamaIndex_Agents implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
tags: string[]
inputs: INodeParams[]
sessionId?: string
badge?: string
constructor(fields?: { sessionId?: string }) {
this.label = 'OpenAI Tool Agent'
this.name = 'openAIToolAgentLlamaIndex'
this.version = 1.0
this.type = 'OpenAIToolAgent'
this.category = 'Agents'
this.icon = 'function.svg'
this.description = `Agent that uses OpenAI Function Calling to pick the tools and args to call using LlamaIndex`
this.baseClasses = [this.type, ...getBaseClasses(OpenAIAgent)]
this.tags = ['LlamaIndex']
this.badge = 'NEW'
this.inputs = [
{
label: 'Tools',
name: 'tools',
type: 'Tool_LlamaIndex',
list: true
},
{
label: 'Memory',
name: 'memory',
type: 'BaseChatMemory'
},
{
label: 'OpenAI/Azure Chat Model',
name: 'model',
type: 'BaseChatModel_LlamaIndex'
},
{
label: 'System Message',
name: 'systemMessage',
type: 'string',
rows: 4,
optional: true,
additionalParams: true
}
]
this.sessionId = fields?.sessionId
}
async init(): Promise<any> {
return null
}
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string | ICommonObject> {
const memory = nodeData.inputs?.memory as FlowiseMemory
const model = nodeData.inputs?.model as OpenAI
const systemMessage = nodeData.inputs?.systemMessage as string
let tools = nodeData.inputs?.tools
tools = flatten(tools)
const chatHistory = [] as ChatMessage[]
if (systemMessage) {
chatHistory.push({
content: systemMessage,
role: 'system'
})
}
const msgs = (await memory.getChatMessages(this.sessionId, false, options.chatHistory)) as IMessage[]
for (const message of msgs) {
if (message.type === 'apiMessage') {
chatHistory.push({
content: message.message,
role: 'assistant'
})
} else if (message.type === 'userMessage') {
chatHistory.push({
content: message.message,
role: 'user'
})
}
}
const agent = new OpenAIAgent({
tools,
llm: model,
prefixMessages: chatHistory,
verbose: process.env.DEBUG === 'true' ? true : false
})
let text = ''
const usedTools: IUsedTool[] = []
const response = await agent.chat({
message: input
})
if (response.sources.length) {
for (const sourceTool of response.sources) {
usedTools.push({
tool: sourceTool.toolName,
toolInput: sourceTool.rawInput,
toolOutput: sourceTool.rawOutput
})
}
}
text = String(response)
await memory.addChatMessages(
[
{
text: input,
type: 'userMessage'
},
{
text: text,
type: 'apiMessage'
}
],
this.sessionId
)
return usedTools.length ? { text: text, usedTools } : text
}
}
module.exports = { nodeClass: OpenAIFunctionAgent_LlamaIndex_Agents }
@@ -18,7 +18,7 @@ class ChatAnthropic_LlamaIndex_ChatModels implements INode {
constructor() {
this.label = 'ChatAnthropic'
this.name = 'chatAnthropic_LlamaIndex'
this.version = 1.0
this.version = 2.0
this.type = 'ChatAnthropic'
this.icon = 'Anthropic.svg'
this.category = 'Chat Models'
@@ -39,22 +39,22 @@ class ChatAnthropic_LlamaIndex_ChatModels implements INode {
options: [
{
label: 'claude-3-opus',
name: 'claude-3-opus-20240229',
name: 'claude-3-opus',
description: 'Most powerful model for highly complex tasks'
},
{
label: 'claude-3-sonnet',
name: 'claude-3-sonnet-20240229',
name: 'claude-3-sonnet',
description: 'Ideal balance of intelligence and speed for enterprise workloads'
},
{
label: 'claude-2',
name: 'claude-2',
label: 'claude-2.1',
name: 'claude-2.1',
description: 'Claude 2 latest major version, automatically get updates to the model as they are released'
},
{
label: 'claude-instant-1',
name: 'claude-instant-1',
label: 'claude-instant-1.2',
name: 'claude-instant-1.2',
description: 'Claude Instant latest major version, automatically get updates to the model as they are released'
}
],
@@ -90,7 +90,7 @@ class ChatAnthropic_LlamaIndex_ChatModels implements INode {
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const temperature = nodeData.inputs?.temperature as string
const modelName = nodeData.inputs?.modelName as 'claude-2' | 'claude-instant-1' | undefined
const modelName = nodeData.inputs?.modelName as 'claude-3-opus' | 'claude-3-sonnet' | 'claude-2.1' | 'claude-instant-1.2'
const maxTokensToSample = nodeData.inputs?.maxTokensToSample as string
const topP = nodeData.inputs?.topP as string
@@ -1,5 +1,5 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { BaseQueryEngine } from 'llamaindex'
import { QueryEngineTool } from 'llamaindex'
class QueryEngine_Tools implements INode {
label: string
@@ -22,7 +22,7 @@ class QueryEngine_Tools implements INode {
this.category = 'Tools'
this.tags = ['LlamaIndex']
this.description = 'Tool used to invoke query engine'
this.baseClasses = [this.type]
this.baseClasses = [this.type, 'Tool_LlamaIndex']
this.inputs = [
{
label: 'Base QueryEngine',
@@ -45,16 +45,16 @@ class QueryEngine_Tools implements INode {
}
async init(nodeData: INodeData): Promise<any> {
const baseQueryEngine = nodeData.inputs?.baseQueryEngine as BaseQueryEngine
const baseQueryEngine = nodeData.inputs?.baseQueryEngine
const toolName = nodeData.inputs?.toolName as string
const toolDesc = nodeData.inputs?.toolDesc as string
const queryEngineTool = {
const queryEngineTool = new QueryEngineTool({
queryEngine: baseQueryEngine,
metadata: {
name: toolName,
description: toolDesc
}
}
})
return queryEngineTool
}