diff --git a/.dockerignore b/.dockerignore index a88c7d61641..95ed9e268e4 100644 --- a/.dockerignore +++ b/.dockerignore @@ -63,7 +63,7 @@ dist # Gatsby files .cache/ -public + # Vuepress build output .vuepress/dist diff --git a/.env.template b/.env.template index 166cc4ef4e4..d53c1be6caa 100644 --- a/.env.template +++ b/.env.template @@ -2,7 +2,7 @@ # Your openai api key. (required) OPENAI_API_KEY=sk-xxxx -# Access passsword, separated by comma. (optional) +# Access password, separated by comma. (optional) CODE=your-password # You can start service behind a proxy diff --git a/app/api/common.ts b/app/api/common.ts index ca8406bb361..a75f2de5cfa 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -43,10 +43,6 @@ export async function requestOpenai(req: NextRequest) { console.log("[Proxy] ", path); console.log("[Base Url]", baseUrl); - // this fix [Org ID] undefined in server side if not using custom point - if (serverConfig.openaiOrgId !== undefined) { - console.log("[Org ID]", serverConfig.openaiOrgId); - } const timeoutId = setTimeout( () => { @@ -116,18 +112,37 @@ export async function requestOpenai(req: NextRequest) { try { const res = await fetch(fetchUrl, fetchOptions); + // Extract the OpenAI-Organization header from the response + const openaiOrganizationHeader = res.headers.get("OpenAI-Organization"); + + // Check if serverConfig.openaiOrgId is defined and not an empty string + if (serverConfig.openaiOrgId && serverConfig.openaiOrgId.trim() !== "") { + // If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present + console.log("[Org ID]", openaiOrganizationHeader); + } else { + console.log("[Org ID] is not set up."); + } + // to prevent browser prompt for credentials const newHeaders = new Headers(res.headers); newHeaders.delete("www-authenticate"); // to disable nginx buffering newHeaders.set("X-Accel-Buffering", "no"); + + // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV) + // Also, this is to prevent the header from being sent to the client + if (!serverConfig.openaiOrgId || serverConfig.openaiOrgId.trim() === "") { + newHeaders.delete("OpenAI-Organization"); + } + // The latest version of the OpenAI API forced the content-encoding to be "br" in json response // So if the streaming is disabled, we need to remove the content-encoding header // Because Vercel uses gzip to compress the response, if we don't remove the content-encoding header // The browser will try to decode the response with brotli and fail newHeaders.delete("content-encoding"); + return new Response(res.body, { status: res.status, statusText: res.statusText, diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index 78e628ad771..408ee704e1c 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -228,19 +228,21 @@ export class ChatGPTApi implements LLMApi { } const text = msg.data; try { - const json = JSON.parse(text) as { - choices: Array<{ - delta: { - content: string; - }; - }>; - }; - const delta = json.choices[0]?.delta?.content; + const json = JSON.parse(text); + const choices = json.choices as Array<{ delta: { content: string } }>; + const delta = choices[0]?.delta?.content; + const textmoderation = json?.prompt_filter_results; + if (delta) { remainText += delta; } + + if (textmoderation && textmoderation.length > 0 && ServiceProvider.Azure) { + const contentFilterResults = textmoderation[0]?.content_filter_results; + console.log(`[${ServiceProvider.Azure}] [Text Moderation] flagged categories result:`, contentFilterResults); + } } catch (e) { - console.error("[Request] parse error", text); + console.error("[Request] parse error", text, msg); } }, onclose() { diff --git a/app/components/message-selector.tsx b/app/components/message-selector.tsx index 840e480cba7..8198a3cd4eb 100644 --- a/app/components/message-selector.tsx +++ b/app/components/message-selector.tsx @@ -227,7 +227,7 @@ export function MessageSelector(props: {
- +
); diff --git a/app/store/chat.ts b/app/store/chat.ts index f97d7d7255e..029c12a74be 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -557,6 +557,7 @@ export const useChatStore = createPersistStore( messages: topicMessages, config: { model: getSummarizeModel(session.mask.modelConfig.model), + stream: false, }, onFinish(message) { get().updateCurrentSession( @@ -600,6 +601,10 @@ export const useChatStore = createPersistStore( historyMsgLength > modelConfig.compressMessageLengthThreshold && modelConfig.sendMemory ) { + /** Destruct max_tokens while summarizing + * this param is just shit + **/ + const { max_tokens, ...modelcfg } = modelConfig; api.llm.chat({ messages: toBeSummarizedMsgs.concat( createMessage({ @@ -609,7 +614,7 @@ export const useChatStore = createPersistStore( }), ), config: { - ...modelConfig, + ...modelcfg, stream: true, model: getSummarizeModel(session.mask.modelConfig.model), },