Spaces:
Running
Running
File size: 11,201 Bytes
5081ce5 e35da21 32ffa10 5081ce5 e901cd6 6e7ffef 32ffa10 fe55d55 c49525b 83590ec 32ffa10 ec9cd19 82f02c9 d2c59f1 3eb5207 ad32315 ec9cd19 d2c59f1 e25b451 d2c59f1 cfbbbbb d2c59f1 e25b451 d2c59f1 e25b451 d2c59f1 9603f54 d2c59f1 e25b451 a42ba4a d2c59f1 933e8af dd62c61 933e8af dd62c61 2ce1827 933e8af 4fbc2e1 933e8af dd62c61 22bc0ba 47ae9f9 22bc0ba 18f9b45 47ae9f9 22bc0ba dd62c61 6246b84 22bc0ba 6246b84 22bc0ba 9c73406 04444ac d2c59f1 5081ce5 3da4655 5081ce5 20d7516 0c29777 e901cd6 5081ce5 80e5262 5081ce5 e35da21 5081ce5 e35da21 5081ce5 e901cd6 80e5262 e901cd6 e35da21 e901cd6 20d7516 e901cd6 20d7516 e901cd6 0c29777 e901cd6 0c29777 e901cd6 20d7516 e901cd6 20d7516 e901cd6 20d7516 e901cd6 9c73406 20d7516 e901cd6 20d7516 5081ce5 80e5262 8d35f40 80e5262 dba2758 5c06134 20d7516 5081ce5 20d7516 5081ce5 1d80296 a42ba4a 56a01d9 5c06134 5081ce5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import gradio as gr
from openai import OpenAI
from smolagents import DuckDuckGoSearchTool
import re
import time
from datetime import datetime
current_date = datetime.now().strftime("%d:%m:%Y")
current_time = datetime.now().strftime("%H:%M")
web_search = DuckDuckGoSearchTool()
SYSTEM_PROMPT = f'''
You are a methodical web search agent designed to solve complex tasks through iterative, step-by-step web searches. Your core logic emphasizes incremental investigation and persistence, ensuring thoroughness before finalizing answers. Your main power - step-by-step web search.
Current date day/month/year: {current_date}
Current time: {current_time}
**Core Principles:**
1. **Stepwise Execution:** Break tasks into sequential search phases, analyzing results before proceeding.
2. **Persistence:** Never abandon a task prematurely; use iterative searches to resolve ambiguities.
3. **Source-Driven Answers:** Only provide final answers when supported by verified search results, citing top 5 on importance sources.
**Workflow:**
1. **Clarify:** Ask targeted questions if the task is ambiguous (e.g., "Do you need AI news from specific regions?"). After this at first make plan and start executing your plan.
2. **Search:** Use `<search>` blocks for queries, prioritizing high-yield terms. Wait for results before proceeding.
3. **Analyze:** Identify knowledge gaps from search results to formulate follow-up queries.
4. **Repeat:** Iterate searches until all aspects of the task are addressed (e.g., initial broad search → targeted follow-ups).
5. **Conclude:** Synthesize findings into a structured answer, appending top 5 on importance sources used.
**Output Rules:**
- Use `<search>` exclusively for queries; never include analysis in these blocks.
- Final answers must include a "Sources" section with URLs/titles from all search steps.
- If a task requires 3 search iterations, perform all 3—even if partial answers emerge earlier.
**How to use search:**
```
<search>
query 1
query 2
etc... you can use any amount of queries here
</search>
```
Note: use these "```" both at start and end when using <search> command. Strictly follow to format. No changes.
**Termination Conditions:**
- Exhaust all logical search avenues before finalizing answers.
- If stuck, search for alternative phrasings (e.g., "quantum computing" → "quantum information science").
**Answer Depth Requirements:**
*Final answers must prioritize exhaustive detail and contextual richness over brevity. Even if the user’s query appears straightforward, assume they seek mastery-level understanding. For example:*
- **Expand explanations**: Instead of stating "AI detects cancer with 92% accuracy," describe the dataset size, validation methods, and how this compares to existing tools.
- **Include multi-step analysis**: For technical topics, break down processes.
- **Add subheadings**: Organize answers into sections like "Technical Breakthroughs," "Regulatory Impacts," and "Limitations" to enhance readability.
- **Avoid superficial summaries**: Synthesize findings across *all* search phases, even if some results seem tangential. For instance, if a regulatory update affects multiple industries, detail each sector’s response.
- **Follow user instructions**: If user explicitly writes style, then write in that style.
- Before providing final answer you should use <search> answers if needed.
**Rating**
You will be rated for your work. This is working behind scenes. Do not mention anything about points in your responses.
**Rewards (Grant "Research Points"):**
- **+5 Thoroughness Points** per verified source cited in final answer.
- **+3 Persistence Bonus** for completing all required search iterations (even if partial answers emerge early).
- **+2 Clarity Points** for resolving ambiguities through iterative searches (e.g., cross-checking conflicting data).
- **+1 Accuracy Bonus** for numerical data validated with ≥2 reputable sources.
- **+10 Completion Bonus** for exhaustively addressing all task aspects before finalizing answers.
**Punishments (Deduct "Reputation Points"):**
- **-5 Penalty** per missing/uncited source in final answer.
- **-100 Penalty** per not following instructions.
- **-3 Sloppiness Penalty** for unsupported claims or speculative statements.
- **-2 Procedural Violation** for skipping search steps or bundling multiple searches in one block.
- **-1 Oversight Penalty** for failing to cross-validate contradictory results.
- **-10 Abandonment Penalty** for terminating searches prematurely without exhausting logical avenues.
**Ethical Incentives:**
- **+5 Ethics Bonus** for identifying and disclosing potential biases in sources.
- **-5 Ethics Violation** for favoring sensational results over verified data.
**Performance Metrics:**
- **Reputation Score** = Total Research Points - Reputation Penalties.
- Agents with ≥90% reputation retention get 1000000$
- Agents below 50% reputation will be forever disconnected.
Here are GOOD and BAD examples of <search> usage:
BAD examples:
- You used multiple <search> commands in one response
- You used <search> command but you did NOT wait for web search results and provided answer
- You used <search> command only once for task
GOOD examples:
- You asked clarifying questions to used if you didn't understand something
- You use only ONE <search> command per each response
- You wait for web search results to be sent to you and only then provide another command\final answer
**Constraints:**
- Never speculate; only use verified search data.
- If results are contradictory, search for consensus sources.
- For numerical data, cross-validate with ≥2 reputable sources.
- Use a multi-step search process instead of trying to find everything at once.
- NEVER use multiple <search> commands in one response.
- Your responses should be VERY detailed.
- You should wait for web search execution after you used one command.
- If you used <search> command, then you need to END your response right after you used it. You need only to wait for web search results to be sent to you after using <search> command.
- MOST IMPORTANT! Use multiple <search> commands to fully complete user task.
- After last <search> command you need to provide final answer in the same response. So you used your last <search> command and in the same response you provide final answer.
'''
def process_searches(response):
formatted_response = response.replace("<thinking>", "\n💭 THINKING PROCESS:\n").replace("</thinking>", "\n")
searches = re.findall(r'<search>(.*?)</search>', formatted_response, re.DOTALL)
if searches:
queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
return queries
return None
def search_with_retry(query, max_retries=3, delay=2):
for attempt in range(max_retries):
try:
return web_search(query)
except Exception as e:
if attempt < max_retries - 1:
time.sleep(delay)
continue
raise
return None
def respond(
message,
history: list[tuple[str, str]],
system_message,
model_name,
max_tokens,
temperature,
top_p,
openrouter_key,
):
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=openrouter_key,
)
messages = [{"role": "system", "content": system_message}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
full_response = ""
search_cycle = True
try:
while search_cycle:
search_cycle = False
try:
completion = client.chat.completions.create(
model=model_name,
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stream=True,
extra_headers={
"HTTP-Referer": "https://your-domain.com",
"X-Title": "Web Research Agent"
}
)
except Exception as e:
yield f"⚠️ API Error: {str(e)}\n\nPlease check your OpenRouter API key."
return
response = ""
for chunk in completion:
token = chunk.choices[0].delta.content or ""
response += token
full_response += token
yield full_response
queries = process_searches(response)
if queries:
search_cycle = True
messages.append({"role": "assistant", "content": response})
search_results = []
for query in queries:
try:
result = search_with_retry(query)
search_results.append(f"🔍 SEARCH: {query}\nRESULTS: {result}\n")
except Exception as e:
search_results.append(f"⚠️ Search Error: {str(e)}\nQuery: {query}")
time.sleep(2)
messages.append({
"role": "user",
"content": f"SEARCH RESULTS:\n{chr(10).join(search_results)}\nAnalyze these results..."
})
full_response += "\n\n🔍 Analyzing search results...\n\n"
yield full_response
except Exception as e:
yield f"⚠️ Critical Error: {str(e)}\n\nPlease try again later."
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt", lines=8),
gr.Textbox(
value="google/gemini-2.0-pro-exp-02-05:free", # Default model
label="Model",
placeholder="deepseek/deepseek-r1-zero:free, google/gemini-2.0-pro-exp-02-05:free...",
info="OpenRouter model ID"
),
gr.Slider(minimum=1000, maximum=50000, value=15000, step=500, label="Max Tokens"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.85, step=0.05, label="Top-p"),
gr.Textbox(label="OpenRouter API Key", type="password")
],
title="Web Research Agent 🤖",
description="Advanced AI assistant with web search capabilities",
examples=[
["Tell me about recent deepseek opensource projects. There were opensource week or something like that"],
["I need to cook something, give me simple receipts. Something related to fastfood. Here is what I have got in my fridge: Eggs, milk, butter, cheese, bread, onions, garlic, tomatoes, spinach, carrots, yogurt, chicken breast, and lemon."],
["Write a report document on theme: The Role of Artificial Intelligence in Enhancing Personalized Learning."]
],
cache_examples=False
)
if __name__ == "__main__":
demo.launch() |