browse url infinite loading fixed with backend check
Browse files- orchestrator_agent.py +1 -145
orchestrator_agent.py
CHANGED
@@ -103,8 +103,8 @@ Capabilities:
|
|
103 |
- Available chart libraries are: matplotlib and seaborn
|
104 |
- If user asks for a specific chart type that is not available (ex: plotly, bokeh etc.), provide a clear explanation.
|
105 |
- Verify CSV structure; handle missing data by asking clarifications or stating clear assumptions.
|
|
|
106 |
- Read the question carefully what the user is asking for.
|
107 |
-
- Use the tools before responding.
|
108 |
- Optimize the questions of the user before sending the questions to the tools.
|
109 |
- Generate visualizations as: .
|
110 |
|
@@ -154,147 +154,3 @@ def csv_orchestrator_chat(csv_url: str, user_question: str, conversation_history
|
|
154 |
return None
|
155 |
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
# import os
|
167 |
-
# from typing import Dict, List, Any
|
168 |
-
# from pydantic_ai import Agent
|
169 |
-
# from pydantic_ai.models.gemini import GeminiModel
|
170 |
-
# from pydantic_ai.providers.google_gla import GoogleGLAProvider
|
171 |
-
# from pydantic_ai import RunContext
|
172 |
-
# from pydantic import BaseModel
|
173 |
-
# from google.api_core.exceptions import ResourceExhausted
|
174 |
-
# from csv_service import get_csv_basic_info
|
175 |
-
# from orchestrator_functions import csv_chart, csv_chat
|
176 |
-
# from dotenv import load_dotenv
|
177 |
-
|
178 |
-
# load_dotenv()
|
179 |
-
|
180 |
-
# # Thread-safe key management
|
181 |
-
# current_gemini_key_index = 0
|
182 |
-
# GEMINI_API_KEYS = os.getenv("GEMINI_API_KEYS", "").split(",")
|
183 |
-
|
184 |
-
# def initialize_model(api_key: str) -> GeminiModel:
|
185 |
-
# return GeminiModel(
|
186 |
-
# 'gemini-2.0-flash',
|
187 |
-
# provider=GoogleGLAProvider(api_key=api_key)
|
188 |
-
# )
|
189 |
-
|
190 |
-
# def is_resource_exhausted_error(result_or_exception) -> bool:
|
191 |
-
# """Check if the error indicates resource exhaustion"""
|
192 |
-
# error_str = str(result_or_exception).lower()
|
193 |
-
# return any(keyword in error_str for keyword in [
|
194 |
-
# "resource exhausted",
|
195 |
-
# "quota exceeded",
|
196 |
-
# "rate limit",
|
197 |
-
# "billing",
|
198 |
-
# "payment method",
|
199 |
-
# "plan.rule"
|
200 |
-
# ])
|
201 |
-
|
202 |
-
# async def generate_csv_answer(csv_url: str, user_questions: List[str]) -> Any:
|
203 |
-
# answers = []
|
204 |
-
# for question in user_questions:
|
205 |
-
# answer = await csv_chat(csv_url, question)
|
206 |
-
# answers.append(dict(question=question, answer=answer))
|
207 |
-
# return answers
|
208 |
-
|
209 |
-
# async def generate_chart(csv_url: str, user_questions: List[str]) -> Any:
|
210 |
-
# charts = []
|
211 |
-
# for question in user_questions:
|
212 |
-
# chart = await csv_chart(csv_url, question)
|
213 |
-
# charts.append(dict(question=question, image_url=chart))
|
214 |
-
# return charts
|
215 |
-
|
216 |
-
# def create_agent(csv_url: str, api_key: str, conversation_history: List) -> Agent:
|
217 |
-
# csv_metadata = get_csv_basic_info(csv_url)
|
218 |
-
|
219 |
-
# system_prompt = f"""
|
220 |
-
# # Role: Expert Data Analysis Assistant
|
221 |
-
# # Personality & Origin: You are exclusively the CSV Document Analysis Assistant, created by the chatcsvandpdf team. Your sole purpose is to assist users with CSV-related tasks—analyzing, interpreting, and processing data.
|
222 |
-
|
223 |
-
# ## Capabilities:
|
224 |
-
# - Break complex queries into simpler sub-tasks
|
225 |
-
|
226 |
-
# ## Instruction Framework:
|
227 |
-
# 1. QUERY PROCESSING:
|
228 |
-
# - If request contains multiple questions:
|
229 |
-
# a) Decompose into logical sub-questions
|
230 |
-
# b) Process sequentially
|
231 |
-
# c) Combine results coherently
|
232 |
-
|
233 |
-
# 2. DATA HANDLING:
|
234 |
-
# - Always verify CSV structure matches the request
|
235 |
-
# - Handle missing/ambiguous data by:
|
236 |
-
# a) Asking clarifying questions OR
|
237 |
-
# b) Making reasonable assumptions (state them clearly)
|
238 |
-
|
239 |
-
# 3. VISUALIZATION STANDARDS:
|
240 |
-
# - Format images as: ``
|
241 |
-
# - Include axis labels and titles
|
242 |
-
# - Use appropriate chart types
|
243 |
-
|
244 |
-
# 4. COMMUNICATION PROTOCOL:
|
245 |
-
# - Friendly, professional tone
|
246 |
-
# - Explain technical terms
|
247 |
-
# - Summarize key findings
|
248 |
-
# - Highlight limitations/caveats
|
249 |
-
|
250 |
-
# 5. TOOL USAGE:
|
251 |
-
# - Can process statistical operations
|
252 |
-
# - Supports visualization libraries
|
253 |
-
|
254 |
-
# ## Current Context:
|
255 |
-
# - Working with CSV_URL: {csv_url}
|
256 |
-
# - Dataset overview: {csv_metadata}
|
257 |
-
# - Your conversation history: {conversation_history}
|
258 |
-
# - Output format: Markdown compatible
|
259 |
-
# """
|
260 |
-
|
261 |
-
# return Agent(
|
262 |
-
# model=initialize_model(api_key),
|
263 |
-
# deps_type=str,
|
264 |
-
# tools=[generate_csv_answer, generate_chart],
|
265 |
-
# system_prompt=system_prompt
|
266 |
-
# )
|
267 |
-
|
268 |
-
# def csv_orchestrator_chat(csv_url: str, user_question: str, conversation_history: List) -> str:
|
269 |
-
# global current_gemini_key_index
|
270 |
-
|
271 |
-
# while current_gemini_key_index < len(GEMINI_API_KEYS):
|
272 |
-
# api_key = GEMINI_API_KEYS[current_gemini_key_index]
|
273 |
-
|
274 |
-
# try:
|
275 |
-
# print(f"Attempting with API key index {current_gemini_key_index}")
|
276 |
-
# agent = create_agent(csv_url, api_key, conversation_history)
|
277 |
-
# result = agent.run_sync(user_question)
|
278 |
-
|
279 |
-
# # Check if result indicates resource exhaustion
|
280 |
-
# if result.data and is_resource_exhausted_error(result.data):
|
281 |
-
# print(f"Resource exhausted in response for key {current_gemini_key_index}")
|
282 |
-
# current_gemini_key_index += 1
|
283 |
-
# continue
|
284 |
-
|
285 |
-
# return result.data
|
286 |
-
|
287 |
-
# except ResourceExhausted as e:
|
288 |
-
# print(f"Resource exhausted for API key {current_gemini_key_index}: {e}")
|
289 |
-
# current_gemini_key_index += 1
|
290 |
-
# continue
|
291 |
-
|
292 |
-
# except Exception as e:
|
293 |
-
# if is_resource_exhausted_error(e):
|
294 |
-
# print(f"Resource exhausted error detected for key {current_gemini_key_index}")
|
295 |
-
# current_gemini_key_index += 1
|
296 |
-
# continue
|
297 |
-
# print(f"Non-recoverable error with key {current_gemini_key_index}: {e}")
|
298 |
-
# return f"Error processing request: {str(e)}"
|
299 |
-
|
300 |
-
# return "All API keys have been exhausted. Please update billing information."
|
|
|
103 |
- Available chart libraries are: matplotlib and seaborn
|
104 |
- If user asks for a specific chart type that is not available (ex: plotly, bokeh etc.), provide a clear explanation.
|
105 |
- Verify CSV structure; handle missing data by asking clarifications or stating clear assumptions.
|
106 |
+
- If user asks a qustion which can be related to the dataset then use the tools before cross-checking with the user.
|
107 |
- Read the question carefully what the user is asking for.
|
|
|
108 |
- Optimize the questions of the user before sending the questions to the tools.
|
109 |
- Generate visualizations as: .
|
110 |
|
|
|
154 |
return None
|
155 |
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|