GithubHelp home page GithubHelp logo

ValueError: An output parsing error occurred. In order to pass this error back to the agent and have it try again, pass `handle_parsing_errors=True` to the AgentExecutor. This is the error: Could not parse tool input: {'arguments': "# First, let's list the tables in the database\nfrom functions import sql_db_list_tables\n\ntables = sql_db_list_tables({})\nprint(tables)", 'name': 'python'} because the `arguments` is not valid JSON. about advanced-qa-and-rag-series HOT 4 CLOSED

charles-123456 avatar charles-123456 commented on July 18, 2024
ValueError: An output parsing error occurred. In order to pass this error back to the agent and have it try again, pass `handle_parsing_errors=True` to the AgentExecutor. This is the error: Could not parse tool input: {'arguments': "# First, let's list the tables in the database\nfrom functions import sql_db_list_tables\n\ntables = sql_db_list_tables({})\nprint(tables)", 'name': 'python'} because the `arguments` is not valid JSON.

from advanced-qa-and-rag-series.

Comments (4)

Farzad-R avatar Farzad-R commented on July 18, 2024

Hi Charles, let's go step by step. First I see an issue in loading the dataframe:

df = pd.read_csv(here(r"D:\Python\csv_data_yani\data\titanic.csv"))

You are using an absolute path in here() function. So, this is not how it works. if you check in your repository, there is a file name ".here". here() function automatically detects that file and adds that to your relative paths. So, I use here to avoid the need for using the absolute path. You can either use:

df = pd.read_csv(r"D:\Python\csv_data_yani\data\titanic.csv")

or in case you put the titanic in the data folder in the project:

df = pd.read_csv(here("data\titanic.csv"))

Please modify this part and start executing the code piece by piece in a notebook and let me know if the error can be fixed or it still persists.

from advanced-qa-and-rag-series.

Farzad-R avatar Farzad-R commented on July 18, 2024

I replicated the code and the following code works fine for me:
Activate the environment:

pip install pandas pyprojroot python-dotenv langchain sqlalchemy langchain_community

code:

import pandas as pd
from pyprojroot import here
from langchain_community.utilities import SQLDatabase
from sqlalchemy import create_engine
import os
from langchain.chat_models import AzureChatOpenAI
from dotenv import load_dotenv
load_dotenv()

df = pd.read_csv(here("data/for_upload/titanic.csv"))
print(df.shape)
print(df.columns.tolist())
display(df.head(3))

db_path = here("data/test.db")
db_path = f"sqlite:///{db_path}"
engine = create_engine(db_path)

df.to_sql("titanic", engine, index=False)
db = SQLDatabase(engine=engine)
print(db.dialect)
print(db.get_usable_table_names())
db.run("SELECT * FROM titanic WHERE Age < 2;")

# Load the LLM
model_name = os.getenv("gpt_deployment_name")
azure_openai_api_key = os.environ["OPENAI_API_KEY"]
azure_openai_endpoint = os.environ["OPENAI_API_BASE"]
llm = AzureChatOpenAI(
    openai_api_version=os.getenv("OPENAI_API_VERSION"),
    azure_deployment=model_name,
    model_name=model_name,
    temperature=0.0)

from langchain_community.agent_toolkits import create_sql_agent
agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True,handle_parsing_errors=True)

agent_executor.invoke({"input": "Tell me more about Anders Johan Andersson"})

Result:

> Entering new SQL Agent Executor chain...
Invoking: `sql_db_list_tables` with `{}`
titanic
Invoking: `sql_db_schema` with `{'table_names': 'titanic'}`
CREATE TABLE titanic (
	"Survived" BIGINT, 
	"Pclass" BIGINT, 
	"Name" TEXT, 
	"Sex" TEXT, 
	"Age" FLOAT, 
	"Siblings/Spouses Aboard" BIGINT, 
	"Parents/Children Aboard" BIGINT, 
	"Fare" FLOAT
)

/*
3 rows from titanic table:
...

[(0, 3, 'Mr. Anders Johan Andersson', 'male', 39.0, 1, 5, 31.275)]Anders Johan Andersson was a male passenger on the Titanic. He was 39 years old and traveled in third class. He had 1 sibling/spouse aboard and 5 parents/children aboard. The fare he paid was 31.275.

> Finished chain.
Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?17ddf478-f1bf-4af5-9506-136cb993c78c) or open in a [text editor](command:workbench.action.openLargeOutput?17ddf478-f1bf-4af5-9506-136cb993c78c). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...

{'input': 'Tell me more about Anders Johan Andersson',
 'output': 'Anders Johan Andersson was a male passenger on the Titanic. He was 39 years old and traveled in third class. He had 1 sibling/spouse aboard and 5 parents/children aboard. The fare he paid was 31.275.'}

from advanced-qa-and-rag-series.

Farzad-R avatar Farzad-R commented on July 18, 2024

Copy and paste the code into a notebook and execute it part by part to understand which part is throwing an error.

from advanced-qa-and-rag-series.

Farzad-R avatar Farzad-R commented on July 18, 2024

I will close the issue.

from advanced-qa-and-rag-series.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.