Skip to content

Commit d35381d

Browse files
committed
custom header to azure open ai model
1 parent 308a6f6 commit d35381d

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

packages/@n8n/nodes-langchain/credentials/AzureOpenAiApi.credentials.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { IAuthenticateGeneric, ICredentialType, INodeProperties } from 'n8n-workflow';
1+
import type {
2+
ICredentialDataDecryptedObject,
3+
ICredentialType,
4+
IDisplayOptions,
5+
IHttpRequestOptions,
6+
INodeProperties,
7+
} from 'n8n-workflow';
28

39
export class AzureOpenAiApi implements ICredentialType {
410
name = 'azureOpenAiApi';
@@ -37,14 +43,50 @@ export class AzureOpenAiApi implements ICredentialType {
3743
default: undefined,
3844
placeholder: 'https://westeurope.api.cognitive.microsoft.com',
3945
},
40-
];
41-
42-
authenticate: IAuthenticateGeneric = {
43-
type: 'generic',
44-
properties: {
45-
headers: {
46-
'api-key': '={{$credentials.apiKey}}',
46+
{
47+
displayName: 'Add Custom Header',
48+
name: 'header',
49+
type: 'boolean',
50+
default: false,
51+
},
52+
{
53+
displayName: 'Header Name',
54+
name: 'headerName',
55+
type: 'string',
56+
displayOptions: {
57+
show: {
58+
header: [true],
59+
},
60+
} as IDisplayOptions,
61+
default: '',
62+
},
63+
{
64+
displayName: 'Header Value',
65+
name: 'headerValue',
66+
type: 'string',
67+
typeOptions: {
68+
password: true,
4769
},
70+
displayOptions: {
71+
show: {
72+
header: [true],
73+
},
74+
} as IDisplayOptions,
75+
default: '',
4876
},
49-
};
77+
];
78+
79+
async authenticate(
80+
credentials: ICredentialDataDecryptedObject,
81+
requestOptions: IHttpRequestOptions,
82+
): Promise<IHttpRequestOptions> {
83+
requestOptions.headers = { 'api-key': credentials.apiKey };
84+
if (credentials.header) {
85+
requestOptions.headers = {
86+
'api-key': credentials.apiKey,
87+
[credentials.headerName as string]: credentials.headerValue,
88+
};
89+
}
90+
return requestOptions;
91+
}
5092
}

packages/@n8n/nodes-langchain/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ export class LmChatOpenAi implements INodeType {
358358
dispatcher: getProxyAgent(configuration.baseURL ?? 'https://api.openai.com/v1'),
359359
};
360360
}
361+
if (credentials.header) {
362+
configuration.defaultHeaders = {
363+
[credentials.headerName as string]: credentials.headerValue as string,
364+
};
365+
}
361366

362367
// Extra options to send to OpenAI, that are not directly supported by LangChain
363368
const modelKwargs: {

packages/@n8n/nodes-langchain/nodes/llms/LmChatAzureOpenAi/LmChatAzureOpenAi.node.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ export class LmChatAzureOpenAi implements INodeType {
103103

104104
this.logger.info(`Instantiating AzureChatOpenAI model with deployment: ${modelName}`);
105105

106+
const credentials = await this.getCredentials('azureOpenAiApi');
107+
const configuration = {
108+
fetchOptions: {
109+
dispatcher: getProxyAgent(),
110+
},
111+
defaultHeaders: {},
112+
};
113+
if (credentials.header) {
114+
configuration.defaultHeaders = {
115+
[credentials.headerName as string]: credentials.headerValue as string,
116+
};
117+
}
106118
// Create and return the model
107119
const model = new AzureChatOpenAI({
108120
azureOpenAIApiDeploymentName: modelName,
@@ -111,11 +123,7 @@ export class LmChatAzureOpenAi implements INodeType {
111123
timeout: options.timeout ?? 60000,
112124
maxRetries: options.maxRetries ?? 2,
113125
callbacks: [new N8nLlmTracing(this)],
114-
configuration: {
115-
fetchOptions: {
116-
dispatcher: getProxyAgent(),
117-
},
118-
},
126+
configuration,
119127
modelKwargs: options.responseFormat
120128
? {
121129
response_format: { type: options.responseFormat },

0 commit comments

Comments
 (0)