# src/chimera/utils/data_processing.py from .logging_config import logger import json def format_api_data_for_llm(api_results: dict) -> str: """ Formats the collected data from various APIs into a string suitable for an LLM prompt. """ formatted_string = "" if "serp" in api_results and isinstance(api_results["serp"], dict): serp_data = api_results["serp"] if "error" in serp_data: formatted_string += f"SERP API Error: {serp_data['error']}\n\n" elif serp_data.get("organic_results"): formatted_string += "Recent Search Results:\n" for i, result in enumerate(serp_data["organic_results"][:5]): # Limit results title = result.get("title", "N/A") link = result.get("link", "N/A") snippet = result.get("snippet", "N/A") formatted_string += f"- Title: {title}\n Snippet: {snippet}\n Link: {link}\n" formatted_string += "\n" else: formatted_string += "SERP: No relevant organic results found or data format unexpected.\n\n" # Add formatting for other APIs (weather, finance, etc.) similarly # Example for a hypothetical weather API result: # if "weather" in api_results and isinstance(api_results["weather"], dict): # weather_data = api_results["weather"] # if "error" in weather_data: # formatted_string += f"Weather API Error: {weather_data['error']}\n\n" # else: # temp = weather_data.get('temperature', 'N/A') # desc = weather_data.get('description', 'N/A') # location = weather_data.get('location', 'N/A') # formatted_string += f"Weather in {location}:\n - Temperature: {temp}\n - Conditions: {desc}\n\n" if not formatted_string: return "No structured data was successfully retrieved from APIs." return formatted_string.strip()