Build a RAG pipeline with fresh web data
Fetch results, format them as numbered sources, and ask your own language model to answer with citations.
from perplexity import Perplexity
client = Perplexity()
def web_context(question: str, k: int = 5) -> str:
"""Fetch fresh web results and format them as numbered context for any LLM."""
search = client.search.create(query=question, max_results=k, max_tokens_per_page=500)
blocks = []
for i, r in enumerate(search.results, start=1):
blocks.append(f"[{i}] {r.title}\nURL: {r.url}\nDate: {r.date}\n{r.snippet}")
return "\n\n".join(blocks)
question = "What changed in the EU AI Act this year?"
context = web_context(question)
prompt = f"""Answer using only the sources below. Cite them like [1], [2].
{context}
Question: {question}"""
# Send `prompt` to the language model of your choice.