Practical examples of using the Morphik Agent for complex analysis tasks
The Morphik Agent enables sophisticated workflows that go far beyond simple document retrieval. This cookbook provides practical examples of how to use the agent for complex analysis tasks.
This example shows how the agent can autonomously analyze financial documents, perform calculations, and generate insights.
Copy
Ask AI
from morphik import Morphikdb = Morphik()# Complex financial analysis queryresult = db.agent_query("""Analyze our quarterly financial reports for the last two years.Calculate key metrics like revenue growth rate, profit margins, and ROI.Identify trends and provide actionable recommendations for improving profitability.Save the key insights for future reference.""")print("Financial Analysis:")print(result["response"])# Check what the agent didprint("\nAgent Tool Usage:")for i, tool in enumerate(result["tool_history"], 1): print(f"{i}. {tool['tool_name']}") if tool['tool_name'] == 'execute_code': print(f" Executed: {tool['tool_args']['code'][:100]}...")# Display structured resultsfor obj in result["display_objects"]: if obj["type"] == "text" and "## Key Metrics" in obj["content"]: print("\nKey Financial Metrics:") print(obj["content"])
Demonstrates how the agent can research customer data, analyze sentiment, and identify opportunities.
Copy
Ask AI
# Multi-step customer analysiscustomer_analysis = db.agent_query("""Research our customer feedback and support tickets from the last quarter.Analyze sentiment trends and identify the top 3 pain points.For each pain point, suggest specific product improvements.Create a priority matrix based on impact and effort.""")print("Customer Research Results:")print(customer_analysis["response"])# Extract any charts or visualizations the agent createdfor obj in customer_analysis["display_objects"]: if obj["type"] == "image": print(f"Generated visualization: {obj['caption']}") # In a real app, you'd display the image from obj["content"]
Shows how to use the agent to explore complex relationships in your data.
Copy
Ask AI
# Knowledge graph explorationkg_insights = db.agent_query("""Explore our product knowledge graph to find unexpected connections between customer segments and product features.Identify potential cross-selling opportunities based on relationship strengths.Create a ranked list of opportunities with supporting evidence.""")print("Knowledge Graph Insights:")print(kg_insights["response"])# Check if the agent found any graph relationshipsfor tool in kg_insights["tool_history"]: if tool["tool_name"] == "knowledge_graph_query": print(f"Graph query: {tool['tool_args']}") print(f"Results: {str(tool['tool_result'])[:200]}...")
Demonstrates automated document analysis and summarization.
Copy
Ask AI
# Intelligent document processingdoc_intelligence = db.agent_query("""Analyze all documents in our legal department folder.Extract key contract terms, identify renewal dates, and flag any risks.Create a summary report with actionable items for the legal team.""")print("Document Intelligence Report:")print(doc_intelligence["response"])# Show how the agent processed different document typesdocuments_analyzed = [ tool for tool in doc_intelligence["tool_history"] if tool["tool_name"] == "document_analyzer"]print(f"\nAnalyzed {len(documents_analyzed)} documents:")for tool in documents_analyzed: doc_id = tool["tool_args"].get("document_id", "unknown") analysis_type = tool["tool_args"].get("analysis_type", "general") print(f"- Document {doc_id}: {analysis_type} analysis")
Shows how the agent can combine multiple data sources for strategic insights.
Copy
Ask AI
# Strategic competitive analysiscompetitive_analysis = db.agent_query("""Analyze our product documentation and market research reports.Compare our features against competitor data in the knowledge graph.Identify gaps in our offering and opportunities for differentiation.Calculate market share implications and recommend strategic priorities.""")print("Competitive Analysis:")print(competitive_analysis["response"])# Extract strategic recommendationsfor obj in competitive_analysis["display_objects"]: if "recommendation" in obj["content"].lower(): print("\nStrategic Recommendations:") print(obj["content"])
Demonstrates how the agent’s memory enables progressive analysis.
Copy
Ask AI
# Step 1: Initial analysis with memorystep1 = db.agent_query("""Analyze our marketing campaign performance across all channels.Calculate ROI for each channel and identify top performers.Save the key findings and metrics for future analysis.""")# Step 2: Build on previous analysisstep2 = db.agent_query("""Based on our previous marketing performance analysis,develop an optimized budget allocation strategy for next quarter.Compare projected ROI with current performance.""")# Step 3: Create action planstep3 = db.agent_query("""Using our marketing analysis and budget optimization,create a detailed implementation plan with timelines and success metrics.""")print("Progressive Analysis Results:")print("Step 1 - Performance Analysis:")print(step1["response"][:200] + "...\n")print("Step 2 - Budget Optimization:")print(step2["response"][:200] + "...\n")print("Step 3 - Implementation Plan:")print(step3["response"])
Shows how the agent can validate data consistency across documents.
Copy
Ask AI
# Data validation workflowvalidation_results = db.agent_query("""Check consistency of financial data across our quarterly reports.Identify any discrepancies in revenue, costs, or key metrics.Calculate data quality scores and flag potential errors.Provide recommendations for improving data accuracy.""")print("Data Validation Results:")print(validation_results["response"])# Check for any code the agent wrote to validate datacode_executions = [ tool for tool in validation_results["tool_history"] if tool["tool_name"] == "execute_code"]print(f"\nAgent performed {len(code_executions)} validation calculations")for execution in code_executions: if "discrepancy" in str(execution["tool_result"]).lower(): print("Found data discrepancy:", execution["tool_result"])
# Break complex projects into phasesphases = [ "Analyze current market position and save findings", "Based on market analysis, identify expansion opportunities", "Create detailed business plan for top opportunity", "Calculate financial projections and risk assessment"]results = []for phase in phases: result = db.agent_query(phase) results.append(result) print(f"Completed: {phase}")
def robust_agent_query(query, max_retries=2): """Agent query with error handling and retries""" for attempt in range(max_retries + 1): try: result = db.agent_query(query) # Check for tool failures failed_tools = [ tool for tool in result.get("tool_history", []) if "error" in str(tool.get("tool_result", "")).lower() ] if failed_tools and attempt < max_retries: print(f"Attempt {attempt + 1} had tool failures, retrying...") continue return result except Exception as e: if attempt < max_retries: print(f"Attempt {attempt + 1} failed: {e}, retrying...") continue else: raise e return None# Use robust queryingresult = robust_agent_query( "Perform complex financial analysis with error checking")
These workflows demonstrate the power of the Morphik Agent for complex, multi-step analysis tasks that would be impossible with traditional retrieval-based systems. The agent’s ability to plan, execute tools, and remember context makes it ideal for sophisticated business intelligence and research workflows.