WebashalarForML commited on
Commit
9c7c267
·
verified ·
1 Parent(s): 7df743a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -31,6 +31,10 @@ from langgraph.prebuilt import ToolNode
31
  # Load environment variables
32
  load_dotenv()
33
 
 
 
 
 
34
  # Set up the DB URI using an environment variable.
35
  # In your .env file, ensure you have:
36
  # DATABASE_URI=sqlite:///employee.db
@@ -46,16 +50,6 @@ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
46
  from langchain_groq import ChatGroq
47
  llm = ChatGroq(model="llama3-70b-8192")
48
 
49
- # Connect to the provided database URI using SQLDatabase (which expects a URI)
50
- #from langchain_community.utilities import SQLDatabase
51
- #db = SQLDatabase.from_uri(DATABASE_URI)
52
-
53
- # Create SQL toolkit and get the tools
54
- #from langchain_community.agent_toolkits import SQLDatabaseToolkit
55
- #toolkit = SQLDatabaseToolkit(db=db, llm=llm)
56
- #tools = toolkit.get_tools()
57
-
58
- # Define a custom query tool for executing SQL queries
59
  @tool
60
  def db_query_tool(query: str) -> str:
61
  """
@@ -63,7 +57,7 @@ def db_query_tool(query: str) -> str:
63
  If the query is invalid or returns no result, an error message will be returned.
64
  In case of an error, the user is advised to rewrite the query and try again.
65
  """
66
- result = db.run_no_throw(query)
67
  if not result:
68
  return "Error: Query failed. Please rewrite your query and try again."
69
  return result
@@ -213,6 +207,24 @@ def create_agent_app(db_path: str):
213
  # Compile and return the agent application workflow.
214
  return workflow.compile()
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  ###############################################################################
217
  # Application Factory: create_app()
218
  #
@@ -238,6 +250,7 @@ def create_app():
238
 
239
  @flask_app.route("/generate", methods=["POST"])
240
  def generate():
 
241
  data = request.json
242
  prompt = data.get("prompt", "")
243
  socketio.emit("log", {"message": f"[INFO]: Received prompt: {prompt}\n"})
@@ -267,20 +280,6 @@ def create_app():
267
 
268
  return flask_app, socketio
269
 
270
- ###############################################################################
271
- # Helper function to run the agent; uses the global agent_app.
272
- ###############################################################################
273
- def run_agent(prompt, socketio):
274
- try:
275
- query = {"messages": [("user", prompt)]}
276
- result = agent_app.invoke(query)
277
- result = result["messages"][-1].tool_calls[0]["args"]["final_answer"]
278
- print("final_answer------>", result)
279
- socketio.emit("final", {"message": f"{result}"})
280
- except Exception as e:
281
- socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
282
- socketio.emit("final", {"message": "Generation failed."})
283
-
284
  # Create the app and assign to "app" for Gunicorn compatibility.
285
  app, socketio_instance = create_app()
286
 
 
31
  # Load environment variables
32
  load_dotenv()
33
 
34
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
35
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
36
+ os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
37
+
38
  # Set up the DB URI using an environment variable.
39
  # In your .env file, ensure you have:
40
  # DATABASE_URI=sqlite:///employee.db
 
50
  from langchain_groq import ChatGroq
51
  llm = ChatGroq(model="llama3-70b-8192")
52
 
 
 
 
 
 
 
 
 
 
 
53
  @tool
54
  def db_query_tool(query: str) -> str:
55
  """
 
57
  If the query is invalid or returns no result, an error message will be returned.
58
  In case of an error, the user is advised to rewrite the query and try again.
59
  """
60
+ result = db_instance.run_no_throw(query)
61
  if not result:
62
  return "Error: Query failed. Please rewrite your query and try again."
63
  return result
 
207
  # Compile and return the agent application workflow.
208
  return workflow.compile()
209
 
210
+ ###############################################################################
211
+ # Helper function to run the agent; uses the global agent_app.
212
+ ###############################################################################
213
+ def run_agent(prompt, socketio):
214
+ try:
215
+ query = {"messages": [("user", prompt)]}
216
+ result = agent_app.invoke(query)
217
+ try:
218
+ result = result["messages"][-1].tool_calls[0]["args"]["final_answer"]
219
+ except Exception:
220
+ result = "Query failed or no valid answer found."
221
+
222
+ print("final_answer------>", result)
223
+ socketio.emit("final", {"message": f"{result}"})
224
+ except Exception as e:
225
+ socketio.emit("log", {"message": f"[ERROR]: {str(e)}"})
226
+ socketio.emit("final", {"message": "Generation failed."})
227
+
228
  ###############################################################################
229
  # Application Factory: create_app()
230
  #
 
250
 
251
  @flask_app.route("/generate", methods=["POST"])
252
  def generate():
253
+ socketio.emit("log", {"message": "[STEP]: Entering query_gen..."})
254
  data = request.json
255
  prompt = data.get("prompt", "")
256
  socketio.emit("log", {"message": f"[INFO]: Received prompt: {prompt}\n"})
 
280
 
281
  return flask_app, socketio
282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  # Create the app and assign to "app" for Gunicorn compatibility.
284
  app, socketio_instance = create_app()
285