Spaces:
Sleeping
Sleeping
Sivabalan Thirunavukkarasu
commited on
W4D1 Midterm - Allow context in responses
Browse files
app.py
CHANGED
@@ -24,6 +24,7 @@ We will load our environment variables here.
|
|
24 |
"""
|
25 |
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
26 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
|
|
27 |
|
28 |
# ---- GLOBAL DECLARATIONS ---- #
|
29 |
|
@@ -40,23 +41,24 @@ documents = document_loader.load()
|
|
40 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
41 |
split_documents = text_splitter.split_documents(documents)
|
42 |
|
|
|
43 |
openai_embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
44 |
|
45 |
-
if os.path.exists(
|
46 |
vectorstore = Qdrant.from_existing_collection(
|
47 |
embeddings=openai_embeddings,
|
48 |
collection_name="airbnb_financials",
|
49 |
-
path=
|
50 |
batch_size=32,
|
51 |
)
|
52 |
print("Loaded Vectorstore")
|
53 |
else:
|
54 |
print("Indexing Files")
|
55 |
-
os.makedirs(
|
56 |
vectorstore = Qdrant.from_documents(
|
57 |
documents=split_documents,
|
58 |
embedding=openai_embeddings,
|
59 |
-
path=
|
60 |
collection_name="airbnb_financials",
|
61 |
batch_size=32,
|
62 |
)
|
@@ -70,7 +72,7 @@ retriever = vectorstore.as_retriever()
|
|
70 |
"""
|
71 |
RAG_PROMPT_TEMPLATE = """\
|
72 |
<|start_header_id|>system<|end_header_id|>
|
73 |
-
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know
|
74 |
|
75 |
<|start_header_id|>user<|end_header_id|>
|
76 |
User Query:
|
@@ -144,6 +146,7 @@ async def main(message: cl.Message):
|
|
144 |
{"query": message.content},
|
145 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
146 |
):
|
|
|
147 |
if chunk != "<|eot_id|>":
|
148 |
await msg.stream_token(chunk)
|
149 |
|
|
|
24 |
"""
|
25 |
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
26 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
27 |
+
VECTOR_STORE_PATH = "./data/vectorstore"
|
28 |
|
29 |
# ---- GLOBAL DECLARATIONS ---- #
|
30 |
|
|
|
41 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
42 |
split_documents = text_splitter.split_documents(documents)
|
43 |
|
44 |
+
# Note: Uses OPENAI_API_KEY env variable to make api calls
|
45 |
openai_embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
46 |
|
47 |
+
if os.path.exists(VECTOR_STORE_PATH):
|
48 |
vectorstore = Qdrant.from_existing_collection(
|
49 |
embeddings=openai_embeddings,
|
50 |
collection_name="airbnb_financials",
|
51 |
+
path=VECTOR_STORE_PATH,
|
52 |
batch_size=32,
|
53 |
)
|
54 |
print("Loaded Vectorstore")
|
55 |
else:
|
56 |
print("Indexing Files")
|
57 |
+
os.makedirs(VECTOR_STORE_PATH, exist_ok=True)
|
58 |
vectorstore = Qdrant.from_documents(
|
59 |
documents=split_documents,
|
60 |
embedding=openai_embeddings,
|
61 |
+
path=VECTOR_STORE_PATH,
|
62 |
collection_name="airbnb_financials",
|
63 |
batch_size=32,
|
64 |
)
|
|
|
72 |
"""
|
73 |
RAG_PROMPT_TEMPLATE = """\
|
74 |
<|start_header_id|>system<|end_header_id|>
|
75 |
+
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
|
76 |
|
77 |
<|start_header_id|>user<|end_header_id|>
|
78 |
User Query:
|
|
|
146 |
{"query": message.content},
|
147 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
148 |
):
|
149 |
+
# Note: Skip printing eot_id token at the end of response. A more elegant solution would be to fix the model's generator config.
|
150 |
if chunk != "<|eot_id|>":
|
151 |
await msg.stream_token(chunk)
|
152 |
|