开始使用免费开始使用

使用 SQL Agent 查询 NYC Taxi 数据集

Databricks 提供了一个开源的 samples catalog,其中包含多个 schema。本练习将创建一个 LangChain Databricks SQL 代理,连接到 nyctaxi schema,并向其提出一个需要用 SQL 才能回答的问题。创建这样的代理后,您无需具备 SQL 先验知识,也能对数据进行分析并就需要 SQL 的问题发问。

本练习是课程的一部分

使用 Python SDK 的 Databricks

查看课程

练习说明

  • 从 Databricks 的 samples catalog 中的 nyctaxi schema 创建 LangChain 的 SQLDatabase 对象。
  • 使用 Databricks 的 Meta Llama 3 模型创建自定义 LangChain LLM。
  • 使用该自定义 LLM 创建 LangChain Databricks SQL Agent。
  • 向 Databricks SQL Agent 发起查询。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Connect to the nyctaxi schema
db = SQLDatabase.from_databricks(
    ____="samples", 
    ____="nyctaxi",  
  	warehouse_id=warehouse_id)

# Create the custom LangChain LLM
meta_llm = ChatDatabricks(
    ____="databricks-meta-llama-3-3-70b-instruct",
    max_tokens=100)
toolkit = SQLDatabaseToolkit(db=db, llm=meta_llm)

# Create the SQL agent
agent = create_sql_agent(llm=____, toolkit=toolkit, verbose=True, handle_parsing_errors=True)

# Query the Databricks SQL Agent
result = agent.____("what's the duration and distance of the longest trip?")
print(result)
编辑并运行代码