gokulp06 commited on
Commit
99ccf60
1 Parent(s): adefe61

Create test.py

Browse files
Files changed (1) hide show
  1. test.py +65 -0
test.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.llms import GooglePalm
2
+ from langchain_community.utilities import SQLDatabase
3
+ from langchain_experimental.sql import SQLDatabaseChain
4
+ from langchain.prompts import SemanticSimilarityExampleSelector
5
+ from langchain_community.embeddings import HuggingFaceEmbeddings
6
+ from langchain_community.vectorstores import Chroma
7
+ from langchain.prompts import FewShotPromptTemplate
8
+ from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _mysql_prompt
9
+ from langchain.prompts.prompt import PromptTemplate
10
+
11
+ from few_shots import few_shots
12
+
13
+ import os
14
+ from dotenv import load_dotenv
15
+
16
+ load_dotenv()
17
+
18
+
19
+ def get_few_shot_db_chain():
20
+ db_user = "root"
21
+ db_password = "root"
22
+ db_host = "localhost"
23
+ db_name = "staples_inventory"
24
+
25
+ db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}",
26
+ sample_rows_in_table_info=3)
27
+ llm = GooglePalm(google_api_key=os.environ["GOOGLE_API_KEY"], temperature=0.1)
28
+
29
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
30
+ to_vectorize = [" ".join(example.values()) for example in few_shots]
31
+ vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=few_shots)
32
+ example_selector = SemanticSimilarityExampleSelector(
33
+ vectorstore=vectorstore,
34
+ k=2,
35
+ )
36
+ mysql_prompt = """You are a MySQL expert. Given an input question, first create a syntactically correct MySQL query to run, then look at the results of the query and return the answer to the input question.
37
+ Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per MySQL. You can order the results to return the most informative data in the database.
38
+ Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers.
39
+ Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
40
+ Pay attention to use CURDATE() function to get the current date, if the question involves "today".
41
+
42
+ Use the following format:
43
+
44
+ Question: Question here
45
+ SQLQuery: Query to run with no pre-amble
46
+ SQLResult: Result of the SQLQuery
47
+ Answer: Final answer here
48
+
49
+ No pre-amble.
50
+ """
51
+
52
+ example_prompt = PromptTemplate(
53
+ input_variables=["Question", "SQLQuery", "SQLResult", "Answer", ],
54
+ template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\nAnswer: {Answer}",
55
+ )
56
+
57
+ few_shot_prompt = FewShotPromptTemplate(
58
+ example_selector=example_selector,
59
+ example_prompt=example_prompt,
60
+ prefix=mysql_prompt,
61
+ suffix=PROMPT_SUFFIX,
62
+ input_variables=["input", "table_info", "top_k"], # These variables are used in the prefix and suffix
63
+ )
64
+ chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, prompt=few_shot_prompt)
65
+ return chain