Merge pull request #85 from StampyAI/moderation

implement moderation
This commit is contained in:
FraserLee
2023-08-22 01:59:33 -04:00
committed by GitHub
2 changed files with 32 additions and 15 deletions
+17 -3
View File
@@ -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 = ''
+15 -12
View File
@@ -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 = <p>Loading: Performing semantic search...</p>; break;
case "prompt": last_entry = <p>Loading: Creating prompt...</p>; break;
case "llm": last_entry = <p>Loading: Waiting for LLM...</p>; break;
}
} else if (loadState.state === "streaming") {
last_entry = <ShowAssistantEntry entry={loadState.response}/>;
}
return (
<>
<Head>
@@ -556,18 +570,7 @@ const Home: NextPage = () => {
<SearchBox search={search} />
{(() => {
if (loadState.state === "loading") {
switch (loadState.phase) {
case "semantic": return <p>Loading: Performing semantic search...</p>;
case "prompt": return <p>Loading: Creating prompt...</p>;
case "llm": return <p>Loading: Waiting for LLM...</p>;
}
} else if (loadState.state === "streaming") {
return <ShowAssistantEntry entry={loadState.response}/>;
}
return <></>;
})()}
{ last_entry }
</ul>
</main>