support json_mode

This commit is contained in:
2023-11-27 11:42:56 +08:00
parent 34d4827580
commit 647098ef83
4 changed files with 17 additions and 3 deletions

View File

@@ -60,6 +60,7 @@ export interface ChatStore {
tts_speed_enabled: boolean;
image_gen_api: string;
image_gen_key: string;
json_mode: boolean;
}
const _defaultAPIEndpoint = "https://api.openai.com/v1/chat/completions";
@@ -79,7 +80,8 @@ export const newChatStore = (
tts_speed_enabled = false,
toolsString = "",
image_gen_api = "https://api.openai.com/v1/images/generations",
image_gen_key = ""
image_gen_key = "",
json_mode = false
): ChatStore => {
return {
chatgpt_api_web_version: CHATGPT_API_WEB_VERSION,
@@ -114,6 +116,7 @@ export const newChatStore = (
tts_speed_enabled: tts_speed_enabled,
image_gen_api: image_gen_api,
image_gen_key: image_gen_key,
json_mode: json_mode,
};
};
@@ -273,7 +276,8 @@ export function App() {
chatStore.tts_speed_enabled,
chatStore.toolsString,
chatStore.image_gen_api,
chatStore.image_gen_key
chatStore.image_gen_key,
chatStore.json_mode
)
);
setSelectedChatIndex(newKey as number);

View File

@@ -222,6 +222,7 @@ export default function ChatBOX(props: {
client.enable_top_p = chatStore.top_p_enabled;
client.frequency_penalty = chatStore.frequency_penalty;
client.presence_penalty = chatStore.presence_penalty;
client.json_mode = chatStore.json_mode;
client.messages = chatStore.history
// only copy non hidden message
.filter(({ hide }) => !hide)

View File

@@ -132,6 +132,7 @@ class Chat {
enable_top_p: boolean;
presence_penalty: number;
frequency_penalty: number;
json_mode: boolean;
constructor(
OPENAI_API_KEY: string | undefined,
@@ -150,6 +151,7 @@ class Chat {
enable_top_p = false,
presence_penalty = 0,
frequency_penalty = 0,
json_mode = false,
} = {}
) {
this.OPENAI_API_KEY = OPENAI_API_KEY ?? "";
@@ -169,6 +171,7 @@ class Chat {
this.enable_top_p = enable_top_p;
this.presence_penalty = presence_penalty;
this.frequency_penalty = frequency_penalty;
this.json_mode = json_mode;
}
_fetch(stream = false) {
@@ -217,6 +220,11 @@ class Chat {
if (this.enable_max_gen_tokens) {
body["max_tokens"] = this.max_gen_tokens;
}
if (this.json_mode) {
body["response_format"] = {
type: "json_object",
};
}
// parse toolsString to function call format
const ts = this.toolsString.trim();

View File

@@ -246,7 +246,7 @@ const Number = (props: {
const Choice = (props: {
chatStore: ChatStore;
setChatStore: (cs: ChatStore) => void;
field: "streamMode" | "develop_mode";
field: "streamMode" | "develop_mode" | "json_mode";
help: string;
}) => {
return (
@@ -420,6 +420,7 @@ export default (props: {
help="开发者模式,拥有更多选项及功能"
{...props}
/>
<Choice field="json_mode" help="JSON Mode" {...props} />
<SelectModel
help="模型,默认 3.5。不同模型性能和定价也不同,请参考 API 文档。GPT-4 模型处于内测阶段,需要向 OPENAI 申请, 请确保您有访问权限)"
{...props}