Automatically add Anthropic "\n\nHuman:" preamble when necessary (khanon/oai-reverse-proxy!23)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { AnthropicKey, Key } from "../../../key-management";
|
||||
import { isCompletionRequest } from "../common";
|
||||
import { ProxyRequestMiddleware } from ".";
|
||||
|
||||
/**
|
||||
* Some keys require the prompt to start with `\n\nHuman:`. There is no way to
|
||||
* know this without trying to send the request and seeing if it fails. If a
|
||||
* key is marked as requiring a preamble, it will be added here.
|
||||
*/
|
||||
export const addAnthropicPreamble: ProxyRequestMiddleware = (
|
||||
_proxyReq,
|
||||
req
|
||||
) => {
|
||||
if (!isCompletionRequest(req) || req.key?.service !== "anthropic") {
|
||||
return;
|
||||
}
|
||||
|
||||
let preamble = "";
|
||||
let prompt = req.body.prompt;
|
||||
assertAnthropicKey(req.key);
|
||||
if (req.key.requiresPreamble) {
|
||||
preamble = prompt.startsWith("\n\nHuman:") ? "" : "\n\nHuman:";
|
||||
req.log.debug({ key: req.key.hash, preamble }, "Adding preamble to prompt");
|
||||
}
|
||||
req.body.prompt = preamble + prompt;
|
||||
};
|
||||
|
||||
function assertAnthropicKey(key: Key): asserts key is AnthropicKey {
|
||||
if (key.service !== "anthropic") {
|
||||
throw new Error(`Expected an Anthropic key, got '${key.service}'`);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export { transformOutboundPayload } from "./transform-outbound-payload";
|
||||
|
||||
// HPM middleware (runs on onProxyReq, cannot be async)
|
||||
export { addKey } from "./add-key";
|
||||
export { addAnthropicPreamble } from "./add-anthropic-preamble";
|
||||
export { finalizeBody } from "./finalize-body";
|
||||
export { languageFilter } from "./language-filter";
|
||||
export { limitCompletions } from "./limit-completions";
|
||||
|
||||
@@ -153,19 +153,10 @@ function openaiToAnthropic(body: any, req: Request) {
|
||||
// Remove duplicates
|
||||
stops = [...new Set(stops)];
|
||||
|
||||
// TEMP: More shitty anthropic API hacks
|
||||
// If you receive a 400 Bad Request error from Anthropic complaining about
|
||||
// "prompt must start with a '\n\nHuman: ' turn", enable this setting.
|
||||
// I will try to fix this when I can identify why it only happens sometimes.
|
||||
let preamble = "";
|
||||
if (process.env.CLAUDE_ADD_HUMAN_PREAMBLE) {
|
||||
preamble = "\n\nHuman: Hello Claude.";
|
||||
}
|
||||
|
||||
return {
|
||||
...rest,
|
||||
model,
|
||||
prompt: preamble + prompt,
|
||||
prompt: prompt,
|
||||
max_tokens_to_sample: rest.max_tokens,
|
||||
stop_sequences: stops,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user