mahynski commited on
Commit
39af631
·
1 Parent(s): 77c0943
Files changed (2) hide show
  1. app.py +43 -22
  2. requirements.txt +3 -1
app.py CHANGED
@@ -5,10 +5,12 @@ import streamlit as st
5
 
6
  from llama_index.llms.gemini import Gemini
7
  from llama_index.llms.huggingface import HuggingFaceLLM
 
8
  from llama_index.llms.mistralai import MistralAI
9
  from llama_index.llms.openai import OpenAI
10
 
11
  from llama_index.embeddings.openai import OpenAIEmbedding
 
12
 
13
  from llama_index.core import (
14
  VectorStoreIndex,
@@ -24,16 +26,6 @@ MAX_OUTPUT_TOKENS = 2048
24
  def main():
25
  with st.sidebar:
26
  st.title('Document Summarization and QA System')
27
- # st.markdown('''
28
- # ## About this application
29
- # Upload a pdf to ask questions about it. This retrieval-augmented generation (RAG) workflow uses:
30
- # - [Streamlit](https://streamlit.io/)
31
- # - [LlamaIndex](https://docs.llamaindex.ai/en/stable/)
32
- # - [OpenAI](https://platform.openai.com/docs/models)
33
- # ''')
34
-
35
- # st.write('Made by ***Nate Mahynski***')
36
- # st.write('nathan.mahynski@nist.gov')
37
 
38
  # Select Provider
39
  provider = st.selectbox(
@@ -54,11 +46,23 @@ def main():
54
  else:
55
  llm_list = []
56
 
57
- llm_name = st.selectbox(
58
- label="Select LLM Model",
59
- options=llm_list,
60
- index=0
61
- )
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # Temperature
64
  temperature = st.slider(
@@ -78,7 +82,7 @@ def main():
78
  # Enter LLM API Key
79
  llm_key = st.text_input(
80
  "Enter your LLM API Key",
81
- value=None,
82
  )
83
 
84
  # Create LLM
@@ -94,11 +98,26 @@ def main():
94
  )
95
  Settings.tokenizer = tiktoken.encoding_for_model(llm_name).encode
96
  Settings.num_output = MAX_OUTPUT_TOKENS
97
- Settings.context_window = 4096 # max possible
98
  Settings.embed_model = OpenAIEmbedding()
 
99
  elif provider == 'huggingface':
100
- os.environ['HFTOKEN'] = str(llm_key)
101
- raise NotImplementedError(f"{provider} is not supported yet")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  else:
103
  raise NotImplementedError(f"{provider} is not supported yet")
104
 
@@ -144,7 +163,7 @@ def main():
144
  # Instructions
145
 
146
  1. Obtain an [API Key](https://cloud.llamaindex.ai/api-key) from LlamaParse to parse your document.
147
- 2. Obtain a similar API Key from your preferred LLM provider.
148
  3. Make selections at the left and upload a document to use as context.
149
  4. Begin asking questions below!
150
  """
@@ -169,8 +188,10 @@ def main():
169
 
170
  if __name__ == '__main__':
171
  # Global configurations
172
- from llama_index.core import set_global_handler
173
- set_global_handler("langfuse")
 
 
174
  st.set_page_config(layout="wide")
175
 
176
  main()
 
5
 
6
  from llama_index.llms.gemini import Gemini
7
  from llama_index.llms.huggingface import HuggingFaceLLM
8
+ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
9
  from llama_index.llms.mistralai import MistralAI
10
  from llama_index.llms.openai import OpenAI
11
 
12
  from llama_index.embeddings.openai import OpenAIEmbedding
13
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding, HuggingFaceInferenceAPIEmbedding
14
 
15
  from llama_index.core import (
16
  VectorStoreIndex,
 
26
  def main():
27
  with st.sidebar:
28
  st.title('Document Summarization and QA System')
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Select Provider
31
  provider = st.selectbox(
 
46
  else:
47
  llm_list = []
48
 
49
+ if provider == 'huggingface':
50
+ llm_name = st.text_input(
51
+ "Model as 'namespace/model-name', e.g. google/gemma-2-9b",
52
+ value=None,
53
+ )
54
+
55
+ # Also give the user the option for different embedding models, too
56
+ embed_name = st.text_input(
57
+ label="Embedding model as 'namespace/model-name', e.g. BAAI/bge-small-en-v1.5",
58
+ value="BAAI/bge-small-en-v1.5",
59
+ )
60
+ else:
61
+ llm_name = st.selectbox(
62
+ label="Select LLM Model",
63
+ options=llm_list,
64
+ index=0
65
+ )
66
 
67
  # Temperature
68
  temperature = st.slider(
 
82
  # Enter LLM API Key
83
  llm_key = st.text_input(
84
  "Enter your LLM API Key",
85
+ value="llx-uxxwLr1gZmDibaHTl99ISQJtpLSjjfhgDvnosGxu92RdRlb7", #None,
86
  )
87
 
88
  # Create LLM
 
98
  )
99
  Settings.tokenizer = tiktoken.encoding_for_model(llm_name).encode
100
  Settings.num_output = MAX_OUTPUT_TOKENS
 
101
  Settings.embed_model = OpenAIEmbedding()
102
+ Settings.context_window = 4096 # max possible
103
  elif provider == 'huggingface':
104
+ if llm_name is not None and embed_name is not None:
105
+ os.environ['HFTOKEN'] = str(llm_key)
106
+ Settings.llm = HuggingFaceInferenceAPI(
107
+ model_name=llm_name,
108
+ token=os.environ.get("HFTOKEN"),
109
+ temperature=temperature,
110
+ max_tokens=MAX_OUTPUT_TOKENS
111
+ )
112
+ Settings.tokenizer = AutoTokenizer.from_pretrained(
113
+ llm_name,
114
+ token=os.environ.get("HFTOKEN"),
115
+ )
116
+ Settings.num_output = MAX_OUTPUT_TOKENS
117
+ Settings.embed_model = HuggingFaceInferenceAPIEmbedding(
118
+ model_name=embed_name
119
+ )
120
+ # Settings.context_window = 4096
121
  else:
122
  raise NotImplementedError(f"{provider} is not supported yet")
123
 
 
163
  # Instructions
164
 
165
  1. Obtain an [API Key](https://cloud.llamaindex.ai/api-key) from LlamaParse to parse your document.
166
+ 2. Obtain a similar API Key from your preferred LLM provider. Note, if you are using [Hugging Face](https://huggingface.co/models) you may need to request access to a model if it is gated.
167
  3. Make selections at the left and upload a document to use as context.
168
  4. Begin asking questions below!
169
  """
 
188
 
189
  if __name__ == '__main__':
190
  # Global configurations
191
+ # from llama_index.core import set_global_handler
192
+ # set_global_handler("langfuse")
193
+ # Also add API Key for this if using
194
+
195
  st.set_page_config(layout="wide")
196
 
197
  main()
requirements.txt CHANGED
@@ -8,4 +8,6 @@ llama-index-llms-mistralai
8
  llama-index-llms-openai
9
  tiktoken
10
  llama-parse
11
- llama-index-callbacks-langfuse
 
 
 
8
  llama-index-llms-openai
9
  tiktoken
10
  llama-parse
11
+ llama-index-callbacks-langfuse
12
+ llama-index-llms-huggingface-api
13
+ llama-index-llms-huggingface