Spaces:
Runtime error
Runtime error
zachpaul38
commited on
Commit
•
0a9df35
1
Parent(s):
28dec3c
Add application file
Browse files
README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
---
|
2 |
-
title: Edai
|
3 |
emoji: ⚡
|
4 |
-
colorFrom:
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.32.0
|
|
|
1 |
---
|
2 |
+
title: Edai Analyst Chatbot Gradio Space
|
3 |
emoji: ⚡
|
4 |
+
colorFrom: green
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.32.0
|
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
# from langchain.agents import create_sql_agent
|
2 |
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
|
3 |
from langchain.sql_database import SQLDatabase
|
4 |
-
|
|
|
|
|
5 |
# from langchain.agents import AgentExecutor
|
|
|
6 |
import openai
|
7 |
import json
|
8 |
import gradio as gr
|
@@ -12,6 +15,8 @@ import os
|
|
12 |
import gradio
|
13 |
import datetime
|
14 |
|
|
|
|
|
15 |
postgres_connection_str = os.environ['POSTGRES_CONNECTION_STR']
|
16 |
access_key = os.environ['AWS_ACCESS_KEY_ID']
|
17 |
secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
|
@@ -23,10 +28,10 @@ s3_client = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_k
|
|
23 |
llm = OpenAI(temperature=0, verbose=True, openai_api_key=openai_api_key)
|
24 |
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
25 |
|
26 |
-
agent_executor = create_sql_agent(
|
27 |
-
|
28 |
-
|
29 |
-
)
|
30 |
|
31 |
# Function to insert the question and response into the table
|
32 |
def insert_question_response(question, response):
|
@@ -45,21 +50,91 @@ def insert_question_response(question, response):
|
|
45 |
|
46 |
def generate_response(question,question2):
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# file_name = 'flagged_text.txt'
|
61 |
# s3_client.put_object(Body=input_text, Bucket=bucket_name, Key=file_name)
|
62 |
-
return
|
63 |
|
64 |
|
65 |
# with gr.Blocks(theme='snehilsanyal/scikit-learn',title='EDai Chatbot') as demo:
|
|
|
1 |
# from langchain.agents import create_sql_agent
|
2 |
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
|
3 |
from langchain.sql_database import SQLDatabase
|
4 |
+
from langchain.llms.openai import OpenAI
|
5 |
+
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
|
6 |
+
from langchain.prompts.prompt import PromptTemplate
|
7 |
# from langchain.agents import AgentExecutor
|
8 |
+
|
9 |
import openai
|
10 |
import json
|
11 |
import gradio as gr
|
|
|
15 |
import gradio
|
16 |
import datetime
|
17 |
|
18 |
+
|
19 |
+
|
20 |
postgres_connection_str = os.environ['POSTGRES_CONNECTION_STR']
|
21 |
access_key = os.environ['AWS_ACCESS_KEY_ID']
|
22 |
secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
|
|
|
28 |
llm = OpenAI(temperature=0, verbose=True, openai_api_key=openai_api_key)
|
29 |
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
30 |
|
31 |
+
# agent_executor = create_sql_agent(
|
32 |
+
# llm=llm,
|
33 |
+
# toolkit=toolkit
|
34 |
+
# )
|
35 |
|
36 |
# Function to insert the question and response into the table
|
37 |
def insert_question_response(question, response):
|
|
|
50 |
|
51 |
def generate_response(question,question2):
|
52 |
|
53 |
+
_DEFAULT_TEMPLATE = """Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
|
54 |
+
Use the following format:
|
55 |
+
|
56 |
+
Question: "Question here"
|
57 |
+
SQLQuery: "SQL Query to run"
|
58 |
+
SQLResult: "Result of the SQLQuery"
|
59 |
+
Answer: "Final answer here"
|
60 |
+
|
61 |
+
Only use the following tables:
|
62 |
+
|
63 |
+
{table_info}
|
64 |
+
|
65 |
+
If someone asks about real estate or available properties, they mean the properties_us_sites_and_buildings table. To filter between a site and a building, use the type field.
|
66 |
+
|
67 |
+
Question: {input}"""
|
68 |
+
|
69 |
+
PROMPT = PromptTemplate(
|
70 |
+
input_variables=["input", "table_info", "dialect"], template=_DEFAULT_TEMPLATE
|
71 |
+
)
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
custom_table_info = {
|
76 |
+
"properties_us_sites_and_buildings": """This table is the real estate table."""
|
77 |
+
}
|
78 |
+
|
79 |
+
db = SQLDatabase.from_uri(
|
80 |
+
postgres_connection_str,
|
81 |
+
schema='langchain_testing',
|
82 |
+
include_tables=[
|
83 |
+
'properties_us_sites_and_buildings',
|
84 |
+
# 'state_incentives_catalog_dfd',
|
85 |
+
# 'local_incentives_sources_dfd',
|
86 |
+
# 'local_incentives_catalog_dfd',
|
87 |
+
# 'located_localities_dfd',
|
88 |
+
# 'located_metros_dfd',
|
89 |
+
# 'located_states_dfd',
|
90 |
+
'incentives_benefits_output',
|
91 |
+
# 'incentives_benefits_program_det',
|
92 |
+
# 'employment_industry_qcew_all_di',
|
93 |
+
# 'located_scores_states',
|
94 |
+
# 'located_scores_localities',
|
95 |
+
# 'located_scores_metros',
|
96 |
+
'incentives_benefits_rollups',
|
97 |
+
'incentives_model_math_summary',
|
98 |
+
'metro_codb_all_sectors_pnl',
|
99 |
+
|
100 |
+
# 'fiscal_benefits_local',
|
101 |
+
# 'fiscal_benefits_state',
|
102 |
+
# 'fiscal_costs_local',
|
103 |
+
# 'economic_impact_rollup',
|
104 |
+
# 'economic_impact_sector_details',
|
105 |
+
|
106 |
+
# 'industry_clusters_locality',
|
107 |
+
# 'industry_clusters_metro',
|
108 |
+
# 'occupation_oews',
|
109 |
+
# 'occupation_oews_state'
|
110 |
+
],
|
111 |
+
sample_rows_in_table_info=1,
|
112 |
+
# custom_table_info=custom_table_info
|
113 |
+
)
|
114 |
+
# db.table_info
|
115 |
+
print(db.table_info)
|
116 |
+
|
117 |
+
db_chain = SQLDatabaseChain.from_llm(llm, db, prompt=PROMPT, verbose=True)#, return_intermediate_steps=True)
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
# db_chain.
|
122 |
+
|
123 |
+
# prompt_template = """
|
124 |
+
# Keep in mind that any site, building, or property related question should be routed to the real estate portal.
|
125 |
+
# Any Local or city or city-sector incentives programs are asking about the local incentives program table.
|
126 |
+
# Any State incentives programs are asking about the state incentives program table.
|
127 |
+
# If you can't find the answer, make sure to look up the program field in the local and state incentives catalogs.
|
128 |
+
# If your final answer is "I don't know", then respond with "Please adjust your question and try asking again."
|
129 |
+
# """
|
130 |
+
|
131 |
+
# chain_response = agent_executor.run(question + prompt_template)
|
132 |
+
# # Insert the question and response into the PostgreSQL table
|
133 |
+
# insert_question_response(question, chain_response)
|
134 |
+
# bucket_name = 'edai-analytics'
|
135 |
# file_name = 'flagged_text.txt'
|
136 |
# s3_client.put_object(Body=input_text, Bucket=bucket_name, Key=file_name)
|
137 |
+
return db_chain.run("How many sites are available in VA?")
|
138 |
|
139 |
|
140 |
# with gr.Blocks(theme='snehilsanyal/scikit-learn',title='EDai Chatbot') as demo:
|