Spaces:
Running
Running
Commit
·
ea4834d
1
Parent(s):
fa86403
nasa demo updates
Browse files- .gitignore +1 -2
- Dockerfile +3 -0
- app.py +30 -43
- ttyd_consts.py +4 -28
- ttyd_functions.py +7 -8
.gitignore
CHANGED
|
@@ -2,5 +2,4 @@
|
|
| 2 |
*.md
|
| 3 |
__pycache__
|
| 4 |
documents
|
| 5 |
-
|
| 6 |
-
gDriveDocs
|
|
|
|
| 2 |
*.md
|
| 3 |
__pycache__
|
| 4 |
documents
|
| 5 |
+
tmp
|
|
|
Dockerfile
CHANGED
|
@@ -15,5 +15,8 @@ ENV GRADIO_SERVER_NAME=0.0.0.0
|
|
| 15 |
# Install any needed packages specified in requirements.txt
|
| 16 |
# RUN pip install --no-cache-dir -r requirements.txt # already installed in base image
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# Use ENTRYPOINT to allow passing user arguments
|
| 19 |
ENTRYPOINT ["python", "app.py"]
|
|
|
|
| 15 |
# Install any needed packages specified in requirements.txt
|
| 16 |
# RUN pip install --no-cache-dir -r requirements.txt # already installed in base image
|
| 17 |
|
| 18 |
+
#to be moved to ttyd_base image
|
| 19 |
+
RUN python -c "from sentence_transformers import SentenceTransformer; model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', cache_folder='./tmp/hfEmbModel')"
|
| 20 |
+
|
| 21 |
# Use ENTRYPOINT to allow passing user arguments
|
| 22 |
ENTRYPOINT ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,28 +1,15 @@
|
|
| 1 |
from dotenv import load_dotenv
|
| 2 |
import datetime
|
| 3 |
import openai
|
| 4 |
-
import uuid
|
| 5 |
import gradio as gr
|
| 6 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 7 |
-
from langchain.vectorstores import Chroma
|
| 8 |
-
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 9 |
from langchain.chains import ConversationalRetrievalChain
|
| 10 |
from langchain.chains import RetrievalQA
|
| 11 |
-
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 12 |
|
| 13 |
import os
|
| 14 |
-
from langchain.chat_models import ChatOpenAI
|
| 15 |
-
from langchain import OpenAI
|
| 16 |
-
from langchain.document_loaders import WebBaseLoader, TextLoader, Docx2txtLoader, PyMuPDFLoader
|
| 17 |
-
from whatsapp_chat_custom import WhatsAppChatLoader # use this instead of from langchain.document_loaders import WhatsAppChatLoader
|
| 18 |
-
|
| 19 |
-
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
|
| 20 |
-
from ibm_watson_machine_learning.foundation_models.utils.enums import DecodingMethods
|
| 21 |
from ibm_watson_machine_learning.foundation_models import Model
|
| 22 |
-
from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
|
| 23 |
from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes
|
| 24 |
|
| 25 |
-
import
|
| 26 |
|
| 27 |
from collections import deque
|
| 28 |
import re
|
|
@@ -68,33 +55,34 @@ if mode.type!='userInputDocs':
|
|
| 68 |
def setOaiApiKey(creds):
|
| 69 |
creds = getOaiCreds(creds)
|
| 70 |
try:
|
| 71 |
-
openai.
|
|
|
|
| 72 |
api_key_st = creds
|
| 73 |
-
return 'OpenAI credentials accepted.', *[
|
| 74 |
except Exception as e:
|
| 75 |
gr.Warning(str(e))
|
| 76 |
-
return [
|
| 77 |
|
| 78 |
def setBamApiKey(creds):
|
| 79 |
creds = getBamCreds(creds)
|
| 80 |
try:
|
| 81 |
-
bam_models =
|
| 82 |
bam_models = sorted(x.id for x in bam_models)
|
| 83 |
api_key_st = creds
|
| 84 |
-
return 'BAM credentials accepted.', *[
|
| 85 |
except Exception as e:
|
| 86 |
gr.Warning(str(e))
|
| 87 |
-
return *[
|
| 88 |
|
| 89 |
def setWxApiKey(key, p_id):
|
| 90 |
creds = getWxCreds(key, p_id)
|
| 91 |
try:
|
| 92 |
Model(model_id='google/flan-ul2', credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 93 |
api_key_st = creds
|
| 94 |
-
return 'Watsonx credentials accepted.', *[
|
| 95 |
except Exception as e:
|
| 96 |
gr.Warning(str(e))
|
| 97 |
-
return [
|
| 98 |
|
| 99 |
|
| 100 |
# convert user uploaded data to vectorstore
|
|
@@ -109,12 +97,9 @@ def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.P
|
|
| 109 |
userUrls = [x.strip() for x in userUrls.split(",")] if userUrls else []
|
| 110 |
#create documents
|
| 111 |
documents = data_ingestion(file_list=file_paths, url_list=userUrls, prog=progress)
|
| 112 |
-
if documents:
|
| 113 |
-
for file in file_paths:
|
| 114 |
-
os.remove(file)
|
| 115 |
-
else:
|
| 116 |
gr.Error('No documents found')
|
| 117 |
-
return {}, '', *[
|
| 118 |
# Splitting and Chunks
|
| 119 |
docs = split_docs(documents)
|
| 120 |
# Embeddings
|
|
@@ -122,7 +107,7 @@ def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.P
|
|
| 122 |
embeddings = getEmbeddingFunc(api_key_st)
|
| 123 |
except Exception as e:
|
| 124 |
gr.Error(str(e))
|
| 125 |
-
return {}, '', *[
|
| 126 |
|
| 127 |
progress(0.5, 'Creating Vector Database')
|
| 128 |
vsDict_st = getVsDict(embeddings, docs, vsDict_st)
|
|
@@ -131,7 +116,9 @@ def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.P
|
|
| 131 |
src_str = str(src_str[1]) + ' source document(s) successfully loaded in vector store.'+'\n\n' + src_str[0]
|
| 132 |
|
| 133 |
progress(1, 'Data loaded')
|
| 134 |
-
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# initialize chatbot function sets the QA Chain, and also sets/updates any other components to start chatting. updateQaChain function only updates QA chain and will be called whenever Adv Settings are updated.
|
| 137 |
def initializeChatbot(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st, progress=gr.Progress()):
|
|
@@ -146,8 +133,8 @@ def initializeChatbot(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st, progr
|
|
| 146 |
welMsg = welcomeMsgDefault
|
| 147 |
print('Chatbot initialized at ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
| 148 |
|
| 149 |
-
return qa_chain_st, chainTuple[1],
|
| 150 |
-
,
|
| 151 |
|
| 152 |
# just update the QA Chain, no updates to any UI
|
| 153 |
def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
|
@@ -171,7 +158,7 @@ def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
|
| 171 |
# settingsUpdated = 'Settings updated:'+ ' Model=' + modelName + ', Temp=' + str(temp)+ ', k=' + str(k)
|
| 172 |
# gr.Info(settingsUpdated)
|
| 173 |
|
| 174 |
-
if 'meta-llama/
|
| 175 |
prompt = promptLlama
|
| 176 |
else:
|
| 177 |
prompt = None
|
|
@@ -193,10 +180,10 @@ def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
|
| 193 |
rephrase_question=rephQs,
|
| 194 |
return_source_documents=True,
|
| 195 |
return_generated_question=True,
|
| 196 |
-
combine_docs_chain_kwargs={'prompt':
|
| 197 |
)
|
| 198 |
|
| 199 |
-
return qa_chain_st,
|
| 200 |
|
| 201 |
|
| 202 |
def respond(message, chat_history, qa_chain):
|
|
@@ -206,10 +193,10 @@ def respond(message, chat_history, qa_chain):
|
|
| 206 |
streaming_answer = ""
|
| 207 |
for ele in "".join(result['answer']):
|
| 208 |
streaming_answer += ele
|
| 209 |
-
yield "", chat_history + [(message, streaming_answer)], src_docs,
|
| 210 |
|
| 211 |
chat_history.extend([(message, result['answer'])])
|
| 212 |
-
yield "", chat_history, src_docs,
|
| 213 |
|
| 214 |
#####################################################################################################
|
| 215 |
|
|
@@ -243,11 +230,11 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 243 |
, info='Internal IBMers only')
|
| 244 |
bamKey_btn = gr.Button("Submit BAM API Key")
|
| 245 |
with gr.Row(visible=mode.uiAddDataVis):
|
| 246 |
-
upload_fb = gr.Files(scale=
|
| 247 |
-
urls_tb = gr.Textbox(scale=
|
| 248 |
, info=url_tb_info\
|
| 249 |
, placeholder=url_tb_ph)
|
| 250 |
-
data_ingest_btn = gr.Button("Load Data")
|
| 251 |
status_tb = gr.TextArea(label='Status Info')
|
| 252 |
initChatbot_btn = gr.Button("Initialize Chatbot", variant="primary", interactive=False)
|
| 253 |
|
|
@@ -268,7 +255,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 268 |
temp_sld = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label="Temperature", info='Sampling temperature to use when calling LLM. Defaults to 0.7')
|
| 269 |
k_sld = gr.Slider(minimum=1, maximum=10, step=1, value=mode.k, label="K", info='Number of relavant documents to return from Vector Store. Defaults to 4')
|
| 270 |
model_dd = gr.Dropdown(label='Model Name'\
|
| 271 |
-
, choices=getModelChoices(
|
| 272 |
, info=model_dd_info)
|
| 273 |
stdlQs_rb = gr.Radio(label='Standalone Question', info=stdlQs_rb_info\
|
| 274 |
, type='index', value=stdlQs_rb_choices[1]\
|
|
@@ -277,7 +264,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 277 |
### Setup the Gradio Event Listeners
|
| 278 |
|
| 279 |
# OpenAI API button
|
| 280 |
-
oaiKey_btn_args = {'fn':setOaiApiKey, 'inputs':[oaiKey_tb], 'outputs':credComps_op}
|
| 281 |
oaiKey_btn.click(**oaiKey_btn_args)
|
| 282 |
oaiKey_tb.submit(**oaiKey_btn_args)
|
| 283 |
|
|
@@ -311,5 +298,5 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 311 |
btn.click(**chat_btn_args)
|
| 312 |
msg.submit(**chat_btn_args)
|
| 313 |
|
| 314 |
-
demo.queue(
|
| 315 |
-
demo.launch(show_error=True)
|
|
|
|
| 1 |
from dotenv import load_dotenv
|
| 2 |
import datetime
|
| 3 |
import openai
|
|
|
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 5 |
from langchain.chains import ConversationalRetrievalChain
|
| 6 |
from langchain.chains import RetrievalQA
|
|
|
|
| 7 |
|
| 8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from ibm_watson_machine_learning.foundation_models import Model
|
|
|
|
| 10 |
from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes
|
| 11 |
|
| 12 |
+
from genai.model import Model as genaiModel
|
| 13 |
|
| 14 |
from collections import deque
|
| 15 |
import re
|
|
|
|
| 55 |
def setOaiApiKey(creds):
|
| 56 |
creds = getOaiCreds(creds)
|
| 57 |
try:
|
| 58 |
+
openAi_models = openai.OpenAI(api_key=creds.get('oai_key','Null')).models.list().data # test the API key
|
| 59 |
+
openAi_models = sorted([x.id for x in openAi_models if x.owned_by=='openai'])
|
| 60 |
api_key_st = creds
|
| 61 |
+
return 'OpenAI credentials accepted.', *[gr.update(interactive=False) for x in credComps_btn_tb], api_key_st, gr.update(choices=getModelChoices(openAi_models, ModelTypes, bam_models_def))
|
| 62 |
except Exception as e:
|
| 63 |
gr.Warning(str(e))
|
| 64 |
+
return *[gr.update() for x in credComps_op], gr.update()
|
| 65 |
|
| 66 |
def setBamApiKey(creds):
|
| 67 |
creds = getBamCreds(creds)
|
| 68 |
try:
|
| 69 |
+
bam_models = genaiModel.models(credentials=creds['bam_creds'])
|
| 70 |
bam_models = sorted(x.id for x in bam_models)
|
| 71 |
api_key_st = creds
|
| 72 |
+
return 'BAM credentials accepted.', *[gr.update(interactive=False) for x in credComps_btn_tb], api_key_st, gr.update(choices=getModelChoices(openAi_models_def, ModelTypes, bam_models))
|
| 73 |
except Exception as e:
|
| 74 |
gr.Warning(str(e))
|
| 75 |
+
return *[gr.update() for x in credComps_op], gr.update()
|
| 76 |
|
| 77 |
def setWxApiKey(key, p_id):
|
| 78 |
creds = getWxCreds(key, p_id)
|
| 79 |
try:
|
| 80 |
Model(model_id='google/flan-ul2', credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 81 |
api_key_st = creds
|
| 82 |
+
return 'Watsonx credentials accepted.', *[gr.update(interactive=False) for x in credComps_btn_tb], api_key_st
|
| 83 |
except Exception as e:
|
| 84 |
gr.Warning(str(e))
|
| 85 |
+
return [gr.update() for x in credComps_op]
|
| 86 |
|
| 87 |
|
| 88 |
# convert user uploaded data to vectorstore
|
|
|
|
| 97 |
userUrls = [x.strip() for x in userUrls.split(",")] if userUrls else []
|
| 98 |
#create documents
|
| 99 |
documents = data_ingestion(file_list=file_paths, url_list=userUrls, prog=progress)
|
| 100 |
+
if not documents:
|
|
|
|
|
|
|
|
|
|
| 101 |
gr.Error('No documents found')
|
| 102 |
+
return {}, '', *[gr.update() for x in opComponents]
|
| 103 |
# Splitting and Chunks
|
| 104 |
docs = split_docs(documents)
|
| 105 |
# Embeddings
|
|
|
|
| 107 |
embeddings = getEmbeddingFunc(api_key_st)
|
| 108 |
except Exception as e:
|
| 109 |
gr.Error(str(e))
|
| 110 |
+
return {}, '', *[gr.update() for x in opComponents]
|
| 111 |
|
| 112 |
progress(0.5, 'Creating Vector Database')
|
| 113 |
vsDict_st = getVsDict(embeddings, docs, vsDict_st)
|
|
|
|
| 116 |
src_str = str(src_str[1]) + ' source document(s) successfully loaded in vector store.'+'\n\n' + src_str[0]
|
| 117 |
|
| 118 |
progress(1, 'Data loaded')
|
| 119 |
+
for file in file_paths:
|
| 120 |
+
os.remove(file)
|
| 121 |
+
return vsDict_st, src_str, *[gr.update(interactive=False) for x in [data_ingest_btn, upload_fb]], gr.update(interactive=False, placeholder=''), gr.update(interactive=True)
|
| 122 |
|
| 123 |
# initialize chatbot function sets the QA Chain, and also sets/updates any other components to start chatting. updateQaChain function only updates QA chain and will be called whenever Adv Settings are updated.
|
| 124 |
def initializeChatbot(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st, progress=gr.Progress()):
|
|
|
|
| 133 |
welMsg = welcomeMsgDefault
|
| 134 |
print('Chatbot initialized at ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
| 135 |
|
| 136 |
+
return qa_chain_st, chainTuple[1], gr.update(interactive=True), gr.update('Chatbot ready. Now visit the chatbot Tab.', interactive=False)\
|
| 137 |
+
, gr.update(), gr.update(selected='cb'), gr.update(value=[('', welMsg)])
|
| 138 |
|
| 139 |
# just update the QA Chain, no updates to any UI
|
| 140 |
def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
|
|
|
| 158 |
# settingsUpdated = 'Settings updated:'+ ' Model=' + modelName + ', Temp=' + str(temp)+ ', k=' + str(k)
|
| 159 |
# gr.Info(settingsUpdated)
|
| 160 |
|
| 161 |
+
if 'meta-llama/' in modelNameDD:
|
| 162 |
prompt = promptLlama
|
| 163 |
else:
|
| 164 |
prompt = None
|
|
|
|
| 180 |
rephrase_question=rephQs,
|
| 181 |
return_source_documents=True,
|
| 182 |
return_generated_question=True,
|
| 183 |
+
combine_docs_chain_kwargs={'prompt':prompt}
|
| 184 |
)
|
| 185 |
|
| 186 |
+
return qa_chain_st, gr.update(value=modelNameDD)
|
| 187 |
|
| 188 |
|
| 189 |
def respond(message, chat_history, qa_chain):
|
|
|
|
| 193 |
streaming_answer = ""
|
| 194 |
for ele in "".join(result['answer']):
|
| 195 |
streaming_answer += ele
|
| 196 |
+
yield "", chat_history + [(message, streaming_answer)], src_docs, gr.update('Please wait...', interactive=False)
|
| 197 |
|
| 198 |
chat_history.extend([(message, result['answer'])])
|
| 199 |
+
yield "", chat_history, src_docs, gr.update('Send Message', interactive=True)
|
| 200 |
|
| 201 |
#####################################################################################################
|
| 202 |
|
|
|
|
| 230 |
, info='Internal IBMers only')
|
| 231 |
bamKey_btn = gr.Button("Submit BAM API Key")
|
| 232 |
with gr.Row(visible=mode.uiAddDataVis):
|
| 233 |
+
upload_fb = gr.Files(scale=1, label="Upload (multiple) Files - pdf/txt/docx supported", file_types=['.doc', '.docx', 'text', '.pdf', '.csv', '.ppt', '.pptx'])
|
| 234 |
+
urls_tb = gr.Textbox(scale=1, label="Enter URLs starting with https (comma separated)"\
|
| 235 |
, info=url_tb_info\
|
| 236 |
, placeholder=url_tb_ph)
|
| 237 |
+
data_ingest_btn = gr.Button(scale=0, value="Load Data")
|
| 238 |
status_tb = gr.TextArea(label='Status Info')
|
| 239 |
initChatbot_btn = gr.Button("Initialize Chatbot", variant="primary", interactive=False)
|
| 240 |
|
|
|
|
| 255 |
temp_sld = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label="Temperature", info='Sampling temperature to use when calling LLM. Defaults to 0.7')
|
| 256 |
k_sld = gr.Slider(minimum=1, maximum=10, step=1, value=mode.k, label="K", info='Number of relavant documents to return from Vector Store. Defaults to 4')
|
| 257 |
model_dd = gr.Dropdown(label='Model Name'\
|
| 258 |
+
, choices=getModelChoices(openAi_models_def, ModelTypes, bam_models_def), allow_custom_value=True\
|
| 259 |
, info=model_dd_info)
|
| 260 |
stdlQs_rb = gr.Radio(label='Standalone Question', info=stdlQs_rb_info\
|
| 261 |
, type='index', value=stdlQs_rb_choices[1]\
|
|
|
|
| 264 |
### Setup the Gradio Event Listeners
|
| 265 |
|
| 266 |
# OpenAI API button
|
| 267 |
+
oaiKey_btn_args = {'fn':setOaiApiKey, 'inputs':[oaiKey_tb], 'outputs':credComps_op+[model_dd]}
|
| 268 |
oaiKey_btn.click(**oaiKey_btn_args)
|
| 269 |
oaiKey_tb.submit(**oaiKey_btn_args)
|
| 270 |
|
|
|
|
| 298 |
btn.click(**chat_btn_args)
|
| 299 |
msg.submit(**chat_btn_args)
|
| 300 |
|
| 301 |
+
demo.queue(default_concurrency_limit=10)
|
| 302 |
+
demo.launch(show_error=True, auth=(os.getenv("USERNAME",''), os.getenv("PASSWORD",'')))
|
ttyd_consts.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from langchain import PromptTemplate
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
load_dotenv()
|
|
@@ -44,33 +44,9 @@ Question: {question} [/INST]
|
|
| 44 |
|
| 45 |
promptLlama=PromptTemplate(input_variables=['context', 'question'], template=llamaPromptTemplate)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
'
|
| 50 |
-
'tiiuae/falcon-40b',
|
| 51 |
-
'ibm/falcon-40b-8lang-instruct',
|
| 52 |
-
'google/flan-t5-xl',
|
| 53 |
-
'google/flan-t5-xxl',
|
| 54 |
-
'google/flan-ul2',
|
| 55 |
-
'eleutherai/gpt-neox-20b',
|
| 56 |
-
'togethercomputer/gpt-neoxt-chat-base-20b',
|
| 57 |
-
'ibm/granite-13b-sft',
|
| 58 |
-
'ibm/granite-13b-sft-cft',
|
| 59 |
-
'ibm/granite-3b-code-v1',
|
| 60 |
-
'meta-llama/llama-2-13b',
|
| 61 |
-
'meta-llama/llama-2-13b-chat',
|
| 62 |
-
'meta-llama/llama-2-13b-chat-beam',
|
| 63 |
-
'meta-llama/llama-2-70b',
|
| 64 |
-
'meta-llama/llama-2-70b-chat',
|
| 65 |
-
'meta-llama/llama-2-7b',
|
| 66 |
-
'meta-llama/llama-2-7b-chat',
|
| 67 |
-
'mosaicml/mpt-30b',
|
| 68 |
-
'ibm/mpt-7b-instruct',
|
| 69 |
-
'bigscience/mt0-xxl',
|
| 70 |
-
'bigcode/starcoder',
|
| 71 |
-
'google/ul2'])
|
| 72 |
-
|
| 73 |
-
openAi_models = ['gpt-3.5-turbo (openai)', 'gpt-3.5-turbo-16k (openai)', 'gpt-4 (openai)', 'text-davinci-003 (Legacy - openai)', 'text-curie-001 (Legacy - openai)', 'babbage-002 (openai)']
|
| 74 |
|
| 75 |
model_dd_info = 'Make sure your credentials are submitted before changing the model. You can also input any OpenAI model name or Watsonx/BAM model ID.'
|
| 76 |
|
|
|
|
| 1 |
+
from langchain.prompts import PromptTemplate
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
load_dotenv()
|
|
|
|
| 44 |
|
| 45 |
promptLlama=PromptTemplate(input_variables=['context', 'question'], template=llamaPromptTemplate)
|
| 46 |
|
| 47 |
+
bam_models_def = ['bigscience/bloom', 'meta-llama/llama-2-13b', 'meta-llama/llama-2-13b-chat', 'add credentials to see full list of models']
|
| 48 |
+
|
| 49 |
+
openAi_models_def = ['gpt-3.5-turbo', 'gpt-4', 'text-davinci-003', 'add credentials to see full list of models']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
model_dd_info = 'Make sure your credentials are submitted before changing the model. You can also input any OpenAI model name or Watsonx/BAM model ID.'
|
| 52 |
|
ttyd_functions.py
CHANGED
|
@@ -3,7 +3,6 @@ import datetime
|
|
| 3 |
import gradio as gr
|
| 4 |
import time
|
| 5 |
import uuid
|
| 6 |
-
import openai
|
| 7 |
from langchain.embeddings import OpenAIEmbeddings
|
| 8 |
from langchain.vectorstores import Chroma
|
| 9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
@@ -24,7 +23,7 @@ import tiktoken
|
|
| 24 |
import gdown
|
| 25 |
|
| 26 |
from langchain.chat_models import ChatOpenAI
|
| 27 |
-
from langchain import OpenAI
|
| 28 |
|
| 29 |
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
|
| 30 |
from ibm_watson_machine_learning.foundation_models.utils.enums import DecodingMethods
|
|
@@ -32,7 +31,7 @@ from ibm_watson_machine_learning.foundation_models import Model
|
|
| 32 |
from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
|
| 33 |
|
| 34 |
|
| 35 |
-
import
|
| 36 |
from genai.extensions.langchain import LangChainInterface
|
| 37 |
from genai.schemas import GenerateParams
|
| 38 |
|
|
@@ -53,7 +52,7 @@ def getOaiCreds(key):
|
|
| 53 |
def getBamCreds(key):
|
| 54 |
key = key if key else 'Null'
|
| 55 |
return {'service': 'bam',
|
| 56 |
-
'bam_creds' :
|
| 57 |
}
|
| 58 |
|
| 59 |
|
|
@@ -257,7 +256,7 @@ def data_ingestion(inputDir=None, file_list=[], url_list=[], gDriveFolder='', pr
|
|
| 257 |
documents = []
|
| 258 |
# Ingestion from Google Drive Folder
|
| 259 |
if gDriveFolder:
|
| 260 |
-
opFolder = './gDriveDocs/'
|
| 261 |
gdown.download_folder(url=gDriveFolder, output=opFolder, quiet=True)
|
| 262 |
files = [str(x) for x in Path(opFolder).glob('**/*')]
|
| 263 |
documents = ingestFiles(documents, files, prog)
|
|
@@ -325,7 +324,7 @@ def getEmbeddingFunc(creds):
|
|
| 325 |
elif creds.get('service')=='watsonx' or creds.get('service')=='bam':
|
| 326 |
# testModel = Model(model_id=ModelTypes.FLAN_UL2, credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 327 |
# del testModel
|
| 328 |
-
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") # for now use OpenSource model for embedding as WX doesnt have any embedding model
|
| 329 |
else:
|
| 330 |
raise Exception('Error: Invalid or None Credentials')
|
| 331 |
return embeddings
|
|
@@ -333,7 +332,7 @@ def getEmbeddingFunc(creds):
|
|
| 333 |
def getVsDict(embeddingFunc, docs, vsDict={}):
|
| 334 |
# create chroma client if doesnt exist
|
| 335 |
if vsDict.get('chromaClient') is None:
|
| 336 |
-
vsDict['chromaDir'] = './vecstore/'+str(uuid.uuid1())
|
| 337 |
vsDict['chromaClient'] = Chroma(embedding_function=embeddingFunc, persist_directory=vsDict['chromaDir'])
|
| 338 |
# clear chroma client before adding new docs
|
| 339 |
if vsDict['chromaClient']._collection.count()>0:
|
|
@@ -374,4 +373,4 @@ def changeModel(oldModel, newModel):
|
|
| 374 |
return newModel
|
| 375 |
|
| 376 |
def getModelChoices(openAi_models, wml_models, bam_models):
|
| 377 |
-
return [model for model in openAi_models] + [model.value+' (watsonx)' for model in wml_models] + [model + ' (bam)' for model in bam_models]
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import time
|
| 5 |
import uuid
|
|
|
|
| 6 |
from langchain.embeddings import OpenAIEmbeddings
|
| 7 |
from langchain.vectorstores import Chroma
|
| 8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 23 |
import gdown
|
| 24 |
|
| 25 |
from langchain.chat_models import ChatOpenAI
|
| 26 |
+
from langchain.llms import OpenAI
|
| 27 |
|
| 28 |
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
|
| 29 |
from ibm_watson_machine_learning.foundation_models.utils.enums import DecodingMethods
|
|
|
|
| 31 |
from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
|
| 32 |
|
| 33 |
|
| 34 |
+
from genai.credentials import Credentials
|
| 35 |
from genai.extensions.langchain import LangChainInterface
|
| 36 |
from genai.schemas import GenerateParams
|
| 37 |
|
|
|
|
| 52 |
def getBamCreds(key):
|
| 53 |
key = key if key else 'Null'
|
| 54 |
return {'service': 'bam',
|
| 55 |
+
'bam_creds' : Credentials(key, api_endpoint='https://bam-api.res.ibm.com/v1')
|
| 56 |
}
|
| 57 |
|
| 58 |
|
|
|
|
| 256 |
documents = []
|
| 257 |
# Ingestion from Google Drive Folder
|
| 258 |
if gDriveFolder:
|
| 259 |
+
opFolder = './tmp/gDriveDocs/'
|
| 260 |
gdown.download_folder(url=gDriveFolder, output=opFolder, quiet=True)
|
| 261 |
files = [str(x) for x in Path(opFolder).glob('**/*')]
|
| 262 |
documents = ingestFiles(documents, files, prog)
|
|
|
|
| 324 |
elif creds.get('service')=='watsonx' or creds.get('service')=='bam':
|
| 325 |
# testModel = Model(model_id=ModelTypes.FLAN_UL2, credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 326 |
# del testModel
|
| 327 |
+
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2", cache_folder='./tmp/hfEmbModel') # for now use OpenSource model for embedding as WX doesnt have any embedding model
|
| 328 |
else:
|
| 329 |
raise Exception('Error: Invalid or None Credentials')
|
| 330 |
return embeddings
|
|
|
|
| 332 |
def getVsDict(embeddingFunc, docs, vsDict={}):
|
| 333 |
# create chroma client if doesnt exist
|
| 334 |
if vsDict.get('chromaClient') is None:
|
| 335 |
+
vsDict['chromaDir'] = './tmp/vecstore/'+str(uuid.uuid1())
|
| 336 |
vsDict['chromaClient'] = Chroma(embedding_function=embeddingFunc, persist_directory=vsDict['chromaDir'])
|
| 337 |
# clear chroma client before adding new docs
|
| 338 |
if vsDict['chromaClient']._collection.count()>0:
|
|
|
|
| 373 |
return newModel
|
| 374 |
|
| 375 |
def getModelChoices(openAi_models, wml_models, bam_models):
|
| 376 |
+
return [model +' (openai)' for model in openAi_models] + [model.value +' (watsonx)' for model in wml_models] + [model + ' (bam)' for model in bam_models]
|