adding app files
Browse files- app.py +103 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import transformers
|
3 |
+
from dotenv import load_dotenv, find_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
|
7 |
+
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
|
8 |
+
|
9 |
+
from huggingface_hub import InferenceClient
|
10 |
+
from pymongo import MongoClient
|
11 |
+
from pymongo.collection import Collection
|
12 |
+
from typing import Dict, Any
|
13 |
+
from datetime import datetime
|
14 |
+
|
15 |
+
|
16 |
+
load_dotenv(find_dotenv('.secrets.env'))
|
17 |
+
|
18 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
19 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
20 |
+
DB_NAME = "txts"
|
21 |
+
COLLECTION_NAME = "txts_collection"
|
22 |
+
VECTOR_SEARCH_INDEX = "vector_index"
|
23 |
+
|
24 |
+
|
25 |
+
@st.cache_resource
|
26 |
+
def init_mongodb():
|
27 |
+
# MongoDB configuration.
|
28 |
+
cluster = MongoClient(MONGO_URI)
|
29 |
+
return cluster[DB_NAME][COLLECTION_NAME]
|
30 |
+
|
31 |
+
@st.cache_resource
|
32 |
+
def init_vector_search() -> MongoDBAtlasVectorSearch:
|
33 |
+
print('CACHING VECTOR SEARCH')
|
34 |
+
return MongoDBAtlasVectorSearch.from_connection_string(
|
35 |
+
connection_string=MONGO_URI,
|
36 |
+
namespace=f"{DB_NAME}.{COLLECTION_NAME}",
|
37 |
+
embedding=embedding_model,
|
38 |
+
index_name=VECTOR_SEARCH_INDEX,
|
39 |
+
)
|
40 |
+
|
41 |
+
@st.cache_resource
|
42 |
+
def init_embedding_model() -> HuggingFaceInferenceAPIEmbeddings:
|
43 |
+
return HuggingFaceInferenceAPIEmbeddings(
|
44 |
+
api_key=HF_TOKEN,
|
45 |
+
model_name="sentence-transformers/all-mpnet-base-v2",
|
46 |
+
)
|
47 |
+
|
48 |
+
def get_context_from_retrived_docs(retrieved_docs):
|
49 |
+
return "\n\n".join(doc.page_content for doc in retrieved_docs)
|
50 |
+
|
51 |
+
def format_prompt(user_query, retreived_context):
|
52 |
+
prompt = f"""Use the following pieces of context to answer the question at the end.
|
53 |
+
|
54 |
+
START OF CONTEXT:
|
55 |
+
{retreived_context}
|
56 |
+
END OF CONTEXT:
|
57 |
+
|
58 |
+
START OF QUESTION:
|
59 |
+
{user_query}
|
60 |
+
END OF QUESTION:
|
61 |
+
|
62 |
+
If you do not know the answer, just say that you do not know.
|
63 |
+
NEVER assume things.
|
64 |
+
""".format(retreived_context=retreived_context, user_query=user_query)
|
65 |
+
|
66 |
+
return prompt
|
67 |
+
|
68 |
+
|
69 |
+
## INITALIZE YOUR DB, EMBEDDER, AND VECTOR SEARCHER, AND INTERFACE CLIENTS.
|
70 |
+
mongodb_collection = init_mongodb()
|
71 |
+
embedding_model = init_embedding_model()
|
72 |
+
vector_search = init_vector_search()
|
73 |
+
hf_client = InferenceClient(api_key=HF_TOKEN)
|
74 |
+
|
75 |
+
# GET USERS INPUT
|
76 |
+
user_query = st.text_area('Ask a question about CTP Class')
|
77 |
+
|
78 |
+
if user_query:
|
79 |
+
|
80 |
+
# DO RAG SEARCH TO GET REVELENT DOCUMENTS
|
81 |
+
relevent_documents = vector_search.similarity_search(query=user_query, k=10) # 10 most similar documents.
|
82 |
+
|
83 |
+
# EXTRACT THE TEXT FROM THE DOCUMENTS
|
84 |
+
context = get_context_from_retrived_docs(relevent_documents)
|
85 |
+
|
86 |
+
# PUT THAT TEXT INTO THE PROMPT
|
87 |
+
prompt = format_prompt(user_query=user_query, retreived_context=context)
|
88 |
+
|
89 |
+
# SEND USER QUERY WITH CONTEXT TO MODEL
|
90 |
+
response = hf_client.chat.completions.create(
|
91 |
+
model="Qwen/Qwen2.5-1.5B-Instruct",
|
92 |
+
messages=[{
|
93 |
+
"role": "system",
|
94 |
+
"content": 'you are an assistant, answer the question below'
|
95 |
+
},{
|
96 |
+
"role": "user",
|
97 |
+
"content": prompt
|
98 |
+
}],
|
99 |
+
max_tokens=1400,
|
100 |
+
temperature=0.2,
|
101 |
+
)
|
102 |
+
model_response = response.choices[0].message.content
|
103 |
+
st.text(model_response)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
huggingface_hub==0.26.2
|
3 |
+
langchain==0.3.7
|
4 |
+
langchain_community==0.3.5
|
5 |
+
pymongo==4.10.1
|
6 |
+
streamlit==1.39.0
|