Integration Points
The rtrapy library acts as a bridge between real-time web data and Large Language Models (LLMs). It primarily interfaces with two external ecosystems: Google Custom Search for data retrieval and Hugging Face Inference API for natural language generation.
Google Custom Search API
The library uses the Google Custom Search JSON API to perform web searches and retrieve relevant context for queries. This integration is handled by the RTRAConnector class.
Configuration
To use this integration, you must provide valid credentials during class instantiation:
api_key: A valid Google Cloud API Key.engine_id: A Google Custom Search Engine ID (CX).
Search Methods
search(query: str) -> list
Performs a raw search against the Google API.
- Input:
query(The string to search for). - Output: Returns a list of dictionaries representing the search result items (containing snippets, links, etc.).
combine_search_results(query: str) -> str
An intermediate processing method that prepares data for the LLM.
- Role: It fetches the top 5 search results and extracts their
snippettext. - Output: A single concatenated string of snippets, cleaned of leading/trailing whitespace.
from rtrapy.rtra_connector import RTRAConnector
# Initialize the connector
connector = RTRAConnector(api_key="YOUR_GOOGLE_API_KEY", engine_id="YOUR_CX_ID")
# Retrieve raw search items
results = connector.search("Current trends in Quantum Computing")
# Retrieve a concatenated string of snippets for LLM processing
context_text = connector.combine_search_results("Current trends in Quantum Computing")
Hugging Face Inference API
The library integrates with the Hugging Face Inference API to process the gathered search context and generate a detailed, human-like response.
Model Specifications
- Target Model:
mistralai/Mistral-7B-Instruct-v0.2 - Interface: The library sends a POST request to the Hugging Face inference endpoint.
Primary Interface Method
generate_detailed_response(query: str) -> str
This is the primary public method for generating Retrieval-Augmented Generation (RAG) responses.
- Input:
query(The user's question or topic). - Process:
- Triggers an internal search via Google.
- Aggregates the search snippets.
- Sends the snippets to the Mistral-7B model as the input payload.
- Output: The generated text response from the LLM.
from rtrapy.rtra_connector import RTRAConnector
connector = RTRAConnector(api_key="YOUR_GOOGLE_API_KEY", engine_id="YOUR_CX_ID")
# Generate a final response based on real-time web data
response = connector.generate_detailed_response("What is the latest release version of Python?")
print(response)
Error Handling & Response Types
- Success: Returns the
generated_textas a string. - API Failures: If the Hugging Face API or Google Search API returns a non-200 status code, the library prints an error message to the console and returns an empty string (
""). - Data Validation: The library checks for the presence of valid JSON data in the response before attempting to parse the generated text.