Skip to content
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

google api using x-goog-api-key header #5541

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion app/api/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async function request(req: NextRequest, apiKey: string) {
},
10 * 60 * 1000,
);
const fetchUrl = `${baseUrl}${path}?key=${apiKey}${
const fetchUrl = `${baseUrl}${path}${
req?.nextUrl?.searchParams?.get("alt") === "sse" ? "&alt=sse" : ""
}`;

Expand All @@ -100,6 +100,9 @@ async function request(req: NextRequest, apiKey: string) {
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-store",
"x-google-api-key":
req.headers.get("x-google-api-key") ||
(req.headers.get("Authorization") ?? "").replace("Bearer "),
},
method: req.method,
body: req.body,
Expand Down
15 changes: 11 additions & 4 deletions app/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ export function getHeaders(ignoreHeaders: boolean = false) {
}

function getAuthHeader(): string {
return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
return isAzure
? "api-key"
: isAnthropic
? "x-api-key"
: isGoogle
? "x-goog-api-key"
: "Authorization";
}

const {
Expand All @@ -283,14 +289,15 @@ export function getHeaders(ignoreHeaders: boolean = false) {
apiKey,
isEnabledAccessControl,
} = getConfig();
// when using google api in app, not set auth header
if (isGoogle && clientConfig?.isApp) return headers;
// when using baidu api in app, not set auth header
if (isBaidu && clientConfig?.isApp) return headers;

const authHeader = getAuthHeader();

const bearerToken = getBearerToken(apiKey, isAzure || isAnthropic);
const bearerToken = getBearerToken(
apiKey,
isAzure || isAnthropic || isGoogle,
);

if (bearerToken) {
headers[authHeader] = bearerToken;
Expand Down
4 changes: 0 additions & 4 deletions app/client/platforms/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ export class GeminiProApi implements LLMApi {
let chatPath = [baseUrl, path].join("/");

chatPath += chatPath.includes("?") ? "&alt=sse" : "?alt=sse";
// if chatPath.startsWith('http') then add key in query string
if (chatPath.startsWith("http") && accessStore.googleApiKey) {
chatPath += `&key=${accessStore.googleApiKey}`;
}
return chatPath;
}
extractMessage(res: any) {
Expand Down
Loading