Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Mar 29, 2024
2 parents e496a33 + e38b527 commit 2fede80
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dist

# Gatsby files
.cache/
public


# Vuepress build output
.vuepress/dist
Expand Down
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions app/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() => {
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 11 additions & 9 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion app/components/message-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export function MessageSelector(props: {
</div>

<div className={styles["checkbox"]}>
<input type="checkbox" checked={isSelected}></input>
<input type="checkbox" checked={isSelected} readOnly></input>
</div>
</div>
);
Expand Down
7 changes: 6 additions & 1 deletion app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ export const useChatStore = createPersistStore(
messages: topicMessages,
config: {
model: getSummarizeModel(session.mask.modelConfig.model),
stream: false,
},
onFinish(message) {
get().updateCurrentSession(
Expand Down Expand Up @@ -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({
Expand All @@ -609,7 +614,7 @@ export const useChatStore = createPersistStore(
}),
),
config: {
...modelConfig,
...modelcfg,
stream: true,
model: getSummarizeModel(session.mask.modelConfig.model),
},
Expand Down

0 comments on commit 2fede80

Please sign in to comment.