Update app.py
Browse files
app.py
CHANGED
|
@@ -68,31 +68,28 @@ def search(query: str, k: int = 2 ):
|
|
| 68 |
# returns scores (List[float]): the retrieval scores from either FAISS (IndexFlatL2 by default) and examples (dict) format
|
| 69 |
# called by talk function that passes prompt
|
| 70 |
|
| 71 |
-
def format_prompt(prompt,retrieved_documents,k):
|
| 72 |
"""using the retrieved documents we will prompt the model to generate our responses"""
|
| 73 |
PROMPT = f"Question:{prompt}\nContext:"
|
| 74 |
for idx in range(k) :
|
| 75 |
PROMPT+= f"{retrieved_documents['0'][idx]}\n"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
#formatted_message = PROMPT + f"{history[0][0]} [/INST] {history[0][1]} </s>"
|
| 87 |
-
|
| 88 |
# Handle conversation history
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
#return formatted_message
|
| 96 |
|
| 97 |
# Called by talk function to add retrieved documents to the prompt. Keeps adding text of retrieved documents to string that are retreived
|
| 98 |
|
|
@@ -101,7 +98,7 @@ def talk(prompt, history):
|
|
| 101 |
scores , retrieved_documents = search(prompt, k) # get retrival scores and examples in dictionary format based on the prompt passed
|
| 102 |
print(retrieved_documents.keys())
|
| 103 |
# print("check4")
|
| 104 |
-
formatted_prompt = format_prompt(prompt,retrieved_documents,k) # create a new prompt using the retrieved documents
|
| 105 |
print("check5")
|
| 106 |
# print(retrieved_documents['0'])
|
| 107 |
# print(formatted_prompt)
|
|
|
|
| 68 |
# returns scores (List[float]): the retrieval scores from either FAISS (IndexFlatL2 by default) and examples (dict) format
|
| 69 |
# called by talk function that passes prompt
|
| 70 |
|
| 71 |
+
def format_prompt(prompt,retrieved_documents,k,history,memory_limit=3):
|
| 72 |
"""using the retrieved documents we will prompt the model to generate our responses"""
|
| 73 |
PROMPT = f"Question:{prompt}\nContext:"
|
| 74 |
for idx in range(k) :
|
| 75 |
PROMPT+= f"{retrieved_documents['0'][idx]}\n"
|
| 76 |
+
if len(history) == 0:
|
| 77 |
+
return PROMPT
|
| 78 |
+
|
| 79 |
+
if len(history) > memory_limit:
|
| 80 |
+
history = history[-memory_limit:]
|
| 81 |
+
|
| 82 |
+
print("checkwohist")
|
| 83 |
+
PROMPT = PROMPT + f"{history[0][0]} [/INST] {history[0][1]} </s>"
|
| 84 |
+
print("checkwthhist")
|
| 85 |
+
print(PROMPT)
|
|
|
|
|
|
|
| 86 |
# Handle conversation history
|
| 87 |
+
for user_message, bot_message in history[1:]:
|
| 88 |
+
PROMPT += f"<s>[INST] {user_msg} [/INST] {model_answer} </s>"
|
| 89 |
+
print("checkwthhist2")
|
| 90 |
+
print(PROMPT)
|
| 91 |
+
return PROMPT
|
| 92 |
|
|
|
|
| 93 |
|
| 94 |
# Called by talk function to add retrieved documents to the prompt. Keeps adding text of retrieved documents to string that are retreived
|
| 95 |
|
|
|
|
| 98 |
scores , retrieved_documents = search(prompt, k) # get retrival scores and examples in dictionary format based on the prompt passed
|
| 99 |
print(retrieved_documents.keys())
|
| 100 |
# print("check4")
|
| 101 |
+
formatted_prompt = format_prompt(prompt,retrieved_documents,k,history,memory_limit=3) # create a new prompt using the retrieved documents
|
| 102 |
print("check5")
|
| 103 |
# print(retrieved_documents['0'])
|
| 104 |
# print(formatted_prompt)
|