Libraries¶
In [7]:
import os
import pandas as pd
import matplotlib.pyplot as matplot
import seaborn as sns
from langchain_classic.agents.agent_types import AgentType
from langchain_community.utilities import SQLDatabase
from langchain_community.llms import HuggingFaceHub
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_classic.agents.agent_toolkits.conversational_retrieval.openai_functions import create_conversational_retrieval_agent
from langchain_classic.agents.agent_toolkits.conversational_retrieval.tool import create_retriever_tool
from langchain_classic import hub
from langchain_core.runnables import RunnablePassthrough
from langchain_community.agent_toolkits.load_tools import load_tools
import sqlite3
# Gemini replacements for the two OpenAI-specific lines:
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_google_genai import GoogleGenerativeAIEmbeddings
Gemini Key¶
In [20]:
import getpass
os.environ["GEMINI_API_KEY"]=getpass.getpass("Enter Your Gemini API Key")
In [21]:
key=os.environ["GEMINI_API_KEY"]
print(f"Key loaded {len(key)} characters, key start with {key[:6]}...")
Key loaded 53 characters, key start with AQ.Ab8...
In [27]:
os.environ["GOOGLE_API_KEY"]=os.environ["GEMINI_API_KEY"]
In [28]:
from langchain_google_genai import ChatGoogleGenerativeAI
llm=ChatGoogleGenerativeAI(model="gemini-3.8-flash", temperature=0)
Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.
In [29]:
response=llm.invoke("say hello in one short sentence")
print(response.content)
[{'type': 'text', 'text': 'Hello, I hope you are having a wonderful day!', 'extras': {'signature': 'EuEHCt4HAWkUfROihfgrlAOdAgipNOvwM4YKPbwPXeWG5a3CNKGIkBeW+hemzUA1B+7NIWmBwOQ3LJynZXx5PcsuJSQgWdW4nwbOfvGNSD9pEFXZ/JFmY0QMIIga6N9YMs+P4SjCMpkEVB2jb3RHHNT99aOu9Pabd+BfezV68t1qycPR+2Aauc0yLqfi/AosXu9Wc9kSijZjYzulit9njzu9ecAmYHj1m/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ASLIbXfRjZyfDlbEKkxT7GqJq'}}]
In [31]:
from langchain_experimental.agents import create_pandas_dataframe_agent
C:\Users\seena.joseph\AppData\Local\Temp\ipykernel_31948\1608223864.py:1: DeprecationWarning: `langchain-experimental` is being sunset and is no longer actively maintained. See https://github.com/langchain-ai/langchain-experimental/issues/87 for details. from langchain_experimental.agents import create_pandas_dataframe_agent
In [33]:
df = pd.read_csv("SAHeartData.txt")
print(df.shape)
print(df.columns.tolist())
df.head()
(462, 11) ['row.names', 'sbp', 'tobacco', 'ldl', 'adiposity', 'famhist', 'typea', 'obesity', 'alcohol', 'age', 'chd']
Out[33]:
| row.names | sbp | tobacco | ldl | adiposity | famhist | typea | obesity | alcohol | age | chd | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 160 | 12.00 | 5.73 | 23.11 | Present | 49 | 25.30 | 97.20 | 52 | 1 |
| 1 | 2 | 144 | 0.01 | 4.41 | 28.61 | Absent | 55 | 28.87 | 2.06 | 63 | 1 |
| 2 | 3 | 118 | 0.08 | 3.48 | 32.28 | Present | 52 | 29.14 | 3.81 | 46 | 0 |
| 3 | 4 | 170 | 7.50 | 6.41 | 38.03 | Present | 51 | 31.99 | 24.26 | 58 | 1 |
| 4 | 5 | 134 | 13.60 | 3.50 | 27.78 | Present | 60 | 25.99 | 57.34 | 49 | 1 |
In [34]:
agent = create_pandas_dataframe_agent(llm, df, verbose=True, allow_dangerous_code=True, handle_parsing_errors=True, max_iterations=15, max_execution_time=240.0)
C:\Users\seena.joseph\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_experimental\agents\agent_toolkits\pandas\base.py:283: UserWarning: Received additional kwargs {'handle_parsing_errors': True} which are no longer supported.
warnings.warn(
In [35]:
response = agent.invoke("How many rows and columns are in this dataset?")
print(response)
> Entering new AgentExecutor chain... Thought: you should always think about what to do Action: python_repl_ast Action Input: df.shape(462, 11)I now know the final answer Final Answer: There are 462 rows and 11 columns in this dataset. > Finished chain. {'input': 'How many rows and columns are in this dataset?', 'output': 'There are 462 rows and 11 columns in this dataset.'}
In [36]:
response = agent.invoke("What are the column names in this dataset, and what does each one look like it represents?")
print(response)
> Entering new AgentExecutor chain... Thought: I should check the column names of the dataframe `df`. Action: python_repl_ast Action Input: df.columns.tolist()['row.names', 'sbp', 'tobacco', 'ldl', 'adiposity', 'famhist', 'typea', 'obesity', 'alcohol', 'age', 'chd']
--------------------------------------------------------------------------- OutputParserException Traceback (most recent call last) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\agents\agent.py:1317, in AgentExecutor._iter_next_step(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager) 1316 # Call the LLM to see what to do. -> 1317 output = self._action_agent.plan( 1318 intermediate_steps, 1319 callbacks=run_manager.get_child() if run_manager else None, 1320 **inputs, 1321 ) 1322 except OutputParserException as e: File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\agents\agent.py:445, in RunnableAgent.plan(self, intermediate_steps, callbacks, **kwargs) 438 if self.stream_runnable: 439 # Use streaming to make sure that the underlying LLM is invoked in a 440 # streaming (...) 443 # Because the response from the plan is not a generator, we need to 444 # accumulate the output into final output and return that. --> 445 for chunk in self.runnable.stream(inputs, config={"callbacks": callbacks}): 446 if final_output is None: File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:3837, in RunnableSequence.stream(self, input, config, **kwargs) 3830 @override 3831 def stream( 3832 self, (...) 3835 **kwargs: Any | None, 3836 ) -> Iterator[Output]: -> 3837 yield from self.transform(iter([input]), config, **kwargs) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:3823, in RunnableSequence.transform(self, input, config, **kwargs) 3816 @override 3817 def transform( 3818 self, (...) 3821 **kwargs: Any | None, 3822 ) -> Iterator[Output]: -> 3823 yield from self._transform_stream_with_config( 3824 input, 3825 self._transform, 3826 patch_config(config, run_name=(config or {}).get("run_name") or self.name), 3827 **kwargs, 3828 ) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:2571, in Runnable._transform_stream_with_config(self, inputs, transformer, config, run_type, **kwargs) 2570 while True: -> 2571 chunk: Output = context.run(next, iterator) 2572 yield chunk File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:3789, in RunnableSequence._transform(self, inputs, run_manager, config, **kwargs) 3787 final_pipeline = step.transform(final_pipeline, config) -> 3789 yield from final_pipeline File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:1804, in Runnable.transform(self, input, config, **kwargs) 1803 if got_first_val: -> 1804 yield from self.stream(final, config, **kwargs) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:1214, in Runnable.stream(self, input, config, **kwargs) 1201 """Default implementation of `stream`, which calls `invoke`. 1202 1203 Subclasses must override this method if they support streaming output. (...) 1212 1213 """ -> 1214 yield self.invoke(input, config, **kwargs) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\output_parsers\base.py:211, in BaseOutputParser.invoke(self, input, config, **kwargs) 210 if isinstance(input, BaseMessage): --> 211 return self._call_with_config( 212 lambda inner_input: self.parse_result( 213 [ChatGeneration(message=inner_input)] 214 ), 215 input, 216 config, 217 run_type="parser", 218 ) 219 return self._call_with_config( 220 lambda inner_input: self.parse_result([Generation(text=inner_input)]), 221 input, 222 config, 223 run_type="parser", 224 ) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\base.py:2305, in Runnable._call_with_config(self, func, input_, config, run_type, serialized, **kwargs) 2302 with set_config_context(child_config) as context: 2303 output = cast( 2304 "Output", -> 2305 context.run( 2306 call_func_with_variable_args, # type: ignore[arg-type] 2307 func, 2308 input_, 2309 config, 2310 run_manager, 2311 **kwargs, 2312 ), 2313 ) 2314 except BaseException as e: File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\runnables\config.py:526, in call_func_with_variable_args(func, input, config, run_manager, **kwargs) 525 kwargs["run_manager"] = run_manager --> 526 return func(input, **kwargs) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\output_parsers\base.py:212, in BaseOutputParser.invoke.<locals>.<lambda>(inner_input) 210 if isinstance(input, BaseMessage): 211 return self._call_with_config( --> 212 lambda inner_input: self.parse_result( 213 [ChatGeneration(message=inner_input)] 214 ), 215 input, 216 config, 217 run_type="parser", 218 ) 219 return self._call_with_config( 220 lambda inner_input: self.parse_result([Generation(text=inner_input)]), 221 input, 222 config, 223 run_type="parser", 224 ) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_core\output_parsers\base.py:268, in BaseOutputParser.parse_result(self, result, partial) 251 """Parse a list of candidate model `Generation` objects into a specific format. 252 253 The return value is parsed from only the first `Generation` in the result, which (...) 266 Structured output. 267 """ --> 268 return self.parse(result[0].text) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\agents\output_parsers\react_single_input.py:76, in ReActSingleInputOutputParser.parse(self, text) 75 msg = f"Could not parse LLM output: `{text}`" ---> 76 raise OutputParserException( 77 msg, 78 observation=MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE, 79 llm_output=text, 80 send_to_llm=True, 81 ) 82 if not re.search( 83 r"[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)", 84 text, 85 re.DOTALL, 86 ): OutputParserException: Could not parse LLM output: `The column names in the dataset and what each represents are as follows: 1. **`row.names`**: An identifier or index number for each observation/subject. 2. **`sbp`**: Systolic Blood Pressure (a measure of blood pressure in mmHg). 3. **`tobacco`**: Cumulative tobacco consumption (typically in kilograms). 4. **`ldl`**: Low-Density Lipoprotein cholesterol (often referred to as "bad" cholesterol). 5. **`adiposity`**: A numeric measure of body fat percentage or adiposity index. 6. **`famhist`**: Family history of heart disease (categorical: 'Present' or 'Absent'). 7. **`typea`**: Type-A behavior pattern score (a psychological measure related to stress/personality). 8. **`obesity`**: Body mass index (BMI) or obesity index. 9. **`alcohol`**: Current alcohol consumption. 10. **`age`**: Age of the individual in years. 11. **`chd`**: Coronary Heart Disease status (binary indicator: 1 = diagnosed/present, 0 = absent).` For troubleshooting, visit: https://docs.langchain.com/oss/python/langchain/errors/OUTPUT_PARSING_FAILURE The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) Cell In[36], line 1 ----> 1 response = agent.invoke("What are the column names in this dataset, and what does each one look like it represents?") 2 print(response) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\chains\base.py:167, in Chain.invoke(self, input, config, **kwargs) 164 try: 165 self._validate_inputs(inputs) 166 outputs = ( --> 167 self._call(inputs, run_manager=run_manager) 168 if new_arg_supported 169 else self._call(inputs) 170 ) 172 final_outputs: dict[str, Any] = self.prep_outputs( 173 inputs, 174 outputs, 175 return_only_outputs, 176 ) 177 except BaseException as e: File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\agents\agent.py:1590, in AgentExecutor._call(self, inputs, run_manager) 1588 # We now enter the agent loop (until it returns something). 1589 while self._should_continue(iterations, time_elapsed): -> 1590 next_step_output = self._take_next_step( 1591 name_to_tool_map, 1592 color_mapping, 1593 inputs, 1594 intermediate_steps, 1595 run_manager=run_manager, 1596 ) 1597 if isinstance(next_step_output, AgentFinish): 1598 return self._return( 1599 next_step_output, 1600 intermediate_steps, 1601 run_manager=run_manager, 1602 ) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\agents\agent.py:1290, in AgentExecutor._take_next_step(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager) 1281 def _take_next_step( 1282 self, 1283 name_to_tool_map: dict[str, BaseTool], (...) 1287 run_manager: CallbackManagerForChainRun | None = None, 1288 ) -> AgentFinish | list[tuple[AgentAction, str]]: 1289 return self._consume_next_step( -> 1290 list( 1291 self._iter_next_step( 1292 name_to_tool_map, 1293 color_mapping, 1294 inputs, 1295 intermediate_steps, 1296 run_manager, 1297 ), 1298 ), 1299 ) File ~\AppData\Local\anaconda3\envs\gl-agent\Lib\site-packages\langchain_classic\agents\agent.py:1334, in AgentExecutor._iter_next_step(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager) 1327 if raise_error: 1328 msg = ( 1329 "An output parsing error occurred. " 1330 "In order to pass this error back to the agent and have it try " 1331 "again, pass `handle_parsing_errors=True` to the AgentExecutor. " 1332 f"This is the error: {e!s}" 1333 ) -> 1334 raise ValueError(msg) from e 1335 text = str(e) 1336 if isinstance(self.handle_parsing_errors, bool): 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 LLM output: `The column names in the dataset and what each represents are as follows: 1. **`row.names`**: An identifier or index number for each observation/subject. 2. **`sbp`**: Systolic Blood Pressure (a measure of blood pressure in mmHg). 3. **`tobacco`**: Cumulative tobacco consumption (typically in kilograms). 4. **`ldl`**: Low-Density Lipoprotein cholesterol (often referred to as "bad" cholesterol). 5. **`adiposity`**: A numeric measure of body fat percentage or adiposity index. 6. **`famhist`**: Family history of heart disease (categorical: 'Present' or 'Absent'). 7. **`typea`**: Type-A behavior pattern score (a psychological measure related to stress/personality). 8. **`obesity`**: Body mass index (BMI) or obesity index. 9. **`alcohol`**: Current alcohol consumption. 10. **`age`**: Age of the individual in years. 11. **`chd`**: Coronary Heart Disease status (binary indicator: 1 = diagnosed/present, 0 = absent).` For troubleshooting, visit: https://docs.langchain.com/oss/python/langchain/errors/OUTPUT_PARSING_FAILURE
In [ ]: