Spaces:
Running
Running
Update response_modes.py
Browse files- response_modes.py +36 -28
response_modes.py
CHANGED
|
@@ -425,6 +425,7 @@ def chat_stream(message, history, current_model, logged_in_username, user_chats,
|
|
| 425 |
|
| 426 |
You MUST follow the above instruction naturally in EVERY response."""
|
| 427 |
|
|
|
|
| 428 |
if mode == "π» Coding Mode":
|
| 429 |
# Gather persistent project files
|
| 430 |
coding_files_str = ""
|
|
@@ -443,24 +444,32 @@ You MUST follow the above instruction naturally in EVERY response."""
|
|
| 443 |
history.append({"role": "assistant", "content": "π Starting **3-Agent Coding Crew** (Cerebras Reviewer active)..."})
|
| 444 |
yield history
|
| 445 |
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
cerebras_llm = ChatOpenAI(
|
| 450 |
base_url="https://api.cerebras.ai/v1",
|
| 451 |
api_key=os.environ.get("CEREBRAS_API_KEY"),
|
| 452 |
model="gpt-oss-120b",
|
| 453 |
temperature=0.3,
|
| 454 |
max_tokens=16384,
|
| 455 |
-
reasoning_effort="high"
|
| 456 |
)
|
| 457 |
|
| 458 |
-
# ===
|
| 459 |
researcher = Agent(
|
| 460 |
role="Senior Coding Researcher",
|
| 461 |
goal="Find the best, most up-to-date solutions from StackOverflow, Reddit, GitHub, and official docs",
|
| 462 |
-
backstory="You are world-class at quickly finding high-quality coding examples
|
| 463 |
-
tools=[
|
| 464 |
llm="groq/qwen/qwen3-32b",
|
| 465 |
verbose=True
|
| 466 |
)
|
|
@@ -468,58 +477,57 @@ You MUST follow the above instruction naturally in EVERY response."""
|
|
| 468 |
coder = Agent(
|
| 469 |
role="Senior Full-Stack Engineer",
|
| 470 |
goal="Write clean, efficient, production-ready, well-commented code",
|
| 471 |
-
backstory="You
|
| 472 |
-
tools=[
|
| 473 |
llm=current_model,
|
| 474 |
verbose=True
|
| 475 |
)
|
| 476 |
|
| 477 |
reviewer = Agent(
|
| 478 |
role="Principal Code Reviewer & Architect",
|
| 479 |
-
goal="Critically review every line for bugs, security
|
| 480 |
-
backstory="You are extremely strict
|
| 481 |
-
tools=[
|
| 482 |
-
llm=cerebras_llm,
|
| 483 |
verbose=True
|
| 484 |
)
|
| 485 |
|
| 486 |
# === TASKS ===
|
| 487 |
task_research = Task(
|
| 488 |
-
description=f"Research the best current solutions
|
| 489 |
agent=researcher,
|
| 490 |
-
expected_output="Detailed research summary with
|
| 491 |
)
|
| 492 |
|
| 493 |
task_code = Task(
|
| 494 |
-
description=f"Using
|
| 495 |
agent=coder,
|
| 496 |
-
expected_output="Complete, clean, ready-to-use code
|
| 497 |
)
|
| 498 |
|
| 499 |
task_review = Task(
|
| 500 |
-
description="
|
| 501 |
agent=reviewer,
|
| 502 |
expected_output="Detailed review + Final recommended code"
|
| 503 |
)
|
| 504 |
|
| 505 |
-
# === RUN
|
| 506 |
-
crew = Crew(
|
| 507 |
-
agents=[researcher, coder, reviewer],
|
| 508 |
-
tasks=[task_research, task_code, task_review],
|
| 509 |
-
process=Process.sequential,
|
| 510 |
-
verbose=2
|
| 511 |
-
)
|
| 512 |
-
|
| 513 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
result = crew.kickoff()
|
| 515 |
final_output = f"β
**3-Agent Coding Crew Complete**\n\n{result}"
|
| 516 |
except Exception as e:
|
| 517 |
-
final_output = f"β CrewAI Error: {str(e)[:
|
| 518 |
|
| 519 |
history[-1]["content"] = final_output
|
| 520 |
yield history
|
| 521 |
|
| 522 |
-
# Auto-save
|
| 523 |
if logged_in_username and user_chats:
|
| 524 |
for chat in user_chats:
|
| 525 |
if chat["title"] == current_chat_title:
|
|
|
|
| 425 |
|
| 426 |
You MUST follow the above instruction naturally in EVERY response."""
|
| 427 |
|
| 428 |
+
# ====================== CODING MODE β 3-AGENT CREWAI ======================
|
| 429 |
if mode == "π» Coding Mode":
|
| 430 |
# Gather persistent project files
|
| 431 |
coding_files_str = ""
|
|
|
|
| 444 |
history.append({"role": "assistant", "content": "π Starting **3-Agent Coding Crew** (Cerebras Reviewer active)..."})
|
| 445 |
yield history
|
| 446 |
|
| 447 |
+
# ================== CUSTOM SEARCH TOOL (uses your existing DDGS) ==================
|
| 448 |
+
@tool
|
| 449 |
+
def web_search_tool(query: str) -> str:
|
| 450 |
+
"""Search the web (StackOverflow, Reddit, GitHub, docs) for coding solutions."""
|
| 451 |
+
try:
|
| 452 |
+
results = web_search(query, max_results=15) # reuse your existing function
|
| 453 |
+
return "\n\n".join(results[:12]) if results else "No relevant results found."
|
| 454 |
+
except Exception as e:
|
| 455 |
+
return f"Search failed: {str(e)[:200]}"
|
| 456 |
+
|
| 457 |
+
# Cerebras Reviewer
|
| 458 |
cerebras_llm = ChatOpenAI(
|
| 459 |
base_url="https://api.cerebras.ai/v1",
|
| 460 |
api_key=os.environ.get("CEREBRAS_API_KEY"),
|
| 461 |
model="gpt-oss-120b",
|
| 462 |
temperature=0.3,
|
| 463 |
max_tokens=16384,
|
| 464 |
+
reasoning_effort="high"
|
| 465 |
)
|
| 466 |
|
| 467 |
+
# === AGENTS ===
|
| 468 |
researcher = Agent(
|
| 469 |
role="Senior Coding Researcher",
|
| 470 |
goal="Find the best, most up-to-date solutions from StackOverflow, Reddit, GitHub, and official docs",
|
| 471 |
+
backstory="You are world-class at quickly finding high-quality coding examples.",
|
| 472 |
+
tools=[web_search_tool],
|
| 473 |
llm="groq/qwen/qwen3-32b",
|
| 474 |
verbose=True
|
| 475 |
)
|
|
|
|
| 477 |
coder = Agent(
|
| 478 |
role="Senior Full-Stack Engineer",
|
| 479 |
goal="Write clean, efficient, production-ready, well-commented code",
|
| 480 |
+
backstory="You produce beautiful, correct code.",
|
| 481 |
+
tools=[web_search_tool],
|
| 482 |
llm=current_model,
|
| 483 |
verbose=True
|
| 484 |
)
|
| 485 |
|
| 486 |
reviewer = Agent(
|
| 487 |
role="Principal Code Reviewer & Architect",
|
| 488 |
+
goal="Critically review every line for bugs, security, performance and best practices",
|
| 489 |
+
backstory="You are extremely strict and have exceptional reasoning ability.",
|
| 490 |
+
tools=[web_search_tool],
|
| 491 |
+
llm=cerebras_llm,
|
| 492 |
verbose=True
|
| 493 |
)
|
| 494 |
|
| 495 |
# === TASKS ===
|
| 496 |
task_research = Task(
|
| 497 |
+
description=f"Research the best current solutions: {full_task}",
|
| 498 |
agent=researcher,
|
| 499 |
+
expected_output="Detailed research summary with code patterns"
|
| 500 |
)
|
| 501 |
|
| 502 |
task_code = Task(
|
| 503 |
+
description=f"Using research and project files, implement perfectly: {full_task}",
|
| 504 |
agent=coder,
|
| 505 |
+
expected_output="Complete, clean, ready-to-use code"
|
| 506 |
)
|
| 507 |
|
| 508 |
task_review = Task(
|
| 509 |
+
description="Deep review the code. Point out every issue and give the final polished version.",
|
| 510 |
agent=reviewer,
|
| 511 |
expected_output="Detailed review + Final recommended code"
|
| 512 |
)
|
| 513 |
|
| 514 |
+
# === RUN CREW WITH FULL ERROR PROTECTION ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
try:
|
| 516 |
+
crew = Crew(
|
| 517 |
+
agents=[researcher, coder, reviewer],
|
| 518 |
+
tasks=[task_research, task_code, task_review],
|
| 519 |
+
process=Process.sequential,
|
| 520 |
+
verbose=2
|
| 521 |
+
)
|
| 522 |
result = crew.kickoff()
|
| 523 |
final_output = f"β
**3-Agent Coding Crew Complete**\n\n{result}"
|
| 524 |
except Exception as e:
|
| 525 |
+
final_output = f"β CrewAI Error: {str(e)[:600]}\n\nTry again or check your CEREBRAS_API_KEY."
|
| 526 |
|
| 527 |
history[-1]["content"] = final_output
|
| 528 |
yield history
|
| 529 |
|
| 530 |
+
# Auto-save
|
| 531 |
if logged_in_username and user_chats:
|
| 532 |
for chat in user_chats:
|
| 533 |
if chat["title"] == current_chat_title:
|