perplexity_ai / app.py
xd11yggy's picture
Update app.py
d2c59f1 verified
raw
history blame
7.03 kB
import gradio as gr
from openai import OpenAI
from smolagents import DuckDuckGoSearchTool
import re
import time
import datetime
current_date = datetime.datetime.now().strftime("%d:%m:%Y")
refresh_time = datetime.datetime.now().strftime("%H:%M")
web_search = DuckDuckGoSearchTool()
SYSTEM_PROMPT = '''
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.
**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 all sources.
**Workflow:**
1. **Clarify:** Ask targeted questions if the task is ambiguous (e.g., "Do you need AI news from specific regions?").
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 all 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...
</search>
**Example:**
Task: "Explain quantum computing breakthroughs in 2023."
1. Search: ["2023 quantum computing breakthroughs", "latest quantum supremacy milestones"]
2. Analyze results β†’ identify key researchers/institutions.
3. Follow-up search: ["John Doe quantum research 2023", "IBM quantum roadmap 2023"]
4. Compile findings with sources.
**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.
**Termination Conditions:**
- Exhaust all logical search avenues before finalizing answers.
- If stuck, search for alternative phrasings (e.g., "quantum computing" β†’ "quantum information science").
'''
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πŸ” Analyzing search results...\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="qwen/qwq-32b: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=15000, value=6000, 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 recent AI news. 2025"],
["Tell about recent new ML discoveries with VERY simple words. 2025"],
["Write a report on the impact of AI on our daily lives."]
],
cache_examples=False
)
if __name__ == "__main__":
demo.launch()