Skip to content

Commit 21c9b9b

Browse files
committed
feat(plugins/anthropic): pull list of models from anthropic SDK
1 parent 22332e5 commit 21c9b9b

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

plugins/anthropic/src/index.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { genkitPluginV2 } from 'genkit/plugin';
17+
import { genkitPluginV2, model, modelActionMetadata } from 'genkit/plugin';
1818
import Anthropic from '@anthropic-ai/sdk';
1919

2020
import {
@@ -31,6 +31,7 @@ import {
3131
} from './claude.js';
3232
import { ModelAction } from 'genkit/model';
3333
import { ActionType } from 'genkit/registry';
34+
import { ActionMetadata } from 'genkit';
3435

3536
export {
3637
claude4Sonnet,
@@ -48,6 +49,28 @@ export interface PluginOptions {
4849
cacheSystemPrompt?: boolean;
4950
}
5051

52+
async function list(client: Anthropic): Promise<ActionMetadata[]> {
53+
const clientModels = (await client.models.list()).data;
54+
55+
return clientModels
56+
.map((modelInfo) => {
57+
// Remove the final part if it's a date (8 digits)
58+
const idParts = modelInfo.id.split('-');
59+
const normalizedId = idParts[idParts.length - 1].match(/^\d{8}$/)
60+
? idParts.slice(0, -1).join('-')
61+
: modelInfo.id;
62+
const ref = SUPPORTED_CLAUDE_MODELS[normalizedId];
63+
return ref
64+
? modelActionMetadata({
65+
name: ref.name,
66+
info: ref.info,
67+
configSchema: ref.configSchema,
68+
})
69+
: null;
70+
})
71+
.filter(Boolean) as ActionMetadata[];
72+
}
73+
5174
/**
5275
* This module provides an interface to the Anthropic AI models through the Genkit plugin system.
5376
* It allows users to interact with various Claude models by providing an API key and optional configuration.
@@ -92,6 +115,7 @@ export const anthropic = (options?: PluginOptions) => {
92115
const client = new Anthropic({ apiKey, defaultHeaders });
93116

94117
const cachedActions: ModelAction[] = [];
118+
let listActionsCache;
95119

96120
return genkitPluginV2({
97121
name: 'anthropic',
@@ -115,12 +139,11 @@ export const anthropic = (options?: PluginOptions) => {
115139
}
116140
return undefined;
117141
},
118-
list: () => {
119-
return cachedActions.map((action) => ({
120-
name: action.name,
121-
type: 'model',
122-
}));
123-
},
142+
list: async () => {
143+
if (listActionsCache) return listActionsCache;
144+
listActionsCache = await list(client);
145+
return listActionsCache;
146+
}
124147
});
125148
};
126149

0 commit comments

Comments
 (0)