Alterlab perplexity

From AlterLab-IEU/AlterLab-Academic-Skills · View interactive version

Alterlab perplexity is an AI agent skill from the GitHub repository AlterLab-IEU/AlterLab-Academic-Skills. Run Perplexity-style grounded searches (answers where every claim cites a current source) for current information and recent literature. Keyless by default: Claude's WebSearch and WebFetch plus keyless scholarly APIs (Europe PMC, Crossref, arXiv) find and open the sources; Perplexity Sonar models (s

How to use this skill

Skill file

Perplexity-Style Grounded Search

Answer questions about current information, recent literature, and facts beyond the training cutoff with web-grounded, citation-backed answers — the Perplexity workflow: a sharp query, current sources, and an answer in which every claim points to a source the reader can open. It needs no API key: you search with WebSearch, read with WebFetch, and query keyless scholarly APIs for papers. Perplexity's own Sonar models (one OpenRouter key covers all of them) are an optional backend when the user asks for Perplexity.

When to Use This Skill

Use for: current developments, latest publications, citation-grounded answers, verifying a claim against current sources, or anything past the training cutoff.

Skip for: arithmetic/logic, code execution, or facts well within training data (answer directly).

Does NOT Trigger

| Scenario | Use Instead |

|----------|-------------|

| Unsure which search backend fits; want automatic routing and results saved to sources/ | alterlab-research-lookup |

| General web, market, or technical research via Parallel, or extracting a known URL | alterlab-parallel-web |

| Per-paper structured experimental data (sample sizes, effect sizes, quality scores) | alterlab-bgpt-search |

| Reproducible biomedical database search by MeSH terms or PMIDs | alterlab-pubmed |

Hand-off rule. When the request matches a row above, open your answer by naming that skill by its exact id (for example alterlab-research-lookup) and saying in one line why it fits better, then stop applying this skill's procedure. Name it even when you also need to ask for missing input. If only part of the request is out of scope, name the skill for that part and do only the in-scope part. A brief general pointer is fine; the named skill does the work.

Default Path: Grounded Search Without a Key

  1. Write the query like a good Sonar prompt — domain + time frame + desired output + source preference ("RCTs on intermittent fasting and HbA1c in adults with type 2 diabetes, 2019–2025, peer-reviewed"). Split a compound question into 2–4 searches.
  2. Search.

https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=TITLE_ABS:(<terms>)&format=json&resultType=core&pageSize=10 (biomedicine, abstracts, sort=CITED%20desc for influential work; DOI:"<doi>" to fetch one record),

https://api.crossref.org/works?query.bibliographic=<terms>&rows=10 (every discipline, is-referenced-by-count; /works/<DOI> for one record),

https://export.arxiv.org/api/query?search_query=all:<word>+AND+all:<word>&max_results=10 (preprints).

