Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,60 @@ import streamlit as st
|
|
7 |
|
8 |
VECTOR_DB ="c8af7dfa-bcad-46e5-b69d-cd85ce9315d1"
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def get_credentials():
|
11 |
return {
|
12 |
"url" : "https://us-south.ml.cloud.ibm.com",
|
@@ -150,28 +204,25 @@ def proximity_search( question ):
|
|
150 |
st.title("π IBM Watson RAG Chatbot")
|
151 |
|
152 |
# User input in Streamlit
|
153 |
-
|
154 |
|
155 |
-
if
|
156 |
-
# Retrieve relevant grounding context
|
157 |
-
grounding = proximity_search(question)
|
158 |
|
159 |
-
#
|
160 |
-
|
161 |
-
{grounding}
|
162 |
-
Question: {question}<|end_of_text|>
|
163 |
-
<|start_of_role|>assistant<|end_of_role|>"""
|
164 |
|
165 |
-
|
166 |
-
prompt_input = "" # Set this dynamically if needed
|
167 |
-
prompt = f"""{prompt_input}{formatted_question}"""
|
168 |
|
169 |
-
#
|
170 |
-
|
|
|
|
|
|
|
171 |
|
172 |
-
#
|
173 |
-
st.
|
174 |
-
|
175 |
|
176 |
-
|
177 |
-
st.
|
|
|
|
7 |
|
8 |
VECTOR_DB ="c8af7dfa-bcad-46e5-b69d-cd85ce9315d1"
|
9 |
|
10 |
+
IBM_API_KEY = os.getenv("IBM_API_KEY")
|
11 |
+
IBM_PROJECT_ID = "a0659778-f4ce-4da1-ba01-43b4f43a026f"
|
12 |
+
|
13 |
+
IBM_URL_TOKEN = "https://iam.cloud.ibm.com/identity/token"
|
14 |
+
IBM_URL_CHAT = "https://us-south.ml.cloud.ibm.com/ml/v1/text/chat?version=2023-10-25"
|
15 |
+
|
16 |
+
##############################################
|
17 |
+
##
|
18 |
+
## IBM API
|
19 |
+
##
|
20 |
+
##############################################
|
21 |
+
def IBM_token():
|
22 |
+
# Define the headers
|
23 |
+
headers = {
|
24 |
+
"Content-Type": "application/x-www-form-urlencoded"
|
25 |
+
}
|
26 |
+
|
27 |
+
# Define the data payload
|
28 |
+
data = {
|
29 |
+
"grant_type": "urn:ibm:params:oauth:grant-type:apikey",
|
30 |
+
"apikey": IBM_API_KEY
|
31 |
+
}
|
32 |
+
|
33 |
+
# Make the POST request
|
34 |
+
response = requests.post(IBM_URL_TOKEN, headers=headers, data=data)
|
35 |
+
st.session_state.IBM_ACCESS_TOKEN = response.json().get("access_token", "")
|
36 |
+
|
37 |
+
|
38 |
+
def IBM_chat (messages):
|
39 |
+
body = {
|
40 |
+
"model_id": "ibm/granite-3-8b-instruct",
|
41 |
+
"project_id": IBM_PROJECT_ID,
|
42 |
+
"messages": messages,
|
43 |
+
"max_tokens": 10000,
|
44 |
+
"temperature": 0.7,
|
45 |
+
"time_limit": 50000
|
46 |
+
}
|
47 |
+
headers = {
|
48 |
+
"Accept": "application/json",
|
49 |
+
"Content-Type": "application/json",
|
50 |
+
"Authorization": "Bearer " + st.session_state.IBM_ACCESS_TOKEN
|
51 |
+
}
|
52 |
+
response = requests.post(
|
53 |
+
IBM_URL_CHAT,
|
54 |
+
headers=headers,
|
55 |
+
json=body
|
56 |
+
)
|
57 |
+
|
58 |
+
if response.status_code != 200:
|
59 |
+
raise Exception("Non-200 response: " + str(response.text))
|
60 |
+
|
61 |
+
response = response.json()
|
62 |
+
return response["choices"][0]["message"]["content"]
|
63 |
+
|
64 |
def get_credentials():
|
65 |
return {
|
66 |
"url" : "https://us-south.ml.cloud.ibm.com",
|
|
|
204 |
st.title("π IBM Watson RAG Chatbot")
|
205 |
|
206 |
# User input in Streamlit
|
207 |
+
user_input = st.text_input("Enter your question:")
|
208 |
|
209 |
+
if user_input:
|
|
|
|
|
210 |
|
211 |
+
# Display user message
|
212 |
+
st.chat_message("user").markdown(user_input)
|
|
|
|
|
|
|
213 |
|
214 |
+
grounding = proximity_search(user_input)
|
|
|
|
|
215 |
|
216 |
+
# add the submissions as context (only in prompt, not in history)
|
217 |
+
prompt = user_input + ". Provide urls where possible. Given the context: " + grounding;
|
218 |
+
messages = st.session_state.messages.copy()
|
219 |
+
messages.append({"role": "user", "content": prompt})
|
220 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
221 |
|
222 |
+
# Get response from IBM
|
223 |
+
with st.spinner("Thinking..."):
|
224 |
+
assistant_reply = IBM_chat(messages)
|
225 |
|
226 |
+
# Display assistant message
|
227 |
+
st.chat_message("assistant").markdown(assistant_reply)
|
228 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_reply})
|