Best for: building Valyu into your product / agents. Use Valyu as a tool spec inside LlamaIndex agents and RAG apps.
Valyu integrates seamlessly with LlamaIndex as a comprehensive tool spec, allowing you to enhance your AI agents and RAG applications with real-time web search and proprietary data sources. The integration provides LLM-ready context from multiple sources including web pages, academic journals, financial data, and more.The package includes two main functions:
search(): Search operations with comprehensive parameter control
get_contents(): Extract clean content from specific URLs
Paste into your AI assistant to wire Valyu into a LlamaIndex agent.
The most powerful way to use Valyu is within LlamaIndex agents, where the AI can dynamically decide when and how to search:
import osfrom llama_index.agent.openai import OpenAIAgentfrom llama_index.tools.valyu import ValyuToolSpec# Set API keysos.environ["VALYU_API_KEY"] = "your-valyu-api-key"os.environ["OPENAI_API_KEY"] = "your-openai-api-key"# Initialize Valyu tool with comprehensive configurationvalyu_tool = ValyuToolSpec( api_key=os.environ["VALYU_API_KEY"], max_price=100, # Maximum cost (optional - adjusts automatically if not provided) fast_mode=True, # Enable fast mode for quicker responses # Contents API configuration contents_summary=True, # Enable AI summarization for content extraction contents_extract_effort="normal", # Extraction thoroughness contents_response_length="medium", # Content length per URL)# Create OpenAI agent with Valyu toolsagent = OpenAIAgent.from_tools( valyu_tool.to_tool_list(), verbose=True,)# Example 1: search queryprint("=== Search Example ===")search_response = agent.chat( "What are the key considerations and empirical evidence for implementing statistical arbitrage strategies using cointegrated pairs trading, specifically focusing on the optimal lookback period for calculating correlation coefficients and the impact of transaction costs on strategy profitability in high-frequency trading environments?")print(search_response)# Example 2: URL content extractionprint("\n=== URL Content Extraction Example ===")content_response = agent.chat( "Please extract and summarize the content from these URLs: https://arxiv.org/abs/1706.03762 and https://en.wikipedia.org/wiki/Transformer_(machine_learning_model)")print(content_response)
Set search defaults at initialization, override per call:
valyu_tool = ValyuToolSpec( api_key="your-api-key", max_price=100, # max cost per search; auto-adjusts if unset relevance_threshold=0.5, # 0.0-1.0 fast_mode=False, # quality vs speed included_sources=["valyu/valyu-arxiv", "valyu/valyu-pubmed"], excluded_sources=["example.com"], response_length="medium", # "short", "medium", "large", "max", or int country_code="US", # 2-letter ISO code # Contents API defaults contents_summary=True, contents_extract_effort="high", # "normal", "high", "auto" contents_response_length="large",)results = valyu_tool.search( query="quantum computing breakthroughs 2024", search_type="all", # "all", "web", or "proprietary" max_num_results=10, # 1-20 (up to 100 with a special API key) start_date="2024-01-01", end_date="2024-12-31",)
get_contents(urls=[...]) extracts clean content from URLs (max 10 per request).
Steer the agent with a system prompt
Pass a system_prompt to your agent: use search_type="all" for broad coverage (web + proprietary), or "web" for current events only; raise relevance_threshold (0.6+) for precision; cite sources; use natural-language queries, not operators. See the Prompting Guide.
Custom query engines and retrievers
Wrap ValyuToolSpec.search() in a CustomQueryEngine or BaseRetriever to plug Valyu into existing LlamaIndex pipelines - call search() in custom_query/_retrieve and map each result doc (with doc.metadata['relevance_score']) into your response or NodeWithScore objects.