Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jul 13, 2024
2 parents 63df872 + 8e4d261 commit ec49f8a
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 97 deletions.
20 changes: 11 additions & 9 deletions app/api/anthropic/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { isModelAvailableInServer } from "@/app/utils/model";
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";

const ALLOWD_PATH = new Set([Anthropic.ChatPath, Anthropic.ChatPath1]);

Expand Down Expand Up @@ -114,7 +115,8 @@ async function request(req: NextRequest) {
10 * 60 * 1000,
);

const fetchUrl = `${baseUrl}${path}`;
// try rebuild url, when using cloudflare ai gateway in server
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}${path}`);

const fetchOptions: RequestInit = {
headers: {
Expand Down Expand Up @@ -164,17 +166,17 @@ async function request(req: NextRequest) {
console.error(`[Anthropic] filter`, e);
}
}
console.log("[Anthropic request]", fetchOptions.headers, req.method);
// console.log("[Anthropic request]", fetchOptions.headers, req.method);
try {
const res = await fetch(fetchUrl, fetchOptions);

console.log(
"[Anthropic response]",
res.status,
" ",
res.headers,
res.url,
);
// console.log(
// "[Anthropic response]",
// res.status,
// " ",
// res.headers,
// res.url,
// );
// to prevent browser prompt for credentials
const newHeaders = new Headers(res.headers);
newHeaders.delete("www-authenticate");
Expand Down
6 changes: 4 additions & 2 deletions app/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ServiceProvider,
} from "../constant";
import { isModelAvailableInServer } from "../utils/model";
import { cloudflareAIGatewayUrl } from "../utils/cloudflare";

const serverConfig = getServerSideConfig();

Expand Down Expand Up @@ -37,7 +38,7 @@ export async function requestOpenai(req: NextRequest) {
);

let baseUrl =
serverConfig.azureUrl || serverConfig.baseUrl || OPENAI_BASE_URL;
(isAzure ? serverConfig.azureUrl : serverConfig.baseUrl) || OPENAI_BASE_URL;

if (!baseUrl.startsWith("http")) {
baseUrl = `https://${baseUrl}`;
Expand Down Expand Up @@ -95,7 +96,8 @@ export async function requestOpenai(req: NextRequest) {
}
}

