Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,9 @@ import os
|
|
21 |
import requests
|
22 |
from bs4 import BeautifulSoup
|
23 |
from langchain_core.documents import Document
|
|
|
|
|
|
|
24 |
|
25 |
# Configurar o tema para dark
|
26 |
st.set_page_config(page_title="RAG Q&A Conversacional", layout="wide", initial_sidebar_state="expanded", page_icon="🤖", menu_items=None)
|
@@ -164,12 +167,22 @@ st.write("Insira uma URL e converse com o conteúdo dela - aqui é usado o model
|
|
164 |
groq_api_key = st.text_input("Insira sua chave de API Groq:", type="password")
|
165 |
huggingface_api_token = st.text_input("Insira seu token de API Hugging Face:", type="password")
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
if groq_api_key and huggingface_api_token:
|
168 |
# Configurar o token da API do Hugging Face
|
169 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = huggingface_api_token
|
170 |
|
171 |
# Inicializar o modelo de linguagem e embeddings
|
172 |
llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.2-90b-text-preview", temperature=0)
|
|
|
173 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
174 |
|
175 |
session_id = st.text_input("Session ID", value="default_session")
|
@@ -184,14 +197,20 @@ if groq_api_key and huggingface_api_token:
|
|
184 |
response = requests.get(url)
|
185 |
response.raise_for_status()
|
186 |
soup = BeautifulSoup(response.text, 'html.parser')
|
187 |
-
|
188 |
# Extract text from the webpage
|
189 |
text = soup.get_text(separator='\n', strip=True)
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
# Create a Document object
|
192 |
document = Document(page_content=text, metadata={"source": url})
|
193 |
|
194 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=
|
195 |
splits = text_splitter.split_documents([document])
|
196 |
|
197 |
# Create FAISS vector store
|
@@ -245,7 +264,8 @@ if groq_api_key and huggingface_api_token:
|
|
245 |
("human", "{input}"),
|
246 |
])
|
247 |
|
248 |
-
|
|
|
249 |
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
|
250 |
|
251 |
def get_session_history(session: str) -> BaseChatMessageHistory:
|
@@ -275,5 +295,9 @@ if groq_api_key and huggingface_api_token:
|
|
275 |
st.write(f"**{message.type}:** {message.content}")
|
276 |
except requests.RequestException as e:
|
277 |
st.error(f"Erro ao acessar a URL: {str(e)}")
|
|
|
|
|
|
|
|
|
278 |
else:
|
279 |
st.warning("Por favor, insira tanto a chave da API do Groq quanto o token da API do Hugging Face.")
|
|
|
21 |
import requests
|
22 |
from bs4 import BeautifulSoup
|
23 |
from langchain_core.documents import Document
|
24 |
+
import time
|
25 |
+
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type
|
26 |
+
from groq.error import RateLimitError
|
27 |
|
28 |
# Configurar o tema para dark
|
29 |
st.set_page_config(page_title="RAG Q&A Conversacional", layout="wide", initial_sidebar_state="expanded", page_icon="🤖", menu_items=None)
|
|
|
167 |
groq_api_key = st.text_input("Insira sua chave de API Groq:", type="password")
|
168 |
huggingface_api_token = st.text_input("Insira seu token de API Hugging Face:", type="password")
|
169 |
|
170 |
+
# Retry decorator for handling rate limit errors
|
171 |
+
@retry(
|
172 |
+
retry=retry_if_exception_type(RateLimitError),
|
173 |
+
wait=wait_exponential(multiplier=1, min=4, max=60),
|
174 |
+
stop=stop_after_attempt(5)
|
175 |
+
)
|
176 |
+
def rate_limited_llm_call(llm, **kwargs):
|
177 |
+
return llm(**kwargs)
|
178 |
+
|
179 |
if groq_api_key and huggingface_api_token:
|
180 |
# Configurar o token da API do Hugging Face
|
181 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = huggingface_api_token
|
182 |
|
183 |
# Inicializar o modelo de linguagem e embeddings
|
184 |
llm = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.2-90b-text-preview", temperature=0)
|
185 |
+
rate_limited_llm = lambda **kwargs: rate_limited_llm_call(llm, **kwargs)
|
186 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
187 |
|
188 |
session_id = st.text_input("Session ID", value="default_session")
|
|
|
197 |
response = requests.get(url)
|
198 |
response.raise_for_status()
|
199 |
soup = BeautifulSoup(response.text, 'html.parser')
|
200 |
+
|
201 |
# Extract text from the webpage
|
202 |
text = soup.get_text(separator='\n', strip=True)
|
203 |
+
|
204 |
+
# Limit the text to a certain number of characters (e.g., 50,000)
|
205 |
+
max_chars = 50000
|
206 |
+
if len(text) > max_chars:
|
207 |
+
text = text[:max_chars]
|
208 |
+
st.warning(f"The webpage content was truncated to {max_chars} characters due to length.")
|
209 |
+
|
210 |
# Create a Document object
|
211 |
document = Document(page_content=text, metadata={"source": url})
|
212 |
|
213 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500) # Reduced chunk size
|
214 |
splits = text_splitter.split_documents([document])
|
215 |
|
216 |
# Create FAISS vector store
|
|
|
264 |
("human", "{input}"),
|
265 |
])
|
266 |
|
267 |
+
# Modify the conversational_rag_chain to use the rate_limited_llm
|
268 |
+
question_answer_chain = create_stuff_documents_chain(rate_limited_llm, qa_prompt)
|
269 |
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
|
270 |
|
271 |
def get_session_history(session: str) -> BaseChatMessageHistory:
|
|
|
295 |
st.write(f"**{message.type}:** {message.content}")
|
296 |
except requests.RequestException as e:
|
297 |
st.error(f"Erro ao acessar a URL: {str(e)}")
|
298 |
+
except RateLimitError as e:
|
299 |
+
st.error(f"Limite de taxa excedido para o modelo LLM. Tente novamente em alguns instantes. Erro: {str(e)}")
|
300 |
+
except Exception as e:
|
301 |
+
st.error(f"Ocorreu um erro inesperado: {str(e)}")
|
302 |
else:
|
303 |
st.warning("Por favor, insira tanto a chave da API do Groq quanto o token da API do Hugging Face.")
|