Add different effort levels for models
Add model variants for OpenAI and Anthropic APIs to support varying levels of reasoning effort, allowing clients to select effort by model name. Replit-Commit-Author: Agent Replit-Commit-Session-Id: dcd090ca-935a-42ae-8db4-503e1bbe165a Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 9c490fc7-dd25-43e2-b6d5-ada682953c75 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/40608c50-a053-47e8-aa03-1bdcc1a2698b/dcd090ca-935a-42ae-8db4-503e1bbe165a/ZBSB1hW Replit-Helium-Checkpoint-Created: true
This commit is contained in:
@@ -14,16 +14,43 @@ const anthropic = new Anthropic({
|
||||
apiKey: process.env["AI_INTEGRATIONS_ANTHROPIC_API_KEY"],
|
||||
});
|
||||
|
||||
const MODELS = [
|
||||
{ id: "gpt-5.2", provider: "openai" },
|
||||
{ id: "gpt-5-mini", provider: "openai" },
|
||||
{ id: "gpt-5-nano", provider: "openai" },
|
||||
{ id: "o4-mini", provider: "openai" },
|
||||
{ id: "o3", provider: "openai" },
|
||||
{ id: "claude-opus-4-6", provider: "anthropic" },
|
||||
{ id: "claude-sonnet-4-6", provider: "anthropic" },
|
||||
{ id: "claude-haiku-4-5", provider: "anthropic" },
|
||||
] as const;
|
||||
const REASONING_BASE_MODELS = [
|
||||
{ id: "gpt-5.2", provider: "openai" as const },
|
||||
{ id: "gpt-5-mini", provider: "openai" as const },
|
||||
{ id: "gpt-5-nano", provider: "openai" as const },
|
||||
{ id: "o4-mini", provider: "openai" as const },
|
||||
{ id: "o3", provider: "openai" as const },
|
||||
{ id: "claude-opus-4-6", provider: "anthropic" as const },
|
||||
{ id: "claude-sonnet-4-6", provider: "anthropic" as const },
|
||||
{ id: "claude-haiku-4-5", provider: "anthropic" as const },
|
||||
];
|
||||
|
||||
const EFFORT_LEVELS = ["minimal", "low", "medium", "high"] as const;
|
||||
type EffortLevel = (typeof EFFORT_LEVELS)[number];
|
||||
|
||||
interface ModelEntry {
|
||||
id: string;
|
||||
provider: "openai" | "anthropic";
|
||||
baseModel: string;
|
||||
effort: EffortLevel | null;
|
||||
}
|
||||
|
||||
const MODELS: ModelEntry[] = REASONING_BASE_MODELS.flatMap((m) => {
|
||||
const variants: ModelEntry[] = [
|
||||
{ id: m.id, provider: m.provider, baseModel: m.id, effort: null },
|
||||
];
|
||||
for (const effort of EFFORT_LEVELS) {
|
||||
variants.push({
|
||||
id: `${m.id}-${effort}`,
|
||||
provider: m.provider,
|
||||
baseModel: m.id,
|
||||
effort,
|
||||
});
|
||||
}
|
||||
return variants;
|
||||
});
|
||||
|
||||
const MODEL_INDEX: Map<string, ModelEntry> = new Map(MODELS.map((m) => [m.id, m]));
|
||||
|
||||
function authorize(req: Request, res: Response): boolean {
|
||||
const expected = process.env["PROXY_API_KEY"];
|
||||
@@ -115,19 +142,36 @@ router.post("/chat/completions", async (req, res) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const stream = body.stream === true;
|
||||
const requestedId = body.model;
|
||||
const entry = MODEL_INDEX.get(requestedId);
|
||||
|
||||
if (isOpenAIModel(body.model)) {
|
||||
await handleOpenAI(req, res, body, stream);
|
||||
} else if (isAnthropicModel(body.model)) {
|
||||
await handleAnthropic(req, res, body, stream);
|
||||
let provider: "openai" | "anthropic";
|
||||
if (entry) {
|
||||
body.model = entry.baseModel;
|
||||
if (entry.effort && !body.reasoning_effort) {
|
||||
body.reasoning_effort = entry.effort;
|
||||
}
|
||||
provider = entry.provider;
|
||||
} else if (isOpenAIModel(requestedId)) {
|
||||
provider = "openai";
|
||||
} else if (isAnthropicModel(requestedId)) {
|
||||
provider = "anthropic";
|
||||
} else {
|
||||
res.status(400).json({
|
||||
error: {
|
||||
message: `Unsupported model: ${body.model}`,
|
||||
message: `Unsupported model: ${requestedId}`,
|
||||
type: "invalid_request_error",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const stream = body.stream === true;
|
||||
|
||||
if (provider === "openai") {
|
||||
await handleOpenAI(req, res, body, stream);
|
||||
} else {
|
||||
await handleAnthropic(req, res, body, stream);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user