fixes google ai gemini

This commit is contained in:
nai-degen 2024-09-25 15:58:52 -05:00
parent 29638cf26e
commit a7fed3136e
3 changed files with 9 additions and 19 deletions

View File

@ -105,8 +105,8 @@ function transformGoogleAIResponse(
const googleAIProxy = createQueuedProxyMiddleware({ const googleAIProxy = createQueuedProxyMiddleware({
target: ({ signedRequest }) => { target: ({ signedRequest }) => {
if (!signedRequest) throw new Error("Must sign request before proxying"); if (!signedRequest) throw new Error("Must sign request before proxying");
const { protocol, hostname, path } = signedRequest; const { protocol, hostname} = signedRequest;
return `${protocol}//${hostname}${path}`; return `${protocol}//${hostname}`;
}, },
mutations: [addGoogleAIKey, finalizeSignedRequest], mutations: [addGoogleAIKey, finalizeSignedRequest],
blockingResponseHandler: googleAIBlockingResponseHandler, blockingResponseHandler: googleAIBlockingResponseHandler,
@ -120,11 +120,7 @@ googleAIRouter.post(
"/v1beta/models/:modelId:(generateContent|streamGenerateContent)", "/v1beta/models/:modelId:(generateContent|streamGenerateContent)",
ipLimiter, ipLimiter,
createPreprocessorMiddleware( createPreprocessorMiddleware(
{ { inApi: "google-ai", outApi: "google-ai", service: "google-ai" },
inApi: "google-ai",
outApi: "google-ai",
service: "google-ai",
},
{ beforeTransform: [maybeReassignModel], afterTransform: [setStreamFlag] } { beforeTransform: [maybeReassignModel], afterTransform: [setStreamFlag] }
), ),
googleAIProxy googleAIProxy

View File

@ -107,23 +107,16 @@ function pinoLoggerPlugin(proxyServer: ProxyServer<Request>) {
"Error occurred while proxying request to target" "Error occurred while proxying request to target"
); );
}); });
proxyServer.on("proxyReq", (proxyReq, req, res) => { proxyServer.on("proxyReq", (proxyReq, req) => {
const originalUrl = req.originalUrl; const from = req.originalUrl;
const targetHost = `${proxyReq.protocol}//${proxyReq.host}`;
const targetPath = res.req.url;
req.log.info( req.log.info(
{ originalUrl, targetHost, targetPath }, { from, to: `${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}` },
"Sending request to upstream API..." "Sending request to upstream API..."
); );
}); });
proxyServer.on("proxyRes", (proxyRes: ProxiedResponse, req, _res) => { proxyServer.on("proxyRes", (proxyRes: ProxiedResponse, req, _res) => {
const originalUrl = req.originalUrl; const target = `${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path}`;
const targetHost = `${proxyRes.req.protocol}//${proxyRes.req.hostname}`;
const targetPath = proxyRes.req.path;
const statusCode = proxyRes.statusCode; const statusCode = proxyRes.statusCode;
req.log.info( req.log.info({ target, statusCode }, "Got response from upstream API.");
{ originalUrl, targetHost, targetPath, statusCode },
"Got response from upstream API."
);
}); });
} }

View File

@ -48,6 +48,7 @@ export function getHttpAgents() {
process.env.HTTP_PROXY = proxyUrl; process.env.HTTP_PROXY = proxyUrl;
process.env.HTTPS_PROXY = proxyUrl; process.env.HTTPS_PROXY = proxyUrl;
process.env.WS_PROXY = proxyUrl; process.env.WS_PROXY = proxyUrl;
process.env.WSS_PROXY = proxyUrl;
httpAgent = new ProxyAgent(); httpAgent = new ProxyAgent();
httpsAgent = httpAgent; // ProxyAgent automatically handles HTTPS httpsAgent = httpAgent; // ProxyAgent automatically handles HTTPS
const proxy = proxyUrl.replace(/:.*@/, "@******"); const proxy = proxyUrl.replace(/:.*@/, "@******");