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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from textwrap import dedent
|
| 9 |
+
from snowflake import connector as sf
|
| 10 |
+
from neo4j import GraphDatabase as nj
|
| 11 |
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
sf__user = os.getenv('sf__user')
|
| 15 |
+
sf__passkey = os.getenv('sf__passkey')
|
| 16 |
+
sf__identity = os.getenv('sf__identity')
|
| 17 |
+
sf__warehouse = os.getenv('sf__warehouse')
|
| 18 |
+
sf__database = os.getenv('sf__database')
|
| 19 |
+
sf__schema = os.getenv('sf__schema')
|
| 20 |
+
|
| 21 |
+
nj__uri = os.getenv('nj__uri')
|
| 22 |
+
nj__username = os.getenv('nj__username')
|
| 23 |
+
nj__password = os.getenv('nj__password')
|
| 24 |
+
|
| 25 |
+
sf__connection = sf.connect(user=sf__user, password=sf__passkey, account=sf__identity, warehouse=sf__warehouse, database=sf__database, schema=sf__schema)
|
| 26 |
+
sf__cursor = sf__connection.cursor()
|
| 27 |
+
sf__dt_updated__table = sf__cursor.execute(f"SELECT * FROM INDIGO_AIRLINES__CUSTOMER_DATA").fetch_pandas_all().to_dict('records')
|
| 28 |
+
sf__cursor.close()
|
| 29 |
+
sf__connection.close()
|
| 30 |
+
|
| 31 |
+
nj__driver = nj.driver(nj__uri, auth=(nj__username, nj__password))
|
| 32 |
+
nj__session = nj__driver.session()
|
| 33 |
+
|
| 34 |
+
nj__n_nodes = nj__session.run("""MATCH (n) RETURN count(n)""", database_ = 'neo4j').single()[0]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if(): pass
|
| 38 |
+
|
| 39 |
+
elif(nj__n_nodes == 0):
|
| 40 |
+
|
| 41 |
+
prmpt_sys__sql2cql = \
|
| 42 |
+
f"""
|
| 43 |
+
name :: neo4jRAG : sql2cql
|
| 44 |
+
|
| 45 |
+
task :: output the 'cypher query' consisting of {{nodes, relationships, property keys}} as per the input data.
|
| 46 |
+
|
| 47 |
+
constraints ::
|
| 48 |
+
- strictly ensure the syntax of the cypher query based on the 'neo4j' syntax.
|
| 49 |
+
- strictly ensure to hide intro, outro and code blocks.
|
| 50 |
+
|
| 51 |
+
structure ::
|
| 52 |
+
input :: [{{...}},...,{{...}}]
|
| 53 |
+
output :: <cypher query(s) seperated with ';'>
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
url = "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.amazon.nova-premier-v1:0/converse"
|
| 57 |
+
|
| 58 |
+
lst_itr__sql2cql = []
|
| 59 |
+
for itr in range(0, len(sf__dt_updated__table), 10):
|
| 60 |
+
payload = {
|
| 61 |
+
"messages": [{"role": "user","content": [{"text": str(sf__dt_updated__table[itr:itr+10])}]}],
|
| 62 |
+
"system" : [{"text" : prmpt_sys__sql2cql}]
|
| 63 |
+
}
|
| 64 |
+
headers = {
|
| 65 |
+
"Content-Type": "application/json",
|
| 66 |
+
"Authorization": f"Bearer {os.getenv('aws__bedrock')}"
|
| 67 |
+
}
|
| 68 |
+
response = requests.request("POST", url, json=payload, headers=headers)
|
| 69 |
+
|
| 70 |
+
lst_itr__sql2cql.append(json.loads(response.text)['output']['message']['content'][0]['text'].replace('\n', ';'))
|
| 71 |
+
|
| 72 |
+
str_qry__sql2cql = ";".join(lst_itr__sql2cql)
|
| 73 |
+
lst_qry__sql2cql = list(filter(None, str_qry__sql2cql.split(";")))
|
| 74 |
+
|
| 75 |
+
itr_qry__sql = 0
|
| 76 |
+
itr_qry__cql = 200
|
| 77 |
+
|
| 78 |
+
while itr_qry__cql>0 and itr_qry__sql<len(lst_qry__sql2cql):
|
| 79 |
+
try:
|
| 80 |
+
nj__session.run(lst_qry__sql2cql[itr_qry__sql])
|
| 81 |
+
itr_qry__cql -= 1
|
| 82 |
+
itr_qry__sql += 1
|
| 83 |
+
except:
|
| 84 |
+
itr_qry__sql += 1
|
| 85 |
+
|
| 86 |
+
else: pass
|
| 87 |
+
|
| 88 |
+
nj__schema = nj__session.run\
|
| 89 |
+
(
|
| 90 |
+
"""
|
| 91 |
+
CALL db.labels() YIELD label
|
| 92 |
+
WITH collect(label) AS lst__labels
|
| 93 |
+
CALL db.relationshipTypes() YIELD relationshipType
|
| 94 |
+
WITH lst__labels, collect(relationshipType) AS lst__relationshipTypes
|
| 95 |
+
CALL db.propertyKeys() YIELD propertyKey
|
| 96 |
+
WITH lst__labels, lst__relationshipTypes, collect(propertyKey) AS lst__propertyKeys
|
| 97 |
+
RETURN
|
| 98 |
+
{
|
| 99 |
+
nj__nodes: lst__labels,
|
| 100 |
+
nj__relationships: lst__relationshipTypes,
|
| 101 |
+
nj__propertykeys: lst__propertyKeys
|
| 102 |
+
}
|
| 103 |
+
AS nj__schema
|
| 104 |
+
"""
|
| 105 |
+
).data()[-1]
|
| 106 |
+
|
| 107 |
+
nj__plot = nj__session.run\
|
| 108 |
+
(
|
| 109 |
+
"""
|
| 110 |
+
MATCH (n)-[r]->(m)
|
| 111 |
+
WITH labels(n) AS sourceLabels,
|
| 112 |
+
type(r) AS relType,
|
| 113 |
+
labels(m) AS targetLabels,
|
| 114 |
+
keys(n) AS nodeProps,
|
| 115 |
+
keys(r) AS relProps
|
| 116 |
+
WHERE size(nodeProps) > 0 OR size(relProps) > 0
|
| 117 |
+
RETURN DISTINCT
|
| 118 |
+
sourceLabels[0] AS fromNode,
|
| 119 |
+
relType,
|
| 120 |
+
targetLabels[0] AS toNode,
|
| 121 |
+
nodeProps AS sourceProperties,
|
| 122 |
+
relProps AS relationshipProperties
|
| 123 |
+
ORDER BY fromNode, relType, toNode
|
| 124 |
+
"""
|
| 125 |
+
).data()
|
| 126 |
+
|
| 127 |
+
### ===================================================================================================================== ###
|
| 128 |
+
|
| 129 |
+
# prmpt_sys__llm_txt2txt = \
|
| 130 |
+
# f"""
|
| 131 |
+
# name :: neo4jRAG : llm_txt2txt
|
| 132 |
+
|
| 133 |
+
# task :: output the 8 cypher queries, prompts based on the 'nj__schema' and 'nj__plot'.
|
| 134 |
+
# constraints ::
|
| 135 |
+
# - strictly ensure the syntax of the cypher query based on the 'neo4j' syntax.
|
| 136 |
+
# - strictly ensure to hide intro, outro and code blocks.
|
| 137 |
+
|
| 138 |
+
# structure ::
|
| 139 |
+
# output :: [{{'prompt' : '...', 'query' : '...'}}]
|
| 140 |
+
|
| 141 |
+
# source ::
|
| 142 |
+
# nj__schema :: {nj__schema}
|
| 143 |
+
# nj__plot :: {nj__plot}
|
| 144 |
+
# """
|
| 145 |
+
|
| 146 |
+
# url = "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.amazon.nova-premier-v1:0/converse"
|
| 147 |
+
|
| 148 |
+
# payload = {
|
| 149 |
+
# "messages": [{"role": "user","content": [{"text": " "}]}],
|
| 150 |
+
# "system" : [{"text" : prmpt_sys__llm_txt2txt}],
|
| 151 |
+
# "temperature": 0.6,
|
| 152 |
+
# "topP" : 0.2
|
| 153 |
+
# }
|
| 154 |
+
# headers = {
|
| 155 |
+
# "Content-Type": "application/json",
|
| 156 |
+
# "Authorization": f"Bearer {os.getenv('aws__bedrock')}"
|
| 157 |
+
# }
|
| 158 |
+
# response = requests.request("POST", url, json=payload, headers=headers)
|
| 159 |
+
|
| 160 |
+
# lst_dct__txt2txt = eval(json.loads(response.text)['output']['message']['content'][0]['text'])
|
| 161 |
+
# for itr in lst_dct__txt2txt:
|
| 162 |
+
# try:
|
| 163 |
+
# print()
|
| 164 |
+
# print(itr['prompt'])
|
| 165 |
+
# print(itr['query'])
|
| 166 |
+
# print(nj__session.run(itr['query']).data())
|
| 167 |
+
# print()
|
| 168 |
+
# except:
|
| 169 |
+
# pass
|
| 170 |
+
|
| 171 |
+
### ===================================================================================================================== ###
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def func__neo4jRAG(args__msg, args__hstry):
|
| 175 |
+
|
| 176 |
+
query = args__msg
|
| 177 |
+
|
| 178 |
+
prmpt_sys__txt2cql = \
|
| 179 |
+
f"""
|
| 180 |
+
name :: neo4jRAG : txt2sql
|
| 181 |
+
|
| 182 |
+
task :: output the cypher query as per the input prompt based on the 'nj__schema' and 'nj__plot'.
|
| 183 |
+
constraints ::
|
| 184 |
+
- strictly ensure the syntax of the cypher query based on the 'neo4j' syntax.
|
| 185 |
+
- strictly ensure to hide intro, outro and code blocks.
|
| 186 |
+
|
| 187 |
+
structure ::
|
| 188 |
+
input :: <prompt text>
|
| 189 |
+
output :: <cypher query>
|
| 190 |
+
|
| 191 |
+
source ::
|
| 192 |
+
nj__schema :: {nj__schema}
|
| 193 |
+
nj__plot :: {nj__plot}
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
url = "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.amazon.nova-premier-v1:0/converse"
|
| 197 |
+
|
| 198 |
+
payload = {
|
| 199 |
+
"messages": [{"role": "user","content": [{"text": f"{query}"}]}],
|
| 200 |
+
"system" : [{"text" : prmpt_sys__txt2cql}],
|
| 201 |
+
"temperature": 0.6,
|
| 202 |
+
"topP" : 0.2
|
| 203 |
+
}
|
| 204 |
+
headers = {
|
| 205 |
+
"Content-Type": "application/json",
|
| 206 |
+
"Authorization": f"Bearer {os.getenv('aws__bedrock')}"
|
| 207 |
+
}
|
| 208 |
+
response = requests.request("POST", url, json=payload, headers=headers)
|
| 209 |
+
|
| 210 |
+
sf_nj__sql2cql = json.loads(response.text)['output']['message']['content'][0]['text']
|
| 211 |
+
# print(nj__session.run(io_nj__txt2sql).data())
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
prmpt_sys__cql2txt = \
|
| 215 |
+
f"""
|
| 216 |
+
name :: neo4jRAG : cql2txt
|
| 217 |
+
|
| 218 |
+
task :: output the info as per the input query based on the data.
|
| 219 |
+
constraints ::
|
| 220 |
+
- strictly ensure the overall summarized information based on the data in terms of data analysis in the business perspective with accurate numerical information in the structure of a paragraph.
|
| 221 |
+
- strictly ensure that the numericals are as per indian standards.
|
| 222 |
+
- strictly ensure to hide intro, outro and code blocks.
|
| 223 |
+
|
| 224 |
+
structure ::
|
| 225 |
+
input :: <prmpt>
|
| 226 |
+
output :: <info>
|
| 227 |
+
|
| 228 |
+
source ::
|
| 229 |
+
data :: {sf_nj__sql2cql}
|
| 230 |
+
"""
|
| 231 |
+
|
| 232 |
+
url = "https://bedrock-runtime.us-east-1.amazonaws.com/model/us.amazon.nova-premier-v1:0/converse"
|
| 233 |
+
|
| 234 |
+
payload = {
|
| 235 |
+
"messages": [{"role": "user","content": [{"text": f"{query}"}]}],
|
| 236 |
+
"system" : [{"text" : prmpt_sys__cql2txt}],
|
| 237 |
+
"temperature": 0.6,
|
| 238 |
+
"topP" : 0.2,
|
| 239 |
+
"maxTokens" : 8888
|
| 240 |
+
}
|
| 241 |
+
headers = {
|
| 242 |
+
"Content-Type": "application/json",
|
| 243 |
+
"Authorization": f"Bearer {os.getenv('aws__bedrock')}"
|
| 244 |
+
}
|
| 245 |
+
response = requests.request("POST", url, json=payload, headers=headers)
|
| 246 |
+
info = json.loads(response.text)['output']['message']['content'][0]['text']
|
| 247 |
+
|
| 248 |
+
queryinfo = \
|
| 249 |
+
f"""
|
| 250 |
+
{info}
|
| 251 |
+
<br>
|
| 252 |
+
```cypher
|
| 253 |
+
{sf_nj__sql2cql.replace("\n", ";")}
|
| 254 |
+
```
|
| 255 |
+
"""
|
| 256 |
+
return dedent(queryinfo)
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
var__neo4jRAG = gr.ChatInterface(func__neo4jRAG, title="neo4jRAG")
|
| 260 |
+
var__neo4jRAG.launch()
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
python-dotenv==1.2.1
|
| 2 |
requests==2.32.5
|
| 3 |
-
pandas==2.3.3
|
| 4 |
neo4j==6.0.3
|
| 5 |
-
snowflake-connector-python==4.1.0
|
| 6 |
gradio==6.0.0
|
|
|
|
| 1 |
python-dotenv==1.2.1
|
| 2 |
requests==2.32.5
|
|
|
|
| 3 |
neo4j==6.0.3
|
| 4 |
+
snowflake-connector-python[pandas]==4.1.0
|
| 5 |
gradio==6.0.0
|