langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.05k stars 14.65k forks source link

python tool issue: NameError: name 're' is not defined #7009

Closed twtdata closed 11 months ago

twtdata commented 1 year ago

System Info

I am using a create_pandas_dataframe_agent like so:

create_pandas_dataframe_agent(ChatOpenAI(model_name='gpt-4', temperature=0), df, verbose=True,
                                         max_iterations=2,
                                         early_stopping_method="generate",
                                         handle_parsing_errors="I'm sorry, but the question you have asked is not available within the dataset. Is there anything else I can help you with?")

When I try to prompt it to do anything that requires a regex the output i get:

Thought: The client wants to know the top 5 hashtags used in the tweets. To find this, I need to extract all the hashtags from the 'full_text' column of the dataframe, count the frequency of each hashtag, and then return the top 5. However, the dataframe does not contain a column for hashtags. Therefore, I need to create a new column 'hashtags' in the dataframe where each entry is a list of hashtags found in the corresponding 'full_text'. Then, I can count the frequency of each hashtag in this new column and return the top 5.

Action: python_repl_ast
Action Input:
```python
import re
import pandas as pd

# Function to extract hashtags from a text
def extract_hashtags(text):
    return re.findall(r'\#\w+', text)

# Create a new column 'hashtags' in the dataframe
df['hashtags'] = df['full_text'].apply(extract_hashtags)

# Flatten the list of hashtags and count the frequency of each hashtag
hashtag_counts = pd.Series([hashtag for hashtags in df['hashtags'] for hashtag in hashtags]).value_counts()

# Get the top 5 hashtags
top_5_hashtags = hashtag_counts.head(5)

top_5_hashtags

Observation: NameError: name 're' is not defined



Can anyone help?

### Who can help?

_No response_

### Information

- [ ] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [X] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

use this agent:
create_pandas_dataframe_agent(ChatOpenAI(model_name='gpt-4', temperature=0), df, verbose=True,
                                         max_iterations=2,
                                         early_stopping_method="generate",
                                         handle_parsing_errors="I'm sorry, but the question you have asked is not available within the dataset. Is there anything else I can help you with?")

then prompt it with

top 5 hashtags used

### Expected behavior

the regex should work and return the answer
HashemAlsaket commented 1 year ago

I was able to reproduce your error using this dataset: https://www.kaggle.com/datasets/eliasdabbas/twitter_dataframe?select=python_tweets.csv

The problem is not one of the API but of how the prompting is done. The prompt is long, abstract and reads a bit more like a statement than an order to the model. Using the regex code provided above, the counts return as:

`

python 456

Python 334

UI 148

CSS 122

HTML 119

`

With the new prompt First, using regex, make a column called 'hashtags' of a list of hashtags extracted from the 'tweet_full_text' column. Then, return the top 5 hashtags in the dataframe by frequency and show the frequency. we see:

`

Entering new chain... Thought: To extract the hashtags from the 'tweet_full_text' column, I can use the str.findall() method with a regex pattern. Then, I can use the value_counts() method to get the frequency of each hashtag and return the top 5.

Action: python_repl_ast Action Input: df['hashtags'] = df['tweet_full_text'].str.findall(r'#\w+') Observation: Thought:I have created a new column called 'hashtags' in the dataframe df which contains a list of hashtags extracted from the 'tweet_full_text' column.

Action: python_repl_ast Action Input: df['hashtags'].value_counts().head(5) Observation: [#CSharp, #HTML, #CSS, #python, #xcode, #SQL, #React, #PHP, #JavaScript, #swift, #ObjectiveC, #UI, #UX] 108 [#Python] 89 [] 63 [#python] 46 [#MachineLearning, #Python] 35 Name: hashtags, dtype: int64 Thought:Final Answer: The top 5 hashtags in the dataframe by frequency are:

  1. [#CSharp, #HTML, #CSS, #python, #xcode, #SQL, #React, #PHP, #JavaScript, #swift, #ObjectiveC, #UI, #UX] - 108 occurrences
  2. [#Python] - 89 occurrences
  3. [] - 63 occurrences
  4. [#python] - 46 occurrences
  5. [#MachineLearning, #Python] - 35 occurrences

Finished chain. 'The top 5 hashtags in the dataframe by frequency are:\n1. [#CSharp, #HTML, #CSS, #python, #xcode, #SQL, #React, #PHP, #JavaScript, #swift, #ObjectiveC, #UI, #UX] - 108 occurrences\n2. [#Python] - 89 occurrences\n3. [] - 63 occurrences\n4. [#python] - 46 occurrences\n5. [#MachineLearning, #Python] - 35 occurrences' `

The counts don't match but changing the prompt can serve as a good guide.

dosubot[bot] commented 11 months ago

Hi, @twtdata! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, the issue you reported is about the re module not being defined when trying to use regular expressions in a Python tool. User HashemAlsaket provided a dataset to reproduce the error and suggested that the problem might be with how the prompting is done. They also proposed changing the prompt as a possible solution. User batmanscode confirmed this solution by giving a thumbs up reaction to the comment.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository!