Skip to content

Commit 4bf900f

Browse files
committed
map gateway to v1/messages for anthropic
1 parent 39493de commit 4bf900f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

packages/cost/models/providers/anthropic.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
AuthResult,
55
ModelProviderConfig,
66
UserEndpointConfig,
7+
Endpoint,
8+
RequestBodyContext,
79
} from "../types";
810

911
export class AnthropicProvider extends BaseProvider {
@@ -24,16 +26,22 @@ export class AnthropicProvider extends BaseProvider {
2426
if (userConfig.gatewayMapping === "NO_MAPPING") {
2527
return "https://api.anthropic.com/v1/messages";
2628
}
27-
return "https://api.anthropic.com/v1/chat/completions";
29+
return "https://api.anthropic.com/v1/messages";
2830
}
2931

3032
authenticate(context: AuthContext): AuthResult {
3133
const headers: Record<string, string> = {};
32-
if (context.bodyMapping === "NO_MAPPING") {
33-
headers["x-api-key"] = context.apiKey || "";
34-
} else {
35-
headers["Authorization"] = `Bearer ${context.apiKey || ""}`;
36-
}
34+
headers["x-api-key"] = context.apiKey || "";
35+
headers["anthropic-version"] = "2023-06-01";
3736
return { headers };
3837
}
38+
39+
buildRequestBody(endpoint: Endpoint, context: RequestBodyContext): string {
40+
const anthropicBody = context.toAnthropic(context.parsedBody);
41+
const updatedBody = {
42+
...anthropicBody,
43+
// anthropic_version: "2023-06-01",
44+
};
45+
return JSON.stringify(updatedBody);
46+
}
3947
}

worker/src/lib/clients/llmmapper/providers/openai/request/toAnthropic.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ function extractSystemMessage(messages: OpenAIRequestBody["messages"]): {
5555
}
5656

5757
function mapModel(model: string): string {
58+
if (model.includes('claude-3.5-haiku')) {
59+
return 'claude-3-5-haiku-20241022';
60+
} else if (model.includes('claude-3.5-sonnet')) {
61+
return 'claude-3-5-sonnet-latest';
62+
}
5863
return model;
5964
}
6065

0 commit comments

Comments
 (0)