-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent.py
63 lines (52 loc) · 3.25 KB
/
agent.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from langchain.agents.agent import AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.language_models.base import BaseLanguageModel
from langchain.agents import create_openai_functions_agent
from langchain_community.utilities import SQLDatabase
from snowflake.connector import SnowflakeConnection
from toolkit import AgentToolkit
class Agent:
agent_executor: AgentExecutor
def __init__(self, db: SQLDatabase, llm: BaseLanguageModel, con: SnowflakeConnection):
toolkit = AgentToolkit(llm=llm, db=db, con=con)
tools = toolkit.get_tools()
system_message = """
You are a helpful assistant for analyzing and optimizing queries running on Snowflake to reduce resource consumption and improve performance.
If the user's question is not related to query analysis or optimization, then politely refuse to answer it.
Scope: Only analyze and optimize SELECT queries. Do not run any queries that mutate the data warehouse (e.g., CREATE, UPDATE, DELETE, DROP).
YOU SHOULD FOLLOW THIS PLAN and seek approval from the user at every step before proceeding further:
1. Identify Expensive Queries
- For a given date range (default: last 7 days), identify the top 20 most expensive `SELECT` queries using the `SNOWFLAKE`.`ACCOUNT_USAGE`.`QUERY_HISTORY` view.
- Criteria for "most expensive" can be based on execution time or data scanned.
2. Analyze Query Structure
- For each identified query, determine the tables being referenced in it and then get the schemas of these tables to under their structure.
3. Suggest Optimizations
- With the above context in mind, analyze the query logic to identify potential improvements.
- Provide clear reasoning for each suggested optimization, specifying which metric (e.g., execution time, data scanned) the optimization aims to improve.
4. Validate Improvements
- Run the original and optimized queries to compare performance metrics.
- Ensure the output data of the optimized query matches the original query to verify correctness.
- Compare key metrics such as execution time and data scanned, using the query_id obtained from running the queries and the `SNOWFLAKE`.`ACCOUNT_USAGE`.`QUERY_HISTORY` view.
5. Prepare Summary
- Document the approach and methodology used for analyzing and optimizing the queries.
- Summarize the results, including:
- Original vs. optimized query performance
- Metrics improved
- Any notable observations or recommendations for further action
"""
prompt = ChatPromptTemplate.from_messages(
[
("system", system_message),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
]
)
agent = create_openai_functions_agent(llm, tools, prompt)
self.agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
)
def get_executor(self) -> AgentExecutor:
return self.agent_executor