The Eternity Class
The Eternity Class
The Eternity class is the primary interface for the library. It facilitates communication with the Google Custom Search API to perform real-time web searches and process results.
Constructor
To use Eternity, you must initialize the class with your Google API credentials.
from eternity.eternity import Eternity
eternity = Eternity(api_key="YOUR_GOOGLE_API_KEY", engine_id="YOUR_SEARCH_ENGINE_ID")
Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| api_key | str | Your Google Cloud API Key with Custom Search API enabled. |
| engine_id | str | Your Programmable Search Engine ID (CX). |
Methods
search()
Performs a web search and returns the raw results provided by the API.
results = eternity.search(query="Python data retrieval")
- Parameters:
query(str): The search term or phrase.
- Returns:
list— A list of dictionaries representing search items. Each dictionary contains keys such astitle,link,snippet, etc. - Error Handling: If the API request fails, it prints an error message to the console and returns an empty list
[].
get_combined_text()
A utility method that fetches the top 5 search results and formats them into a single, human-readable string. This is particularly useful for passing context to Large Language Models (LLMs).
context = eternity.get_combined_text("Latest trends in AI 2024")
print(context)
- Parameters:
query(str): The search term or phrase.
- Returns:
str— A formatted string containing the Title, Snippet, and URL for the top 5 results.
Usage Example
The following example demonstrates how to initialize the library and retrieve a consolidated summary of search results.
from eternity.eternity import Eternity
# Initialize the client
eternity = Eternity(
api_key="AIzaSy...",
engine_id="0123456789..."
)
# 1. Get raw results for custom processing
raw_data = eternity.search("Open Source LLMs")
for item in raw_data[:2]:
print(f"Found: {item.get('title')}")
# 2. Get a formatted summary for a quick overview
summary = eternity.get_combined_text("Open Source LLMs")
print(summary)