2023-11-29 03:13:55 -08:00
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType ,
type INodeType ,
type INodeTypeDescription ,
2024-10-28 03:37:23 -07:00
type ISupplyDataFunctions ,
2023-11-29 03:13:55 -08:00
type SupplyData ,
} from 'n8n-workflow' ;
2024-03-07 02:36:36 -08:00
import { Cohere } from '@langchain/cohere' ;
2023-11-29 03:13:55 -08:00
import { getConnectionHintNoticeField } from '../../../utils/sharedFields' ;
2024-05-12 12:12:07 -07:00
import { N8nLlmTracing } from '../N8nLlmTracing' ;
2024-11-08 01:17:11 -08:00
import { makeN8nLlmFailedAttemptHandler } from '../n8nLlmFailedAttemptHandler' ;
2023-11-29 03:13:55 -08:00
export class LmCohere implements INodeType {
description : INodeTypeDescription = {
displayName : 'Cohere Model' ,
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
name : 'lmCohere' ,
2024-07-12 00:28:46 -07:00
icon : { light : 'file:cohere.svg' , dark : 'file:cohere.dark.svg' } ,
2023-11-29 03:13:55 -08:00
group : [ 'transform' ] ,
version : 1 ,
description : 'Language Model Cohere' ,
defaults : {
name : 'Cohere Model' ,
} ,
codex : {
categories : [ 'AI' ] ,
subcategories : {
2024-08-05 04:59:02 -07:00
AI : [ 'Language Models' , 'Root Nodes' ] ,
2024-07-23 07:40:28 -07:00
'Language Models' : [ 'Text Completion Models' ] ,
2023-11-29 03:13:55 -08:00
} ,
resources : {
primaryDocumentation : [
{
url : 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmcohere/' ,
} ,
] ,
} ,
} ,
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
inputs : [ ] ,
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
outputs : [ NodeConnectionType . AiLanguageModel ] ,
outputNames : [ 'Model' ] ,
credentials : [
{
name : 'cohereApi' ,
required : true ,
} ,
] ,
properties : [
getConnectionHintNoticeField ( [ NodeConnectionType . AiChain , NodeConnectionType . AiAgent ] ) ,
{
displayName : 'Options' ,
name : 'options' ,
placeholder : 'Add Option' ,
description : 'Additional options to add' ,
type : 'collection' ,
default : { } ,
options : [
{
displayName : 'Maximum Number of Tokens' ,
name : 'maxTokens' ,
default : 250 ,
description :
'The maximum number of tokens to generate in the completion. Most models have a context length of 2048 tokens (except for the newest models, which support 32,768).' ,
type : 'number' ,
typeOptions : {
maxValue : 32768 ,
} ,
} ,
{
displayName : 'Model' ,
name : 'model' ,
type : 'string' ,
description : 'The name of the model to use' ,
default : '' ,
} ,
{
displayName : 'Sampling Temperature' ,
name : 'temperature' ,
default : 0 ,
typeOptions : { maxValue : 1 , minValue : 0 , numberPrecision : 1 } ,
description :
'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.' ,
type : 'number' ,
} ,
] ,
} ,
] ,
} ;
2024-10-28 03:37:23 -07:00
async supplyData ( this : ISupplyDataFunctions , itemIndex : number ) : Promise < SupplyData > {
2023-11-29 03:13:55 -08:00
const credentials = await this . getCredentials ( 'cohereApi' ) ;
const options = this . getNodeParameter ( 'options' , itemIndex , { } ) as object ;
const model = new Cohere ( {
apiKey : credentials.apiKey as string ,
. . . options ,
2024-05-12 12:12:07 -07:00
callbacks : [ new N8nLlmTracing ( this ) ] ,
2024-11-08 01:17:11 -08:00
onFailedAttempt : makeN8nLlmFailedAttemptHandler ( this ) ,
2023-11-29 03:13:55 -08:00
} ) ;
return {
2024-05-12 12:12:07 -07:00
response : model ,
2023-11-29 03:13:55 -08:00
} ;
}
}