Update utils.py
Browse files
utils.py
CHANGED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.utilities import SQLDatabase
|
| 2 |
+
from langchain_core.callbacks import BaseCallbackHandler
|
| 3 |
+
from typing import TYPE_CHECKING, Any, Optional, TypeVar, Union
|
| 4 |
+
from uuid import UUID
|
| 5 |
+
from langchain_community.agent_toolkits import create_sql_agent
|
| 6 |
+
from langchain_openai import ChatOpenAI
|
| 7 |
+
from langchain_community.vectorstores import Chroma
|
| 8 |
+
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
|
| 9 |
+
from langchain_openai import OpenAIEmbeddings
|
| 10 |
+
from langchain.agents.agent_toolkits import create_retriever_tool
|
| 11 |
+
from langchain_core.output_parsers import JsonOutputParser
|
| 12 |
+
import os
|
| 13 |
+
from langchain_core.prompts import (
|
| 14 |
+
ChatPromptTemplate,
|
| 15 |
+
FewShotPromptTemplate,
|
| 16 |
+
MessagesPlaceholder,
|
| 17 |
+
PromptTemplate,
|
| 18 |
+
SystemMessagePromptTemplate,
|
| 19 |
+
)
|
| 20 |
+
import ast
|
| 21 |
+
import re
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def query_as_list(db, query):
|
| 25 |
+
res = db.run(query)
|
| 26 |
+
res = [el for sub in ast.literal_eval(res) for el in sub if el]
|
| 27 |
+
res = [re.sub(r"\b\d+\b", "", string).strip() for string in res]
|
| 28 |
+
return list(set(res))
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def get_answer(user_query):
|
| 32 |
+
|
| 33 |
+
global retriever_tool, example_selector, db, llm
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
system_prefix = """You are an agent designed to interact with a SQL database.
|
| 37 |
+
Given an input question, create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
|
| 38 |
+
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most {top_k} results.
|
| 39 |
+
You can order the results by a relevant column to return the most interesting examples in the database.
|
| 40 |
+
Never query for all the columns from a specific table, only ask for the relevant columns given the question.
|
| 41 |
+
You have access to tools for interacting with the database.
|
| 42 |
+
Only use the given tools. Only use the information returned by the tools to construct your final answer.
|
| 43 |
+
You MUST double check your query before executing it. If you get an error while executing a query, rewrite the query and try again.
|
| 44 |
+
|
| 45 |
+
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database.
|
| 46 |
+
|
| 47 |
+
If the question does not seem related to the database, just return "I don't know" as the answer.
|
| 48 |
+
|
| 49 |
+
Here are some examples of user inputs and their corresponding SQL queries:"""
|
| 50 |
+
|
| 51 |
+
few_shot_prompt = FewShotPromptTemplate(
|
| 52 |
+
example_selector=example_selector,
|
| 53 |
+
example_prompt=PromptTemplate.from_template(
|
| 54 |
+
"User input: {input}\nSQL query: {query}"
|
| 55 |
+
),
|
| 56 |
+
input_variables=["input", "dialect", "top_k"],
|
| 57 |
+
prefix=system_prefix,
|
| 58 |
+
suffix="",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
employee = query_as_list(db, "SELECT FullName FROM Employee")
|
| 62 |
+
system_unique_name_prompt = """
|
| 63 |
+
If you need to filter on a proper noun, you must ALWAYS first look up the filter value using the "search_proper_nouns" tool!
|
| 64 |
+
|
| 65 |
+
You have access to the following tables: {table_names}
|
| 66 |
+
|
| 67 |
+
If the question does not seem related to the database, just return "I don't know" as the answer.
|
| 68 |
+
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
prompt_val = few_shot_prompt.invoke(
|
| 73 |
+
{
|
| 74 |
+
"input": user_query,
|
| 75 |
+
"top_k": 5,
|
| 76 |
+
"dialect": "SQLite",
|
| 77 |
+
|
| 78 |
+
"agent_scratchpad": [],
|
| 79 |
+
}
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
final_prompt = prompt_val.to_string() + '\n' + system_unique_name_prompt
|
| 83 |
+
full_prompt = ChatPromptTemplate.from_messages(
|
| 84 |
+
[
|
| 85 |
+
("system",final_prompt),
|
| 86 |
+
("human", "{input}"),
|
| 87 |
+
MessagesPlaceholder("agent_scratchpad"),
|
| 88 |
+
]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
agent = create_sql_agent(
|
| 93 |
+
llm=llm,
|
| 94 |
+
db=db,
|
| 95 |
+
max_iterations = 40,
|
| 96 |
+
extra_tools=[retriever_tool],
|
| 97 |
+
prompt=full_prompt,
|
| 98 |
+
agent_type="openai-tools",
|
| 99 |
+
verbose=True,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
result = agent.invoke({'input': user_query})
|
| 103 |
+
|
| 104 |
+
return result['output']
|