|
| 1 | +""" |
| 2 | +Example demonstrating the ValyuRetriever for URL content extraction. |
| 3 | +
|
| 4 | +This example shows how to use the ValyuRetriever to extract content from URLs |
| 5 | +and integrate it with LlamaIndex retrieval pipelines. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +from llama_index.tools.valyu import ValyuRetriever |
| 10 | +from llama_index.core import QueryBundle |
| 11 | + |
| 12 | + |
| 13 | +def main(): |
| 14 | + """Demonstrate ValyuRetriever usage.""" |
| 15 | + |
| 16 | + # Initialize the Valyu retriever |
| 17 | + valyu_retriever = ValyuRetriever( |
| 18 | + api_key=os.environ.get("VALYU_API_KEY", "your-api-key-here"), |
| 19 | + verbose=True, |
| 20 | + # Configure contents extraction (user-controlled settings) |
| 21 | + contents_summary=True, # Enable AI summarization |
| 22 | + contents_extract_effort="normal", # Extraction thoroughness |
| 23 | + contents_response_length="medium", # Content length per URL |
| 24 | + # Note: contents_max_price is set by developer/user, not exposed to models |
| 25 | + ) |
| 26 | + |
| 27 | + # Example 1: Single URL retrieval |
| 28 | + print("=== Single URL Retrieval ===") |
| 29 | + query_bundle = QueryBundle( |
| 30 | + query_str="https://en.wikipedia.org/wiki/Transformer_(machine_learning_model)" |
| 31 | + ) |
| 32 | + |
| 33 | + try: |
| 34 | + nodes = valyu_retriever.retrieve(query_bundle) |
| 35 | + print(f"Retrieved {len(nodes)} documents:") |
| 36 | + |
| 37 | + for i, node in enumerate(nodes): |
| 38 | + print(f"\nDocument {i+1}:") |
| 39 | + print(f"URL: {node.node.metadata.get('url', 'N/A')}") |
| 40 | + print(f"Title: {node.node.metadata.get('title', 'N/A')}") |
| 41 | + print(f"Content length: {len(node.node.text)} characters") |
| 42 | + print(f"Score: {node.score}") |
| 43 | + # Show content preview |
| 44 | + preview = ( |
| 45 | + node.node.text[:200] + "..." |
| 46 | + if len(node.node.text) > 200 |
| 47 | + else node.node.text |
| 48 | + ) |
| 49 | + print(f"Content preview: {preview}") |
| 50 | + |
| 51 | + except Exception as e: |
| 52 | + print(f"Error: {e}") |
| 53 | + print("Note: This example requires a valid VALYU_API_KEY environment variable") |
| 54 | + |
| 55 | + # Example 2: Multiple URLs |
| 56 | + print("\n=== Multiple URLs Retrieval ===") |
| 57 | + multi_url_query = QueryBundle( |
| 58 | + query_str="https://arxiv.org/abs/1706.03762 https://en.wikipedia.org/wiki/Attention_(machine_learning)" |
| 59 | + ) |
| 60 | + |
| 61 | + try: |
| 62 | + nodes = valyu_retriever.retrieve(multi_url_query) |
| 63 | + print(f"Retrieved {len(nodes)} documents from multiple URLs") |
| 64 | + |
| 65 | + for i, node in enumerate(nodes): |
| 66 | + print( |
| 67 | + f"Document {i+1}: {node.node.metadata.get('title', 'Unknown')} - {len(node.node.text)} chars" |
| 68 | + ) |
| 69 | + |
| 70 | + except Exception as e: |
| 71 | + print(f"Error: {e}") |
| 72 | + |
| 73 | + # Example 3: Natural language query with URLs |
| 74 | + print("\n=== Natural Language Query with URLs ===") |
| 75 | + natural_query = QueryBundle( |
| 76 | + query_str="Please extract content from these research papers: https://arxiv.org/abs/1706.03762 and also from https://en.wikipedia.org/wiki/Large_language_model" |
| 77 | + ) |
| 78 | + |
| 79 | + try: |
| 80 | + nodes = valyu_retriever.retrieve(natural_query) |
| 81 | + print( |
| 82 | + f"Extracted content from {len(nodes)} URLs found in natural language query" |
| 83 | + ) |
| 84 | + |
| 85 | + except Exception as e: |
| 86 | + print(f"Error: {e}") |
| 87 | + |
| 88 | + |
| 89 | +def demonstrate_url_parsing(): |
| 90 | + """Demonstrate URL parsing capabilities.""" |
| 91 | + print("\n=== URL Parsing Examples ===") |
| 92 | + |
| 93 | + retriever = ValyuRetriever( |
| 94 | + api_key="test-key" |
| 95 | + ) # API key not needed for parsing demo |
| 96 | + |
| 97 | + test_cases = [ |
| 98 | + "https://example.com", |
| 99 | + "https://site1.com, https://site2.com", |
| 100 | + "Please extract content from https://news.com and https://blog.com", |
| 101 | + "Check out these links: https://paper1.org https://paper2.org", |
| 102 | + "No URLs in this text", |
| 103 | + ] |
| 104 | + |
| 105 | + for i, test_case in enumerate(test_cases, 1): |
| 106 | + urls = retriever._parse_urls_from_query(test_case) |
| 107 | + print(f"Test {i}: '{test_case}'") |
| 108 | + print(f" Extracted URLs: {urls}") |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
| 113 | + demonstrate_url_parsing() |
0 commit comments