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
synthetic-consumer-data/utils/anthropicClient.ts

48 lines
1.2 KiB
TypeScript

import 'dotenv/config';
import Anthropic from '@anthropic-ai/sdk';
export interface BaseTool {
readonly name: string;
readonly input_schema: {
readonly type: 'object';
readonly properties: Record<string, unknown>;
readonly required?: readonly string[];
readonly description?: string;
};
}
export async function makeRequest<T extends BaseTool>(prompt: string, tool: T) {
if (!process.env.ANTHROPIC_API_KEY) {
throw Error('No Anthropic API key found.');
}
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
try {
const response = await client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 8192,
temperature: 1,
tools: [tool],
messages: [{ role: 'user', content: prompt }]
});
if (response.stop_reason && response.stop_reason !== 'tool_use') {
throw Error(JSON.stringify(response));
}
if (response.content.length != 2) {
throw Error(JSON.stringify(response));
}
const content = response.content as [
{ type: string; text: string },
{ type: string; input: object }
];
return content[1].input;
} catch (error) {
throw Error('Anthropic client error.');
}
}