mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 03:01:10 +03:00
Bugfix/Anthropic messages in agentflow (#3801)
fix anthropic messages in agentflow
This commit is contained in:
@@ -7,7 +7,7 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models'
|
|||||||
import { ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate, PromptTemplate } from '@langchain/core/prompts'
|
import { ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate, PromptTemplate } from '@langchain/core/prompts'
|
||||||
import { formatToOpenAIToolMessages } from 'langchain/agents/format_scratchpad/openai_tools'
|
import { formatToOpenAIToolMessages } from 'langchain/agents/format_scratchpad/openai_tools'
|
||||||
import { type ToolsAgentStep } from 'langchain/agents/openai/output_parser'
|
import { type ToolsAgentStep } from 'langchain/agents/openai/output_parser'
|
||||||
import { getBaseClasses, handleEscapeCharacters, removeInvalidImageMarkdown } from '../../../src/utils'
|
import { extractOutputFromArray, getBaseClasses, handleEscapeCharacters, removeInvalidImageMarkdown } from '../../../src/utils'
|
||||||
import {
|
import {
|
||||||
FlowiseMemory,
|
FlowiseMemory,
|
||||||
ICommonObject,
|
ICommonObject,
|
||||||
@@ -181,12 +181,7 @@ class ToolAgent_Agents implements INode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let output = res?.output
|
let output = res?.output
|
||||||
if (Array.isArray(output)) {
|
output = extractOutputFromArray(res?.output)
|
||||||
output = output[0]?.text || ''
|
|
||||||
} else if (typeof output === 'object') {
|
|
||||||
output = output?.text || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
output = removeInvalidImageMarkdown(output)
|
output = removeInvalidImageMarkdown(output)
|
||||||
|
|
||||||
// Claude 3 Opus tends to spit out <thinking>..</thinking> as well, discard that in final output
|
// Claude 3 Opus tends to spit out <thinking>..</thinking> as well, discard that in final output
|
||||||
|
|||||||
@@ -23,7 +23,14 @@ import {
|
|||||||
ConversationHistorySelection
|
ConversationHistorySelection
|
||||||
} from '../../../src/Interface'
|
} from '../../../src/Interface'
|
||||||
import { ToolCallingAgentOutputParser, AgentExecutor, SOURCE_DOCUMENTS_PREFIX, ARTIFACTS_PREFIX } from '../../../src/agents'
|
import { ToolCallingAgentOutputParser, AgentExecutor, SOURCE_DOCUMENTS_PREFIX, ARTIFACTS_PREFIX } from '../../../src/agents'
|
||||||
import { getInputVariables, getVars, handleEscapeCharacters, prepareSandboxVars, removeInvalidImageMarkdown } from '../../../src/utils'
|
import {
|
||||||
|
extractOutputFromArray,
|
||||||
|
getInputVariables,
|
||||||
|
getVars,
|
||||||
|
handleEscapeCharacters,
|
||||||
|
prepareSandboxVars,
|
||||||
|
removeInvalidImageMarkdown
|
||||||
|
} from '../../../src/utils'
|
||||||
import {
|
import {
|
||||||
customGet,
|
customGet,
|
||||||
getVM,
|
getVM,
|
||||||
@@ -827,6 +834,7 @@ async function agentNode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let outputContent = typeof result === 'string' ? result : result.content || result.output
|
let outputContent = typeof result === 'string' ? result : result.content || result.output
|
||||||
|
outputContent = extractOutputFromArray(outputContent)
|
||||||
outputContent = removeInvalidImageMarkdown(outputContent)
|
outputContent = removeInvalidImageMarkdown(outputContent)
|
||||||
|
|
||||||
if (nodeData.inputs?.updateStateMemoryUI || nodeData.inputs?.updateStateMemoryCode) {
|
if (nodeData.inputs?.updateStateMemoryUI || nodeData.inputs?.updateStateMemoryCode) {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import {
|
|||||||
ConversationHistorySelection
|
ConversationHistorySelection
|
||||||
} from '../../../src/Interface'
|
} from '../../../src/Interface'
|
||||||
import { AgentExecutor } from '../../../src/agents'
|
import { AgentExecutor } from '../../../src/agents'
|
||||||
import { getInputVariables, getVars, handleEscapeCharacters, prepareSandboxVars } from '../../../src/utils'
|
import { extractOutputFromArray, getInputVariables, getVars, handleEscapeCharacters, prepareSandboxVars } from '../../../src/utils'
|
||||||
import {
|
import {
|
||||||
ExtractTool,
|
ExtractTool,
|
||||||
convertStructuredSchemaToZod,
|
convertStructuredSchemaToZod,
|
||||||
@@ -607,6 +607,8 @@ async function agentNode(
|
|||||||
} else {
|
} else {
|
||||||
result.name = name
|
result.name = name
|
||||||
result.additional_kwargs = { ...result.additional_kwargs, nodeId: nodeData.id }
|
result.additional_kwargs = { ...result.additional_kwargs, nodeId: nodeData.id }
|
||||||
|
let outputContent = typeof result === 'string' ? result : result.content
|
||||||
|
result.content = extractOutputFromArray(outputContent)
|
||||||
return {
|
return {
|
||||||
...returnedOutput,
|
...returnedOutput,
|
||||||
messages: [result]
|
messages: [result]
|
||||||
@@ -627,6 +629,8 @@ async function agentNode(
|
|||||||
} else {
|
} else {
|
||||||
result.name = name
|
result.name = name
|
||||||
result.additional_kwargs = { ...result.additional_kwargs, nodeId: nodeData.id }
|
result.additional_kwargs = { ...result.additional_kwargs, nodeId: nodeData.id }
|
||||||
|
let outputContent = typeof result === 'string' ? result : result.content
|
||||||
|
result.content = extractOutputFromArray(outputContent)
|
||||||
return {
|
return {
|
||||||
messages: [result]
|
messages: [result]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1001,6 +1001,21 @@ export const removeInvalidImageMarkdown = (output: string): string => {
|
|||||||
return typeof output === 'string' ? output.replace(/!\[.*?\]\((?!https?:\/\/).*?\)/g, '') : output
|
return typeof output === 'string' ? output.replace(/!\[.*?\]\((?!https?:\/\/).*?\)/g, '') : output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract output from array
|
||||||
|
* @param {any} output
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
export const extractOutputFromArray = (output: any): string => {
|
||||||
|
if (Array.isArray(output)) {
|
||||||
|
return output.map((o) => o.text).join('\n')
|
||||||
|
} else if (typeof output === 'object') {
|
||||||
|
if (output.text) return output.text
|
||||||
|
else return JSON.stringify(output)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loop through the object and replace the key with the value
|
* Loop through the object and replace the key with the value
|
||||||
* @param {any} obj
|
* @param {any} obj
|
||||||
|
|||||||
Reference in New Issue
Block a user