Skip to content

Commit

Permalink
fix anthropic messages in agentflow
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryHengZJ committed Jan 3, 2025
1 parent e22c601 commit cb1228a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
9 changes: 2 additions & 7 deletions packages/components/nodes/agents/ToolAgent/ToolAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models'
import { ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate, PromptTemplate } from '@langchain/core/prompts'
import { formatToOpenAIToolMessages } from 'langchain/agents/format_scratchpad/openai_tools'
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 {
FlowiseMemory,
ICommonObject,
Expand Down Expand Up @@ -181,12 +181,7 @@ class ToolAgent_Agents implements INode {
}

let output = res?.output
if (Array.isArray(output)) {
output = output[0]?.text || ''
} else if (typeof output === 'object') {
output = output?.text || ''
}

output = extractOutputFromArray(res?.output)
output = removeInvalidImageMarkdown(output)

// Claude 3 Opus tends to spit out <thinking>..</thinking> as well, discard that in final output
Expand Down
10 changes: 9 additions & 1 deletion packages/components/nodes/sequentialagents/Agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ import {
ConversationHistorySelection
} from '../../../src/Interface'
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 {
customGet,
getVM,
Expand Down Expand Up @@ -827,6 +834,7 @@ async function agentNode(
}

let outputContent = typeof result === 'string' ? result : result.content || result.output
outputContent = extractOutputFromArray(outputContent)
outputContent = removeInvalidImageMarkdown(outputContent)

if (nodeData.inputs?.updateStateMemoryUI || nodeData.inputs?.updateStateMemoryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ConversationHistorySelection
} from '../../../src/Interface'
import { AgentExecutor } from '../../../src/agents'
import { getInputVariables, getVars, handleEscapeCharacters, prepareSandboxVars } from '../../../src/utils'
import { extractOutputFromArray, getInputVariables, getVars, handleEscapeCharacters, prepareSandboxVars } from '../../../src/utils'
import {
ExtractTool,
convertStructuredSchemaToZod,
Expand Down Expand Up @@ -607,6 +607,8 @@ async function agentNode(
} else {
result.name = name
result.additional_kwargs = { ...result.additional_kwargs, nodeId: nodeData.id }
let outputContent = typeof result === 'string' ? result : result.content
result.content = extractOutputFromArray(outputContent)
return {
...returnedOutput,
messages: [result]
Expand All @@ -627,6 +629,8 @@ async function agentNode(
} else {
result.name = name
result.additional_kwargs = { ...result.additional_kwargs, nodeId: nodeData.id }
let outputContent = typeof result === 'string' ? result : result.content
result.content = extractOutputFromArray(outputContent)
return {
messages: [result]
}
Expand Down
15 changes: 15 additions & 0 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,21 @@ export const removeInvalidImageMarkdown = (output: string): string => {
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
* @param {any} obj
Expand Down

0 comments on commit cb1228a

Please sign in to comment.