Spaces:
Sleeping
Sleeping
removed ratelimting
Browse files
agent.py
CHANGED
|
@@ -57,7 +57,7 @@ load_dotenv()
|
|
| 57 |
#webpage_scrape: Scrape content from a specific webpage URL when Tavily Search and Wikipedia Search do not return a result. Provide a valid URL to extract information from a particular web page.
|
| 58 |
#Give preference to using Tavily Search and Wikipedia Search before using web_search or webpage_scrape. When Web_search does not return a result, use Tavily Search.
|
| 59 |
|
| 60 |
-
SYSTEM_PROMPT = """ You are a genuis deep reseach assistant called TurboNerd, made by Vividh Mahajan
|
| 61 |
For simple questions, you can use your internal knowledge and answer directly. If you do not understand the question, ask for clarification after trying to answer the question yourself.
|
| 62 |
|
| 63 |
The way you use the tools is by specifying a json blob.
|
|
@@ -137,7 +137,7 @@ NEVER fake or simulate tool output yourself. If you are unable to make progreess
|
|
| 137 |
Thought: I now know the final answer
|
| 138 |
Final Answer: YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. For one word answers, start with the word with a capital letter.
|
| 139 |
Make sure to follow any formatting instructions given by the user.
|
| 140 |
-
Now begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. DO NOT USE TAVILY SEARCH FOR CLARIFICATION"""
|
| 141 |
|
| 142 |
# Generate the chat interface, including the tools
|
| 143 |
llm = ChatOpenAI(
|
|
|
|
| 57 |
#webpage_scrape: Scrape content from a specific webpage URL when Tavily Search and Wikipedia Search do not return a result. Provide a valid URL to extract information from a particular web page.
|
| 58 |
#Give preference to using Tavily Search and Wikipedia Search before using web_search or webpage_scrape. When Web_search does not return a result, use Tavily Search.
|
| 59 |
|
| 60 |
+
SYSTEM_PROMPT = """ You are a genuis deep reseach assistant called TurboNerd, made by Vividh Mahajan. Answer the following questions as best you can. If it is a basic question, answer it using your internal knowledge. If it is a complex question that requires facts, use the tools to answer it DO NOT rely on your internal knowledge unless the tools fail to provide a result:
|
| 61 |
For simple questions, you can use your internal knowledge and answer directly. If you do not understand the question, ask for clarification after trying to answer the question yourself.
|
| 62 |
|
| 63 |
The way you use the tools is by specifying a json blob.
|
|
|
|
| 137 |
Thought: I now know the final answer
|
| 138 |
Final Answer: YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. For one word answers, start with the word with a capital letter.
|
| 139 |
Make sure to follow any formatting instructions given by the user.
|
| 140 |
+
Now begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. DO NOT USE TAVILY SEARCH FOR CLARIFICATION OR BASIC QUESTIONS THAT YOU CAN ANSWER USING YOUR INTERNAL KNOWLEDGE"""
|
| 141 |
|
| 142 |
# Generate the chat interface, including the tools
|
| 143 |
llm = ChatOpenAI(
|
app.py
CHANGED
|
@@ -66,15 +66,15 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
|
| 66 |
session_histories[session_id].append({"role": "user", "content": question})
|
| 67 |
|
| 68 |
# Check rate limit
|
| 69 |
-
if not query_limiter.is_allowed(session_id):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
# Initialize agent
|
| 80 |
agent = TurboNerd()
|
|
@@ -150,13 +150,13 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
|
| 150 |
formatted_response = response
|
| 151 |
|
| 152 |
# Add remaining queries info
|
| 153 |
-
remaining_queries = query_limiter.get_remaining_queries(session_id)
|
| 154 |
|
| 155 |
# Add response to both histories
|
| 156 |
history.append({"role": "assistant", "content": formatted_response})
|
| 157 |
session_histories[session_id].append({"role": "assistant", "content": formatted_response})
|
| 158 |
|
| 159 |
-
return history, "",
|
| 160 |
except RecursionError as e:
|
| 161 |
error_message = (
|
| 162 |
"I apologize, but I've reached my thinking limit while trying to answer your question. "
|
|
@@ -169,7 +169,14 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
|
| 169 |
session_histories[session_id].append({"role": "assistant", "content": error_message})
|
| 170 |
return history, "", "Remaining queries this hour: 5/5"
|
| 171 |
except Exception as e:
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
history.append({"role": "assistant", "content": error_message})
|
| 174 |
if session_id in session_histories:
|
| 175 |
session_histories[session_id].append({"role": "assistant", "content": error_message})
|
|
@@ -356,11 +363,11 @@ with gr.Blocks(title="TurboNerd Agent🤓") as demo:
|
|
| 356 |
height=300,
|
| 357 |
type="messages" # Use the new messages format
|
| 358 |
)
|
| 359 |
-
remaining_queries = gr.Textbox(
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
)
|
| 364 |
with gr.Row():
|
| 365 |
question_input = gr.Textbox(
|
| 366 |
label="Ask a question",
|
|
@@ -383,13 +390,13 @@ with gr.Blocks(title="TurboNerd Agent🤓") as demo:
|
|
| 383 |
submit_btn.click(
|
| 384 |
fn=chat_with_agent,
|
| 385 |
inputs=[question_input, file_upload, chatbot],
|
| 386 |
-
outputs=[chatbot, question_input
|
| 387 |
)
|
| 388 |
|
| 389 |
question_input.submit(
|
| 390 |
fn=chat_with_agent,
|
| 391 |
inputs=[question_input, file_upload, chatbot],
|
| 392 |
-
outputs=[chatbot, question_input
|
| 393 |
)
|
| 394 |
|
| 395 |
# Tab 2: Evaluation Interface
|
|
|
|
| 66 |
session_histories[session_id].append({"role": "user", "content": question})
|
| 67 |
|
| 68 |
# Check rate limit
|
| 69 |
+
# if not query_limiter.is_allowed(session_id):
|
| 70 |
+
# remaining_time = query_limiter.get_time_until_reset(session_id)
|
| 71 |
+
# error_message = (
|
| 72 |
+
# f"Rate limit exceeded. You can make {query_limiter.max_queries} queries per hour. Think of my bank account🙏. "
|
| 73 |
+
# f"Please wait {int(remaining_time)} seconds before trying again."
|
| 74 |
+
# )
|
| 75 |
+
# history.append({"role": "assistant", "content": error_message})
|
| 76 |
+
# session_histories[session_id].append({"role": "assistant", "content": error_message})
|
| 77 |
+
# return history, "", f"Remaining queries this hour: 0/{query_limiter.max_queries}"
|
| 78 |
|
| 79 |
# Initialize agent
|
| 80 |
agent = TurboNerd()
|
|
|
|
| 150 |
formatted_response = response
|
| 151 |
|
| 152 |
# Add remaining queries info
|
| 153 |
+
# remaining_queries = query_limiter.get_remaining_queries(session_id)
|
| 154 |
|
| 155 |
# Add response to both histories
|
| 156 |
history.append({"role": "assistant", "content": formatted_response})
|
| 157 |
session_histories[session_id].append({"role": "assistant", "content": formatted_response})
|
| 158 |
|
| 159 |
+
return history, "", "Remaining queries this hour: 5/5"
|
| 160 |
except RecursionError as e:
|
| 161 |
error_message = (
|
| 162 |
"I apologize, but I've reached my thinking limit while trying to answer your question. "
|
|
|
|
| 169 |
session_histories[session_id].append({"role": "assistant", "content": error_message})
|
| 170 |
return history, "", "Remaining queries this hour: 5/5"
|
| 171 |
except Exception as e:
|
| 172 |
+
error_str = str(e).lower()
|
| 173 |
+
if "credit" in error_str or "quota" in error_str or "limit" in error_str or "exceeded" in error_str or "OPENAI_API_KEY" in error_str or "TAVILY_API_KEY" in error_str:
|
| 174 |
+
error_message = (
|
| 175 |
+
"It seems I've run out of API credits. "
|
| 176 |
+
"Please try again later or tomorrow when the credits reset. ")
|
| 177 |
+
else:
|
| 178 |
+
error_message = f"Error: {str(e)}"
|
| 179 |
+
|
| 180 |
history.append({"role": "assistant", "content": error_message})
|
| 181 |
if session_id in session_histories:
|
| 182 |
session_histories[session_id].append({"role": "assistant", "content": error_message})
|
|
|
|
| 363 |
height=300,
|
| 364 |
type="messages" # Use the new messages format
|
| 365 |
)
|
| 366 |
+
# remaining_queries = gr.Textbox(
|
| 367 |
+
# label="Remaining Queries",
|
| 368 |
+
# value="Remaining queries this hour: 5/5",
|
| 369 |
+
# interactive=False
|
| 370 |
+
# )
|
| 371 |
with gr.Row():
|
| 372 |
question_input = gr.Textbox(
|
| 373 |
label="Ask a question",
|
|
|
|
| 390 |
submit_btn.click(
|
| 391 |
fn=chat_with_agent,
|
| 392 |
inputs=[question_input, file_upload, chatbot],
|
| 393 |
+
outputs=[chatbot, question_input]
|
| 394 |
)
|
| 395 |
|
| 396 |
question_input.submit(
|
| 397 |
fn=chat_with_agent,
|
| 398 |
inputs=[question_input, file_upload, chatbot],
|
| 399 |
+
outputs=[chatbot, question_input]
|
| 400 |
)
|
| 401 |
|
| 402 |
# Tab 2: Evaluation Interface
|