This repository has been archived on 2026-02-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
siri-shortcuts/utils/anthropicClient.ts
Riccardo Senica 30c67a308b Data storage (#3)
* feat: add prisma

* fix: generate prisma
2025-06-23 19:04:01 +02:00

44 lines
1.1 KiB
TypeScript

import Anthropic from '@anthropic-ai/sdk';
interface AnthropicResponse {
text: string;
tokensUsed: number;
}
export async function getMessage(text: string): Promise<AnthropicResponse> {
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
console.info('Anthropic request with text: ', text);
const response = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 2048,
messages: [{ role: 'user', content: text }]
});
console.info('Anthropic response: ', response);
try {
const data = response.content as [{ type: string; text: string }];
const tokensUsed =
(response.usage?.input_tokens || 0) +
(response.usage?.output_tokens || 0);
console.info('Token usage:', {
input_tokens: response.usage?.input_tokens,
output_tokens: response.usage?.output_tokens,
total: tokensUsed
});
return {
text: data[0].text,
tokensUsed
};
} catch (error) {
throw new Error(`Anthropic client error: ${JSON.stringify(error)}.`);
}
}