RTRAConnector Class
RTRAConnector Class
The RTRAConnector is the core class of the rtrapy library. It facilitates the integration between the Google Custom Search API and Hugging Face Inference models to provide Retrieval-Augmented Generation (RAG) capabilities. It allows you to fetch real-time search results and use them as context for generating detailed, informed responses.
Initialization
To use the connector, you must provide your Google API credentials.
from rtrapy.rtra_connector import RTRAConnector
connector = RTRAConnector(
api_key="YOUR_GOOGLE_API_KEY",
engine_id="YOUR_CUSTOM_SEARCH_ENGINE_ID"
)
Parameters:
api_key(str): Your Google Custom Search API key.engine_id(str): Your Google Programmable Search Engine ID (CX).
Methods
generate_detailed_response(query)
This is the primary method for end-users. It performs a search based on the query, extracts relevant snippets, and passes them to a language model (Mistral-7B-Instruct-v0.2) to generate a comprehensive answer.
- Parameters:
query(str): The search term or question you want to research.
- Returns:
str: The AI-generated response based on the search context. Returns an empty string if the API call fails.
Example:
response = connector.generate_detailed_response("What are the latest developments in quantum computing?")
print(response)
search(query)
Performs a direct search using the Google Custom Search API and returns the raw results.
- Parameters:
query(str): The search term.
- Returns:
list[dict]: A list of result items (dictionaries) containing metadata, links, and snippets from Google.
Example:
results = connector.search("Python 3.12 features")
for item in results:
print(item.get('title'))
combine_search_results(query)
A utility method that retrieves the top 5 search results and concatenates their snippets into a single block of text. This is used internally to prepare context for the LLM but can be called independently.
- Parameters:
query(str): The search term.
- Returns:
str: A consolidated string of search snippets.
Example:
context = connector.combine_search_results("SpaceX Starship launch updates")
print(context)
Error Handling
The class includes basic error logging to the console:
- If the Google API returns a non-200 status code, it prints an error message and returns an empty list.
- If the Hugging Face API fails or returns empty data, it prints an error message and returns an empty string.