-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathanthropic.ts
More file actions
93 lines (81 loc) · 2.37 KB
/
Copy pathanthropic.ts
File metadata and controls
93 lines (81 loc) · 2.37 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
buildStudioLlmOutputLimitExceededMessage,
type StudioLlmRequest,
} from "../../data/llm";
type FetchLike = (...args: Parameters<typeof fetch>) => ReturnType<typeof fetch>;
export const ANTHROPIC_DEMO_MODEL = "claude-haiku-4-5-20251001";
const ANTHROPIC_API_URL = "https://api.anthropic.com/v1/messages";
const ANTHROPIC_API_VERSION = "2023-06-01";
export const ANTHROPIC_MAX_TOKENS = 2048;
interface AnthropicMessageResponse {
content?: Array<{
text?: string;
type?: string;
}>;
error?: {
message?: string;
};
stop_reason?: string | null;
}
export class AnthropicOutputLimitError extends Error {
constructor(message: string) {
super(message);
this.name = "AnthropicOutputLimitError";
}
}
export async function runAnthropicLlmRequest(args: {
apiKey: string;
fetchImplementation?: FetchLike;
request: StudioLlmRequest;
}): Promise<string> {
const { apiKey, fetchImplementation = fetch, request } = args;
const httpRequest = {
body: JSON.stringify({
max_tokens: ANTHROPIC_MAX_TOKENS,
messages: [
{
content: request.prompt,
role: "user",
},
],
model: ANTHROPIC_DEMO_MODEL,
}),
headers: {
"anthropic-version": ANTHROPIC_API_VERSION,
"content-type": "application/json",
"x-api-key": apiKey,
},
method: "POST",
} satisfies RequestInit;
console.info("[demo][anthropic] request", {
maxTokens: ANTHROPIC_MAX_TOKENS,
method: httpRequest.method,
model: ANTHROPIC_DEMO_MODEL,
promptLength: request.prompt.length,
task: request.task,
url: ANTHROPIC_API_URL,
});
const response = await fetchImplementation(ANTHROPIC_API_URL, httpRequest);
const payload = (await response.json()) as AnthropicMessageResponse;
if (!response.ok) {
throw new Error(
payload.error?.message ??
`Anthropic request failed (${response.status} ${response.statusText}).`,
);
}
if (payload.stop_reason === "max_tokens") {
throw new AnthropicOutputLimitError(
buildStudioLlmOutputLimitExceededMessage({
maxTokens: ANTHROPIC_MAX_TOKENS,
provider: "Anthropic",
}),
);
}
const firstTextBlock = payload.content?.find(
(block) => block.type === "text",
);
if (!firstTextBlock?.text) {
throw new Error("Anthropic response did not include any text content.");
}
return firstTextBlock.text;
}