मुख्य सामग्री पर जाएं
import asyncio
from dedalus_labs import AsyncDedalus, DedalusRunner
from dedalus_labs.utils.stream import stream_async
from dotenv import load_dotenv

load_dotenv()

def execute_python_code(code: str) -> str:
    """
    Execute Python code and return the result.
    Safely executes code in a controlled namespace.
    """
    try:
        namespace = {}
        exec(code, {"__builtins__": __builtins__}, namespace)

        if 'result' in namespace:
            return str(namespace['result'])

        results = {k: v for k, v in namespace.items() if not k.startswith('_')}
        return str(results) if results else "Code executed successfully"
    except Exception as e:
        return f"Error executing code: {str(e)}"

async def main():
    client = AsyncDedalus()
    runner = DedalusRunner(client)

    result = runner.run(
        input="""Research the current stock price of Tesla (TSLA) and Apple (AAPL).
        Then write and execute Python code to:
        1. Compare their current prices
        2. Calculate the percentage difference
        3. Determine which stock has grown more in the past year based on the data you find
        4. Provide investment insights based on your analysis

        Use web search to get the latest stock information.""",
        model="openai/gpt-5",
        tools=[execute_python_code],
        mcp_servers=["windsor/brave-search-mcp"],
        stream=True
    )

    await stream_async(result)

if __name__ == "__main__":
    asyncio.run(main())
यह डेटा एनालिस्ट उदाहरण रियल‑टाइम वेब सर्च को कोड निष्पादन की क्षमताओं के साथ जोड़ता है:
  • Brave Search MCP (windsor/brave-search-mcp): वेब से रियल‑टाइम डेटा प्राप्त करता है
  • execute_python_code tool: एजेंट को विश्लेषण के लिए Python कोड लिखने और चलाने की अनुमति देता है
एजेंट वर्तमान जानकारी खोज सकता है, प्रासंगिक डेटा निकाल सकता है, फिर उसका विश्लेषण करने और इनसाइट्स जेनरेट करने के लिए डायनेमिक रूप से कोड लिख सकता है।नोट: प्रोडक्शन एनवायरनमेंट में सुरक्षा के लिए सैंडबॉक्स्ड कोड निष्पादन का उपयोग करने पर विचार करें।