UnSearch Docs

Build an AI Agent

Build an AI agent with real-time web search capabilities.

Build an AI Agent with UnSearch

Give your AI agent the ability to search the web in real time.

Prerequisites

  • Python 3.9+ or Node.js 18+
  • An UnSearch API key
  • An LLM API key (OpenAI, Anthropic, etc.)

Python Example

from unsearch import UnSearch
from openai import OpenAI

unsearch = UnSearch(api_key="sk_live_...")
openai = OpenAI()

def research_agent(question: str) -> str:
    # Step 1: Search the web
    results = unsearch.search(
        query=question,
        max_results=5,
        scrape_content=True
    )

    # Step 2: Build context from results
    context = "\n\n".join([
        f"Source: {r.url}\n{r.content[:1000]}"
        for r in results
    ])

    # Step 3: Generate answer with LLM
    response = openai.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "Answer based on the provided sources. Cite your sources."},
            {"role": "user", "content": f"Sources:\n{context}\n\nQuestion: {question}"}
        ]
    )

    return response.choices[0].message.content

answer = research_agent("What are the latest developments in quantum computing?")
print(answer)

Next Steps

On this page