Skip to content

Commit 1af829d

Browse files
committed
feat: Improve display_results function for better formatting
1 parent 3fa76bd commit 1af829d

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

sonar-use-cases/research_finder/research_finder.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,54 @@ def research_topic(self, query: str, model: str = DEFAULT_MODEL) -> Dict[str, An
146146
return {"error": error_message}
147147
except json.JSONDecodeError:
148148
return {"error": "Failed to parse API response as JSON", "raw_response": response.text if 'response' in locals() else 'No response object'}
149-
except Exception as e:
150-
return {"error": f"An unexpected error occurred: {str(e)}"}
151-
149+
except Exception as e:
150+
return {"error": f"An unexpected error occurred: {str(e)}"}
151+
152+
153+
def display_results(results: Dict[str, Any], output_json: bool = False): # Added output_json flag
154+
"""
155+
Display the research results in a human-readable format or as JSON.
156+
"""
157+
if output_json:
158+
print(json.dumps(results, indent=2))
159+
return
160+
161+
if "error" in results:
162+
print(f"\n❌ Error: {results['error']}")
163+
if "raw_response" in results:
164+
print("\n📄 Raw Response Snippet:")
165+
raw_response_str = json.dumps(results["raw_response"]) if isinstance(results["raw_response"], dict) else str(results["raw_response"])
166+
print(raw_response_str[:500] + ("..." if len(raw_response_str) > 500 else ""))
167+
return
168+
169+
print("\n✅ Research Complete!")
170+
print("\n📝 SUMMARY:")
171+
print(results.get("summary", "No summary provided."))
172+
173+
sources = results.get("sources")
174+
if sources:
175+
print("\n🔗 SOURCES:")
176+
if isinstance(sources, list):
177+
for i, source in enumerate(sources, 1):
178+
# Handle potential dict format from citations or simple string/url
179+
if isinstance(source, dict):
180+
title = source.get('title', 'No Title')
181+
url = source.get('url', '')
182+
print(f" {i}. {title}{(' (' + url + ')') if url else ''}")
183+
elif isinstance(source, str):
184+
print(f" {i}. {source}")
185+
else:
186+
print(f" {i}. {str(source)}") # Fallback
187+
else:
188+
print(f" {sources}") # Handle if sources isn't a list
189+
else:
190+
print("\n🔗 SOURCES: No sources were explicitly listed or extracted.")
191+
# Optionally show raw response if no sources found
192+
# if "raw_response" in results:
193+
# print("(Check raw response below for potential sources within the text)")
194+
# print("\n📄 Raw Response:")
195+
# print(results["raw_response"])
152196

153-
def display_results(results: Dict[str, Any]):
154-
"""Placeholder for displaying results."""
155-
print("\n--- Results ---")
156-
print(f"Summary: {results.get('summary')}")
157-
print(f"Sources: {results.get('sources')}")
158-
print("---------------")
159197

160198
def main():
161199
"""Main entry point."""

0 commit comments

Comments
 (0)