Streamlined interface for executing automation tasks using the Narada Python SDK
The agent method provides a modern, streamlined interface for executing automation tasks in the Narada Python SDK. It returns a simplified AgentResponse object with cleaner error handling and better type safety compared to the legacy dispatch_request method.
The agent method is the recommended approach for new integrations. It provides a cleaner API surface and better developer experience than the legacy dispatch_request method.
A file attachment to provide to the agent. Files must be uploaded first using the upload_file() method, which returns a File object that can be passed here.
Copy
# Upload a file firstwith open("document.pdf", "rb") as f: file = await window.upload_file(file=f)# Pass it to the agentattachment=file
Attachments are particularly useful with the GENERALIST agent for document analysis, summarization, and question answering tasks.
A dictionary of variables for secure substitution in prompts. Variables protect sensitive data by performing substitution after the LLM generates its action plan, ensuring the LLM never sees the actual values.Use ${variable_name} syntax in your prompt, and provide the actual values in this dictionary. This is ideal for API keys, passwords, personal information, or any data you want to keep private from the LLM.
Copy
response = await window.agent( prompt="Search for ${search_term} and download the first PDF result", variables={"search_term": "confidential research topic"})
Variables are substituted at action time, after the LLM has planned its actions. The LLM will see the placeholder ${variable_name} but never the actual value.
import asynciofrom narada import Naradaasync def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() # Simple web automation task response = await window.agent( prompt="Search for 'Python tutorials' on Google and get the title of the first result" ) print(f"Status: {response.status}") print(f"Result: {response.text}") print(f"Actions used: {response.usage.actions}")if __name__ == "__main__": asyncio.run(main())
Copy
import asynciofrom narada import Naradafrom pydantic import BaseModel, Fieldclass SearchResult(BaseModel): query: str = Field(description="The search query used") result_count: str = Field(description="Number of results found") top_result_title: str = Field(description="Title of the first result") top_result_url: str = Field(description="URL of the first result")async def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() # Extract structured data from web automation response = await window.agent( prompt="Search for 'AI research papers' and extract search details", output_schema=SearchResult ) if response.status == "success" and response.structured_output: data = response.structured_output print(f"Query: {data.query}") print(f"Results: {data.result_count}") print(f"Top result: {data.top_result_title}") print(f"URL: {data.top_result_url}")if __name__ == "__main__": asyncio.run(main())
Copy
import asynciofrom narada import Agent, Naradaasync def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() # Start a conversation with the Generalist agent response = await window.agent( prompt="Pick a lucky number between 1 and 100 for me", agent=Agent.GENERALIST, clear_chat=True ) print(f"Agent: {response.text}") # Continue the conversation response = await window.agent( prompt="What did you pick again?", agent=Agent.GENERALIST, clear_chat=False # Maintain conversation context ) print(f"Agent: {response.text}") # Ask a follow-up question response = await window.agent( prompt="What's double that number?", agent=Agent.GENERALIST, clear_chat=False ) print(f"Agent: {response.text}")if __name__ == "__main__": asyncio.run(main())
Copy
import asynciofrom narada import Narada, NaradaTimeoutErrorasync def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() max_attempts = 3 for attempt in range(max_attempts): try: response = await window.agent( prompt="Search for 'complex data analysis tutorial' and summarize the top 3 results", timeout=60, # 1 minute timeout generate_gif=True # Record for debugging ) if response.status == "success": print("Task completed successfully!") print(f"Result: {response.text}") break else: print(f"Task failed with status: {response.status}") break except NaradaTimeoutError: if attempt == max_attempts - 1: print("Task failed after maximum attempts") raise print(f"Attempt {attempt + 1} timed out, retrying...") # Reinitialize to cancel any inflight requests await window.reinitialize()if __name__ == "__main__": asyncio.run(main())
Copy
import asynciofrom narada import Naradafrom pydantic import BaseModel, Fieldclass CompanyInfo(BaseModel): name: str = Field(description="Company name") industry: str = Field(description="Primary industry") employee_count: str = Field(description="Number of employees") headquarters: str = Field(description="Headquarters location")async def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() # Complex automation with structured output and recording response = await window.agent( prompt="Navigate to TechCorp's about page and extract company information", output_schema=CompanyInfo, generate_gif=True, timeout=180, # 3 minutes for complex navigation time_zone="America/New_York" ) print(f"Execution time: {response.usage.actions} actions") print(f"Credits used: {response.usage.credits}") if response.structured_output: company = response.structured_output print(f"Company: {company.name}") print(f"Industry: {company.industry}") print(f"Size: {company.employee_count}") print(f"Location: {company.headquarters}")if __name__ == "__main__": asyncio.run(main())
Copy
import asynciofrom narada import Naradaasync def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() # Use variables to protect sensitive credentials from the LLM # The LLM will see ${username} and ${password} but not the actual values response = await window.agent( prompt="Navigate to https://example-app.com/login and log in with username ${username} and password ${password}", variables={ "username": "[email protected]", "password": "super_secret_password_123" } ) print(f"Status: {response.status}") print(f"Result: {response.text}")if __name__ == "__main__": asyncio.run(main())
Copy
import asynciofrom narada import Agent, Naradafrom pydantic import BaseModel, Fieldclass InvoiceData(BaseModel): invoice_number: str = Field(description="Invoice number") date: str = Field(description="Invoice date") vendor_name: str = Field(description="Vendor or supplier name") total_amount: str = Field(description="Total amount with currency") line_items: list[str] = Field(description="List of items or services")async def main(): async with Narada() as narada: window = await narada.open_and_initialize_browser_window() # Upload a file to extract structured data from it with open("/path/to/invoice.pdf", "rb") as f: file = await window.upload_file(file=f) # Extract structured information from the attached file response = await window.agent( prompt="Extract the invoice details from the attached document", agent=Agent.GENERALIST, attachment=file, output_schema=InvoiceData ) invoice = response.structured_output print(f"Invoice: {invoice.invoice_number}") print(f"Date: {invoice.date}") print(f"Vendor: {invoice.vendor_name}") print(f"Total: {invoice.total_amount}") print(f"Items: {', '.join(invoice.line_items)}")if __name__ == "__main__": asyncio.run(main())