Skip to main content

A LangChain SQL agent for querying Looker via its Open SQL Interface (JDBC/Avatica).

Project description

LangChain Looker SQL Agent

Connect LangChain to your Looker instance for conversational data querying using Looker's Open SQL Interface and its governed semantic layer.

This project provides a Python package, langchain-looker-agent, that allows you to build LangChain agents capable of interacting with your Looker data via its Avatica JDBC driver. Ask questions in natural language, and the agent will translate them into Looker-specific SQL, execute them, and provide answers based on your curated LookML models.

License: Apache-2.0

Key Features

  • Leverage Looker's Semantic Layer: Queries use your existing LookML models, ensuring consistent metrics and business logic.
  • Data Governance: Respects Looker's permission model through JDBC authentication.
  • Looker SQL Syntax Aware: The agent is prompted to use backticked identifiers (`model_name`.`explore_name`, `view_name.field_name`), AGGREGATE() for measures, and other Calcite/Looker SQL nuances.
  • Conversational Interaction: Supports multi-turn conversations (when used with memory).
  • Metadata Richness: Informs the LLM with Looker-specific field metadata (labels, descriptions, categories) and filters hidden fields.
  • Standard LangChain Integration: Built with core LangChain components for easy use.

Quickstart (Using the Library)

This example shows how to use the langchain-looker-agent library in your own Python project after installing it.

  1. Install the package:

    pip install langchain-looker-agent 
    

    (Note: This assumes the package will be published to PyPI. For local development, see "Development Setup" below).

  2. Set Environment Variables: Ensure the following are set in your environment or a .env file (see .env.example):

    • OPENAI_API_KEY (or your chosen LLM's API key)
    • LOOKER_INSTANCE_URL (e.g., https://yourcompany.cloud.looker.com)
    • LOOKML_MODEL_NAME (e.g., analytics)
    • LOOKER_CLIENT_ID
    • LOOKER_CLIENT_SECRET
    • LOOKER_JDBC_DRIVER_PATH (absolute path to your avatica-...-looker.jar)
    • JAVA_HOME (pointing to your JRE/JDK root)
  3. Example Python Script:

    import os
    from dotenv import load_dotenv
    from langchain_looker_agent import LookerSQLDatabase, LookerSQLToolkit, create_looker_sql_agent
    from langchain_openai import ChatOpenAI # Or your preferred LLM
    
    load_dotenv() # Loads .env file from current directory or parent
    
    # Initialize components
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    db = LookerSQLDatabase(
        looker_instance_url=os.environ["LOOKER_INSTANCE_URL"],
        lookml_model_name=os.environ["LOOKML_MODEL_NAME"],
        client_id=os.environ["LOOKER_CLIENT_ID"],
        client_secret=os.environ["LOOKER_CLIENT_SECRET"],
        jdbc_driver_path=os.environ["LOOKER_JDBC_DRIVER_PATH"],
        sample_rows_in_table_info=0 # Keep it minimal for quickstart
    )
    toolkit = LookerSQLToolkit(db=db)
    agent_executor = create_looker_sql_agent(llm=llm, toolkit=toolkit, verbose=False)
    
    # Ask a question
    question = "How many explores are available in the model?"
    response = agent_executor.invoke({"input": question, "chat_history": []})
    print(f"Question: {question}")
    print(f"Answer: {response.get('output')}")
    
    db.close()
    

Running the Example Notebooks

This repository includes detailed Jupyter Notebooks to demonstrate setup, testing, and conversational use.

  • For detailed setup instructions for running the example notebooks (including Java, Python environment, and JDBC driver placement), please see: EXAMPLES_SETUP.md
  • Notebooks available:
    • notebooks/00_comprehensive_agent_tests.ipynb: Thorough tests of all components.
    • notebooks/01_conversational_looker_agent.ipynb: Interactive chat demo.

Technical Details

For a deeper dive into the implementation, JDBC interaction, specific Looker SQL syntax handled, and design choices (like why a custom LookerSQLDatabase wrapper was used over a new SQLAlchemy dialect), please refer to:

Core Python Module

The main logic is encapsulated in the langchain_looker_agent package, primarily in src/langchain_looker_agent/agent.py. This includes:

  • LookerSQLDatabase: Manages JDBC connection and Looker-specific metadata/querying.
  • LookerSQLToolkit: Bundles LangChain tools.
  • create_looker_sql_agent: Factory for the ReAct agent.

Troubleshooting

Common issues and solutions are discussed in the EXAMPLES_SETUP.md and TECHNICAL_DETAILS.md. Key areas include:

  • JAVA_HOME configuration.
  • Correct path to the Looker JDBC driver JAR.
  • Looker credentials and permissions for the SQL Interface.
  • Python package import errors (ensure pip install -e . or package installation).

Contributing

(Optional: Add guidelines if you plan to accept contributions)

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_looker_agent-0.1.0.tar.gz (27.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_looker_agent-0.1.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file langchain_looker_agent-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_looker_agent-0.1.0.tar.gz
  • Upload date:
  • Size: 27.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for langchain_looker_agent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c257fea891342f6dab85d64064cd96ad7649d214f5c5003e5abc2f22cc98728e
MD5 3aa75cce538d0cc92c05d0b58bae13d7
BLAKE2b-256 06b9ad64666f9be07856f7575cc40889c00316d01e16a4b0d0e35de892584c9e

See more details on using hashes here.

File details

Details for the file langchain_looker_agent-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_looker_agent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0315e197a26d08ee7e3b1604d35e72aad828dd19fb6081e46ed86b963e3d89ba
MD5 1ffcf7f6b4296fd96e5be5aad4e4c46c
BLAKE2b-256 d78a67174ca7ff8898f6b0159fa0ccd883333a6f5a4e13e4bbb776124f663a78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page