mgbam commited on
Commit
2e2ec9c
·
verified ·
1 Parent(s): 9ae1f0e

Create src/chimera/utils/data_processing.py

Browse files
src/chimera/utils/data_processing.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/chimera/utils/data_processing.py
2
+ from .logging_config import logger
3
+ import json
4
+
5
+ def format_api_data_for_llm(api_results: dict) -> str:
6
+ """
7
+ Formats the collected data from various APIs into a string suitable for an LLM prompt.
8
+ """
9
+ formatted_string = ""
10
+
11
+ if "serp" in api_results and isinstance(api_results["serp"], dict):
12
+ serp_data = api_results["serp"]
13
+ if "error" in serp_data:
14
+ formatted_string += f"SERP API Error: {serp_data['error']}\n\n"
15
+ elif serp_data.get("organic_results"):
16
+ formatted_string += "Recent Search Results:\n"
17
+ for i, result in enumerate(serp_data["organic_results"][:5]): # Limit results
18
+ title = result.get("title", "N/A")
19
+ link = result.get("link", "N/A")
20
+ snippet = result.get("snippet", "N/A")
21
+ formatted_string += f"- Title: {title}\n Snippet: {snippet}\n Link: {link}\n"
22
+ formatted_string += "\n"
23
+ else:
24
+ formatted_string += "SERP: No relevant organic results found or data format unexpected.\n\n"
25
+
26
+
27
+ # Add formatting for other APIs (weather, finance, etc.) similarly
28
+ # Example for a hypothetical weather API result:
29
+ # if "weather" in api_results and isinstance(api_results["weather"], dict):
30
+ # weather_data = api_results["weather"]
31
+ # if "error" in weather_data:
32
+ # formatted_string += f"Weather API Error: {weather_data['error']}\n\n"
33
+ # else:
34
+ # temp = weather_data.get('temperature', 'N/A')
35
+ # desc = weather_data.get('description', 'N/A')
36
+ # location = weather_data.get('location', 'N/A')
37
+ # formatted_string += f"Weather in {location}:\n - Temperature: {temp}\n - Conditions: {desc}\n\n"
38
+
39
+ if not formatted_string:
40
+ return "No structured data was successfully retrieved from APIs."
41
+
42
+ return formatted_string.strip()