adds ?debug=true query param to have proxy respond with transformed prompt

This commit is contained in:
nai-degen
2024-03-14 08:16:38 -05:00
parent 276a1a1d44
commit 367ac3d075
10 changed files with 71 additions and 105 deletions
-4
View File
@@ -56,10 +56,6 @@ export function sendProxyError(
? `The proxy encountered an error while trying to process your prompt.`
: `The proxy encountered an error while trying to send your prompt to the upstream service.`;
if (req.tokenizerInfo && typeof errorPayload.error === "object") {
errorPayload.error.proxy_tokenizer = req.tokenizerInfo;
}
sendErrorToClient({
options: {
format: req.inboundApi,
@@ -26,15 +26,6 @@ function getMessageContent({
"error": {
"type": "not_found_error",
"message": "model: some-invalid-model-id",
"proxy_tokenizer": {
"tokenizer": "@anthropic-ai/tokenizer",
"token_count": 6104,
"tokenization_duration_ms": 4.0765,
"prompt_tokens": 6104,
"completion_tokens": 30,
"max_model_tokens": 200000,
"max_proxy_tokens": 9007199254740991
}
},
"proxy_note": "The requested Claude model might not exist, or the key might not be provisioned for it."
}
+34
View File
@@ -23,6 +23,7 @@ import {
import { handleStreamedResponse } from "./handle-streamed-response";
import { logPrompt } from "./log-prompt";
import { saveImage } from "./save-image";
import { config } from "../../../config";
const DECODER_MAP = {
gzip: util.promisify(zlib.gunzip),
@@ -105,6 +106,7 @@ export const createOnProxyResHandler = (apiMiddleware: ProxyResMiddleware) => {
} else {
middlewareStack.push(
trackRateLimit,
addProxyInfo,
handleUpstreamErrors,
countResponseTokens,
incrementUsage,
@@ -706,6 +708,38 @@ const copyHttpHeaders: ProxyResHandlerWithBody = async (
});
};
/**
* Injects metadata into the response, such as the tokenizer used, logging
* status, upstream API endpoint used, and whether the input prompt was modified
* or transformed.
* Only used for non-streaming requests.
*/
const addProxyInfo: ProxyResHandlerWithBody = async (
_proxyRes,
req,
res,
body
) => {
const { service, inboundApi, outboundApi, tokenizerInfo } = req;
const native = inboundApi === outboundApi;
const info: any = {
logged: config.promptLogging,
tokens: tokenizerInfo,
service,
in_api: inboundApi,
out_api: outboundApi,
prompt_transformed: !native,
};
if (req.query?.debug?.length) {
info.final_request_body = req.signedRequest?.body || req.body;
}
if (typeof body === "object") {
body.proxy = info;
}
};
function getAwsErrorType(header: string | string[] | undefined) {
const val = String(header).match(/^(\w+):?/)?.[1];
return val || String(header);