Skip to content

Add Anthropic thinking params and responses support #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 69 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
f48f5a6
initial refactor from o1_like to reasoning
Apr 25, 2025
dce6a69
rough attempt at translating reasoning_effort (openai) to thinking (c…
Apr 25, 2025
fbc05e8
support anthropic thinking responses
Apr 28, 2025
19cae64
handle reasoning oai-like messages
Apr 29, 2025
c5b50e6
assume a more generalized reasoning object
Apr 29, 2025
342a4ea
allow reasoning_effort to be defaulted
Apr 30, 2025
fdb87e9
add support for gemini
Apr 30, 2025
f85073b
should include the reasoning delta
May 3, 2025
3c7bc81
include the reasoning type to chunk delta
May 3, 2025
bebacfd
include reasoning when we're done with the stream
May 3, 2025
d43314b
temp: use local dir for proxy to appease build errors
May 3, 2025
ea0bacf
small cleanup
May 7, 2025
cbb8ec0
Merge branch 'main' into thinking-params
May 7, 2025
383514b
start local tests for proxyV1 and add coverage for anthorpic
May 8, 2025
5250106
one cannot assume the bodyData is a valid json
May 8, 2025
de802e3
add test coverage for reasoning and improve type handling when we ext…
May 8, 2025
3084245
start to move types from core to new types/openai.ts
May 9, 2025
b311dae
add google test coverage and upgrade us away from deprecated library …
May 9, 2025
0914e78
return bedrock & openai (responses) reasoning
May 9, 2025
7d5a0d2
Merge branch 'main' into thinking-params
May 9, 2025
0e4de2d
bump version
May 9, 2025
c1876db
use 0.0.86
May 13, 2025
67a5bdc
get ci/cd working again
May 13, 2025
4a9eb62
icnrease timeout
May 13, 2025
3beba3b
add test coverage and fix mapping methods
May 13, 2025
d8eae39
add some dev notes
May 13, 2025
7ff7188
reconcile the latest on gemini models & vertex ai
May 15, 2025
33677b1
add support for reasoning_effort: undefined to disable reasoning toke…
May 15, 2025
c6ba935
PR feedback
May 15, 2025
b10694a
add coverage for openai call checking
May 16, 2025
1bd2e52
Merge branch 'main' into thinking-params
May 16, 2025
0e0ebb3
avoid including includeThoughts if disabled
May 16, 2025
c5858dd
fix tests
May 16, 2025
3562ca7
fix disable + no max tokens.. add coverage
May 16, 2025
517ecf0
add type overrides
May 16, 2025
080535d
Merge branch 'main' into thinking-params
May 19, 2025
fdb4625
add reasoning budget to model spec and set defaults
May 20, 2025
bd0b391
let's get reasoning_enabled and reasoning_budget translated
May 20, 2025
7106774
refactor mapping to handle the whole mapping
May 20, 2025
92ac83c
Merge branch 'main' into thinking-params
May 20, 2025
f14807e
Merge branch 'main' into thinking-params
May 20, 2025
fba1dff
remove the bare import
May 21, 2025
7d19720
fix max tokens adjustment for anthorpic
May 21, 2025
2f11f2a
make sure to include reasoning tokens if available
May 21, 2025
f68bb95
remove extra debugger
May 21, 2025
45fb63a
update google test now that gemini models return reasoning tokens
May 21, 2025
4f68885
fix tests
May 21, 2025
81c6968
add gemini flash preview 05-20
May 21, 2025
6e661b4
disable vertex test again
May 21, 2025
c3915df
Merge branch 'main' into thinking-params
May 21, 2025
c24f911
Merge branch 'main' into thinking-params
May 22, 2025
0d570b3
Merge branch 'main' into thinking-params
May 22, 2025
0f68f3b
Merge branch 'main' into thinking-params
May 22, 2025
5755308
set the reasoning for claude 4
May 22, 2025
a0277d7
try reverting the ci change
May 23, 2025
94e0a63
Merge branch 'main' into thinking-params
May 23, 2025
ba02e9d
Merge branch 'main' into thinking-params
May 23, 2025
8cd6c41
switch to default export with cacheControl and jsonwebtoken
May 24, 2025
b9ddec6
Merge remote-tracking branch 'origin/thinking-params' into thinking-p…
May 24, 2025
56b38df
add `skott` to assert no circular dependencies issues
May 25, 2025
3e42887
Merge branch 'fix-dependency-issues' into thinking-params
May 26, 2025
7438e82
fix more circ dependencies
May 26, 2025
4981ced
Merge branch 'main' into thinking-params
May 27, 2025
c95c56a
update bedrock/vertex ai opus/sonnet 4 to have reasoning
May 27, 2025
19a2510
Merge remote-tracking branch 'origin/thinking-params' into thinking-p…
May 27, 2025
fcaf564
Merge branch 'main' into thinking-params
May 30, 2025
f9cd4eb
add reasoning enabled and budget for bedrock/vertex claude providers
May 30, 2025
35fd2c0
Merge remote-tracking branch 'origin/thinking-params' into thinking-p…
May 30, 2025
b01da09
add delta override with new reasoning chunk
May 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source_up_if_exists
dotenv_if_exists
2 changes: 1 addition & 1 deletion packages/proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"@anthropic-ai/sdk": "^0.39.0",
"@apidevtools/json-schema-ref-parser": "^11.9.1",
"@aws-sdk/client-bedrock-runtime": "^3.738.0",
"@braintrust/core": "^0.0.85",
"@braintrust/core": "link:../../../sdk/core/js",
"@breezystack/lamejs": "^1.2.7",
"@google/generative-ai": "^0.24.0",
"@opentelemetry/api": "^1.7.0",
Expand Down
106 changes: 97 additions & 9 deletions packages/proxy/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,79 @@ export const modelParamToModelParam: {
stream_options: null,
parallel_tool_calls: null,
response_format: null,
reasoning_effort: null,
reasoning_effort: "reasoning_effort",
stop: null,
};

const effortToBudgetMultiplier = {
low: 0.2,
medium: 0.5,
high: 0.8,
} as const;

const getBudgetMultiplier = (effort: keyof typeof effortToBudgetMultiplier) => {
return effortToBudgetMultiplier[effort] || effortToBudgetMultiplier.low;
};

export const modelParamMappers: {
[name in ModelFormat]?: {
[param: string]: (params: any) => Record<string, unknown>;
};
} = {
anthropic: {
reasoning_effort: ({
reasoning_effort,
max_tokens,
max_completion_tokens,
temperature: _,
...params
}) => {
// Max tokens are inclusive of budget. If the max tokens are too low (below 1024), then the API will raise an exception.
const maxTokens = Math.max(
max_completion_tokens || max_tokens || 0,
1024 / effortToBudgetMultiplier.low,
);

const budget = getBudgetMultiplier(reasoning_effort || "low") * maxTokens;

return {
...params,
max_tokens: maxTokens,
// must be set when using thinking
temperature: 1,
thinking: {
budget_tokens: budget,
type: "enabled",
},
};
},
},
google: {
reasoning_effort: ({
reasoning_effort,
max_tokens,
max_completion_tokens,
...params
}) => {
const maxTokens = Math.max(
max_completion_tokens || max_tokens || 0,
1024 / effortToBudgetMultiplier.low,
);

const budget = getBudgetMultiplier(reasoning_effort || "low") * maxTokens;

return {
...params,
thinkingConfig: {
thinkingBudget: budget,
includeThoughts: true,
},
maxOutputTokens: maxTokens,
};
},
},
};

export const sliderSpecs: {
// min, max, step, required
[name: string]: [number, number, number, boolean];
Expand Down Expand Up @@ -82,13 +151,15 @@ export const defaultModelParamSettings: {
response_format: null,
stop: undefined,
use_cache: true,
reasoning_effort: "medium",
},
anthropic: {
temperature: undefined,
max_tokens: undefined,
top_p: 0.7,
top_k: 5,
use_cache: true,
reasoning_effort: "medium",
},
google: {
temperature: undefined,
Expand Down Expand Up @@ -121,6 +192,17 @@ export const modelProviderHasTools: {
converse: true,
};

export const modelProviderHasReasoning: {
[name in ModelFormat]?: RegExp;
} = {
openai: /^o[1-4]/i,
anthropic: /^claude-3\.7/i,
google: /gemini-2.0-flash$|gemini-2.5/i,
js: undefined,
window: undefined,
converse: undefined,
};

export const DefaultEndpointTypes: {
[name in ModelFormat]: ModelEndpointType[];
} = {
Expand Down Expand Up @@ -427,23 +509,29 @@ export function translateParams(
toProvider: ModelFormat,
params: Record<string, unknown>,
): Record<string, unknown> {
const translatedParams: Record<string, unknown> = {};
let translatedParams: Record<string, unknown> = {};
for (const [k, v] of Object.entries(params || {})) {
const safeValue = v ?? undefined; // Don't propagate "null" along
const mapper = modelParamMappers[toProvider]?.[k];
if (mapper) {
translatedParams = mapper(translatedParams);
continue;
}

const translatedKey = modelParamToModelParam[k as keyof ModelParams] as
| keyof ModelParams
| undefined
| null;

if (translatedKey === null) {
continue;
} else if (
translatedKey !== undefined &&
defaultModelParamSettings[toProvider][translatedKey] !== undefined
) {
translatedParams[translatedKey] = safeValue;
} else {
translatedParams[k] = safeValue;
}

const hasDefaultParam =
translatedKey !== undefined &&
defaultModelParamSettings[toProvider][translatedKey] !== undefined;

translatedParams[hasDefaultParam ? translatedKey : k] = safeValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would we want to set translatedParams[k] to anything if translatedKey is undefined? this is also the case prior to your change so im curious if you found the use case. ill revisit in #217 so no need to do anything

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I haven't checked. I honestly think we need to remove this section and instead just do the translate params mappers that I started. the idea is that you get openai as input and get anthropic params as the output.

This way we centralize that kind of translation to a spot vs. right now it's a pseudo translation and then have each provider decide how to grab the data from openai-like params.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but right now this PR is doing something in between. i think we should revert this bit so we don't regress behavior and figure out the right thing to do in another PR

}

return translatedParams;
Expand Down
37 changes: 22 additions & 15 deletions packages/proxy/schema/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const ModelSchema = z.object({
input_cost_per_mil_tokens: z.number().nullish(),
output_cost_per_mil_tokens: z.number().nullish(),
displayName: z.string().nullish(),
o1_like: z.boolean().nullish(),
o1_like: z.boolean().nullish().describe('DEPRECATED use "reasoning" instead'),
reasoning: z.boolean().nullish(),
experimental: z.boolean().nullish(),
deprecated: z.boolean().nullish(),
parent: z.string().nullish(),
Expand Down Expand Up @@ -159,15 +160,15 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: true,
input_cost_per_mil_tokens: 1.1,
output_cost_per_mil_tokens: 4.4,
o1_like: true,
reasoning: true,
},
"o4-mini-2025-04-16": {
format: "openai",
flavor: "chat",
multimodal: true,
input_cost_per_mil_tokens: 1.1,
output_cost_per_mil_tokens: 4.4,
o1_like: true,
reasoning: true,
parent: "o4-mini",
},
"o3-mini": {
Expand All @@ -176,15 +177,15 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: true,
input_cost_per_mil_tokens: 1.1,
output_cost_per_mil_tokens: 4.4,
o1_like: true,
reasoning: true,
},
"o3-mini-2025-01-31": {
format: "openai",
flavor: "chat",
multimodal: true,
input_cost_per_mil_tokens: 1.1,
output_cost_per_mil_tokens: 4.4,
o1_like: true,
reasoning: true,
parent: "o3-mini",
},
o3: {
Expand All @@ -193,15 +194,15 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: true,
input_cost_per_mil_tokens: 10.0,
output_cost_per_mil_tokens: 40,
o1_like: true,
reasoning: true,
},
"o3-2025-04-16": {
format: "openai",
flavor: "chat",
multimodal: true,
input_cost_per_mil_tokens: 10.0,
output_cost_per_mil_tokens: 40,
o1_like: true,
reasoning: true,
parent: "o3",
},
o1: {
Expand All @@ -210,15 +211,15 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: true,
input_cost_per_mil_tokens: 15.0,
output_cost_per_mil_tokens: 60,
o1_like: true,
reasoning: true,
},
"o1-2024-12-17": {
format: "openai",
flavor: "chat",
multimodal: true,
input_cost_per_mil_tokens: 15.0,
output_cost_per_mil_tokens: 60,
o1_like: true,
reasoning: true,
parent: "o1",
},
"o1-mini": {
Expand All @@ -227,15 +228,15 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: false,
input_cost_per_mil_tokens: 3.0,
output_cost_per_mil_tokens: 12.0,
o1_like: true,
reasoning: true,
},
"o1-mini-2024-09-12": {
format: "openai",
flavor: "chat",
multimodal: false,
input_cost_per_mil_tokens: 3.0,
output_cost_per_mil_tokens: 12.0,
o1_like: true,
reasoning: true,
parent: "o1-mini",
},
"o1-pro": {
Expand All @@ -244,15 +245,15 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: true,
input_cost_per_mil_tokens: 150,
output_cost_per_mil_tokens: 600,
o1_like: true,
reasoning: true,
},
"o1-pro-2025-03-19": {
format: "openai",
flavor: "chat",
multimodal: true,
input_cost_per_mil_tokens: 150,
output_cost_per_mil_tokens: 600,
o1_like: true,
reasoning: true,
parent: "o1-pro",
},
"chatgpt-4o-latest": {
Expand Down Expand Up @@ -349,8 +350,8 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: false,
input_cost_per_mil_tokens: 15.0,
output_cost_per_mil_tokens: 60,
o1_like: true,
experimental: true,
reasoning: true,
parent: "o1",
},
"o1-preview-2024-09-12": {
Expand All @@ -359,8 +360,8 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
multimodal: false,
input_cost_per_mil_tokens: 15.0,
output_cost_per_mil_tokens: 60.0,
o1_like: true,
experimental: true,
reasoning: true,
parent: "o1",
},
"gpt-4o-search-preview": {
Expand Down Expand Up @@ -547,6 +548,7 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
input_cost_per_mil_tokens: 3,
output_cost_per_mil_tokens: 15,
displayName: "Claude 3.7 Sonnet",
reasoning: true,
},
"claude-3-7-sonnet-20250219": {
format: "anthropic",
Expand Down Expand Up @@ -1917,6 +1919,7 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
output_cost_per_mil_tokens: 0.6,
multimodal: true,
experimental: false,
reasoning: true,
displayName: "Gemini 2.5 Flash Preview",
},
"gemini-2.5-pro-preview-03-25": {
Expand All @@ -1926,6 +1929,7 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
output_cost_per_mil_tokens: 10,
multimodal: true,
experimental: false,
reasoning: true,
displayName: "Gemini 2.5 Pro Preview",
},
"gemini-2.5-pro-exp-03-25": {
Expand All @@ -1935,6 +1939,7 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
output_cost_per_mil_tokens: 0,
multimodal: true,
experimental: true,
reasoning: true,
displayName: "Gemini 2.5 Pro Experimental",
},
"gemini-2.0-flash-exp": {
Expand Down Expand Up @@ -2523,6 +2528,8 @@ export const AvailableModels: { [name: string]: ModelSpec } = {
displayName: "Command Light",
},

// TODO: add anthropic 3.7 converse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going to have do it as a follow-up


// VERTEX MODELS
"publishers/google/models/gemini-2.0-flash": {
format: "google",
Expand Down
1 change: 1 addition & 0 deletions packages/proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./util";
export * from "./proxy";
export * from "./metrics";
export * from "./types";
Loading
Loading