From 01498c42acc8cf4fc1cdb0554a67094bacf627cf Mon Sep 17 00:00:00 2001 From: Fraser Date: Tue, 22 Aug 2023 01:41:29 -0400 Subject: [PATCH 1/2] implement moderation --- api/chat.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/api/chat.py b/api/chat.py index db32d46..789a2b2 100644 --- a/api/chat.py +++ b/api/chat.py @@ -129,7 +129,7 @@ def construct_prompt(query: str, mode: str, history: List[Dict[str, str]], conte "rather than just giving a formal definition.\n\n" elif mode != "default": raise ValueError("Invalid mode: " + mode) - + question_prompt += "Q: " + query @@ -153,10 +153,24 @@ def talk_to_robot_internal(index, query: str, mode: str, history: List[Dict[str, yield {"state": "loading", "phase": "prompt"} prompt = construct_prompt(query, mode, history, top_k_blocks) - # 3. Count number of tokens left for completion (-50 for a buffer) + # 3. Run both the standalone query and the full prompt through + # moderation to see if it will be accepted by OpenAI's api + + mod_res = openai.Moderation.create( + input = [ + query, + '\n\n'.join([message["content"] for message in prompt]), + ] + ) + + if any(map(lambda x: x["flagged"], mod_res["results"])): + raise ValueError("This conversation was rejected by OpenAI's moderation filter. Sorry.") + + + # 4. Count number of tokens left for completion (-50 for a buffer) max_tokens_completion = NUM_TOKENS - sum([len(ENCODER.encode(message["content"]) + ENCODER.encode(message["role"])) for message in prompt]) - 50 - # 4. Answer the user query + # 5. Answer the user query yield {"state": "loading", "phase": "llm"} t1 = time.time() response = '' From bd3bda25766c44baed01089036acccc61be89805 Mon Sep 17 00:00:00 2001 From: Fraser Date: Tue, 22 Aug 2023 01:58:54 -0400 Subject: [PATCH 2/2] fix small UI bugs around error. --- web/src/pages/index.tsx | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index 7593e4f..d8fed20 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -418,6 +418,7 @@ const Home: NextPage = () => { case "error": setEntries([...new_entries, {role: "error", content: data.error}]); + setLoadState({state: "idle"}); break read; } @@ -479,6 +480,19 @@ const Home: NextPage = () => { } }; + var last_entry = <>; + if (loadState.state === "loading") { + switch (loadState.phase) { + case "semantic": last_entry =

Loading: Performing semantic search...

; break; + case "prompt": last_entry =

Loading: Creating prompt...

; break; + case "llm": last_entry =

Loading: Waiting for LLM...

; break; + } + } else if (loadState.state === "streaming") { + last_entry = ; + } + + + return ( <> @@ -556,18 +570,7 @@ const Home: NextPage = () => { - {(() => { - if (loadState.state === "loading") { - switch (loadState.phase) { - case "semantic": return

Loading: Performing semantic search...

; - case "prompt": return

Loading: Creating prompt...

; - case "llm": return

Loading: Waiting for LLM...

; - } - } else if (loadState.state === "streaming") { - return ; - } - return <>; - })()} + { last_entry }