From 647098ef830dcfb89746aa0fe09e6f8ff5eb7e15 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Mon, 27 Nov 2023 11:42:56 +0800 Subject: [PATCH] support json_mode --- src/app.tsx | 8 ++++++-- src/chatbox.tsx | 1 + src/chatgpt.ts | 8 ++++++++ src/settings.tsx | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/app.tsx b/src/app.tsx index 37c3e9e..6bbd67e 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -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); diff --git a/src/chatbox.tsx b/src/chatbox.tsx index 5eb3caa..55328d8 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -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) diff --git a/src/chatgpt.ts b/src/chatgpt.ts index 724765c..842f4b9 100644 --- a/src/chatgpt.ts +++ b/src/chatgpt.ts @@ -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(); diff --git a/src/settings.tsx b/src/settings.tsx index 50cd13a..ca73e00 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -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} /> +