Spaces:
Running
Running
Update be.py
Browse files
be.py
CHANGED
@@ -1,115 +1,115 @@
|
|
1 |
-
from example import example
|
2 |
-
from datetime import datetime
|
3 |
-
import pandas as pd
|
4 |
-
# agent will directly create query and run the query in DB
|
5 |
-
from langchain.agents import create_sql_agent
|
6 |
-
# Simple chain to create the SQL statements, it doesn't execute the query
|
7 |
-
from langchain.chains import create_sql_query_chain
|
8 |
-
# to execute the query
|
9 |
-
from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool
|
10 |
-
from langchain_community.agent_toolkits import SQLDatabaseToolkit
|
11 |
-
from langchain.sql_database import SQLDatabase
|
12 |
-
from langchain.agents import AgentExecutor
|
13 |
-
from langchain.agents.agent_types import AgentType
|
14 |
-
from langchain_experimental.sql import SQLDatabaseChain
|
15 |
-
from langchain_community.vectorstores import Chroma
|
16 |
-
from langchain.prompts import SemanticSimilarityExampleSelector
|
17 |
-
# Prompt input for MYSQL
|
18 |
-
from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt
|
19 |
-
# Create the prompt template for creating the prompt for mysqlprompt
|
20 |
-
from langchain.prompts.prompt import PromptTemplate
|
21 |
-
from langchain.prompts import FewShotPromptTemplate
|
22 |
-
# to create the tools to be used by agent
|
23 |
-
from langchain.agents import Tool
|
24 |
-
|
25 |
-
# create the agent prompts
|
26 |
-
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
27 |
-
# Huggingface embeddings using Langchain
|
28 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
29 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
30 |
-
from langchain_core.prompts import ChatPromptTemplate
|
31 |
-
from langchain_core.prompts import HumanMessagePromptTemplate
|
32 |
-
from langchain_core.output_parsers import StrOutputParser
|
33 |
-
# Load Env parameters
|
34 |
-
from dotenv import load_dotenv
|
35 |
-
from langchain_openai import ChatOpenAI
|
36 |
-
from sqlalchemy import create_engine, text, URL
|
37 |
-
|
38 |
-
|
39 |
-
def config():
|
40 |
-
load_dotenv() # load env parameters
|
41 |
-
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo") # create LLM
|
42 |
-
#llm = OpenAI(temperature=0.5) # create LLM
|
43 |
-
return llm
|
44 |
-
|
45 |
-
# Setting up URL parameter to connect to MySQL Database
|
46 |
-
def get_db_chain(question):
|
47 |
-
db_user="
|
48 |
-
db_password="
|
49 |
-
db_host="
|
50 |
-
db_name="retail"
|
51 |
-
|
52 |
-
# create LLM
|
53 |
-
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
|
54 |
-
# Initialize SQL DB using Langchain
|
55 |
-
db = SQLDatabase.from_uri(f"mysql://{db_user}:{db_password}@{db_host}/{db_name}")
|
56 |
-
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
57 |
-
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
58 |
-
# create the list with only values and ready to be vectorized
|
59 |
-
to_vectorize = [" ".join(example.values()) for example in example] # use join to convert Dict to list
|
60 |
-
# Setup the Chroma database and vectorize
|
61 |
-
vectorstore = Chroma.from_texts(to_vectorize, embedding=embeddings, metadatas=example)
|
62 |
-
# Based on the user question, convert them to vector and take the similar looking vectors from Chroma DB
|
63 |
-
example_selector = SemanticSimilarityExampleSelector(
|
64 |
-
vectorstore = vectorstore,
|
65 |
-
k=2)
|
66 |
-
example_prompt = PromptTemplate(
|
67 |
-
input_variables=["Question", "SQLQuery", "SQLResult", "Answer",],
|
68 |
-
template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\n?Answer: {Answer}",)
|
69 |
-
few_shot_prompt = FewShotPromptTemplate(
|
70 |
-
example_selector=example_selector, # Hey LLM, if you dont know refer the examples giving in vector DB
|
71 |
-
example_prompt=example_prompt, # This is the Prompt template we have created
|
72 |
-
prefix=_mysql_prompt, # This is prefix of the prompt
|
73 |
-
suffix=PROMPT_SUFFIX, # This is suffix of the prompt
|
74 |
-
input_variables=["input", "table_info", "top_k"], # variables used in forming the prompt to LLM
|
75 |
-
)
|
76 |
-
chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_prompt)
|
77 |
-
response = chain.invoke(question)
|
78 |
-
return response
|
79 |
-
# Call the LLM with the question and the fewshotprompt
|
80 |
-
# write_query = create_sql_query_chain(llm=llm,db=db, prompt=few_shot_prompt)
|
81 |
-
#print(write_query)
|
82 |
-
# Execute the Query using QuerySQLDataBaseTool
|
83 |
-
#execute_query = QuerySQLDataBaseTool(db=db)
|
84 |
-
# Chain to combine write SQL and Execute SQL
|
85 |
-
#chain = write_query | execute_query | llm
|
86 |
-
#response = chain.invoke("Question")
|
87 |
-
def get_store_address(store):
|
88 |
-
url_object = URL.create(
|
89 |
-
"mysql",
|
90 |
-
username="root",
|
91 |
-
password="root", # plain (unescaped) text
|
92 |
-
host="localhost",
|
93 |
-
database="retail",
|
94 |
-
)
|
95 |
-
engine = create_engine(url_object)
|
96 |
-
#connect to engine
|
97 |
-
connection = engine.connect()
|
98 |
-
sql_query = "SELECT STORE_NUMBER, STORE_ADDRESS FROM STORES WHERE STORE_NUMBER = " + store
|
99 |
-
df = pd.read_sql(sql_query, con=engine)
|
100 |
-
response = df.to_string()
|
101 |
-
return response
|
102 |
-
def outreach_sms_message(outreach_input):
|
103 |
-
# create LLM
|
104 |
-
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo", verbose=True)
|
105 |
-
prompt = ChatPromptTemplate.from_template("You are a expert in writing a text message for appointment setup with less than 35 words."
|
106 |
-
"With {outreach_input}, generate a text message for appointment to be sent to customer")
|
107 |
-
output_parser = StrOutputParser()
|
108 |
-
chain = prompt | llm | output_parser
|
109 |
-
response = chain.invoke({"outreach_input": outreach_input})
|
110 |
-
return response
|
111 |
-
|
112 |
-
#if __name__ == "__main__":
|
113 |
-
# chain = get_db_chain()
|
114 |
-
# print(chain.run("List of all sales transactions for Trevor Nelson in June 2020"))
|
115 |
-
# Setting up URL parameter to connect to MySQL Database
|
|
|
1 |
+
from example import example
|
2 |
+
from datetime import datetime
|
3 |
+
import pandas as pd
|
4 |
+
# agent will directly create query and run the query in DB
|
5 |
+
from langchain.agents import create_sql_agent
|
6 |
+
# Simple chain to create the SQL statements, it doesn't execute the query
|
7 |
+
from langchain.chains import create_sql_query_chain
|
8 |
+
# to execute the query
|
9 |
+
from langchain_community.tools.sql_database.tool import QuerySQLDataBaseTool
|
10 |
+
from langchain_community.agent_toolkits import SQLDatabaseToolkit
|
11 |
+
from langchain.sql_database import SQLDatabase
|
12 |
+
from langchain.agents import AgentExecutor
|
13 |
+
from langchain.agents.agent_types import AgentType
|
14 |
+
from langchain_experimental.sql import SQLDatabaseChain
|
15 |
+
from langchain_community.vectorstores import Chroma
|
16 |
+
from langchain.prompts import SemanticSimilarityExampleSelector
|
17 |
+
# Prompt input for MYSQL
|
18 |
+
from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt
|
19 |
+
# Create the prompt template for creating the prompt for mysqlprompt
|
20 |
+
from langchain.prompts.prompt import PromptTemplate
|
21 |
+
from langchain.prompts import FewShotPromptTemplate
|
22 |
+
# to create the tools to be used by agent
|
23 |
+
from langchain.agents import Tool
|
24 |
+
|
25 |
+
# create the agent prompts
|
26 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
27 |
+
# Huggingface embeddings using Langchain
|
28 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
29 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
30 |
+
from langchain_core.prompts import ChatPromptTemplate
|
31 |
+
from langchain_core.prompts import HumanMessagePromptTemplate
|
32 |
+
from langchain_core.output_parsers import StrOutputParser
|
33 |
+
# Load Env parameters
|
34 |
+
from dotenv import load_dotenv
|
35 |
+
from langchain_openai import ChatOpenAI
|
36 |
+
from sqlalchemy import create_engine, text, URL
|
37 |
+
|
38 |
+
|
39 |
+
def config():
|
40 |
+
load_dotenv() # load env parameters
|
41 |
+
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo") # create LLM
|
42 |
+
#llm = OpenAI(temperature=0.5) # create LLM
|
43 |
+
return llm
|
44 |
+
|
45 |
+
# Setting up URL parameter to connect to MySQL Database
|
46 |
+
def get_db_chain(question):
|
47 |
+
db_user="admin"
|
48 |
+
db_password="Epperson"
|
49 |
+
db_host="retail.cd6uaise2moh.us-west-2.rds.amazonaws.com"
|
50 |
+
db_name="retail"
|
51 |
+
|
52 |
+
# create LLM
|
53 |
+
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
|
54 |
+
# Initialize SQL DB using Langchain
|
55 |
+
db = SQLDatabase.from_uri(f"mysql://{db_user}:{db_password}@{db_host}/{db_name}")
|
56 |
+
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
57 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
58 |
+
# create the list with only values and ready to be vectorized
|
59 |
+
to_vectorize = [" ".join(example.values()) for example in example] # use join to convert Dict to list
|
60 |
+
# Setup the Chroma database and vectorize
|
61 |
+
vectorstore = Chroma.from_texts(to_vectorize, embedding=embeddings, metadatas=example)
|
62 |
+
# Based on the user question, convert them to vector and take the similar looking vectors from Chroma DB
|
63 |
+
example_selector = SemanticSimilarityExampleSelector(
|
64 |
+
vectorstore = vectorstore,
|
65 |
+
k=2)
|
66 |
+
example_prompt = PromptTemplate(
|
67 |
+
input_variables=["Question", "SQLQuery", "SQLResult", "Answer",],
|
68 |
+
template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\n?Answer: {Answer}",)
|
69 |
+
few_shot_prompt = FewShotPromptTemplate(
|
70 |
+
example_selector=example_selector, # Hey LLM, if you dont know refer the examples giving in vector DB
|
71 |
+
example_prompt=example_prompt, # This is the Prompt template we have created
|
72 |
+
prefix=_mysql_prompt, # This is prefix of the prompt
|
73 |
+
suffix=PROMPT_SUFFIX, # This is suffix of the prompt
|
74 |
+
input_variables=["input", "table_info", "top_k"], # variables used in forming the prompt to LLM
|
75 |
+
)
|
76 |
+
chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_prompt)
|
77 |
+
response = chain.invoke(question)
|
78 |
+
return response
|
79 |
+
# Call the LLM with the question and the fewshotprompt
|
80 |
+
# write_query = create_sql_query_chain(llm=llm,db=db, prompt=few_shot_prompt)
|
81 |
+
#print(write_query)
|
82 |
+
# Execute the Query using QuerySQLDataBaseTool
|
83 |
+
#execute_query = QuerySQLDataBaseTool(db=db)
|
84 |
+
# Chain to combine write SQL and Execute SQL
|
85 |
+
#chain = write_query | execute_query | llm
|
86 |
+
#response = chain.invoke("Question")
|
87 |
+
def get_store_address(store):
|
88 |
+
url_object = URL.create(
|
89 |
+
"mysql",
|
90 |
+
username="root",
|
91 |
+
password="root", # plain (unescaped) text
|
92 |
+
host="localhost",
|
93 |
+
database="retail",
|
94 |
+
)
|
95 |
+
engine = create_engine(url_object)
|
96 |
+
#connect to engine
|
97 |
+
connection = engine.connect()
|
98 |
+
sql_query = "SELECT STORE_NUMBER, STORE_ADDRESS FROM STORES WHERE STORE_NUMBER = " + store
|
99 |
+
df = pd.read_sql(sql_query, con=engine)
|
100 |
+
response = df.to_string()
|
101 |
+
return response
|
102 |
+
def outreach_sms_message(outreach_input):
|
103 |
+
# create LLM
|
104 |
+
llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo", verbose=True)
|
105 |
+
prompt = ChatPromptTemplate.from_template("You are a expert in writing a text message for appointment setup with less than 35 words."
|
106 |
+
"With {outreach_input}, generate a text message for appointment to be sent to customer")
|
107 |
+
output_parser = StrOutputParser()
|
108 |
+
chain = prompt | llm | output_parser
|
109 |
+
response = chain.invoke({"outreach_input": outreach_input})
|
110 |
+
return response
|
111 |
+
|
112 |
+
#if __name__ == "__main__":
|
113 |
+
# chain = get_db_chain()
|
114 |
+
# print(chain.run("List of all sales transactions for Trevor Nelson in June 2020"))
|
115 |
+
# Setting up URL parameter to connect to MySQL Database
|