-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_anthropic.js
More file actions
184 lines (152 loc) · 4.71 KB
/
api_anthropic.js
File metadata and controls
184 lines (152 loc) · 4.71 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// This file was generated with the assistance of an AI coding tool.
function parseArguments(argumentsText) {
if (!argumentsText) return {};
try {
return JSON.parse(argumentsText);
} catch {
return {};
}
}
function toAnthropicTools(tools = []) {
return tools.map((tool) => ({
name: tool.function.name,
description: tool.function.description,
input_schema: tool.function.parameters,
}));
}
function toAnthropicAssistantContent(message) {
const content = [];
if (message.content) {
content.push({ type: "text", text: message.content });
}
for (const toolCall of message.tool_calls ?? []) {
content.push({
type: "tool_use",
id: toolCall.id,
name: toolCall.function.name,
input: parseArguments(toolCall.function.arguments),
});
}
if (content.length === 0) {
return "";
}
return content.length === 1 && content[0].type === "text" ? content[0].text : content;
}
function toAnthropicUserContent(message) {
return typeof message.content === "string" ? message.content : JSON.stringify(message.content ?? "");
}
function toAnthropicToolResult(message) {
return {
type: "tool_result",
tool_use_id: message.tool_call_id,
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content ?? ""),
};
}
function splitSystemAndMessages(messages = []) {
const system = [];
const anthropicMessages = [];
let pendingToolResults = [];
const flushToolResults = () => {
if (pendingToolResults.length === 0) return;
anthropicMessages.push({ role: "user", content: pendingToolResults });
pendingToolResults = [];
};
for (const message of messages) {
if (message.role === "system") {
if (message.content) {
system.push(message.content);
}
continue;
}
if (message.role === "tool") {
pendingToolResults.push(toAnthropicToolResult(message));
continue;
}
flushToolResults();
if (message.role === "user") {
anthropicMessages.push({
role: "user",
content: toAnthropicUserContent(message),
});
continue;
}
if (message.role === "assistant") {
anthropicMessages.push({
role: "assistant",
content: toAnthropicAssistantContent(message),
});
}
}
flushToolResults();
return {
system: system.join("\n\n"),
messages: anthropicMessages,
};
}
function toChatCompletionResponse(response) {
const text = [];
const toolCalls = [];
for (const block of response.content ?? []) {
if (block.type === "text") {
text.push(block.text);
continue;
}
if (block.type === "tool_use") {
toolCalls.push({
id: block.id,
type: "function",
function: {
name: block.name,
arguments: JSON.stringify(block.input ?? {}),
},
});
}
}
const message = { role: "assistant" };
const content = text.join("\n").trim();
if (content) {
message.content = content;
}
if (toolCalls.length) {
message.tool_calls = toolCalls;
}
return {
choices: [
{
message,
},
],
};
}
export async function chat({ apiKey, model, messages, tools }) {
const request = splitSystemAndMessages(messages);
const anthropicTools = toAnthropicTools(tools);
// Mark the last tool with cache_control so the entire tool list is cached
if (anthropicTools.length > 0) {
anthropicTools[anthropicTools.length - 1].cache_control = { type: "ephemeral" };
}
const body = {
model,
max_tokens: 4096,
messages: request.messages,
tools: anthropicTools,
};
if (request.system) {
body.system = [{ type: "text", text: request.system, cache_control: { type: "ephemeral" } }];
}
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"anthropic-dangerous-direct-browser-access": "true",
},
body: JSON.stringify(body),
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Anthropic error ${res.status}: ${text}`);
}
return toChatCompletionResponse(await res.json());
}