Skip to content

Add MongoDB Text-to-MQL Agent Tutorial to Gen-AI Showcase #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MMBazel
Copy link
Collaborator

@MMBazel MMBazel commented May 28, 2025

Add MongoDB Text-to-MQL Agent Tutorial to Gen-AI Showcase

This PR adds a comprehensive tutorial showcasing how to build natural language agents using MongoDB Atlas and LangChain's Text-to-MQL tools. The guide walks developers through two agent architectures:

  • ReAct-style agent – ideal for fast prototyping, demos, and dynamic tool usage.
  • LangGraph-style agent – designed for production with deterministic, auditable flows.

Key Features

  • Use the langchain_mongodb.agent_toolkit to generate, validate, and execute secure MongoDB aggregation pipelines from plain English.
  • Simplified prompt design using MONGODB_AGENT_SYSTEM_PROMPT.
  • Detailed comparison of ReAct vs. graph-style agents (speed, accuracy, tradeoffs).
  • Patterns for extending agents with memory, hybrid search (Vector + Full-Text + MQL), and observability.

Audience

This tutorial is intended for developers and teams building:

  • GenAI agents over structured data
  • Internal tools and analytics bots
  • Production-grade agentic systems with audit/control requirements

@MMBazel MMBazel requested a review from a team as a code owner May 28, 2025 21:49
@MMBazel MMBazel added the enhancement New feature or request label May 28, 2025
@ajosh0504
Copy link
Collaborator

Nice work! I like the comparison between an easy (React) and a more customized (LangGraph) agent. Few comments:

  • I found the wording "graph-based agent" a bit confusing. I took it to mean it was using graph-based retrieval or GraphRAG. Might want to specifically say "custom LangGraph agent" or similar.
  • The long outputs at every step make the notebook a bit hard to read. Consider clearing the outputs of some cells
  • In the LangGraph agent, curious why there is a conditional edge after generate_query? Shouldn't check_query, run_query, etc., always be run if the agent decides to generate a query?
  • Instead of saving the output to a local file, you could persist it in MongoDB? Could also use the MongoDB LangGraph checkpointer to show how to use these persisted outputs as memory for the agent.

@MMBazel
Copy link
Collaborator Author

MMBazel commented Jun 12, 2025

Nice work! I like the comparison between an easy (ReAct) and a more customized (LangGraph) agent. Few comments:

  • I found the wording "graph-based agent" a bit confusing. I took it to mean it was using graph-based retrieval or GraphRAG. Might want to specifically say "custom LangGraph agent" or similar.
  • The long outputs at every step make the notebook a bit hard to read. Consider clearing the outputs of some cells.
  • In the LangGraph agent, curious why there is a conditional edge after generate_query? Shouldn't check_query, run_query, etc., always be run if the agent decides to generate a query?
  • Instead of saving the output to a local file, you could persist it in MongoDB? Could also use the MongoDB LangGraph checkpointer to show how to use these persisted outputs as memory for the agent.

V2 Changes & Updates Made

1) Functionality added

  • Persistent Memory: Replaced file-based saving with MongoDBSaver (LangGraph’s built-in checkpointer) and added an LLMSummarizingMongoDBSaver class that generates human-readable step summaries via the LLM.
  • Quick Reference & Smoke Test: Appended a System Initialization & Quick Reference section and a if __name__ == "__main__": test_enhanced_summarization() smoke-test snippet for one-click validation.
  • Enhanced Memory Management: Improved inspection and cleanup utilities—inspect_thread_with_summaries_enhanced, list_conversation_threads, clear_thread_history, and memory_system_stats—for better observability and maintenance.
  • Interactive CLI: Refined interactive_query() with clear commands (exit, threads, switch, debug) and seamless thread switching to support live exploratory sessions.

Screenshot 2025-06-12 at 2 21 03 PM
Screenshot 2025-06-12 at 2 21 37 PM

2) Workflow restructuring

  • Introduction overhaul: Reorganized front-matter into logical sections—Overview, Use Cases, Business Applications, Technical Components, Prerequisites, and Network Setup—to guide readers step by step.
  • Error-handling cleanup: Converted every bare except: into except Exception as e: and surfaced exception details in logs.
  • Control-flow clarification:
    • Rationale for keeping the conditional edge:
      1. Future-proofing: If the LLM ever returns a non-tool response (e.g. a clarification request or “I don’t know”), the branch cleanly skips validation and execution without graph rewrites.
      2. Semantic clarity: Explicitly encodes “only validate/execute when a query was generated,” making the intent and node roles crystal clear.
      3. Scalable branching: Allows easy addition of fallback or help branches later by adding new conditional paths from generate_query rather than rewiring hard-coded edges.

3) New uses

Screenshot 2025-06-12 at 2 22 04 PM

  • Business applications: Added real-world query examples (analytics, recommendations, trend analysis, geographic analysis) to showcase practical value.

  • Demos & agent comparison: Structured demo functions (demo_basic_queries, demo_conversation_memory, compare_agents_with_memory) under clear headings, with usage instructions and side-by-side performance analysis.

Screenshot 2025-06-12 at 2 23 36 PM
Screenshot 2025-06-12 at 2 24 11 PM
Screenshot 2025-06-12 at 2 24 33 PM

Screenshot 2025-06-12 at 2 24 56 PM

  • Comprehensive test suite: Introduced run_comparison_tests() to automatically run simple, moderate, and complex query scenarios across both agents.

  • Interactive exploration: Enhanced live notebook usage via the improved interactive_query() interface, encouraging hands-on data exploration and debugging.

Screenshot 2025-06-12 at 2 26 04 PM
Screenshot 2025-06-12 at 2 26 21 PM

@MMBazel MMBazel requested a review from Copilot June 12, 2025 21:58
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants