feat(Anthropic Chat Model Node): Add configurable base URL for Anthropic API (#15063)

This commit is contained in:
oleg
2025-05-05 09:57:10 +02:00
committed by GitHub
parent 54499fc6d1
commit 4b5f045281
4 changed files with 56 additions and 6 deletions

View File

@@ -266,8 +266,10 @@ export class LmChatAnthropic implements INodeType {
};
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('anthropicApi');
const credentials = await this.getCredentials<{ url?: string; apiKey?: string }>(
'anthropicApi',
);
const baseURL = credentials.url ?? 'https://api.anthropic.com';
const version = this.getNode().typeVersion;
const modelName =
version >= 1.3
@@ -317,8 +319,9 @@ export class LmChatAnthropic implements INodeType {
}
const model = new ChatAnthropic({
anthropicApiKey: credentials.apiKey as string,
anthropicApiKey: credentials.apiKey,
modelName,
anthropicApiUrl: baseURL,
maxTokens: options.maxTokensToSample,
temperature: options.temperature,
topK: options.topK,

View File

@@ -40,6 +40,7 @@ describe('searchModels', () => {
beforeEach(() => {
mockContext = {
getCredentials: jest.fn().mockResolvedValue({}),
helpers: {
httpRequestWithAuthentication: jest.fn().mockResolvedValue({
data: mockModels,
@@ -50,11 +51,47 @@ describe('searchModels', () => {
afterEach(() => {
jest.clearAllMocks();
// Reset the getCredentials mock to its default value
mockContext.getCredentials = jest.fn().mockResolvedValue({});
});
it('should fetch models from Anthropic API', async () => {
it('should fetch models from default Anthropic API URL when no custom URL is provided', async () => {
const result = await searchModels.call(mockContext);
expect(mockContext.getCredentials).toHaveBeenCalledWith('anthropicApi');
expect(mockContext.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith('anthropicApi', {
url: 'https://api.anthropic.com/v1/models',
headers: {
'anthropic-version': '2023-06-01',
},
});
expect(result.results).toHaveLength(5);
});
it('should fetch models from custom Anthropic API URL when provided in credentials', async () => {
const customUrl = 'https://custom-anthropic-api.example.com';
// Override the default mock to return credentials with a custom URL
mockContext.getCredentials = jest.fn().mockResolvedValue({ url: customUrl });
const result = await searchModels.call(mockContext);
expect(mockContext.getCredentials).toHaveBeenCalledWith('anthropicApi');
expect(mockContext.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith('anthropicApi', {
url: `${customUrl}/v1/models`,
headers: {
'anthropic-version': '2023-06-01',
},
});
expect(result.results).toHaveLength(5);
});
it('should use default URL when empty URL is provided in credentials', async () => {
// Override the default mock to return credentials with an empty URL
mockContext.getCredentials = jest.fn().mockResolvedValue({ url: null });
const result = await searchModels.call(mockContext);
expect(mockContext.getCredentials).toHaveBeenCalledWith('anthropicApi');
expect(mockContext.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith('anthropicApi', {
url: 'https://api.anthropic.com/v1/models',
headers: {

View File

@@ -15,8 +15,11 @@ export async function searchModels(
this: ILoadOptionsFunctions,
filter?: string,
): Promise<INodeListSearchResult> {
const credentials = await this.getCredentials<{ url?: string }>('anthropicApi');
const baseURL = credentials.url ?? 'https://api.anthropic.com';
const response = (await this.helpers.httpRequestWithAuthentication.call(this, 'anthropicApi', {
url: 'https://api.anthropic.com/v1/models',
url: `${baseURL}/v1/models`,
headers: {
'anthropic-version': '2023-06-01',
},