Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Extending BaseTool #43

Open
Kahl84 opened this issue May 20, 2023 · 1 comment
Open

Extending BaseTool #43

Kahl84 opened this issue May 20, 2023 · 1 comment

Comments

@Kahl84
Copy link

Kahl84 commented May 20, 2023

I am trying to create a basetool that takes a CSV from google drive and convert to a dataframe. It is literally driving me crazy because I get an error at line 95 but there is no line 95. It must be a problem with Lanchain import but I can't figure out what

`import pandas as pd
import re
import requests
from io import StringIO
from langchain.tools import BaseTool
from langchain.agents import create_pandas_dataframe_agent

class CustomPandaCsvTool(BaseTool):
name = "CustomPandaCsvTool"
description = "Retrieve data from a CSV file in Google Drive and process it with Pandas"
return_direct = True

def __init__(self, llm):
    super().__init__()
    self.llm = llm

def _run(self, link: str) -> pd.DataFrame:
    if not self._is_valid_drive_link(link):
        return pd.DataFrame({
            'error': True,
            'message': "Invalid Google Drive link"
        }, index=[0])

    csv_data = self._download_csv_from_drive(link)
    if not self._is_valid_csv(csv_data):
        return pd.DataFrame({
            'error': True,
            'message': "Invalid CSV data"
        }, index=[0])

    df = pd.read_csv(StringIO(csv_data))
    return create_pandas_dataframe_agent(llm=self.llm, df=df, verbose=True)

@staticmethod
def _is_valid_drive_link(link: str) -> bool:
    return bool(re.match(r"https://drive\.google\.com/.*", link))

@staticmethod
def _download_csv_from_drive(gdrive_link: str) -> StringIO:
    file_id = re.findall(r"/d/([a-zA-Z0-9-_]+)", gdrive_link)
    if not file_id:
        return None

    file_id = file_id[0]
    download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
    response = requests.get(download_url)
    csv_data = StringIO(response.text)
    return csv_data

@staticmethod
def _is_valid_csv(csv_data: StringIO) -> bool:
    try:
        pd.read_csv(csv_data)
        return True
    except pd.errors.ParserError:
        return False`
@SDcodehub
Copy link

can you paste full error trace? that will help to debug

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants