Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,144 +5,6 @@ import sentence_transformers
|
|
5 |
|
6 |
import streamlit as st
|
7 |
|
8 |
-
def get_credentials():
|
9 |
-
return {
|
10 |
-
"url" : "https://us-south.ml.cloud.ibm.com",
|
11 |
-
"apikey" : os.getenv("IBM_API_KEY")
|
12 |
-
}
|
13 |
-
|
14 |
-
model_id = "ibm/granite-3-8b-instruct"
|
15 |
-
|
16 |
-
parameters = {
|
17 |
-
"decoding_method": "greedy",
|
18 |
-
"max_new_tokens": 900,
|
19 |
-
"min_new_tokens": 0,
|
20 |
-
"repetition_penalty": 1
|
21 |
-
}
|
22 |
-
|
23 |
-
project_id = os.getenv("IBM_PROJECT_ID")
|
24 |
-
space_id = os.getenv("IBM_SPACE_ID")
|
25 |
-
|
26 |
-
from ibm_watsonx_ai.foundation_models import ModelInference
|
27 |
-
|
28 |
-
model = ModelInference(
|
29 |
-
model_id = model_id,
|
30 |
-
params = parameters,
|
31 |
-
credentials = get_credentials(),
|
32 |
-
project_id = project_id,
|
33 |
-
space_id = space_id
|
34 |
-
)
|
35 |
-
|
36 |
-
from ibm_watsonx_ai.client import APIClient
|
37 |
-
|
38 |
-
wml_credentials = get_credentials()
|
39 |
-
client = APIClient(credentials=wml_credentials, project_id=project_id) #, space_id=space_id)
|
40 |
-
|
41 |
-
vector_index_id = "14c14504-5f45-4e6c-8f0f-25f2378a1d99"
|
42 |
-
vector_index_details = client.data_assets.get_details(vector_index_id)
|
43 |
-
vector_index_properties = vector_index_details["entity"]["vector_index"]
|
44 |
-
|
45 |
-
top_n = 20 if vector_index_properties["settings"].get("rerank") else int(vector_index_properties["settings"]["top_k"])
|
46 |
-
|
47 |
-
def rerank( client, documents, query, top_n ):
|
48 |
-
from ibm_watsonx_ai.foundation_models import Rerank
|
49 |
-
|
50 |
-
reranker = Rerank(
|
51 |
-
model_id="cross-encoder/ms-marco-minilm-l-12-v2",
|
52 |
-
api_client=client,
|
53 |
-
params={
|
54 |
-
"return_options": {
|
55 |
-
"top_n": top_n
|
56 |
-
},
|
57 |
-
"truncate_input_tokens": 512
|
58 |
-
}
|
59 |
-
)
|
60 |
-
|
61 |
-
reranked_results = reranker.generate(query=query, inputs=documents)["results"]
|
62 |
-
|
63 |
-
new_documents = []
|
64 |
-
|
65 |
-
for result in reranked_results:
|
66 |
-
result_index = result["index"]
|
67 |
-
new_documents.append(documents[result_index])
|
68 |
-
|
69 |
-
return new_documents
|
70 |
-
|
71 |
-
from ibm_watsonx_ai.foundation_models.embeddings.sentence_transformer_embeddings import SentenceTransformerEmbeddings
|
72 |
-
|
73 |
-
emb = SentenceTransformerEmbeddings('sentence-transformers/all-MiniLM-L6-v2')
|
74 |
-
|
75 |
-
import subprocess
|
76 |
-
import gzip
|
77 |
-
import json
|
78 |
-
import chromadb
|
79 |
-
import random
|
80 |
-
import string
|
81 |
-
|
82 |
-
def hydrate_chromadb():
|
83 |
-
data = client.data_assets.get_content(vector_index_id)
|
84 |
-
content = gzip.decompress(data)
|
85 |
-
stringified_vectors = str(content, "utf-8")
|
86 |
-
vectors = json.loads(stringified_vectors)
|
87 |
-
|
88 |
-
#chroma_client = chromadb.Client()
|
89 |
-
#chroma_client = chromadb.InMemoryClient()
|
90 |
-
chroma_client = chromadb.PersistentClient(path="./chroma_db")
|
91 |
-
|
92 |
-
# make sure collection is empty if it already existed
|
93 |
-
collection_name = "my_collection"
|
94 |
-
try:
|
95 |
-
collection = chroma_client.delete_collection(name=collection_name)
|
96 |
-
except:
|
97 |
-
print("Collection didn't exist - nothing to do.")
|
98 |
-
collection = chroma_client.create_collection(name=collection_name)
|
99 |
-
|
100 |
-
vector_embeddings = []
|
101 |
-
vector_documents = []
|
102 |
-
vector_metadatas = []
|
103 |
-
vector_ids = []
|
104 |
-
|
105 |
-
for vector in vectors:
|
106 |
-
vector_embeddings.append(vector["embedding"])
|
107 |
-
vector_documents.append(vector["content"])
|
108 |
-
metadata = vector["metadata"]
|
109 |
-
lines = metadata["loc"]["lines"]
|
110 |
-
clean_metadata = {}
|
111 |
-
clean_metadata["asset_id"] = metadata["asset_id"]
|
112 |
-
clean_metadata["asset_name"] = metadata["asset_name"]
|
113 |
-
clean_metadata["url"] = metadata["url"]
|
114 |
-
clean_metadata["from"] = lines["from"]
|
115 |
-
clean_metadata["to"] = lines["to"]
|
116 |
-
vector_metadatas.append(clean_metadata)
|
117 |
-
asset_id = vector["metadata"]["asset_id"]
|
118 |
-
random_string = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
|
119 |
-
id = "{}:{}-{}-{}".format(asset_id, lines["from"], lines["to"], random_string)
|
120 |
-
vector_ids.append(id)
|
121 |
-
|
122 |
-
collection.add(
|
123 |
-
embeddings=vector_embeddings,
|
124 |
-
documents=vector_documents,
|
125 |
-
metadatas=vector_metadatas,
|
126 |
-
ids=vector_ids
|
127 |
-
)
|
128 |
-
return collection
|
129 |
-
|
130 |
-
chroma_collection = hydrate_chromadb()
|
131 |
-
|
132 |
-
def proximity_search( question ):
|
133 |
-
query_vectors = emb.embed_query(question)
|
134 |
-
query_result = chroma_collection.query(
|
135 |
-
query_embeddings=query_vectors,
|
136 |
-
n_results=top_n,
|
137 |
-
include=["documents", "metadatas", "distances"]
|
138 |
-
)
|
139 |
-
|
140 |
-
documents = list(reversed(query_result["documents"][0]))
|
141 |
-
|
142 |
-
if vector_index_properties["settings"].get("rerank"):
|
143 |
-
documents = rerank(client, documents, question, vector_index_properties["settings"]["top_k"])
|
144 |
-
|
145 |
-
return "\n".join(documents)
|
146 |
|
147 |
# Streamlit UI
|
148 |
st.title("π IBM Watson RAG Chatbot")
|
@@ -152,7 +14,7 @@ question = st.text_input("Enter your question:")
|
|
152 |
|
153 |
if question:
|
154 |
# Retrieve relevant grounding context
|
155 |
-
grounding =
|
156 |
|
157 |
# Format the question with retrieved context
|
158 |
formatted_question = f"""<|start_of_role|>user<|end_of_role|>Use the following pieces of context to answer the question.
|
|
|
5 |
|
6 |
import streamlit as st
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Streamlit UI
|
10 |
st.title("π IBM Watson RAG Chatbot")
|
|
|
14 |
|
15 |
if question:
|
16 |
# Retrieve relevant grounding context
|
17 |
+
grounding = RAG_proximity_search(question)
|
18 |
|
19 |
# Format the question with retrieved context
|
20 |
formatted_question = f"""<|start_of_role|>user<|end_of_role|>Use the following pieces of context to answer the question.
|