Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template
|
| 2 |
from flask_socketio import SocketIO, emit
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
-
from langchain.agents import initialize_agent, AgentType
|
|
|
|
| 5 |
from langchain_community.agent_toolkits import create_sql_agent, SQLDatabaseToolkit
|
|
|
|
| 6 |
from langchain_community.utilities import SQLDatabase
|
| 7 |
from langchain.tools import Tool
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
|
@@ -168,14 +170,25 @@ def init_sql_agent_from_uri(sql_uri: str):
|
|
| 168 |
- Return code or tool names.
|
| 169 |
- Give wrong Answers.
|
| 170 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
agent = create_sql_agent(
|
| 172 |
llm=llm,
|
| 173 |
toolkit=toolkit,
|
| 174 |
verbose=True,
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
agent_executor = agent
|
| 181 |
db_mode = 'sql'
|
|
@@ -377,15 +390,49 @@ def init_mongo_agent_from_uri(mongo_uri: str, database_name: str = None):
|
|
| 377 |
# - User: "How many users registered this month?"
|
| 378 |
# Assistant: Use AggregateGroupBy and summarize results in a clear sentence.
|
| 379 |
# """
|
| 380 |
-
agent = initialize_agent(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
tools=tools,
|
| 382 |
-
llm=llm,
|
| 383 |
-
agent_type=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
|
| 384 |
memory=memory,
|
| 385 |
verbose=True,
|
| 386 |
-
|
| 387 |
-
handle_parsing_errors=True
|
| 388 |
)
|
|
|
|
| 389 |
agent_executor = agent
|
| 390 |
db_mode = 'mongo'
|
| 391 |
logger.info('Mongo agent initialized')
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template
|
| 2 |
from flask_socketio import SocketIO, emit
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
# from langchain.agents import initialize_agent, AgentType
|
| 5 |
+
from langchain.agents import AgentExecutor, create_react_agent
|
| 6 |
from langchain_community.agent_toolkits import create_sql_agent, SQLDatabaseToolkit
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
from langchain_community.utilities import SQLDatabase
|
| 9 |
from langchain.tools import Tool
|
| 10 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 170 |
- Return code or tool names.
|
| 171 |
- Give wrong Answers.
|
| 172 |
'''
|
| 173 |
+
# agent = create_sql_agent(
|
| 174 |
+
# llm=llm,
|
| 175 |
+
# toolkit=toolkit,
|
| 176 |
+
# verbose=True,
|
| 177 |
+
# prefix=prefix,
|
| 178 |
+
# agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 179 |
+
# memory=memory,
|
| 180 |
+
# agent_executor_kwargs={"handle_parsing_errors": True},
|
| 181 |
+
# )
|
| 182 |
agent = create_sql_agent(
|
| 183 |
llm=llm,
|
| 184 |
toolkit=toolkit,
|
| 185 |
verbose=True,
|
| 186 |
+
agent_type="openai-tools", # modern default; or "zero-shot-react-description" as string
|
| 187 |
+
agent_executor_kwargs={
|
| 188 |
+
"handle_parsing_errors": True,
|
| 189 |
+
"memory": memory,
|
| 190 |
+
},
|
| 191 |
+
extra_tools=[],
|
| 192 |
)
|
| 193 |
agent_executor = agent
|
| 194 |
db_mode = 'sql'
|
|
|
|
| 390 |
# - User: "How many users registered this month?"
|
| 391 |
# Assistant: Use AggregateGroupBy and summarize results in a clear sentence.
|
| 392 |
# """
|
| 393 |
+
# agent = initialize_agent(
|
| 394 |
+
# tools=tools,
|
| 395 |
+
# llm=llm,
|
| 396 |
+
# agent_type=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
|
| 397 |
+
# memory=memory,
|
| 398 |
+
# verbose=True,
|
| 399 |
+
# prefix=prefix,
|
| 400 |
+
# handle_parsing_errors=True
|
| 401 |
+
# )
|
| 402 |
+
|
| 403 |
+
from langchain_core.prompts import PromptTemplate
|
| 404 |
+
|
| 405 |
+
prompt_template = PromptTemplate.from_template(
|
| 406 |
+
prefix + """
|
| 407 |
+
You have access to the following tools:
|
| 408 |
+
{tools}
|
| 409 |
+
|
| 410 |
+
Use the following format:
|
| 411 |
+
Question: the input question you must answer
|
| 412 |
+
Thought: you should always think about what to do
|
| 413 |
+
Action: the action to take, should be one of [{tool_names}]
|
| 414 |
+
Action Input: the input to the action
|
| 415 |
+
Observation: the result of the action
|
| 416 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 417 |
+
Thought: I now know the final answer
|
| 418 |
+
Final Answer: the final answer to the original input question
|
| 419 |
+
|
| 420 |
+
Previous conversation history:
|
| 421 |
+
{chat_history}
|
| 422 |
+
|
| 423 |
+
Question: {input}
|
| 424 |
+
{agent_scratchpad}"""
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
react_agent = create_react_agent(llm=llm, tools=tools, prompt=prompt_template)
|
| 428 |
+
agent = AgentExecutor(
|
| 429 |
+
agent=react_agent,
|
| 430 |
tools=tools,
|
|
|
|
|
|
|
| 431 |
memory=memory,
|
| 432 |
verbose=True,
|
| 433 |
+
handle_parsing_errors=True,
|
|
|
|
| 434 |
)
|
| 435 |
+
|
| 436 |
agent_executor = agent
|
| 437 |
db_mode = 'mongo'
|
| 438 |
logger.info('Mongo agent initialized')
|