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 68 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
4 changes: 4 additions & 0 deletions .github/workflows/js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ jobs:
pnpm install
pnpm run test
pnpm run build
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
.*\.(json|prisma|svg)|
.*pnpm-lock.yaml
)$
args: ["-L rouge,coo,couldn,unsecure"]
args: ["-L rouge,coo,couldn,unsecure,afterall"]
- repo: https://github.com/rbubley/mirrors-prettier
rev: v3.3.2
hooks:
Expand Down
20 changes: 20 additions & 0 deletions packages/proxy/edge/deps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import skott from "skott";
import { describe, expect, it } from "vitest";

describe("proxy/edge", () => {
it("no circ dependencies", async () => {
const { useGraph } = await skott({
entrypoint: `${__dirname}/index.ts`,
tsConfigPath: `${__dirname}/../tsconfig.json`,
dependencyTracking: {
builtin: false,
thirdParty: true,
typeOnly: true,
},
});

const { findCircularDependencies } = useGraph();

expect(findCircularDependencies()).toEqual([]);
});
});
19 changes: 14 additions & 5 deletions packages/proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@braintrust/proxy",
"version": "0.0.7",
"version": "0.0.8",
"description": "A proxy server that load balances across AI providers.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -36,12 +36,19 @@
"import": "./utils/dist/index.mjs",
"module": "./utils/dist/index.mjs",
"require": "./utils/dist/index.js"
},
"./types": {
"types": "./types/dist/index.d.ts",
"import": "./types/dist/index.mjs",
"module": "./types/dist/index.mjs",
"require": "./types/dist/index.js"
}
},
"files": [
"dist/**/*",
"edge/dist/**/*",
"schema/dist/**/*"
"schema/dist/**/*",
"types/dist/**/*"
],
"license": "MIT",
"publishConfig": {
Expand Down Expand Up @@ -74,7 +81,9 @@
"@types/yargs": "^17.0.33",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"esbuild": "^0.19.10",
"msw": "^2.8.2",
"npm-run-all": "^4.1.5",
"skott": "^0.35.4",
"tsup": "^8.4.0",
"typescript": "5.5.4",
"vite-tsconfig-paths": "^4.3.2",
Expand All @@ -84,10 +93,10 @@
"dependencies": {
"@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",
"@aws-sdk/client-bedrock-runtime": "^3.806.0",
"@braintrust/core": "^0.0.87",
"@breezystack/lamejs": "^1.2.7",
"@google/generative-ai": "^0.24.0",
"@google/genai": "^0.13.0",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/core": "^1.19.0",
"@opentelemetry/resources": "^1.19.0",
Expand Down
20 changes: 20 additions & 0 deletions packages/proxy/schema/deps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import skott from "skott";
import { describe, expect, it } from "vitest";

describe("proxy/schema", () => {
it("no circ dependencies", async () => {
const { useGraph } = await skott({
entrypoint: `${__dirname}/index.ts`,
tsConfigPath: `${__dirname}/../tsconfig.json`,
dependencyTracking: {
builtin: false,
thirdParty: true,
typeOnly: true,
},
});

const { findCircularDependencies } = useGraph();

expect(findCircularDependencies()).toEqual([]);
});
});
270 changes: 270 additions & 0 deletions packages/proxy/schema/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
import { MessageCreateParamsBase } from "@anthropic-ai/sdk/resources/messages";
import { GenerateContentParameters } from "@google/genai";
import { ChatCompletionCreateParams } from "openai/resources";
import { expect, it } from "vitest";
import { ModelFormat, translateParams } from "./index";

const examples: Record<
string,
{
openai: ChatCompletionCreateParams;
} & ( // NOTE: these are not strictly the API params.
| { google: GenerateContentParameters }
| { anthropic: MessageCreateParamsBase }
)
> = {
simple: {
openai: {
model: "gpt-4o",
max_tokens: 1500,
temperature: 0.7,
top_p: 0.9,
frequency_penalty: 0.1,
presence_penalty: 0.2,
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello, how are you?" },
],
stream: true,
},
google: {
maxOutputTokens: 1500,
max_tokens: 1500,
messages: [
{
content: "You are a helpful assistant.",
role: "system",
},
{
content: "Hello, how are you?",
role: "user",
},
],
model: "gpt-4o",
stream: true,
temperature: 0.7,
top_p: 0.9,
},
anthropic: {
max_tokens: 1500,
messages: [
{
content: "You are a helpful assistant.",
// @ts-expect-error -- TODO: shouldn't we have translated this to a non system role?
role: "system",
},
{
content: "Hello, how are you?",
role: "user",
},
],
model: "gpt-4o",
stream: true,
temperature: 0.7,
top_p: 0.9,
},
},
reasoning_effort: {
openai: {
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
max_tokens: 1000,
reasoning_effort: "high",
stream: false,
},
google: {
model: "gpt-4o",
// notice how this is still an intermediate param
// google's api expects a content instead of messages, for example
messages: [
{
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
thinkingConfig: {
thinkingBudget: 800,
includeThoughts: true,
},
maxOutputTokens: 1000,
max_tokens: 1000,
stream: false,
},
anthropic: {
model: "gpt-4o",
messages: [
{
// @ts-expect-error -- we use the role to later manipulate the request
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 1,
stream: false,
max_tokens: 1536,
thinking: {
budget_tokens: 1024,
type: "enabled",
},
},
},
"reasoning disable": {
openai: {
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
reasoning_enabled: false,
reasoning_budget: 1024,
stream: false,
},
google: {
model: "gpt-4o",
// notice how this is still an intermediate param
// google's api expects a content instead of messages, for example
messages: [
{
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
thinkingConfig: {
thinkingBudget: 0,
},
stream: false,
},
anthropic: {
model: "gpt-4o",
messages: [
{
// @ts-expect-error -- we use the role to later manipulate the request
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
stream: false,
max_tokens: 1024,
thinking: {
type: "disabled",
},
},
},
"reasoning budget": {
openai: {
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
reasoning_enabled: true,
reasoning_budget: 1024,
stream: false,
},
google: {
model: "gpt-4o",
// notice how this is still an intermediate param
// google's api expects a content instead of messages, for example
messages: [
{
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 0,
thinkingConfig: {
thinkingBudget: 1024,
includeThoughts: true,
},
stream: false,
},
anthropic: {
model: "gpt-4o",
messages: [
{
// @ts-expect-error -- we use the role to later manipulate the request
role: "system",
content: "You are a detailed reasoning assistant.",
},
{
role: "user",
content: "Explain how to solve 2x + 4 = 12 step by step.",
},
],
temperature: 1,
stream: false,
max_tokens: 1536,
thinking: {
budget_tokens: 1024,
type: "enabled",
},
},
},
};

Object.entries(examples).forEach(([example, { openai, ...providers }]) => {
Object.entries(providers).forEach(([provider, expected]) => {
it(`[${example}] translate openai to ${provider} params`, () => {
const result = translateParams(
provider as ModelFormat,
openai as unknown as Record<string, unknown>,
);
try {
expect(result).toEqual(expected);
} catch (error) {
console.warn(
`Exact openai -> ${provider} translation failed. Found:`,
JSON.stringify(result, null, 2),
);
expect.soft(result).toEqual(expected);
}
});
});
});
Loading