Merge remote-tracking branch 'upstream/main'

# Conflicts:
#	app/layout.tsx
#	package.json
#	yarn.lock
This commit is contained in:
sijinhui 2024-01-02 22:46:56 +08:00
commit 8a5915d122
17 changed files with 113 additions and 91 deletions

View File

@ -14,8 +14,8 @@ PROXY_URL=http://localhost:7890
GOOGLE_API_KEY= GOOGLE_API_KEY=
# (optional) # (optional)
# Default: https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent # Default: https://generativelanguage.googleapis.com/
# Googel Gemini Pro API url, set if you want to customize Google Gemini Pro API url. # Googel Gemini Pro API url without pathname, set if you want to customize Google Gemini Pro API url.
GOOGLE_URL= GOOGLE_URL=
# Override openai api request base url. (optional) # Override openai api request base url. (optional)

View File

@ -1,5 +1,5 @@
<div align="center"> <div align="center">
<img src="./docs/images/icon.svg" alt="icon"/> <img src="./docs/images/head-cover.png" alt="icon"/>
<h1 align="center">NextChat (ChatGPT Next Web)</h1> <h1 align="center">NextChat (ChatGPT Next Web)</h1>
@ -61,10 +61,11 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4
## What's New ## What's New
- 🚀 v2.0 is released, now you can create prompt templates, turn your ideas into reality! Read this: [ChatGPT Prompt Engineering Tips: Zero, One and Few Shot Prompting](https://www.allabtai.com/prompt-engineering-tips-zero-one-and-few-shot-prompting/). - 🚀 v2.10.1 support Google Gemini Pro model.
- 🚀 v2.7 let's share conversations as image, or share to ShareGPT!
- 🚀 v2.8 now we have a client that runs across all platforms!
- 🚀 v2.9.11 you can use azure endpoint now. - 🚀 v2.9.11 you can use azure endpoint now.
- 🚀 v2.8 now we have a client that runs across all platforms!
- 🚀 v2.7 let's share conversations as image, or share to ShareGPT!
- 🚀 v2.0 is released, now you can create prompt templates, turn your ideas into reality! Read this: [ChatGPT Prompt Engineering Tips: Zero, One and Few Shot Prompting](https://www.allabtai.com/prompt-engineering-tips-zero-one-and-few-shot-prompting/).
## 主要功能 ## 主要功能
@ -360,9 +361,11 @@ If you want to add a new translation, read this [document](./docs/translation.md
[@Licoy](https://github.com/Licoy) [@Licoy](https://github.com/Licoy)
[@shangmin2009](https://github.com/shangmin2009) [@shangmin2009](https://github.com/shangmin2009)
### Contributor ### Contributors
[Contributors](https://github.com/Yidadaa/ChatGPT-Next-Web/graphs/contributors) <a href="https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web/graphs/contributors">
<img src="https://contrib.rocks/image?repo=ChatGPTNextWeb/ChatGPT-Next-Web" />
</a>
## LICENSE ## LICENSE

View File

@ -9,6 +9,7 @@ import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client"; import { getClientConfig } from "@/app/config/client";
import Locale from "../../locales"; import Locale from "../../locales";
import { getServerSideConfig } from "@/app/config/server"; import { getServerSideConfig } from "@/app/config/server";
import de from "@/app/locales/de";
export class GeminiProApi implements LLMApi { export class GeminiProApi implements LLMApi {
extractMessage(res: any) { extractMessage(res: any) {
console.log("[Response] gemini-pro response: ", res); console.log("[Response] gemini-pro response: ", res);
@ -20,6 +21,7 @@ export class GeminiProApi implements LLMApi {
); );
} }
async chat(options: ChatOptions): Promise<void> { async chat(options: ChatOptions): Promise<void> {
const apiClient = this;
const messages = options.messages.map((v) => ({ const messages = options.messages.map((v) => ({
role: v.role.replace("assistant", "model").replace("system", "user"), role: v.role.replace("assistant", "model").replace("system", "user"),
parts: [{ text: v.content }], parts: [{ text: v.content }],
@ -57,12 +59,29 @@ export class GeminiProApi implements LLMApi {
topP: modelConfig.top_p, topP: modelConfig.top_p,
// "topK": modelConfig.top_k, // "topK": modelConfig.top_k,
}, },
safetySettings: [
{
category: "HARM_CATEGORY_HARASSMENT",
threshold: "BLOCK_ONLY_HIGH",
},
{
category: "HARM_CATEGORY_HATE_SPEECH",
threshold: "BLOCK_ONLY_HIGH",
},
{
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold: "BLOCK_ONLY_HIGH",
},
{
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
threshold: "BLOCK_ONLY_HIGH",
},
],
}; };
console.log("[Request] google payload: ", requestPayload); console.log("[Request] google payload: ", requestPayload);
// todo: support stream later const shouldStream = !!options.config.stream;
const shouldStream = false;
const controller = new AbortController(); const controller = new AbortController();
options.onController?.(controller); options.onController?.(controller);
try { try {
@ -82,13 +101,23 @@ export class GeminiProApi implements LLMApi {
if (shouldStream) { if (shouldStream) {
let responseText = ""; let responseText = "";
let remainText = ""; let remainText = "";
let streamChatPath = chatPath.replace(
"generateContent",
"streamGenerateContent",
);
let finished = false; let finished = false;
let existingTexts: string[] = [];
const finish = () => {
finished = true;
options.onFinish(existingTexts.join(""));
};
// animate response to make it looks smooth // animate response to make it looks smooth
function animateResponseText() { function animateResponseText() {
if (finished || controller.signal.aborted) { if (finished || controller.signal.aborted) {
responseText += remainText; responseText += remainText;
console.log("[Response Animation] finished"); finish();
return; return;
} }
@ -105,87 +134,55 @@ export class GeminiProApi implements LLMApi {
// start animaion // start animaion
animateResponseText(); animateResponseText();
fetch(streamChatPath, chatPayload)
.then((response) => {
const reader = response?.body?.getReader();
const decoder = new TextDecoder();
let partialData = "";
const finish = () => { return reader?.read().then(function processText({
if (!finished) { done,
value,
}): Promise<any> {
if (done) {
console.log("Stream complete");
// options.onFinish(responseText + remainText);
finished = true; finished = true;
options.onFinish(responseText + remainText); return Promise.resolve();
} }
};
controller.signal.onabort = finish; partialData += decoder.decode(value, { stream: true });
fetchEventSource(chatPath, { try {
...chatPayload, let data = JSON.parse(ensureProperEnding(partialData));
async onopen(res) {
clearTimeout(requestTimeoutId); const textArray = data.reduce(
const contentType = res.headers.get("content-type"); (acc: string[], item: { candidates: any[] }) => {
console.log( const texts = item.candidates.map((candidate) =>
"[OpenAI] request response content type: ", candidate.content.parts
contentType, .map((part: { text: any }) => part.text)
.join(""),
);
return acc.concat(texts);
},
[],
); );
if (contentType?.startsWith("text/plain")) { if (textArray.length > existingTexts.length) {
responseText = await res.clone().text(); const deltaArray = textArray.slice(existingTexts.length);
return finish(); existingTexts = textArray;
remainText += deltaArray.join("");
}
} catch (error) {
// console.log("[Response Animation] error: ", error,partialData);
// skip error message when parsing json
} }
if ( return reader.read().then(processText);
!res.ok || });
!res.headers })
.get("content-type") .catch((error) => {
?.startsWith(EventStreamContentType) || console.error("Error:", error);
res.status !== 200
) {
const responseTexts = [responseText];
let extraInfo = await res.clone().text();
try {
const resJson = await res.clone().json();
extraInfo = prettyObject(resJson);
} catch {}
if (res.status === 401) {
responseTexts.push(Locale.Error.Unauthorized);
}
if (extraInfo) {
responseTexts.push(extraInfo);
}
responseText = responseTexts.join("\n\n");
return finish();
}
},
onmessage(msg) {
if (msg.data === "[DONE]" || finished) {
return finish();
}
const text = msg.data;
try {
const json = JSON.parse(text) as {
choices: Array<{
delta: {
content: string;
};
}>;
};
const delta = json.choices[0]?.delta?.content;
if (delta) {
remainText += delta;
}
} catch (e) {
console.error("[Request] parse error", text);
}
},
onclose() {
finish();
},
onerror(e) {
options.onError?.(e);
throw e;
},
openWhenHidden: true,
}); });
} else { } else {
const res = await fetch(chatPath, chatPayload); const res = await fetch(chatPath, chatPayload);
@ -220,3 +217,10 @@ export class GeminiProApi implements LLMApi {
return "/api/google/" + path; return "/api/google/" + path;
} }
} }
function ensureProperEnding(str: string) {
if (str.startsWith("[") && !str.endsWith("]")) {
return str + "]";
}
return str;
}

View File

@ -91,8 +91,7 @@ export const Azure = {
}; };
export const Google = { export const Google = {
ExampleEndpoint: ExampleEndpoint: "https://generativelanguage.googleapis.com/",
"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent",
ChatPath: "v1beta/models/gemini-pro:generateContent", ChatPath: "v1beta/models/gemini-pro:generateContent",
// /api/openai/v1/chat/completions // /api/openai/v1/chat/completions

View File

@ -4,6 +4,10 @@ import "./styles/markdown.scss";
import "./styles/highlight.scss"; import "./styles/highlight.scss";
import { getClientConfig } from "./config/client"; import { getClientConfig } from "./config/client";
import { type Metadata } from "next"; import { type Metadata } from "next";
import { SpeedInsights } from "@vercel/speed-insights/next";
import { getServerSideConfig } from "./config/server";
const serverConfig = getServerSideConfig();
import { Providers } from "@/app/providers"; import { Providers } from "@/app/providers";
import { Viewport } from "next"; import { Viewport } from "next";
@ -43,6 +47,11 @@ export default function RootLayout({
</head> </head>
<body> <body>
<Providers>{children}</Providers> <Providers>{children}</Providers>
{serverConfig?.isVercel && (
<>
<SpeedInsights />
</>
)}
</body> </body>
</html> </html>
); );

View File

@ -362,7 +362,7 @@ const cn = {
Endpoint: { Endpoint: {
Title: "接口地址", Title: "接口地址",
SubTitle: "样例:", SubTitle: "不包含请求路径,样例:",
}, },
ApiVerion: { ApiVerion: {

View File

@ -18,7 +18,11 @@ export default async function App() {
return ( return (
<> <>
<Home /> <Home />
{serverConfig?.isVercel && <Analytics />} {serverConfig?.isVercel && (
<>
<Analytics />
</>
)}
</> </>
); );
} }

View File

@ -689,7 +689,9 @@ export const useChatStore = createPersistStore(
const contextPrompts = session.mask.context.slice(); const contextPrompts = session.mask.context.slice();
// system prompts, to get close to OpenAI Web ChatGPT // system prompts, to get close to OpenAI Web ChatGPT
const shouldInjectSystemPrompts = modelConfig.enableInjectSystemPrompts; const shouldInjectSystemPrompts =
modelConfig.enableInjectSystemPrompts &&
session.mask.modelConfig.model.startsWith("gpt-");
var systemPrompts: ChatMessage[] = []; var systemPrompts: ChatMessage[] = [];
systemPrompts = shouldInjectSystemPrompts systemPrompts = shouldInjectSystemPrompts

BIN
docs/images/head-cover.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

@ -24,6 +24,7 @@
"@tremor/react": "^3.12.1", "@tremor/react": "^3.12.1",
"@vercel/analytics": "^1.1.1", "@vercel/analytics": "^1.1.1",
"echarts": "^5.4.3", "echarts": "^5.4.3",
"@vercel/speed-insights": "^1.0.2",
"emoji-picker-react": "^4.5.15", "emoji-picker-react": "^4.5.15",
"fuse.js": "^7.0.0", "fuse.js": "^7.0.0",
"html-to-image": "^1.11.11", "html-to-image": "^1.11.11",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 633 B

After

Width:  |  Height:  |  Size: 719 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 74 KiB