-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
45 lines (37 loc) · 1.08 KB
/
agent.py
File metadata and controls
45 lines (37 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from langchain_community.utilities import SQLDatabase
from langchain_community.agent_toolkits import create_sql_agent
from langchain_anthropic import ChatAnthropic
from dotenv import load_dotenv
import os
load_dotenv()
# 1. Connect to the Chinook database
db = SQLDatabase.from_uri("sqlite:///chinook.db")
# 2. Load Claude as the LLM
llm = ChatAnthropic(
model="claude-sonnet-4-20250514",
anthropic_api_key=os.getenv("ANTHROPIC_API_KEY"),
temperature=0
)
# 3. Create the SQL agent
agent = create_sql_agent(
llm=llm,
db=db,
verbose=True,
handle_parsing_errors=True
)
# 4. Ask it a question
# 4. Ask multiple questions
questions = [
"Who are the top 5 customers by total spending? Show their name and total amount spent.",
"Which genre has the most tracks?",
"Which country has the most customers?"
]
for question in questions:
print("\n" + "="*50)
print(f"QUESTION: {question}")
print("="*50 + "\n")
response = agent.invoke({"input": question})
print("\n" + "="*50)
print("FINAL ANSWER:")
print(response["output"])
print("="*50)