Search Operations
Search Operations
The RTRAConnector class provides a suite of methods to perform web searches, aggregate metadata, and generate AI-driven responses based on live search data.
Initialization
Before performing search operations, you must initialize the connector with your Google Custom Search credentials.
from rtrapy.rtra_connector import RTRAConnector
# Initialize with Google API Key and Custom Search Engine ID
connector = RTRAConnector(api_key="YOUR_GOOGLE_API_KEY", engine_id="YOUR_ENGINE_ID")
Basic Search Retrieval
The search method performs a direct query against the Google Custom Search API and returns the raw result set.
search(query)
Executes a query and returns a list of result items.
- Parameters:
query(str): The search string to look up.
- Returns:
list[dict]: A list of dictionaries containing search result data (e.g., title, link, snippet). Returns an empty list if the request fails.
Example:
results = connector.search("Python asynchronous programming")
for item in results:
print(item.get('title'))
Result Aggregation
The combine_search_results method is used to extract and concatenate text from multiple search results, providing a simplified context for further processing.
combine_search_results(query)
Retrieves the top 5 search results and merges their text snippets into a single string.
- Parameters:
query(str): The search string to aggregate.
- Returns:
str: A concatenated string of snippets, each separated by a newline.
Example:
context = connector.combine_search_results("Quantum computing breakthroughs 2024")
print(context)
AI-Generated Responses
The generate_detailed_response method implements a Retrieval-Augmented Generation (RAG) workflow. It fetches live search data and uses a hosted LLM (Mistral-7B) to synthesize a comprehensive answer.
generate_detailed_response(query)
Performs a search, aggregates the top results, and sends them to the Hugging Face Inference API to generate a natural language response.
- Parameters:
query(str): The question or topic to generate a report on.
- Returns:
str: The AI-generated text based on the search context. Returns an empty string if an error occurs during the API call.
Example:
response = connector.generate_detailed_response("What are the latest updates on space exploration?")
print(f"Detailed Analysis: {response}")
Note: This method utilizes the Hugging Face Inference API. Ensure you have a stable internet connection for the model to process the aggregated search context.