const fetchUrl = `${baseUrl}/${path}`;
const fetchUrl = cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
console.log("fetchUrl", fetchUrl);
const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
Expand Down
8 changes: 5 additions & 3 deletions app/client/platforms/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ACCESS_CODE_PREFIX, Anthropic, ApiPath } from "@/app/constant";
import { ChatOptions, getHeaders, LLMApi, MultimodalContent, } from "../api";
import { ChatOptions, getHeaders, LLMApi, MultimodalContent } from "../api";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
import { getClientConfig } from "@/app/config/client";
import { DEFAULT_API_HOST } from "@/app/constant";
Expand All @@ -12,6 +12,7 @@ import {
import Locale from "../../locales";
import { prettyObject } from "@/app/utils/format";
import { getMessageTextContent, isVisionModel } from "@/app/utils";
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";

export type MultiBlockContent = {
type: "image" | "text";
Expand Down Expand Up @@ -190,7 +191,7 @@ export class ClaudeApi implements LLMApi {
body: JSON.stringify(requestBody),
signal: controller.signal,
headers: {
...getHeaders(), // get common headers
...getHeaders(), // get common headers
"anthropic-version": accessStore.anthropicApiVersion,
// do not send `anthropicApiKey` in browser!!!
// Authorization: getAuthKey(accessStore.anthropicApiKey),
Expand Down Expand Up @@ -375,7 +376,8 @@ export class ClaudeApi implements LLMApi {

baseUrl = trimEnd(baseUrl, "/");

return `${baseUrl}/${path}`;
// try rebuild url, when using cloudflare ai gateway in client
return cloudflareAIGatewayUrl(`${baseUrl}/${path}`);
}
}

Expand Down
15 changes: 6 additions & 9 deletions app/client/platforms/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,13 @@ export class GeminiProApi implements LLMApi {
const controller = new AbortController();
options.onController?.(controller);
try {
// let baseUrl = accessStore.googleUrl;

if (!baseUrl) {
baseUrl = isApp
? DEFAULT_API_HOST +
"/api/proxy/google/" +
Google.ChatPath(modelConfig.model)
: this.path(Google.ChatPath(modelConfig.model));
if (!baseUrl && isApp) {
baseUrl = DEFAULT_API_HOST + "/api/proxy/google/";
}

baseUrl = `${baseUrl}/${Google.ChatPath(modelConfig.model)}`.replaceAll(
"//",
"/",
);
if (isApp) {
baseUrl += `?key=${accessStore.googleApiKey}`;
}
Expand Down
4 changes: 3 additions & 1 deletion app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@/app/constant";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
import { collectModelsWithDefaultModel } from "@/app/utils/model";
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";

import {
ChatOptions,
Expand Down Expand Up @@ -94,7 +95,8 @@ export class ChatGPTApi implements LLMApi {

console.log("[Proxy Endpoint] ", baseUrl, path);

return [baseUrl, path].join("/");
// try rebuild url, when using cloudflare ai gateway in client
return cloudflareAIGatewayUrl([baseUrl, path].join("/"));
}

extractMessage(res: any) {
Expand Down
3 changes: 1 addition & 2 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,7 @@ export function Settings() {
<ListItem
title={Locale.Settings.Access.Baidu.Endpoint.Title}
subTitle={
Locale.Settings.Access.Anthropic.Endpoint.SubTitle +
Baidu.ExampleEndpoint
Locale.Settings.Access.Baidu.Endpoint.SubTitle
}
>
<input
Expand Down
1 change: 1 addition & 0 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum ApiPath {
Azure = "/api/azure",
OpenAI = "/api/openai",
Anthropic = "/api/anthropic",
Google = "/api/google",
Baidu = "/api/baidu",
ByteDance = "/api/bytedance",
Alibaba = "/api/alibaba",
Expand Down
6 changes: 3 additions & 3 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,18 @@ const cn = {
},
Baidu: {
ApiKey: {
Title: "接口密钥",
Title: "API Key",
SubTitle: "使用自定义 Baidu API Key",
Placeholder: "Baidu API Key",
},
SecretKey: {
Title: "接口密钥",
Title: "Secret Key",
SubTitle: "使用自定义 Baidu Secret Key",
Placeholder: "Baidu Secret Key",
},
Endpoint: {
Title: "接口地址",
SubTitle: "样例:",
SubTitle: "不支持自定义前往.env配置",
},
},
ByteDance: {
Expand Down
10 changes: 5 additions & 5 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ const en: LocaleType = {

Endpoint: {
Title: "Endpoint Address",
SubTitle: "Example:",
SubTitle: "Example: ",
},

ApiVerion: {
Expand All @@ -347,7 +347,7 @@ const en: LocaleType = {
},
Endpoint: {
Title: "Endpoint Address",
SubTitle: "Example:",
SubTitle: "not supported, configure in .env",
},
},
ByteDance: {
Expand All @@ -358,7 +358,7 @@ const en: LocaleType = {
},
Endpoint: {
Title: "Endpoint Address",
SubTitle: "Example:",
SubTitle: "Example: ",
},
},
Alibaba: {
Expand All @@ -369,7 +369,7 @@ const en: LocaleType = {
},
Endpoint: {
Title: "Endpoint Address",
SubTitle: "Example:",
SubTitle: "Example: ",
},
},
CustomModel: {
Expand All @@ -385,7 +385,7 @@ const en: LocaleType = {

Endpoint: {
Title: "Endpoint Address",
SubTitle: "Example:",
SubTitle: "Example: ",
},

ApiVersion: {
Expand Down
Loading

0 comments on commit ec49f8a

Please sign in to comment.