Semantic Scholar (https://api.semanticscholar.org/graph/v1/paper/search?query=<terms>&fields=title,year,venue,externalIds,citationCount) also answers keyless, but its keyless pool is shared and rate-limited (HTTP 429 when busy; back off and retry) — use it as an extra, not the only source.

  1. Open what you cite. WebFetch the primary page — the journal article or its Europe PMC record, the agency report, the trial registry — not a news summary of it. A claim seen only in a search snippet is labelled "snippet only, not verified".
  2. Answer grounded, the way Perplexity does: a direct answer first, then the evidence with an inline citation [n] on every factual sentence, exact figures as the source states them (effect sizes with CIs, dates, counts), conflicts between sources flagged with which is stronger (evidence hierarchy: meta-analyses > RCTs > cohort > case reports > expert opinion), and a numbered source list: title, publisher/journal, year, DOI or URL, and the retrieval date. Cite the underlying sources, never "Perplexity" or "a search engine".
  3. Keep a record when asked (or for anything the user will cite): Write sources/search_YYYYMMDD_HHMMSS_<topic>.md with the queries, retrieval date, the answer and the source list — the keyless equivalent of --output results.json.

If the user asked specifically for Perplexity and there is no key, say so in the first line ("Perplexity needs OPENROUTER_API_KEY, which is not set"), then — unless they only wanted Perplexity — give the keyless answer above, labelled as WebSearch-based, never presented as Perplexity output.

Optional Backend: Perplexity Sonar via OpenRouter

Use it when the user asks for Perplexity or a specific Sonar model and OPENROUTER_API_KEY is set. Without the key the script exits 1 before sending anything and prints the keyless alternative.

Setup (one-time)

# 1. Get a key at https://openrouter.ai/keys and add credits ($5+ recommended)
# 2. Configure (either form works; setup_env.py writes .env with owner-only permissions)
export OPENROUTER_API_KEY='sk-or-v1-your-key-here'
python scripts/setup_env.py --api-key sk-or-v1-your-key-here   # writes .env

# 3. Verify. LiteLLM needs no install step: uv resolves it per call, without a virtualenv
uv run --python ">=3.11" --with litellm python scripts/perplexity_search.py --check-setup

--check-setup reads the live environment, not .env. After setup_env.py, load the file with set -a; source .env; set +a (plain source does not export) or use python-dotenv, so the variable is actually in scope. Ask the user to set the key themselves rather than pasting it into the conversation. See references/openrouter_setup.md for full setup, security, and troubleshooting.

Basic usage

uv run --python ">=3.11" --with litellm python scripts/perplexity_search.py "What are the latest developments in CRISPR gene editing?"
uv run --python ">=3.11" --with litellm python scripts/perplexity_search.py "Recent CAR-T therapy clinical trials" --output results.json
uv run --python ">=3.11" --with litellm python scripts/perplexity_search.py "Compare mRNA and viral vector vaccines" --model sonar-pro-search
uv run --python ">=3.11" --with litellm python scripts/perplexity_search.py "Quantum computing for drug discovery" --verbose

Where LiteLLM is already installed, plain python scripts/perplexity_search.py … works the same. The CLI takes the bare model name (e.g. sonar-pro); the script prepends openrouter/perplexity/ automatically.

Models

Select with --model (or set DEFAULT_MODEL):

| Model | Use it for |

|-------|------------|

| sonar-pro (default) | General research, balanced cost/quality |

| sonar-pro-search | Most advanced agentic, multi-step search; comprehensive comparisons. Highest cost; OpenRouter-exclusive |

| sonar | Simple fact lookups, cost-sensitive bulk queries |

| sonar-reasoning-pro | Tasks needing explicit step-by-step reasoning |

sonar-reasoning was retired on OpenRouter (not listed as of 2026-09-23) and the CLI no longer accepts it; a stale DEFAULT_MODEL=sonar-reasoning now fails fast with a clear error. See references/model_comparison.md for detailed trade-offs, pricing, context windows, and sonar-deep-research for programmatic use. On the keyless path, the equivalent of sonar-pro-search is simply more searches: plan sub-questions, search each, and open more sources before comparing.

Programmatic access

from scripts.perplexity_search import search_with_perplexity

result = search_with_perplexity(
    query="What are the latest CRISPR developments?",
    model="openrouter/perplexity/sonar-pro",  # full path required when calling directly
    max_tokens=4000,
    temperature=0.2,
)
if result["success"]:
    print(result["answer"])
    print(f"Tokens: {result['usage']['total_tokens']}")
else:
    print(f"Error: {result['error']}")

Saved JSON (--output) carries answer, model, usage, and citations — a deduplicated list of {url, title} gathered from OpenRouter's url_citation annotations and Perplexity's citations/search_results fields, present when the model returns sources. Process with jq '.answer' or jq '.citations[].url'. Cite the underlying sources, not Perplexity, and open any source you quote: answers are generated summaries and can misattribute a claim.

Cost

Pricing is per token (plus a per-request fee on sonar-pro-search); rough per-query order: sonar < sonar-pro < sonar-reasoning-pro < sonar-pro-search. Control spend by matching the model to the query, capping --max-tokens, and setting a spending limit in the OpenRouter dashboard. Monitor usage at https://openrouter.ai/activity. Exact rates: references/model_comparison.md and https://openrouter.ai/perplexity.

Environment variables (optional backend only)

Writing good queries

Be specific (domain + time frame + desired output). Examples that work well on both paths:

Add source preferences ("peer-reviewed", "clinicaltrials.gov", "FDA-approved") for higher-quality grounding. Full query-design patterns, domain templates, and pitfalls live in references/search_strategies.md.

Resources

Part of the AlterLab Academic Skills suite.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.