diff --git a/api/chat.py b/api/chat.py index 0284bb8..f948601 100644 --- a/api/chat.py +++ b/api/chat.py @@ -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)) diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx index ec90794..3c6712c 100644 --- a/web/src/pages/index.tsx +++ b/web/src/pages/index.tsx @@ -23,7 +23,6 @@ type UserEntry = { type AssistantEntry = { role: "assistant"; content: string; - display_content: string; citations: Map; } @@ -89,7 +88,7 @@ const ShowEntry: React.FC<{entry: Entry}> = ({entry}) => { return (
{ // split into paragraphs - entry.display_content.split("\n").map(paragraph => (

{ + entry.content.split("\n").map(paragraph => (

{ 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);