@varlabs/ai.anthropic
Version:
AI sdk for interfacing with AI models
97 lines • 3.72 kB
JavaScript
import { defineProvider } from '@varlabs/ai/provider';
import fetch from '@varlabs/ai.utils/fetch.server';
import { handleStreamResponse } from '@varlabs/ai/utils/streaming';
const aiModels = [
// Claude 4 Models
'claude-opus-4-20250514',
'claude-opus-4-0', // alias
'claude-sonnet-4-20250514',
'claude-sonnet-4-0', // alias
// Claude 3.7 Models
'claude-3-7-sonnet-20250219',
'claude-3-7-sonnet-latest', // alias
// Claude 3.5 Models
'claude-3-5-haiku-20241022',
'claude-3-5-haiku-latest', // alias
'claude-3-5-sonnet-20241022',
'claude-3-5-sonnet-latest', // alias
'claude-3-5-sonnet-20240620', // previous version
// Claude 3 Models
'claude-3-opus-20240229',
'claude-3-opus-latest', // alias
'claude-3-sonnet-20240229',
'claude-3-haiku-20240307',
];
export const customTool = (tool) => {
return tool;
};
const anthropicProvider = defineProvider({
name: 'anthropic',
context: {
config: {
apiKey: '',
baseUrl: 'https://api.anthropic.com/v1',
apiVersion: '2023-06-01',
},
},
models: {
claude: {
messages: async (input, ctx) => {
const { apiKey, baseUrl, apiVersion } = ctx.config;
const request = await fetch(`${baseUrl}/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': apiVersion,
},
body: JSON.stringify({
...input.input,
tools: input.input.tools?.map(tool => {
if (tool.type === 'custom') {
return {
name: tool.name,
type: 'custom',
description: tool.description,
cache_control: tool.cache_control,
input_schema: tool.input_schema,
};
}
return tool;
}),
}),
MAX_FETCH_TIME: input.config?.fetchTimeout
});
for (const content of request.content) {
if (content.type === 'tool_use') {
const tool = input.input.tools?.find(t => t.name === content.name);
if (tool && tool.type === 'custom') {
const result = await tool.execute(content.input);
content.result = result;
}
}
}
return request;
},
stream: async (input, ctx) => {
const { apiKey, baseUrl, apiVersion } = ctx.config;
const response = await fetch(`${baseUrl}/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': apiVersion,
},
body: JSON.stringify({
...input.input,
stream: true,
}),
MAX_FETCH_TIME: input.config?.fetchTimeout
}, false);
return handleStreamResponse(response);
},
}
}
});
export default anthropicProvider;
//# sourceMappingURL=index.js.map