|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "Have you ever wanted to ask a question about your PySpark data in plain English instead of writing SQL?\n", |
| 8 | + "[LangChain's Spark SQL Toolkit](https://python.langchain.com/docs/integrations/tools/spark_sql/) enables natural language data querying by:\n", |
| 9 | + "- Translating your requests into SQL\n", |
| 10 | + "- Executing them against your Spark cluster\n", |
| 11 | + "- Returning the results in a readable format\n", |
| 12 | + "This makes it much easier to work with large-scale data while still leveraging Spark's powerful distributed computing capabilities.\n", |
| 13 | + "To demonstrate, we'll create a simple DataFrame and use LangChain's Spark SQL tool to query it.\n", |
| 14 | + "```python\n", |
| 15 | + "from pyspark.sql import SparkSession, Row\n", |
| 16 | + "# Create sample data and DataFrame\n", |
| 17 | + "data = [Row(name=\"Alice\", age=30), Row(name=\"Bob\", age=25)]\n", |
| 18 | + "spark = SparkSession.builder.getOrCreate()\n", |
| 19 | + "df = spark.createDataFrame(data)\n", |
| 20 | + "df.write.saveAsTable(\"people\")\n", |
| 21 | + "df.show()\n", |
| 22 | + "```\n", |
| 23 | + "This creates a table `people` accessible via SQL.\n", |
| 24 | + "Next, we'll set up the key components that enable natural language querying of our Spark data. Here are the steps:\n", |
| 25 | + "1. Initialize the Spark SQL tool which provides the interface to our Spark database.\n", |
| 26 | + "2. Initialize a language model.\n", |
| 27 | + "3. Initialize the Spark SQL toolkit, which connects the language model with the Spark database.\n", |
| 28 | + "4. Create an agent executor that combines a language model with the Spark SQL toolkit.\n", |
| 29 | + "```python\n", |
| 30 | + "# Initialize Spark SQL tool\n", |
| 31 | + "spark_sql = SparkSQL(schema=\"default\")\n", |
| 32 | + "# Initialize LLM\n", |
| 33 | + "llm = ChatOpenAI(temperature=0)\n", |
| 34 | + "# Initialize toolkit\n", |
| 35 | + "toolkit = SparkSQLToolkit(db=spark_sql, llm=llm)\n", |
| 36 | + "# Create agent executor\n", |
| 37 | + "agent_executor = create_spark_sql_agent(llm=llm, toolkit=toolkit, verbose=True)\n", |
| 38 | + "```\n", |
| 39 | + "For a hands-on guide on how to build coordinated AI agents with LangGraph, check out [Building Coordinated AI Agents with LangGraph: A Hands-On Tutorial](https://codecut.ai/building-multi-agent-ai-langgraph-tutorial/).\n", |
| 40 | + "Now we can ask the agent to query the data.\n", |
| 41 | + "```python\n", |
| 42 | + "agent_executor.run(\"What is the average age of people in the table?\")\n", |
| 43 | + "```\n", |
| 44 | + "```\n", |
| 45 | + "> Entering new AgentExecutor chain...\n", |
| 46 | + "Action: list_tables_sql_db\n", |
| 47 | + "Action Input:\n", |
| 48 | + "Observation: people\n", |
| 49 | + "Thought:I can query the \"people\" table for the average age.\n", |
| 50 | + "Action: query_sql_db\n", |
| 51 | + "Action Input: SELECT AVG(age) FROM people\n", |
| 52 | + "Observation: [('27.5',)]\n", |
| 53 | + "Thought:The average age of people in the table is 27.5.\n", |
| 54 | + "Final Answer: 27.5\n", |
| 55 | + "> Finished chain.\n", |
| 56 | + "```\n", |
| 57 | + "The answer for the average age is correct.\n", |
| 58 | + "The output shows that the agent:\n", |
| 59 | + "- Looked up the available tables\n", |
| 60 | + "- Queried the `people` table for the average age\n", |
| 61 | + "- Got the result\n", |
| 62 | + "- Answered the question with the result\n", |
| 63 | + "Let's try another question.\n", |
| 64 | + "```python\n", |
| 65 | + "agent_executor.run(\"Who is the oldest person in the table?\")\n", |
| 66 | + "```\n", |
| 67 | + "```\n", |
| 68 | + "> Entering new AgentExecutor chain...\n", |
| 69 | + "Action: list_tables_sql_db\n", |
| 70 | + "Action Input:\n", |
| 71 | + "Observation: people\n", |
| 72 | + "Thought:I should query the \"people\" table to find the oldest person.\n", |
| 73 | + "Action: schema_sql_db\n", |
| 74 | + "Action Input: people\n", |
| 75 | + "Observation: CREATE TABLE spark_catalog.default.people (\n", |
| 76 | + " name STRING,\n", |
| 77 | + " age BIGINT)\n", |
| 78 | + ";\n", |
| 79 | + "/*\n", |
| 80 | + "3 rows from people table:\n", |
| 81 | + "name age\n", |
| 82 | + "Alice 30\n", |
| 83 | + "Bob 25\n", |
| 84 | + "*/\n", |
| 85 | + "Thought:I should write a query to select the oldest person from the \"people\" table.\n", |
| 86 | + "Action: query_sql_db\n", |
| 87 | + "Action Input: SELECT name, age FROM people ORDER BY age DESC LIMIT 1\n", |
| 88 | + "Observation: [('Alice', '30')]\n", |
| 89 | + "Thought:I now know the final answer\n", |
| 90 | + "Final Answer: Alice\n", |
| 91 | + "> Finished chain.\n", |
| 92 | + "```\n", |
| 93 | + "The answer for the oldest person is also correct.\n", |
| 94 | + "## Related Resources\n", |
| 95 | + "For deeper exploration of LangChain and Spark integrations:\n", |
| 96 | + "- **Multi-Agent Systems**: [Building Coordinated AI Agents with LangGraph: A Hands-On Tutorial](https://codecut.ai/building-multi-agent-ai-langgraph-tutorial/) for advanced AI agent coordination with LangChain\n", |
| 97 | + "- **Data Science Workflows**: [Build Data Science Workflows with DeepSeek and LangChain](https://codecut.ai/build-data-science-workflows-deepseek-langchain/) for comprehensive data analysis pipelines\n", |
| 98 | + "- **Private AI Solutions**: [Private AI Workflows with LangChain and Ollama](https://codecut.ai/private-ai-workflows-langchain-ollama/) for local model deployment and privacy-focused implementations\n", |
| 99 | + "- **Official Documentation**: [LangChain Spark SQL Tool](https://python.langchain.com/docs/integrations/tools/spark_sql/) for complete API reference and advanced configuration options" |
| 100 | + ] |
| 101 | + } |
| 102 | + ], |
| 103 | + "metadata": { |
| 104 | + "kernelspec": { |
| 105 | + "display_name": "Python 3 (ipykernel)", |
| 106 | + "language": "python", |
| 107 | + "name": "python3", |
| 108 | + "path": "/Users/khuyentran/codecut_content/codecut_articles/.venv/share/jupyter/kernels/python3" |
| 109 | + } |
| 110 | + }, |
| 111 | + "nbformat": 4, |
| 112 | + "nbformat_minor": 4 |
| 113 | +} |
0 commit comments