forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore/upgrade llamaindex version (FlowiseAI#2440)
* updates to loader to support file upload * adding a todo * upgrade llamaindex * update groq icon * update azure models * update llamaindex version --------- Co-authored-by: Henry <hzj94@hotmail.com>
- Loading branch information
1 parent
e83dcb0
commit ff23817
Showing
22 changed files
with
1,340 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/components/nodes/agents/LlamaIndexAgents/AnthropicAgent/Anthropic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
...es/components/nodes/agents/LlamaIndexAgents/OpenAIToolAgent/OpenAIToolAgent_LlamaIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
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 = 2.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.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 isStreamingEnabled = options.socketIO && options.socketIOClientId | ||
|
||
const chatHistory = [] as ChatMessage[] | ||
|
||
if (systemMessage) { | ||
chatHistory.push({ | ||
content: systemMessage, | ||
role: 'system' | ||
}) | ||
} | ||
|
||
const msgs = (await memory.getChatMessages(this.sessionId, false)) 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, | ||
chatHistory: chatHistory, | ||
verbose: process.env.DEBUG === 'true' ? true : false | ||
}) | ||
|
||
let text = '' | ||
let isStreamingStarted = false | ||
const usedTools: IUsedTool[] = [] | ||
|
||
if (isStreamingEnabled) { | ||
const stream = await agent.chat({ | ||
message: input, | ||
chatHistory, | ||
stream: true, | ||
verbose: process.env.DEBUG === 'true' ? true : false | ||
}) | ||
for await (const chunk of stream) { | ||
//console.log('chunk', chunk) | ||
text += chunk.response.delta | ||
if (!isStreamingStarted) { | ||
isStreamingStarted = true | ||
options.socketIO.to(options.socketIOClientId).emit('start', chunk.response.delta) | ||
if (chunk.sources.length) { | ||
for (const sourceTool of chunk.sources) { | ||
usedTools.push({ | ||
tool: sourceTool.tool?.metadata.name ?? '', | ||
toolInput: sourceTool.input, | ||
toolOutput: sourceTool.output as any | ||
}) | ||
} | ||
options.socketIO.to(options.socketIOClientId).emit('usedTools', usedTools) | ||
} | ||
} | ||
|
||
options.socketIO.to(options.socketIOClientId).emit('token', chunk.response.delta) | ||
} | ||
} else { | ||
const response = await agent.chat({ message: input, chatHistory, verbose: process.env.DEBUG === 'true' ? true : false }) | ||
if (response.sources.length) { | ||
for (const sourceTool of response.sources) { | ||
usedTools.push({ | ||
tool: sourceTool.tool?.metadata.name ?? '', | ||
toolInput: sourceTool.input, | ||
toolOutput: sourceTool.output as any | ||
}) | ||
} | ||
} | ||
|
||
text = response.response.message.content as string | ||
} | ||
|
||
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 } |
9 changes: 9 additions & 0 deletions
9
packages/components/nodes/agents/LlamaIndexAgents/OpenAIToolAgent/function.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.