import requests
import json
from dotenv import load_dotenv
import os
load_dotenv()
my_api_key = os.getenv('BIGDATA_API_KEY')
# With research_plan - agent follows predefined steps
payload = {
"template": {
"name": "Structured Credit Analysis",
"prompt": "Perform a credit analysis of {{ company_id }} following the research plan.",
"expected_input": {
"company_id": {"type": "rp_entity_id"}
},
"research_plan": {
"title": "Credit Analysis Framework",
"steps": [
{"description": "Analyze business model and market position"},
{"description": "Review financial statements and leverage metrics"},
{"description": "Assess liquidity and debt maturity profile"},
{"description": "Evaluate credit ratings and outlook"},
{"description": "Identify key risks and mitigants"},
{"description": "Formulate credit recommendation"}
]
}
},
"input": {
"company_id": "4E4980" # Apple
},
"time_range": "last_180_days"
}
headers = {
"X-API-KEY": my_api_key,
"Content-Type": "application/json"
}
current_step = None
with requests.post(
"https://agents.bigdata.com/v1/workflow/execute",
headers=headers,
json=payload,
stream=True,
timeout=180
) as r:
r.raise_for_status()
for raw_line in r.iter_lines(decode_unicode=True):
if not raw_line or not raw_line.startswith("data: "):
continue
event = json.loads(raw_line[6:])
delta = event.get("delta", {})
msg_type = delta.get("type")
if msg_type == "PLANNING":
plan = delta.get("plan", {})
print(f"\n=== {plan.get('title')} ===")
for i, step in enumerate(plan.get("steps", [])):
status = step.get("status", "NOT_STARTED")
icon = {"NOT_STARTED": " ", "IN_PROGRESS": ">", "COMPLETED": "x", "SKIPPED": "-", "FAILED": "!"}.get(status, " ")
print(f" [{icon}] {step.get('description')}")
if status == "IN_PROGRESS" and current_step != i:
current_step = i
print(f"\n--- Working on: {step.get('description')} ---")
elif msg_type == "ANSWER":
print(delta.get("content", ""), end="", flush=True)
elif msg_type == "COMPLETE":
print("\n\n=== Analysis Complete ===")