mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 18:05:25 +03:00
Updates docs and cookbooks to import ChatOpenAI, OpenAI, and OpenAI Embeddings from `langchain_openai` There are likely more --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
9.4 KiB
9.4 KiB
In [1]:
!pip install databricks-sql-connectorIn [2]:
# Connecting to Databricks with SQLDatabase wrapper
from langchain_community.utilities import SQLDatabase
db = SQLDatabase.from_databricks(catalog="samples", schema="nyctaxi")In [3]:
# Creating a OpenAI Chat LLM wrapper
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(temperature=0, model_name="gpt-4")In [4]:
from langchain_community.utilities import SQLDatabaseChain
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)In [5]:
db_chain.run(
"What is the average duration of taxi rides that start between midnight and 6am?"
)Out [5]:
[1m> Entering new SQLDatabaseChain chain...[0m What is the average duration of taxi rides that start between midnight and 6am? SQLQuery:[32;1m[1;3mSELECT AVG(UNIX_TIMESTAMP(tpep_dropoff_datetime) - UNIX_TIMESTAMP(tpep_pickup_datetime)) as avg_duration FROM trips WHERE HOUR(tpep_pickup_datetime) >= 0 AND HOUR(tpep_pickup_datetime) < 6[0m SQLResult: [33;1m[1;3m[(987.8122786304605,)][0m Answer:[32;1m[1;3mThe average duration of taxi rides that start between midnight and 6am is 987.81 seconds.[0m [1m> Finished chain.[0m
'The average duration of taxi rides that start between midnight and 6am is 987.81 seconds.'
In [7]:
from langchain.agents import create_sql_agent
from langchain_community.agent_toolkits import SQLDatabaseToolkit
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True)In [8]:
agent.run("What is the longest trip distance and how long did it take?")Out [8]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3mAction: list_tables_sql_db Action Input: [0m Observation: [38;5;200m[1;3mtrips[0m Thought:[32;1m[1;3mI should check the schema of the trips table to see if it has the necessary columns for trip distance and duration. Action: schema_sql_db Action Input: trips[0m Observation: [33;1m[1;3m CREATE TABLE trips ( tpep_pickup_datetime TIMESTAMP, tpep_dropoff_datetime TIMESTAMP, trip_distance FLOAT, fare_amount FLOAT, pickup_zip INT, dropoff_zip INT ) USING DELTA /* 3 rows from trips table: tpep_pickup_datetime tpep_dropoff_datetime trip_distance fare_amount pickup_zip dropoff_zip 2016-02-14 16:52:13+00:00 2016-02-14 17:16:04+00:00 4.94 19.0 10282 10171 2016-02-04 18:44:19+00:00 2016-02-04 18:46:00+00:00 0.28 3.5 10110 10110 2016-02-17 17:13:57+00:00 2016-02-17 17:17:55+00:00 0.7 5.0 10103 10023 */[0m Thought:[32;1m[1;3mThe trips table has the necessary columns for trip distance and duration. I will write a query to find the longest trip distance and its duration. Action: query_checker_sql_db Action Input: SELECT trip_distance, tpep_dropoff_datetime - tpep_pickup_datetime as duration FROM trips ORDER BY trip_distance DESC LIMIT 1[0m Observation: [31;1m[1;3mSELECT trip_distance, tpep_dropoff_datetime - tpep_pickup_datetime as duration FROM trips ORDER BY trip_distance DESC LIMIT 1[0m Thought:[32;1m[1;3mThe query is correct. I will now execute it to find the longest trip distance and its duration. Action: query_sql_db Action Input: SELECT trip_distance, tpep_dropoff_datetime - tpep_pickup_datetime as duration FROM trips ORDER BY trip_distance DESC LIMIT 1[0m Observation: [36;1m[1;3m[(30.6, '0 00:43:31.000000000')][0m Thought:[32;1m[1;3mI now know the final answer. Final Answer: The longest trip distance is 30.6 miles and it took 43 minutes and 31 seconds.[0m [1m> Finished chain.[0m
'The longest trip distance is 30.6 miles and it took 43 minutes and 31 seconds.'