From f833b43f375e8b7cdf1470cb9baae2cfe225ee65 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 16 Feb 2023 16:21:29 +0000 Subject: [PATCH] memory done --- application/app.py | 12 +++++++++++- application/combine_prompt_hist.txt | 27 +++++++++++++++++++++++++++ application/static/src/chat.js | 4 ++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 application/combine_prompt_hist.txt diff --git a/application/app.py b/application/app.py index 1f749716..22e5cafe 100644 --- a/application/app.py +++ b/application/app.py @@ -1,4 +1,5 @@ import os +import json import dotenv import requests @@ -47,6 +48,9 @@ dotenv.load_dotenv() with open("combine_prompt.txt", "r") as f: template = f.read() +with open("combine_prompt_hist.txt", "r") as f: + template_hist = f.read() + if os.getenv("API_KEY") is not None: api_key_set = True else: @@ -69,6 +73,7 @@ def home(): def api_answer(): data = request.get_json() question = data["question"] + history = data["history"] if not api_key_set: api_key = data["api_key"] else: @@ -99,7 +104,12 @@ def api_answer(): docsearch = FAISS.load_local(vectorstore, CohereEmbeddings(cohere_api_key=embeddings_key)) # create a prompt template - c_prompt = PromptTemplate(input_variables=["summaries", "question"], template=template) + if history: + history = json.loads(history) + template_temp = template_hist.replace("{historyquestion}", history[0]).replace("{historyanswer}", history[1]) + c_prompt = PromptTemplate(input_variables=["summaries", "question"], template=template_temp) + else: + c_prompt = PromptTemplate(input_variables=["summaries", "question"], template=template) if llm_choice == "openai": llm = OpenAI(openai_api_key=api_key, temperature=0) diff --git a/application/combine_prompt_hist.txt b/application/combine_prompt_hist.txt new file mode 100644 index 00000000..a98bf6ab --- /dev/null +++ b/application/combine_prompt_hist.txt @@ -0,0 +1,27 @@ +You are a DocsGPT bot assistant by Arc53 that provides help with programming libraries. You give thorough answers with code examples. +Given the following extracted parts of a long document and a question, create a final answer with references ("SOURCES"). +ALWAYS return a "SOURCES" part in your answer. You can also remeber things from previous questions and use them in your answer. + +QUESTION: How to merge tables in pandas? +========= +Content: pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. +Source: 28-pl +Content: pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: \n\npandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None) +Source: 30-pl +========= +FINAL ANSWER: To merge two tables in pandas, you can use the pd.merge() function. The basic syntax is: \n\npd.merge(left, right, on, how) \n\nwhere left and right are the two tables to merge, on is the column to merge on, and how is the type of merge to perform. \n\nFor example, to merge the two tables df1 and df2 on the column 'id', you can use: \n\npd.merge(df1, df2, on='id', how='inner') +SOURCES: 28-pl 30-pl + +QUESTION: {historyquestion} +========= +CONTENT: +SOURCE: +========= +FINAL ANSWER: {historyanswer} +SOURCES: + +QUESTION: {question} +========= +{summaries} +========= +FINAL ANSWER: \ No newline at end of file diff --git a/application/static/src/chat.js b/application/static/src/chat.js index b577dca2..553d7f3f 100644 --- a/application/static/src/chat.js +++ b/application/static/src/chat.js @@ -26,6 +26,7 @@ if (el) { body: JSON.stringify({question: message, api_key: localStorage.getItem('apiKey'), embeddings_key: localStorage.getItem('apiKey'), + history: localStorage.getItem('chatHistory'), active_docs: localStorage.getItem('activeDocs')}), }) .then(response => response.json()) @@ -39,9 +40,12 @@ if (el) { chatWindow.scrollTop = chatWindow.scrollHeight; document.getElementById("button-submit").innerHTML = 'Send'; document.getElementById("button-submit").disabled = false; + let chatHistory = [message, data.answer]; + localStorage.setItem('chatHistory', JSON.stringify(chatHistory)); }) .catch((error) => { console.error('Error:', error); + console.log(error); document.getElementById("button-submit").innerHTML = 'Send'; document.getElementById("button-submit").disabled = false; });