Spaces:
Runtime error
Runtime error
Commit ·
e7c1910
1
Parent(s): 9f78bd3
Upload 4 files
Browse files- app.py +6 -2
- configProd.py +2 -2
- gptManager.py +16 -9
app.py
CHANGED
|
@@ -58,7 +58,10 @@ def respond(message, chatHistory):
|
|
| 58 |
"""gpt response handler for gradio ui"""
|
| 59 |
global queryHelper
|
| 60 |
try:
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
errorMessage = {"function":"queryHelper.getQueryForUserInput","error":str(e), "userInput":message}
|
| 64 |
saveLog(errorMessage, 'error')
|
|
@@ -91,7 +94,8 @@ def testSQL(sql):
|
|
| 91 |
if not isDataQuery(sql):
|
| 92 |
return "Sorry not allowed to run. As the query modifies the data."
|
| 93 |
try:
|
| 94 |
-
|
|
|
|
| 95 |
df = pd.read_sql_query(sql, con=conn)
|
| 96 |
return disclaimerOutputStripping + str(pd.DataFrame(df))
|
| 97 |
except Exception as e:
|
|
|
|
| 58 |
"""gpt response handler for gradio ui"""
|
| 59 |
global queryHelper
|
| 60 |
try:
|
| 61 |
+
if "modify" in message:
|
| 62 |
+
botMessage, prospectTablesAndCols = queryHelper.getQueryForUserInput(message, chatHistory)
|
| 63 |
+
else:
|
| 64 |
+
botMessage, prospectTablesAndCols = queryHelper.getQueryForUserInput(message, [])
|
| 65 |
except Exception as e:
|
| 66 |
errorMessage = {"function":"queryHelper.getQueryForUserInput","error":str(e), "userInput":message}
|
| 67 |
saveLog(errorMessage, 'error')
|
|
|
|
| 94 |
if not isDataQuery(sql):
|
| 95 |
return "Sorry not allowed to run. As the query modifies the data."
|
| 96 |
try:
|
| 97 |
+
dbEngine.connect()
|
| 98 |
+
conn = dbEngine.getConnection()
|
| 99 |
df = pd.read_sql_query(sql, con=conn)
|
| 100 |
return disclaimerOutputStripping + str(pd.DataFrame(df))
|
| 101 |
except Exception as e:
|
configProd.py
CHANGED
|
@@ -7,8 +7,8 @@ userDB = os.getenv("user_db")
|
|
| 7 |
pwdDB = os.getenv("pwd_db")
|
| 8 |
host = os.getenv("host_db")
|
| 9 |
port = os.getenv("port_db")
|
| 10 |
-
|
| 11 |
-
GPT_MODEL = "gpt-3.5-turbo-1106"
|
| 12 |
|
| 13 |
HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
|
| 14 |
|
|
|
|
| 7 |
pwdDB = os.getenv("pwd_db")
|
| 8 |
host = os.getenv("host_db")
|
| 9 |
port = os.getenv("port_db")
|
| 10 |
+
GPT_MODEL = "gpt-4"
|
| 11 |
+
# GPT_MODEL = "gpt-3.5-turbo-1106"
|
| 12 |
|
| 13 |
HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
|
| 14 |
|
gptManager.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import re
|
|
|
|
| 2 |
|
| 3 |
class ChatgptManager:
|
| 4 |
-
def __init__(self, openAIClient, model="gpt-3.5-turbo-1106", tokenLimit=8000):
|
| 5 |
self.client = openAIClient
|
| 6 |
self.tokenLimit = tokenLimit
|
| 7 |
self.model = model
|
|
|
|
| 8 |
|
| 9 |
def _chatHistoryToGptMessages(self, chatHistory=[]):
|
| 10 |
messages = []
|
|
@@ -39,14 +41,19 @@ class ChatgptManager:
|
|
| 39 |
# messages=self.messages,
|
| 40 |
# temperature=0,
|
| 41 |
# )
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
self.messages.append({"role": "assistant", "content": gptResponse})
|
| 52 |
return gptResponse
|
|
@@ -71,6 +78,6 @@ class ChatgptManager:
|
|
| 71 |
combinedContent = " ".join(msg["content"] for msg in messages)
|
| 72 |
else:
|
| 73 |
combinedContent = ""
|
| 74 |
-
|
| 75 |
currentTokensInMessages = getWordsCount(combinedContent)
|
| 76 |
return currentTokensInMessages
|
|
|
|
| 1 |
import re
|
| 2 |
+
from persistStorage import saveLog
|
| 3 |
|
| 4 |
class ChatgptManager:
|
| 5 |
+
def __init__(self, openAIClient, model="gpt-3.5-turbo-1106", tokenLimit=8000, throwError=False):
|
| 6 |
self.client = openAIClient
|
| 7 |
self.tokenLimit = tokenLimit
|
| 8 |
self.model = model
|
| 9 |
+
self.throwError = throwError
|
| 10 |
|
| 11 |
def _chatHistoryToGptMessages(self, chatHistory=[]):
|
| 12 |
messages = []
|
|
|
|
| 41 |
# messages=self.messages,
|
| 42 |
# temperature=0,
|
| 43 |
# )
|
| 44 |
+
try:
|
| 45 |
+
completion = self.client.chat.completions.create(
|
| 46 |
+
model=self.model,
|
| 47 |
+
messages=self.messages,
|
| 48 |
+
temperature=0,
|
| 49 |
+
)
|
| 50 |
+
gptResponse = completion.choices[0].message.content
|
| 51 |
+
except Exception as e:
|
| 52 |
+
if not self.throwError:
|
| 53 |
+
gptResponse = "Error while connecting with gpt " + str(e)[:50] + "..."
|
| 54 |
+
|
| 55 |
|
| 56 |
+
|
| 57 |
|
| 58 |
self.messages.append({"role": "assistant", "content": gptResponse})
|
| 59 |
return gptResponse
|
|
|
|
| 78 |
combinedContent = " ".join(msg["content"] for msg in messages)
|
| 79 |
else:
|
| 80 |
combinedContent = ""
|
| 81 |
+
|
| 82 |
currentTokensInMessages = getWordsCount(combinedContent)
|
| 83 |
return currentTokensInMessages
|