Answer questions, commit work in progress
Browse files- BuildingAChainlitApp.md +4 -0
- aimakerspace/qa_pipeline.py +16 -4
BuildingAChainlitApp.md
CHANGED
|
@@ -133,6 +133,8 @@ Simply put, this downloads the file as a temp file, we load it in with `TextFile
|
|
| 133 |
|
| 134 |
Why do we want to support streaming? What about streaming is important, or useful?
|
| 135 |
|
|
|
|
|
|
|
| 136 |
## On Chat Start:
|
| 137 |
|
| 138 |
The next scope is where "the magic happens". On Chat Start is when a user begins a chat session. This will happen whenever a user opens a new chat window, or refreshes an existing chat window.
|
|
@@ -175,6 +177,8 @@ Now, we'll save that into our user session!
|
|
| 175 |
|
| 176 |
Why are we using User Session here? What about Python makes us need to use this? Why not just store everything in a global variable?
|
| 177 |
|
|
|
|
|
|
|
| 178 |
## On Message
|
| 179 |
|
| 180 |
First, we load our chain from the user session:
|
|
|
|
| 133 |
|
| 134 |
Why do we want to support streaming? What about streaming is important, or useful?
|
| 135 |
|
| 136 |
+
It helps reduce the user's wait time until the first token appears, making chatbot apps seem more responsive.
|
| 137 |
+
|
| 138 |
## On Chat Start:
|
| 139 |
|
| 140 |
The next scope is where "the magic happens". On Chat Start is when a user begins a chat session. This will happen whenever a user opens a new chat window, or refreshes an existing chat window.
|
|
|
|
| 177 |
|
| 178 |
Why are we using User Session here? What about Python makes us need to use this? Why not just store everything in a global variable?
|
| 179 |
|
| 180 |
+
It separates a user's info from other users, which is important in order to keep track of conversational context, uploaded documents, and other session-specific information.
|
| 181 |
+
|
| 182 |
## On Message
|
| 183 |
|
| 184 |
First, we load our chain from the user session:
|
aimakerspace/qa_pipeline.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from rank_bm25 import BM25Plus
|
|
|
|
| 2 |
|
| 3 |
from .openai_utils.prompts import (
|
| 4 |
SystemRolePrompt
|
|
@@ -17,14 +18,23 @@ def bm25plus_rerank(corpus, query, initial_ranking, top_n=3):
|
|
| 17 |
ranked_indices = [initial_ranking[i] for i in bm25_scores.argsort()[::-1]]
|
| 18 |
return ranked_indices[:top_n]
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
class RetrievalAugmentedQAPipeline:
|
| 22 |
-
def __init__(self, llm: ChatOpenAI(), vector_db_retriever
|
| 23 |
self.llm = llm
|
| 24 |
self.vector_db_retriever = vector_db_retriever
|
| 25 |
|
| 26 |
async def arun_pipeline(self, user_query: str):
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
context_prompt = ""
|
| 30 |
for context in context_list:
|
|
@@ -46,8 +56,10 @@ class RerankedQAPipeline(RetrievalAugmentedQAPipeline):
|
|
| 46 |
async def arun_pipeline(self, user_query: str, rerank: bool=False) -> str:
|
| 47 |
# Retrieve the top 10 results. Either return the top 3, or rerank with BM25 and then return
|
| 48 |
# the new top 3
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
# Convert from tuples to strings
|
| 52 |
context_list_str = [context_list[i][0] for i in range(len(context_list))]
|
| 53 |
|
|
|
|
| 1 |
from rank_bm25 import BM25Plus
|
| 2 |
+
from langchain.vectorstores import Qdrant
|
| 3 |
|
| 4 |
from .openai_utils.prompts import (
|
| 5 |
SystemRolePrompt
|
|
|
|
| 18 |
ranked_indices = [initial_ranking[i] for i in bm25_scores.argsort()[::-1]]
|
| 19 |
return ranked_indices[:top_n]
|
| 20 |
|
| 21 |
+
def search_by_text(qdrant: Qdrant, query_text: str, k: int, return_as_text: bool = False) -> List[Tuple[str, float]]:
|
| 22 |
+
results = qdrant.similarity_search_with_score(query_text, k)
|
| 23 |
+
if return_as_text:
|
| 24 |
+
return [result[0].page_content for result in results]
|
| 25 |
+
return [(result[0].page_content, result[1]) for result in results]
|
| 26 |
+
|
| 27 |
|
| 28 |
class RetrievalAugmentedQAPipeline:
|
| 29 |
+
def __init__(self, llm: ChatOpenAI(), vector_db_retriever) -> None:
|
| 30 |
self.llm = llm
|
| 31 |
self.vector_db_retriever = vector_db_retriever
|
| 32 |
|
| 33 |
async def arun_pipeline(self, user_query: str):
|
| 34 |
+
if type(self.vector_db_retriever == "Qdrant"):
|
| 35 |
+
context_list = search_by_text(self.vector_db_retriever,user_query, k=4)
|
| 36 |
+
else:
|
| 37 |
+
context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
|
| 38 |
|
| 39 |
context_prompt = ""
|
| 40 |
for context in context_list:
|
|
|
|
| 56 |
async def arun_pipeline(self, user_query: str, rerank: bool=False) -> str:
|
| 57 |
# Retrieve the top 10 results. Either return the top 3, or rerank with BM25 and then return
|
| 58 |
# the new top 3
|
| 59 |
+
if type(self.vector_db_retriever == "Qdrant"):
|
| 60 |
+
context_list = search_by_text(self.vector_db_retriever,user_query, k=10)
|
| 61 |
+
else:
|
| 62 |
+
context_list = self.vector_db_retriever.search_by_text(user_query, k=10)
|
| 63 |
# Convert from tuples to strings
|
| 64 |
context_list_str = [context_list[i][0] for i in range(len(context_list))]
|
| 65 |
|