Spaces:
Runtime error
Runtime error
Commit
·
24c14af
1
Parent(s):
9d6b12b
added BAM support
Browse files- app.py +57 -48
- requirements.txt +2 -1
- ttyd_consts.py +35 -4
- ttyd_functions.py +70 -13
app.py
CHANGED
|
@@ -17,12 +17,13 @@ from langchain import OpenAI
|
|
| 17 |
from langchain.document_loaders import WebBaseLoader, TextLoader, Docx2txtLoader, PyMuPDFLoader
|
| 18 |
from whatsapp_chat_custom import WhatsAppChatLoader # use this instead of from langchain.document_loaders import WhatsAppChatLoader
|
| 19 |
|
| 20 |
-
from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes
|
| 21 |
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
|
| 22 |
from ibm_watson_machine_learning.foundation_models.utils.enums import DecodingMethods
|
| 23 |
from ibm_watson_machine_learning.foundation_models import Model
|
| 24 |
from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
|
| 25 |
|
|
|
|
|
|
|
| 26 |
from collections import deque
|
| 27 |
import re
|
| 28 |
from bs4 import BeautifulSoup
|
|
@@ -64,28 +65,37 @@ if mode.type!='userInputDocs':
|
|
| 64 |
|
| 65 |
###############################################################################################
|
| 66 |
|
| 67 |
-
def setOaiApiKey(
|
| 68 |
-
|
| 69 |
-
api_key = getOaiCreds(api_key)
|
| 70 |
try:
|
| 71 |
-
openai.Model.list(api_key=
|
| 72 |
-
api_key_st =
|
| 73 |
-
return
|
| 74 |
except Exception as e:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def setWxApiKey(key, p_id):
|
| 79 |
-
|
| 80 |
-
api_key = getWxCreds(key, p_id)
|
| 81 |
try:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
return *[x.update('Watsonx credentials accepted', interactive=False, type='text') for x in [wxKey_tb, wxPid_tb]], *[x.update(interactive=False) for x in credComps], api_key_st
|
| 86 |
except Exception as e:
|
| 87 |
-
|
|
|
|
| 88 |
|
|
|
|
| 89 |
# convert user uploaded data to vectorstore
|
| 90 |
def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.Progress()):
|
| 91 |
opComponents = [data_ingest_btn, upload_fb, urls_tb]
|
|
@@ -102,6 +112,7 @@ def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.P
|
|
| 102 |
for file in file_paths:
|
| 103 |
os.remove(file)
|
| 104 |
else:
|
|
|
|
| 105 |
return {}, '', *[x.update() for x in opComponents]
|
| 106 |
# Splitting and Chunks
|
| 107 |
docs = split_docs(documents)
|
|
@@ -109,7 +120,8 @@ def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.P
|
|
| 109 |
try:
|
| 110 |
embeddings = getEmbeddingFunc(api_key_st)
|
| 111 |
except Exception as e:
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
progress(0.5, 'Creating Vector Database')
|
| 115 |
vsDict_st = getVsDict(embeddings, docs, vsDict_st)
|
|
@@ -130,45 +142,30 @@ def initializeChatbot(temp, k, modelName, stdlQs, api_key_st, vsDict_st, progres
|
|
| 130 |
if mode.welcomeMsg:
|
| 131 |
welMsg = mode.welcomeMsg
|
| 132 |
else:
|
| 133 |
-
welMsg = qa_chain_st({'question': initialize_prompt, 'chat_history':[]})['answer']
|
|
|
|
| 134 |
print('Chatbot initialized at ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
| 135 |
|
| 136 |
return qa_chain_st, chainTuple[1], btn.update(interactive=True), initChatbot_btn.update('Chatbot ready. Now visit the chatbot Tab.', interactive=False)\
|
| 137 |
-
,
|
| 138 |
|
| 139 |
# just update the QA Chain, no updates to any UI
|
| 140 |
def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
| 141 |
# if we are not adding data from ui, then use vsDict_hard as vectorstore
|
| 142 |
if vsDict_st=={} and mode.type!='userInputDocs': vsDict_st=vsDict_hard
|
| 143 |
|
| 144 |
-
if api_key_st
|
| 145 |
if not 'openai' in modelNameDD:
|
| 146 |
modelNameDD = 'gpt-3.5-turbo (openai)' # default model for openai
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
try:
|
| 150 |
-
ChatOpenAI(openai_api_key=api_key_st.get('oai_key','Null'), temperature=0,model_name=modelName,max_tokens=1).predict('')
|
| 151 |
-
llm = ChatOpenAI(openai_api_key=api_key_st.get('oai_key','Null'), temperature=float(temp),model_name=modelName)
|
| 152 |
-
except:
|
| 153 |
-
OpenAI(openai_api_key=api_key_st.get('oai_key','Null'), temperature=0,model_name=modelName,max_tokens=1).predict('')
|
| 154 |
-
llm = OpenAI(openai_api_key=api_key_st.get('oai_key','Null'), temperature=float(temp),model_name=modelName)
|
| 155 |
-
elif api_key_st.get('service')=='watsonx':
|
| 156 |
if not 'watsonx' in modelNameDD:
|
| 157 |
modelNameDD = 'meta-llama/llama-2-70b-chat (watsonx)' # default model for watsonx
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
GenParams.TEMPERATURE: float(temp),
|
| 164 |
-
GenParams.TOP_K: 50,
|
| 165 |
-
GenParams.TOP_P: 1
|
| 166 |
-
}
|
| 167 |
-
flan_ul2_model = Model(
|
| 168 |
-
model_id=modelName,
|
| 169 |
-
params=wxModelParams,
|
| 170 |
-
credentials=api_key_st['credentials'], project_id=api_key_st['project_id'])
|
| 171 |
-
llm = WatsonxLLM(model=flan_ul2_model)
|
| 172 |
else:
|
| 173 |
raise Exception('Error: Invalid or None Credentials')
|
| 174 |
# settingsUpdated = 'Settings updated:'+ ' Model=' + modelName + ', Temp=' + str(temp)+ ', k=' + str(k)
|
|
@@ -196,7 +193,7 @@ def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
|
| 196 |
|
| 197 |
|
| 198 |
def respond(message, chat_history, qa_chain):
|
| 199 |
-
result = qa_chain({'question': message, "chat_history": [tuple(x) for x in chat_history]})
|
| 200 |
src_docs = getSourcesFromMetadata([x.metadata for x in result["source_documents"]], sourceOnly=False)[0]
|
| 201 |
# streaming
|
| 202 |
streaming_answer = ""
|
|
@@ -227,6 +224,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 227 |
oaiKey_tb = gr.Textbox(label="OpenAI API Key", type='password'\
|
| 228 |
, info='You can find OpenAI API key at https://platform.openai.com/account/api-keys')
|
| 229 |
oaiKey_btn = gr.Button("Submit OpenAI API Key")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
with gr.Column():
|
| 231 |
wxKey_tb = gr.Textbox(label="Watsonx API Key", type='password'\
|
| 232 |
, info='You can find IBM Cloud API Key at Manage > Access (IAM) > API keys on https://cloud.ibm.com/iam/overview')
|
|
@@ -239,12 +240,15 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 239 |
, info=url_tb_info\
|
| 240 |
, placeholder=url_tb_ph)
|
| 241 |
data_ingest_btn = gr.Button("Load Data")
|
| 242 |
-
status_tb = gr.TextArea(label='Status
|
| 243 |
initChatbot_btn = gr.Button("Initialize Chatbot", variant="primary")
|
| 244 |
|
|
|
|
|
|
|
|
|
|
| 245 |
with gr.Tab('Chatbot', id='cb'):
|
| 246 |
with gr.Row():
|
| 247 |
-
chatbot = gr.Chatbot(label="Chat History", scale=2)
|
| 248 |
srcDocs = gr.TextArea(label="References")
|
| 249 |
msg = gr.Textbox(label="User Input",placeholder="Type your questions here")
|
| 250 |
with gr.Row():
|
|
@@ -266,12 +270,17 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue='orange', secondary_hue='gray
|
|
| 266 |
### Setup the Gradio Event Listeners
|
| 267 |
|
| 268 |
# OpenAI API button
|
| 269 |
-
oaiKey_btn_args = {'fn':setOaiApiKey, 'inputs':[oaiKey_tb], 'outputs':
|
| 270 |
oaiKey_btn.click(**oaiKey_btn_args)
|
| 271 |
oaiKey_tb.submit(**oaiKey_btn_args)
|
| 272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
# Watsonx Creds button
|
| 274 |
-
wxKey_btn_args = {'fn':setWxApiKey, 'inputs':[wxKey_tb, wxPid_tb], 'outputs':
|
| 275 |
wxKey_btn.click(**wxKey_btn_args)
|
| 276 |
|
| 277 |
# Data Ingest Button
|
|
|
|
| 17 |
from langchain.document_loaders import WebBaseLoader, TextLoader, Docx2txtLoader, PyMuPDFLoader
|
| 18 |
from whatsapp_chat_custom import WhatsAppChatLoader # use this instead of from langchain.document_loaders import WhatsAppChatLoader
|
| 19 |
|
|
|
|
| 20 |
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
|
| 21 |
from ibm_watson_machine_learning.foundation_models.utils.enums import DecodingMethods
|
| 22 |
from ibm_watson_machine_learning.foundation_models import Model
|
| 23 |
from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
|
| 24 |
|
| 25 |
+
import genai
|
| 26 |
+
|
| 27 |
from collections import deque
|
| 28 |
import re
|
| 29 |
from bs4 import BeautifulSoup
|
|
|
|
| 65 |
|
| 66 |
###############################################################################################
|
| 67 |
|
| 68 |
+
def setOaiApiKey(creds):
|
| 69 |
+
creds = getOaiCreds(creds)
|
|
|
|
| 70 |
try:
|
| 71 |
+
openai.Model.list(api_key=creds.get('oai_key','Null')) # test the API key
|
| 72 |
+
api_key_st = creds
|
| 73 |
+
return 'OpenAI credentials accepted', *[x.update(interactive=False) for x in credComps_btn_tb], api_key_st
|
| 74 |
except Exception as e:
|
| 75 |
+
gr.Warning(str(e))
|
| 76 |
+
return [x.update() for x in credComps_op]
|
| 77 |
+
|
| 78 |
+
def setBamApiKey(creds):
|
| 79 |
+
creds = getBamCreds(creds)
|
| 80 |
+
try:
|
| 81 |
+
genai.Model.models(credentials=creds['bam_creds'])
|
| 82 |
+
api_key_st = creds
|
| 83 |
+
return 'BAM credentials accepted', *[x.update(interactive=False) for x in credComps_btn_tb], api_key_st
|
| 84 |
+
except Exception as e:
|
| 85 |
+
gr.Warning(str(e))
|
| 86 |
+
return [x.update() for x in credComps_op]
|
| 87 |
|
| 88 |
def setWxApiKey(key, p_id):
|
| 89 |
+
creds = getWxCreds(key, p_id)
|
|
|
|
| 90 |
try:
|
| 91 |
+
Model(model_id='google/flan-ul2', credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 92 |
+
api_key_st = creds
|
| 93 |
+
return 'Watsonx credentials accepted', *[x.update(interactive=False) for x in credComps_btn_tb], api_key_st
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
+
gr.Warning(str(e))
|
| 96 |
+
return [x.update() for x in credComps_op]
|
| 97 |
|
| 98 |
+
|
| 99 |
# convert user uploaded data to vectorstore
|
| 100 |
def uiData_vecStore(userFiles, userUrls, api_key_st, vsDict_st={}, progress=gr.Progress()):
|
| 101 |
opComponents = [data_ingest_btn, upload_fb, urls_tb]
|
|
|
|
| 112 |
for file in file_paths:
|
| 113 |
os.remove(file)
|
| 114 |
else:
|
| 115 |
+
gr.Error('No documents found')
|
| 116 |
return {}, '', *[x.update() for x in opComponents]
|
| 117 |
# Splitting and Chunks
|
| 118 |
docs = split_docs(documents)
|
|
|
|
| 120 |
try:
|
| 121 |
embeddings = getEmbeddingFunc(api_key_st)
|
| 122 |
except Exception as e:
|
| 123 |
+
gr.Error(str(e))
|
| 124 |
+
return {}, '', *[x.update() for x in opComponents]
|
| 125 |
|
| 126 |
progress(0.5, 'Creating Vector Database')
|
| 127 |
vsDict_st = getVsDict(embeddings, docs, vsDict_st)
|
|
|
|
| 142 |
if mode.welcomeMsg:
|
| 143 |
welMsg = mode.welcomeMsg
|
| 144 |
else:
|
| 145 |
+
# welMsg = qa_chain_st({'question': initialize_prompt, 'chat_history':[]})['answer']
|
| 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], btn.update(interactive=True), initChatbot_btn.update('Chatbot ready. Now visit the chatbot Tab.', interactive=False)\
|
| 150 |
+
, status_tb.update(), gr.Tabs.update(selected='cb'), chatbot.update(value=[('Hi', welMsg)])
|
| 151 |
|
| 152 |
# just update the QA Chain, no updates to any UI
|
| 153 |
def updateQaChain(temp, k, modelNameDD, stdlQs, api_key_st, vsDict_st):
|
| 154 |
# if we are not adding data from ui, then use vsDict_hard as vectorstore
|
| 155 |
if vsDict_st=={} and mode.type!='userInputDocs': vsDict_st=vsDict_hard
|
| 156 |
|
| 157 |
+
if api_key_st['service']=='openai':
|
| 158 |
if not 'openai' in modelNameDD:
|
| 159 |
modelNameDD = 'gpt-3.5-turbo (openai)' # default model for openai
|
| 160 |
+
llm = getOaiLlm(temp, modelNameDD, api_key_st)
|
| 161 |
+
elif api_key_st['service']=='watsonx':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
if not 'watsonx' in modelNameDD:
|
| 163 |
modelNameDD = 'meta-llama/llama-2-70b-chat (watsonx)' # default model for watsonx
|
| 164 |
+
llm = getWxLlm(temp, modelNameDD, api_key_st)
|
| 165 |
+
elif api_key_st['service']=='bam':
|
| 166 |
+
if not 'bam' in modelNameDD:
|
| 167 |
+
modelNameDD = 'ibm/granite-13b-sft (bam)' # default model for bam
|
| 168 |
+
llm = getBamLlm(temp, modelNameDD, api_key_st)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
else:
|
| 170 |
raise Exception('Error: Invalid or None Credentials')
|
| 171 |
# settingsUpdated = 'Settings updated:'+ ' Model=' + modelName + ', Temp=' + str(temp)+ ', k=' + str(k)
|
|
|
|
| 193 |
|
| 194 |
|
| 195 |
def respond(message, chat_history, qa_chain):
|
| 196 |
+
result = qa_chain({'question': message, "chat_history": [tuple(x) for x in chat_history[1:]]})
|
| 197 |
src_docs = getSourcesFromMetadata([x.metadata for x in result["source_documents"]], sourceOnly=False)[0]
|
| 198 |
# streaming
|
| 199 |
streaming_answer = ""
|
|
|
|
| 224 |
oaiKey_tb = gr.Textbox(label="OpenAI API Key", type='password'\
|
| 225 |
, info='You can find OpenAI API key at https://platform.openai.com/account/api-keys')
|
| 226 |
oaiKey_btn = gr.Button("Submit OpenAI API Key")
|
| 227 |
+
with gr.Column():
|
| 228 |
+
bamKey_tb = gr.Textbox(label="BAM API Key", type='password'\
|
| 229 |
+
, info='Internal IBMers only')
|
| 230 |
+
bamKey_btn = gr.Button("Submit BAM API Key")
|
| 231 |
with gr.Column():
|
| 232 |
wxKey_tb = gr.Textbox(label="Watsonx API Key", type='password'\
|
| 233 |
, info='You can find IBM Cloud API Key at Manage > Access (IAM) > API keys on https://cloud.ibm.com/iam/overview')
|
|
|
|
| 240 |
, info=url_tb_info\
|
| 241 |
, placeholder=url_tb_ph)
|
| 242 |
data_ingest_btn = gr.Button("Load Data")
|
| 243 |
+
status_tb = gr.TextArea(label='Status Info')
|
| 244 |
initChatbot_btn = gr.Button("Initialize Chatbot", variant="primary")
|
| 245 |
|
| 246 |
+
credComps_btn_tb = [oaiKey_tb, oaiKey_btn, bamKey_tb, bamKey_btn, wxKey_tb, wxPid_tb, wxKey_btn]
|
| 247 |
+
credComps_op = [status_tb] + credComps_btn_tb + [api_key_state]
|
| 248 |
+
|
| 249 |
with gr.Tab('Chatbot', id='cb'):
|
| 250 |
with gr.Row():
|
| 251 |
+
chatbot = gr.Chatbot(label="Chat History", scale=2, avatar_images=(user_avatar, bot_avatar))
|
| 252 |
srcDocs = gr.TextArea(label="References")
|
| 253 |
msg = gr.Textbox(label="User Input",placeholder="Type your questions here")
|
| 254 |
with gr.Row():
|
|
|
|
| 270 |
### Setup the Gradio Event Listeners
|
| 271 |
|
| 272 |
# OpenAI API button
|
| 273 |
+
oaiKey_btn_args = {'fn':setOaiApiKey, 'inputs':[oaiKey_tb], 'outputs':credComps_op}
|
| 274 |
oaiKey_btn.click(**oaiKey_btn_args)
|
| 275 |
oaiKey_tb.submit(**oaiKey_btn_args)
|
| 276 |
|
| 277 |
+
# BAM API button
|
| 278 |
+
bamKey_btn_args = {'fn':setBamApiKey, 'inputs':[bamKey_tb], 'outputs':credComps_op}
|
| 279 |
+
bamKey_btn.click(**bamKey_btn_args)
|
| 280 |
+
bamKey_tb.submit(**bamKey_btn_args)
|
| 281 |
+
|
| 282 |
# Watsonx Creds button
|
| 283 |
+
wxKey_btn_args = {'fn':setWxApiKey, 'inputs':[wxKey_tb, wxPid_tb], 'outputs':credComps_op}
|
| 284 |
wxKey_btn.click(**wxKey_btn_args)
|
| 285 |
|
| 286 |
# Data Ingest Button
|
requirements.txt
CHANGED
|
@@ -9,4 +9,5 @@ PyMuPDF
|
|
| 9 |
gdown
|
| 10 |
docx2txt
|
| 11 |
sentence-transformers
|
| 12 |
-
ibm-watson-machine-learning
|
|
|
|
|
|
| 9 |
gdown
|
| 10 |
docx2txt
|
| 11 |
sentence-transformers
|
| 12 |
+
ibm-watson-machine-learning
|
| 13 |
+
ibm-generative-ai
|
ttyd_consts.py
CHANGED
|
@@ -8,6 +8,11 @@ initialize_prompt = """Write a short welcome message to the user. Describe the d
|
|
| 8 |
If this data is about a person, mention his name instead of using pronouns. After describing the overview, you should mention top 3 example questions that the user can ask about this data.\
|
| 9 |
\n\nYour response should be short and precise. Format of your response should be Summary:\n{Description and Summary} \n\n Example Questions:\n{Example Questions}"""
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
nustian_exps = ['Tell me about NUSTIAN',
|
| 12 |
'Who is the NUSTIAN regional lead for Silicon Valley?',
|
| 13 |
'Tell me details about NUSTIAN coaching program.',
|
|
@@ -23,10 +28,35 @@ stdlQs_rb_choices = ['Retrieve relavant docs using original question, send orig
|
|
| 23 |
, 'Retrieve relavant docs using standalone question, send standalone question to LLM']
|
| 24 |
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
url_tb_info = 'Upto 100 domain webpages will be crawled for each URL. You can also enter online PDF files.'
|
| 32 |
|
|
@@ -70,6 +100,7 @@ welcomeMsgArslan = """Summary: The document provides a comprehensive overview of
|
|
| 70 |
3. Tell me about Arslan's educational background.
|
| 71 |
"""
|
| 72 |
|
|
|
|
| 73 |
|
| 74 |
class TtydMode():
|
| 75 |
def __init__(self, name='', title='', type='', dir=None, files=[], urls=[], vis=False, welMsg='', def_k=4):
|
|
|
|
| 8 |
If this data is about a person, mention his name instead of using pronouns. After describing the overview, you should mention top 3 example questions that the user can ask about this data.\
|
| 9 |
\n\nYour response should be short and precise. Format of your response should be Summary:\n{Description and Summary} \n\n Example Questions:\n{Example Questions}"""
|
| 10 |
|
| 11 |
+
|
| 12 |
+
user_avatar = 'https://cdn-icons-png.flaticon.com/512/6861/6861326.png'
|
| 13 |
+
# user_avatar = None
|
| 14 |
+
bot_avatar = 'https://cdn-icons-png.flaticon.com/512/1782/1782384.png'
|
| 15 |
+
|
| 16 |
nustian_exps = ['Tell me about NUSTIAN',
|
| 17 |
'Who is the NUSTIAN regional lead for Silicon Valley?',
|
| 18 |
'Tell me details about NUSTIAN coaching program.',
|
|
|
|
| 28 |
, 'Retrieve relavant docs using standalone question, send standalone question to LLM']
|
| 29 |
|
| 30 |
|
| 31 |
+
bam_models = sorted(['bigscience/bloom',
|
| 32 |
+
'salesforce/codegen2-16b',
|
| 33 |
+
'codellama/codellama-34b-instruct',
|
| 34 |
+
'tiiuae/falcon-40b',
|
| 35 |
+
'ibm/falcon-40b-8lang-instruct',
|
| 36 |
+
'google/flan-t5-xl',
|
| 37 |
+
'google/flan-t5-xxl',
|
| 38 |
+
'google/flan-ul2',
|
| 39 |
+
'eleutherai/gpt-neox-20b',
|
| 40 |
+
'togethercomputer/gpt-neoxt-chat-base-20b',
|
| 41 |
+
'ibm/granite-13b-sft',
|
| 42 |
+
'ibm/granite-13b-sft-cft',
|
| 43 |
+
'ibm/granite-3b-code-v1',
|
| 44 |
+
'meta-llama/llama-2-13b',
|
| 45 |
+
'meta-llama/llama-2-13b-chat',
|
| 46 |
+
'meta-llama/llama-2-13b-chat-beam',
|
| 47 |
+
'meta-llama/llama-2-70b',
|
| 48 |
+
'meta-llama/llama-2-70b-chat',
|
| 49 |
+
'meta-llama/llama-2-7b',
|
| 50 |
+
'meta-llama/llama-2-7b-chat',
|
| 51 |
+
'mosaicml/mpt-30b',
|
| 52 |
+
'ibm/mpt-7b-instruct',
|
| 53 |
+
'bigscience/mt0-xxl',
|
| 54 |
+
'bigcode/starcoder',
|
| 55 |
+
'google/ul2'])
|
| 56 |
+
|
| 57 |
+
model_dd_info = 'You can also input any OpenAI model name or BAM model ID.'
|
| 58 |
+
|
| 59 |
+
model_dd_choices = ['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)'] + [model.value+' (watsonx)' for model in ModelTypes] + [model + ' (bam)' for model in bam_models]
|
| 60 |
|
| 61 |
url_tb_info = 'Upto 100 domain webpages will be crawled for each URL. You can also enter online PDF files.'
|
| 62 |
|
|
|
|
| 100 |
3. Tell me about Arslan's educational background.
|
| 101 |
"""
|
| 102 |
|
| 103 |
+
welcomeMsgDefault = """Hello and welcome! I'm your personal data assistant. Ask me anything about your data and I'll try my best to answer."""
|
| 104 |
|
| 105 |
class TtydMode():
|
| 106 |
def __init__(self, name='', title='', type='', dir=None, files=[], urls=[], vis=False, welMsg='', def_k=4):
|
ttyd_functions.py
CHANGED
|
@@ -20,6 +20,19 @@ import mimetypes
|
|
| 20 |
from pathlib import Path
|
| 21 |
import tiktoken
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Regex pattern to match a URL
|
| 24 |
HTTP_URL_PATTERN = r'^http[s]*://.+'
|
| 25 |
|
|
@@ -28,21 +41,26 @@ media_files = tuple([x for x in mimetypes.types_map if mimetypes.types_map[x].sp
|
|
| 28 |
filter_strings = ['/email-protection#']
|
| 29 |
|
| 30 |
def getOaiCreds(key):
|
| 31 |
-
if key
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def getWxCreds(key, p_id):
|
| 39 |
-
if key
|
| 40 |
-
|
|
|
|
| 41 |
'credentials' : {"url": "https://us-south.ml.cloud.ibm.com", "apikey": key },
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
else:
|
| 45 |
-
return {}
|
| 46 |
|
| 47 |
def getPersonalBotApiKey():
|
| 48 |
if os.getenv("OPENAI_API_KEY"):
|
|
@@ -52,6 +70,45 @@ def getPersonalBotApiKey():
|
|
| 52 |
else:
|
| 53 |
return {}
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def get_hyperlinks(url):
|
| 56 |
try:
|
| 57 |
reqs = requests.get(url)
|
|
@@ -249,7 +306,7 @@ def getEmbeddingFunc(creds):
|
|
| 249 |
if creds.get('service')=='openai':
|
| 250 |
embeddings = OpenAIEmbeddings(openai_api_key=creds.get('oai_key','Null'))
|
| 251 |
# WX key used
|
| 252 |
-
elif creds.get('service')=='watsonx':
|
| 253 |
# testModel = Model(model_id=ModelTypes.FLAN_UL2, credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 254 |
# del testModel
|
| 255 |
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") # for now use OpenSource model for embedding as WX doesnt have any embedding model
|
|
|
|
| 20 |
from pathlib import Path
|
| 21 |
import tiktoken
|
| 22 |
|
| 23 |
+
from langchain.chat_models import ChatOpenAI
|
| 24 |
+
from langchain import OpenAI
|
| 25 |
+
|
| 26 |
+
from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
|
| 27 |
+
from ibm_watson_machine_learning.foundation_models.utils.enums import DecodingMethods
|
| 28 |
+
from ibm_watson_machine_learning.foundation_models import Model
|
| 29 |
+
from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
import genai
|
| 33 |
+
from genai.extensions.langchain import LangChainInterface
|
| 34 |
+
from genai.schemas import GenerateParams
|
| 35 |
+
|
| 36 |
# Regex pattern to match a URL
|
| 37 |
HTTP_URL_PATTERN = r'^http[s]*://.+'
|
| 38 |
|
|
|
|
| 41 |
filter_strings = ['/email-protection#']
|
| 42 |
|
| 43 |
def getOaiCreds(key):
|
| 44 |
+
key = key if key else 'Null'
|
| 45 |
+
return {'service': 'openai',
|
| 46 |
+
'oai_key' : key
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def getBamCreds(key):
|
| 51 |
+
key = key if key else 'Null'
|
| 52 |
+
return {'service': 'bam',
|
| 53 |
+
'bam_creds' : genai.Credentials(key, api_endpoint='https://bam-api.res.ibm.com/v1')
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
|
| 57 |
def getWxCreds(key, p_id):
|
| 58 |
+
key = key if key else 'Null'
|
| 59 |
+
p_id = p_id if p_id else 'Null'
|
| 60 |
+
return {'service': 'watsonx',
|
| 61 |
'credentials' : {"url": "https://us-south.ml.cloud.ibm.com", "apikey": key },
|
| 62 |
+
'project_id': p_id
|
| 63 |
+
}
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def getPersonalBotApiKey():
|
| 66 |
if os.getenv("OPENAI_API_KEY"):
|
|
|
|
| 70 |
else:
|
| 71 |
return {}
|
| 72 |
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def getOaiLlm(temp, modelNameDD, api_key_st):
|
| 76 |
+
modelName = modelNameDD.split('(')[0].strip()
|
| 77 |
+
# check if the input model is chat model or legacy model
|
| 78 |
+
try:
|
| 79 |
+
ChatOpenAI(openai_api_key=api_key_st['oai_key'], temperature=0,model_name=modelName,max_tokens=1).predict('')
|
| 80 |
+
llm = ChatOpenAI(openai_api_key=api_key_st['oai_key'], temperature=float(temp),model_name=modelName)
|
| 81 |
+
except:
|
| 82 |
+
OpenAI(openai_api_key=api_key_st['oai_key'], temperature=0,model_name=modelName,max_tokens=1).predict('')
|
| 83 |
+
llm = OpenAI(openai_api_key=api_key_st['oai_key'], temperature=float(temp),model_name=modelName)
|
| 84 |
+
return llm
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def getWxLlm(temp, modelNameDD, api_key_st):
|
| 88 |
+
modelName = modelNameDD.split('(')[0].strip()
|
| 89 |
+
wxModelParams = {
|
| 90 |
+
GenParams.DECODING_METHOD: DecodingMethods.SAMPLE,
|
| 91 |
+
GenParams.MAX_NEW_TOKENS: 1000,
|
| 92 |
+
GenParams.MIN_NEW_TOKENS: 1,
|
| 93 |
+
GenParams.TEMPERATURE: float(temp),
|
| 94 |
+
GenParams.TOP_K: 50,
|
| 95 |
+
GenParams.TOP_P: 1
|
| 96 |
+
}
|
| 97 |
+
model = Model(
|
| 98 |
+
model_id=modelName,
|
| 99 |
+
params=wxModelParams,
|
| 100 |
+
credentials=api_key_st['credentials'], project_id=api_key_st['project_id'])
|
| 101 |
+
llm = WatsonxLLM(model=model)
|
| 102 |
+
return llm
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def getBamLlm(temp, modelNameDD, api_key_st):
|
| 106 |
+
modelName = modelNameDD.split('(')[0].strip()
|
| 107 |
+
parameters = GenerateParams(decoding_method="sample", max_new_tokens=1024, temperature=float(temp), top_k=50, top_p=1)
|
| 108 |
+
llm = LangChainInterface(model=modelName, params=parameters, credentials=api_key_st['bam_creds'])
|
| 109 |
+
return llm
|
| 110 |
+
|
| 111 |
+
|
| 112 |
def get_hyperlinks(url):
|
| 113 |
try:
|
| 114 |
reqs = requests.get(url)
|
|
|
|
| 306 |
if creds.get('service')=='openai':
|
| 307 |
embeddings = OpenAIEmbeddings(openai_api_key=creds.get('oai_key','Null'))
|
| 308 |
# WX key used
|
| 309 |
+
elif creds.get('service')=='watsonx' or creds.get('service')=='bam':
|
| 310 |
# testModel = Model(model_id=ModelTypes.FLAN_UL2, credentials=creds['credentials'], project_id=creds['project_id']) # test the API key
|
| 311 |
# del testModel
|
| 312 |
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") # for now use OpenSource model for embedding as WX doesnt have any embedding model
|