Finish restructure prompt

I've A/B tested *a lot*, and find doing it this way (sources initially, then truncated conversation, then final question) ends up allowing the user to ask clarifying questions better, with minimal if any drop in citation knowledge and relevance
This commit is contained in:
Fraser
2023-04-03 20:20:36 -04:00
parent 6e4f828cbd
commit 386614fa68
2 changed files with 17 additions and 6 deletions
+14 -1
View File
@@ -4,8 +4,8 @@ from get_blocks import get_top_k_blocks, Block
from typing import List, Dict
import openai
import os
import tiktoken
import re
# OpenAI models
EMBEDDING_MODEL = "text-embedding-ada-002"
@@ -70,9 +70,18 @@ def construct_prompt(query: str, history: List[Dict[str, str]], context: List[Bl
source_prompt += block_str
token_count += block_tc
source_prompt = source_prompt.strip();
if len(history) > 0:
source_prompt += "\n\n"\
"Before the question (\"Q: \"), there will be a history of previous questions and answers. " \
"These sources only apply to the last question. Any sources used in previous answers " \
"are invalid."
prompt.append({"role": "system", "content": source_prompt.strip()})
# Write a version of the last 10 messages into history, cutting things off when we hit the token limit.
token_count = 0
history_trnc = []
@@ -82,6 +91,10 @@ def construct_prompt(query: str, history: List[Dict[str, str]], context: List[Bl
token_count += len(ENCODER.encode("Q: " + message["content"]))
else:
content = cap(message["content"], int(NUM_TOKENS * HISTORY_FRACTION) - token_count)
# censor all source letters into [x]
content = re.sub(r"\[[0-9]+\]", "[x]", content)
history_trnc.append({"role": "assistant", "content": content})
token_count += len(ENCODER.encode(content))
+3 -5
View File
@@ -23,7 +23,6 @@ type UserEntry = {
type AssistantEntry = {
role: "assistant";
content: string;
display_content: string;
citations: Map<number, Citation>;
}
@@ -89,7 +88,7 @@ const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => {
return (
<div className="mt-3 mb-8">
{ // split into paragraphs
entry.display_content.split("\n").map(paragraph => ( <p> {
entry.content.split("\n").map(paragraph => ( <p> {
paragraph.split(in_text_citation_regex).map((text, i) => {
if (i % 2 === 0) {
return text.trim();
@@ -143,7 +142,7 @@ const Home: NextPage = () => {
.map((entry) => {
return {
"role" : entry.role,
"content" : entry.content
"content" : entry.content.trim(),
}
})
})
@@ -234,8 +233,7 @@ const Home: NextPage = () => {
});
setEntries([...new_entries, {role: "assistant",
content: await data.response,
display_content: response,
content: response,
citations: citations}]);
setLoading(false);