From 49367fa62a578b5cf4207f609db80655c684eadc Mon Sep 17 00:00:00 2001 From: Andy Brenneke Date: Thu, 1 Aug 2024 09:03:15 -0700 Subject: [PATCH] Add "additional parameters" to chat node --- packages/core/src/model/nodes/ChatNode.ts | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/packages/core/src/model/nodes/ChatNode.ts b/packages/core/src/model/nodes/ChatNode.ts index ff12b7077..00012fd53 100644 --- a/packages/core/src/model/nodes/ChatNode.ts +++ b/packages/core/src/model/nodes/ChatNode.ts @@ -54,6 +54,7 @@ export type ChatNodeConfigData = { toolChoiceFunction?: string; responseFormat?: 'text' | 'json'; parallelFunctionCalling?: boolean; + additionalParameters?: { key: string; value: string }[]; }; export type ChatNodeData = ChatNodeConfigData & { @@ -75,6 +76,7 @@ export type ChatNodeData = ChatNodeConfigData & { useToolChoiceInput?: boolean; useToolChoiceFunctionInput?: boolean; useResponseFormatInput?: boolean; + useAdditionalParametersInput?: boolean; /** Given the same set of inputs, return the same output without hitting GPT */ cache: boolean; @@ -131,6 +133,9 @@ export class ChatNodeImpl extends NodeImpl { useAsGraphPartialOutput: true, parallelFunctionCalling: true, + + additionalParameters: [], + useAdditionalParametersInput: false, }, }; @@ -322,6 +327,15 @@ export class ChatNodeImpl extends NodeImpl { }); } + if (this.data.useAdditionalParametersInput) { + inputs.push({ + dataType: 'object', + id: 'additionalParameters' as PortId, + title: 'Additional Parameters', + description: 'Additional chat completion parameters to send to the API.', + }); + } + return inputs; } @@ -630,6 +644,16 @@ export class ChatNodeImpl extends NodeImpl { helperMessage: 'If on, streaming responses from this node will be shown in Subgraph nodes that call this graph.', }, + { + type: 'keyValuePair', + label: 'Additional Parameters', + dataKey: 'additionalParameters', + useInputToggleDataKey: 'useAdditionalParametersInput', + keyPlaceholder: 'Parameter', + valuePlaceholder: 'Value', + helperMessage: + 'Additional chat completion parameters to send to the API. If the value appears to be a number, it will be sent as a number.', + }, ], }, ]; @@ -723,6 +747,19 @@ export class ChatNodeImpl extends NodeImpl { headersFromData : headersFromData; + const additionalParametersFromData = (this.data.additionalParameters ?? []).reduce( + (acc, param) => { + acc[param.key] = Number.isNaN(parseFloat(param.value)) ? param.value : parseFloat(param.value); + return acc; + }, + {} as Record, + ); + const additionalParameters = this.data.useAdditionalParametersInput + ? (coerceTypeOptional(inputs['additionalParameters' as PortId], 'object') as + | Record + | undefined) ?? additionalParametersFromData + : additionalParametersFromData; + // If using a model input, that's priority, otherwise override > main const finalModel = this.data.useModelInput && inputs['model' as PortId] != null ? model : overrideModel || model; @@ -816,7 +853,9 @@ export class ChatNodeImpl extends NodeImpl { seed, response_format: openaiResponseFormat, tool_choice: toolChoice, + ...additionalParameters, }; + const cacheKey = JSON.stringify(options); if (this.data.cache) {