Spaces:
Sleeping
Sleeping
File size: 1,928 Bytes
2e2ec9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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() |