-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclaude.ts
63 lines (52 loc) · 1.49 KB
/
claude.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// import { Client, AI_PROMPT, HUMAN_PROMPT } from '@anthropic-ai/sdk';
export const HUMAN_PROMPT = "\n\nHuman:";
export const AI_PROMPT = "\n\nAssistant:";
export type SamplingParameters = {
prompt: string;
temperature?: number;
max_tokens_to_sample: number;
stop_sequences: string[];
top_k?: number;
top_p?: number;
model: string;
tags?: { [key: string]: string };
};
export type CompletionResponse = {
completion: string;
stop: string | null;
stop_reason: "stop_sequence" | "max_tokens";
truncated: boolean;
exception: string | null;
log_id: string;
};
const DEFAULT_API_URL = "https://api.anthropic.com";
const apiKey = process.env.ANTHROPIC_API_KEY as string;
// export const client = new Client(apiKey);
export async function complete(
params: SamplingParameters,
options?: { signal?: AbortSignal }
): Promise<CompletionResponse> {
const response = await fetch(`${DEFAULT_API_URL}/v1/complete`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Client: "anthropic-typescript/0.4.3",
"X-API-Key": apiKey,
},
body: JSON.stringify({ ...params, stream: false }),
signal: options?.signal,
});
if (!response.ok) {
const error = new Error(
`Sampling error: ${response.status} ${response.statusText}`
);
console.error(error);
throw error;
}
const completion = (await response.json()) as CompletionResponse;
return completion;
}
export default {
complete,
}