Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#4462 from ChatGPTNextWeb/chore-fix
Browse files Browse the repository at this point in the history
feat: fix no max_tokens in payload when calling openai vision model
  • Loading branch information
Dean-YZG authored Apr 8, 2024
2 parents 9b03ab8 + 9b982b4 commit 1b83dd0
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ export interface OpenAIListModelResponse {
}>;
}

interface RequestPayload {
messages: {
role: "system" | "user" | "assistant";
content: string | MultimodalContent[];
}[];
stream?: boolean;
model: string;
temperature: number;
presence_penalty: number;
frequency_penalty: number;
top_p: number;
max_tokens?: number;
}

export class ChatGPTApi implements LLMApi {
private disableListModels = true;

Expand Down Expand Up @@ -98,7 +112,7 @@ export class ChatGPTApi implements LLMApi {
},
};

const requestPayload = {
const requestPayload: RequestPayload = {
messages,
stream: options.config.stream,
model: modelConfig.model,
Expand All @@ -112,12 +126,7 @@ export class ChatGPTApi implements LLMApi {

// add max_tokens to vision model
if (visionModel) {
Object.defineProperty(requestPayload, "max_tokens", {
enumerable: true,
configurable: true,
writable: true,
value: modelConfig.max_tokens,
});
requestPayload["max_tokens"] = Math.max(modelConfig.max_tokens, 4000);
}

console.log("[Request] openai payload: ", requestPayload);
Expand Down Expand Up @@ -229,17 +238,27 @@ export class ChatGPTApi implements LLMApi {
const text = msg.data;
try {
const json = JSON.parse(text);
const choices = json.choices as Array<{ delta: { content: string } }>;
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);
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, msg);
Expand Down

0 comments on commit 1b83dd0

Please sign in to comment.