2024-02-23 01:27:39 -08:00
import { NodeConnectionType , NodeOperationError , jsonStringify } from 'n8n-workflow' ;
import type { EventNamesAiNodesType , IDataObject , IExecuteFunctions } from 'n8n-workflow' ;
2024-03-07 02:36:36 -08:00
import { BaseChatModel } from '@langchain/core/language_models/chat_models' ;
2024-02-21 05:59:37 -08:00
import type { BaseOutputParser } from '@langchain/core/output_parsers' ;
2024-03-07 02:36:36 -08:00
import type { BaseMessage } from '@langchain/core/messages' ;
import { DynamicTool , type Tool } from '@langchain/core/tools' ;
2023-11-29 03:13:55 -08:00
export function getMetadataFiltersValues (
ctx : IExecuteFunctions ,
itemIndex : number ,
) : Record < string , never > | undefined {
const metadata = ctx . getNodeParameter ( 'options.metadata.metadataValues' , itemIndex , [ ] ) as Array < {
name : string ;
value : string ;
} > ;
if ( metadata . length > 0 ) {
return metadata . reduce ( ( acc , { name , value } ) = > ( { . . . acc , [ name ] : value } ) , { } ) ;
}
return undefined ;
}
2024-02-05 07:09:23 -08:00
// TODO: Remove this function once langchain package is updated to 0.1.x
2024-02-21 05:59:37 -08:00
// eslint-disable-next-line @typescript-eslint/no-duplicate-type-constituents
2024-03-07 02:36:36 -08:00
export function isChatInstance ( model : any ) : model is BaseChatModel {
return model instanceof BaseChatModel ;
2024-02-05 07:09:23 -08:00
}
2024-02-21 05:59:37 -08:00
export async function getOptionalOutputParsers (
ctx : IExecuteFunctions ,
) : Promise < Array < BaseOutputParser < unknown > >> {
let outputParsers : BaseOutputParser [ ] = [ ] ;
if ( ctx . getNodeParameter ( 'hasOutputParser' , 0 , true ) === true ) {
outputParsers = ( await ctx . getInputConnectionData (
NodeConnectionType . AiOutputParser ,
0 ,
) ) as BaseOutputParser [ ] ;
}
return outputParsers ;
}
export function getPromptInputByType ( options : {
ctx : IExecuteFunctions ;
i : number ;
promptTypeKey : string ;
inputKey : string ;
} ) {
const { ctx , i , promptTypeKey , inputKey } = options ;
const prompt = ctx . getNodeParameter ( promptTypeKey , i ) as string ;
let input ;
if ( prompt === 'auto' ) {
input = ctx . evaluateExpression ( '{{ $json["chatInput"] }}' , i ) as string ;
} else {
input = ctx . getNodeParameter ( inputKey , i ) as string ;
}
if ( input === undefined ) {
throw new NodeOperationError ( ctx . getNode ( ) , 'No prompt specified' , {
description :
"Expected to find the prompt in an input field called 'chatInput' (this is what the chat trigger node outputs). To use something else, change the 'Prompt' parameter" ,
} ) ;
}
return input ;
}
2024-02-23 01:27:39 -08:00
2024-02-27 05:01:15 -08:00
export function getSessionId (
ctx : IExecuteFunctions ,
itemIndex : number ,
selectorKey = 'sessionIdType' ,
autoSelect = 'fromInput' ,
customKey = 'sessionKey' ,
) {
let sessionId = '' ;
const selectorType = ctx . getNodeParameter ( selectorKey , itemIndex ) as string ;
if ( selectorType === autoSelect ) {
sessionId = ctx . evaluateExpression ( '{{ $json.sessionId }}' , itemIndex ) as string ;
if ( sessionId === '' || sessionId === undefined ) {
throw new NodeOperationError ( ctx . getNode ( ) , 'No session ID found' , {
description :
"Expected to find the session ID in an input field called 'sessionId' (this is what the chat trigger node outputs). To use something else, change the 'Session ID' parameter" ,
itemIndex ,
} ) ;
}
} else {
sessionId = ctx . getNodeParameter ( customKey , itemIndex , '' ) as string ;
if ( sessionId === '' || sessionId === undefined ) {
throw new NodeOperationError ( ctx . getNode ( ) , 'Key parameter is empty' , {
description :
"Provide a key to use as session ID in the 'Key' parameter or use the 'Take from previous node automatically' option to use the session ID from the previous node, e.t. chat trigger node" ,
itemIndex ,
} ) ;
}
}
return sessionId ;
}
2024-02-23 01:27:39 -08:00
export async function logAiEvent (
executeFunctions : IExecuteFunctions ,
event : EventNamesAiNodesType ,
data? : IDataObject ,
) {
try {
await executeFunctions . logAiEvent ( event , data ? jsonStringify ( data ) : undefined ) ;
} catch ( error ) {
executeFunctions . logger . debug ( ` Error logging AI event: ${ event } ` ) ;
}
}
2024-02-26 05:35:00 -08:00
2024-02-27 05:01:15 -08:00
export function serializeChatHistory ( chatHistory : BaseMessage [ ] ) : string {
2024-02-26 05:35:00 -08:00
return chatHistory
. map ( ( chatMessage ) = > {
if ( chatMessage . _getType ( ) === 'human' ) {
return ` Human: ${ chatMessage . content } ` ;
} else if ( chatMessage . _getType ( ) === 'ai' ) {
return ` Assistant: ${ chatMessage . content } ` ;
} else {
return ` ${ chatMessage . content } ` ;
}
} )
. join ( '\n' ) ;
}
2024-02-29 03:28:38 -08:00
export const getConnectedTools = async ( ctx : IExecuteFunctions , enforceUniqueNames : boolean ) = > {
2024-03-07 02:36:36 -08:00
const connectedTools =
( ( await ctx . getInputConnectionData ( NodeConnectionType . AiTool , 0 ) ) as Tool [ ] ) || [ ] ;
2024-02-29 03:28:38 -08:00
2024-03-07 02:36:36 -08:00
if ( ! enforceUniqueNames ) return connectedTools ;
2024-02-29 03:28:38 -08:00
2024-03-07 02:36:36 -08:00
const seenNames = new Set < string > ( ) ;
2024-02-29 03:28:38 -08:00
2024-03-07 02:36:36 -08:00
for ( const tool of connectedTools ) {
if ( ! ( tool instanceof DynamicTool ) ) continue ;
2024-02-29 03:28:38 -08:00
2024-03-07 02:36:36 -08:00
const { name } = tool ;
if ( seenNames . has ( name ) ) {
throw new NodeOperationError (
ctx . getNode ( ) ,
` You have multiple tools with the same name: ' ${ name } ', please rename them to avoid conflicts ` ,
) ;
}
seenNames . add ( name ) ;
}
2024-02-29 03:28:38 -08:00
2024-03-07 02:36:36 -08:00
return connectedTools ;
2024-02-29 03:28:38 -08:00
} ;