-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathbase.py
479 lines (407 loc) · 15.6 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import json
import os
import re
import uuid
from typing import List, Optional, Union
import pandasai.pandas as pd
from pandasai.agent.base_security import BaseSecurity
from pandasai.llm.bamboo_llm import BambooLLM
from pandasai.pipelines.chat.chat_pipeline_input import ChatPipelineInput
from pandasai.pipelines.chat.code_execution_pipeline_input import (
CodeExecutionPipelineInput,
)
from pandasai.vectorstores.vectorstore import VectorStore
from ..config import load_config_from_json
from ..connectors import BaseConnector, PandasConnector
from ..constants import DEFAULT_CACHE_DIRECTORY, DEFAULT_CHART_DIRECTORY
from ..exceptions import (
InvalidLLMOutputType,
MaliciousQueryError,
MissingVectorStoreError,
)
from ..helpers.df_info import df_type
from ..helpers.folder import Folder
from ..helpers.logger import Logger
from ..helpers.memory import Memory
from ..llm.base import LLM
from ..llm.langchain import LangchainLLM, is_langchain_llm
from ..pipelines.pipeline_context import PipelineContext
from ..prompts.base import BasePrompt
from ..prompts.clarification_questions_prompt import ClarificationQuestionPrompt
from ..prompts.explain_prompt import ExplainPrompt
from ..prompts.rephase_query_prompt import RephraseQueryPrompt
from ..schemas.df_config import Config
from ..skills import Skill
from .callbacks import Callbacks
class BaseAgent:
"""
Base Agent class to improve the conversational experience in PandasAI
"""
def __init__(
self,
dfs: Union[
pd.DataFrame, BaseConnector, List[Union[pd.DataFrame, BaseConnector]]
],
config: Optional[Union[Config, dict]] = None,
memory_size: Optional[int] = 10,
vectorstore: Optional[VectorStore] = None,
description: str = None,
security: BaseSecurity = None,
):
"""
Args:
df (Union[pd.DataFrame, List[pd.DataFrame]]): Pandas or Modin dataframe
Polars or Database connectors
memory_size (int, optional): Conversation history to use during chat.
Defaults to 1.
"""
self.last_prompt = None
self.last_prompt_id = None
self.last_result = None
self.last_code_generated = None
self.last_code_executed = None
self.agent_info = description
self.conversation_id = uuid.uuid4()
self.dfs = self.get_dfs(dfs)
# Instantiate the context
self.config = self.get_config(config)
self.context = PipelineContext(
dfs=self.dfs,
config=self.config,
memory=Memory(memory_size, agent_info=description),
vectorstore=vectorstore,
)
# Instantiate the logger
self.logger = Logger(
save_logs=self.config.save_logs, verbose=self.config.verbose
)
# Instantiate the vectorstore
self._vectorstore = vectorstore
if self._vectorstore is None and os.environ.get("PANDASAI_API_KEY"):
try:
from pandasai.vectorstores.bamboo_vectorstore import BambooVectorStore
except ImportError as e:
raise ImportError(
"Could not import BambooVectorStore. Please install the required dependencies."
) from e
self._vectorstore = BambooVectorStore(logger=self.logger)
self.context.vectorstore = self._vectorstore
self._callbacks = Callbacks(self)
self.configure()
self.pipeline = None
self.security = security
def configure(self):
# Add project root path if save_charts_path is default
if (
self.config.save_charts
and self.config.save_charts_path == DEFAULT_CHART_DIRECTORY
):
Folder.create(self.config.save_charts_path)
# Add project root path if cache_path is default
if self.config.enable_cache:
Folder.create(DEFAULT_CACHE_DIRECTORY)
def get_config(self, config: Union[Config, dict]):
"""
Load a config to be used to run the queries.
Args:
config (Union[Config, dict]): Config to be used
"""
config = load_config_from_json(config)
if isinstance(config, dict) and config.get("llm") is not None:
config["llm"] = self.get_llm(config["llm"])
config = Config(**config)
if config.llm is None:
config.llm = BambooLLM()
return config
def get_llm(self, llm: LLM) -> LLM:
"""
Load a LLM to be used to run the queries.
Check if it is a PandasAI LLM or a Langchain LLM.
If it is a Langchain LLM, wrap it in a PandasAI LLM.
Args:
llm (object): LLMs option to be used for API access
Raises:
BadImportError: If the LLM is a Langchain LLM but the langchain package
is not installed
"""
if is_langchain_llm(llm):
llm = LangchainLLM(llm)
return llm
def get_dfs(
self,
dfs: Union[
pd.DataFrame, BaseConnector, List[Union[pd.DataFrame, BaseConnector]]
],
):
"""
Load all the dataframes to be used in the agent.
Args:
dfs (List[Union[pd.DataFrame, Any]]): Pandas dataframe
"""
# Inline import to avoid circular import
from pandasai.smart_dataframe import SmartDataframe
# If only one dataframe is passed, convert it to a list
if not isinstance(dfs, list):
dfs = [dfs]
connectors = []
for df in dfs:
if isinstance(df, BaseConnector):
connectors.append(df)
elif isinstance(df, (pd.DataFrame, pd.Series, list, dict, str)):
connectors.append(PandasConnector({"original_df": df}))
elif df_type(df) == "modin":
connectors.append(PandasConnector({"original_df": df}))
elif isinstance(df, SmartDataframe) and isinstance(
df.dataframe, BaseConnector
):
connectors.append(df.dataframe)
else:
try:
import polars as pl
if isinstance(df, pl.DataFrame):
from ..connectors.polars import PolarsConnector
connectors.append(PolarsConnector({"original_df": df}))
else:
raise ValueError(
"Invalid input data. We cannot convert it to a dataframe."
)
except ImportError as e:
raise ValueError(
"Invalid input data. We cannot convert it to a dataframe."
) from e
return connectors
def add_skills(self, *skills: Skill):
"""
Add Skills to PandasAI
"""
self.context.skills_manager.add_skills(*skills)
def call_llm_with_prompt(self, prompt: BasePrompt):
"""
Call LLM with prompt using error handling to retry based on config
Args:
prompt (BasePrompt): BasePrompt to pass to LLM's
"""
retry_count = 0
while retry_count < self.context.config.max_retries:
try:
result: str = self.context.config.llm.call(prompt)
if prompt.validate(result):
return result
else:
raise InvalidLLMOutputType("Response validation failed!")
except Exception:
if (
not self.context.config.use_error_correction_framework
or retry_count >= self.context.config.max_retries - 1
):
raise
retry_count += 1
def check_malicious_keywords_in_query(self, query):
dangerous_pattern = re.compile(
r"\b(os|io|chr|b64decode)\b|"
r"(\.os|\.io|'os'|'io'|\"os\"|\"io\"|chr\(|chr\)|chr |\(chr)"
)
return bool(dangerous_pattern.search(query))
def chat(self, query: str, output_type: Optional[str] = None):
"""
Simulate a chat interaction with the assistant on Dataframe.
"""
if not self.pipeline:
return (
"Unfortunately, I was not able to get your answers, "
"because of the following error: No pipeline exists"
)
try:
self.logger.log(f"Question: {query}")
self.logger.log(
f"Running PandasAI with {self.context.config.llm.type} LLM..."
)
self.assign_prompt_id()
if self.config.security in [
"standard",
"advanced",
] and self.check_malicious_keywords_in_query(query):
raise MaliciousQueryError(
"The query contains references to io or os modules or b64decode method which can be used to execute or access system resources in unsafe ways."
)
if self.security and self.security.evaluate(query):
raise MaliciousQueryError("Query can result in a malicious code")
pipeline_input = ChatPipelineInput(
query, output_type, self.conversation_id, self.last_prompt_id
)
return self.pipeline.run(pipeline_input)
except Exception as exception:
return (
"Unfortunately, I was not able to get your answers, "
"because of the following error:\n"
f"\n{exception}\n"
)
def generate_code(self, query: str, output_type: Optional[str] = None):
"""
Simulate code generation with the assistant on Dataframe.
"""
if not self.pipeline:
return (
"Unfortunately, I was not able to get your answers, "
"because of the following error: No pipeline exists"
)
try:
self.logger.log(f"Question: {query}")
self.logger.log(
f"Running PandasAI with {self.context.config.llm.type} LLM..."
)
self.assign_prompt_id()
pipeline_input = ChatPipelineInput(
query, output_type, self.conversation_id, self.last_prompt_id
)
return self.pipeline.run_generate_code(pipeline_input)
except Exception as exception:
return (
"Unfortunately, I was not able to get your answers, "
"because of the following error:\n"
f"\n{exception}\n"
)
def execute_code(
self, code: Optional[str] = None, output_type: Optional[str] = None
):
"""
Execute code Generated with the assistant on Dataframe.
"""
if not self.pipeline:
return (
"Unfortunately, I was not able to get your answers, "
"because of the following error: No pipeline exists to execute try Agent class"
)
try:
if code is None:
code = self.last_code_generated
self.logger.log(f"Code: {code}")
self.logger.log(
f"Running PandasAI with {self.context.config.llm.type} LLM..."
)
self.assign_prompt_id()
pipeline_input = CodeExecutionPipelineInput(
code, output_type, self.conversation_id, self.last_prompt_id
)
return self.pipeline.run_execute_code(pipeline_input)
except Exception as exception:
return (
"Unfortunately, I was not able to get your answers, "
"because of the following error:\n"
f"\n{exception}\n"
)
def train(
self,
queries: Optional[List[str]] = None,
codes: Optional[List[str]] = None,
docs: Optional[List[str]] = None,
) -> None:
"""
Trains the context to be passed to model
Args:
queries (Optional[str], optional): user user
codes (Optional[str], optional): generated code
docs (Optional[List[str]], optional): additional docs
Raises:
ImportError: if default vector db lib is not installed it raises an error
"""
if self._vectorstore is None:
raise MissingVectorStoreError(
"No vector store provided. Please provide a vector store to train the agent."
)
if (queries and not codes) or (not queries and codes):
raise ValueError(
"If either queries or codes are provided, both must be provided."
)
if docs is not None:
self._vectorstore.add_docs(docs)
if queries and codes:
self._vectorstore.add_question_answer(queries, codes)
self.logger.log("Agent successfully trained on the data")
def clear_memory(self):
"""
Clears the memory
"""
self.context.memory.clear()
self.conversation_id = uuid.uuid4()
def add_message(self, message, is_user=False):
"""
Add message to the memory. This is useful when you want to add a message
to the memory without calling the chat function (for example, when you
need to add a message from the agent).
"""
self.context.memory.add(message, is_user=is_user)
def assign_prompt_id(self):
"""Assign a prompt ID"""
self.last_prompt_id = uuid.uuid4()
if self.logger:
self.logger.log(f"Prompt ID: {self.last_prompt_id}")
def clarification_questions(self, query: str) -> List[str]:
"""
Generate clarification questions based on the data
"""
prompt = ClarificationQuestionPrompt(
context=self.context,
query=query,
)
result = self.call_llm_with_prompt(prompt)
self.logger.log(
f"""Clarification Questions: {result}
"""
)
result = result.replace("```json", "").replace("```", "")
questions: list[str] = json.loads(result)
return questions[:3]
def start_new_conversation(self):
"""
Clears the previous conversation
"""
self.clear_memory()
def explain(self) -> str:
"""
Returns the explanation of the code how it reached to the solution
"""
try:
prompt = ExplainPrompt(
context=self.context,
code=self.last_code_executed,
)
response = self.call_llm_with_prompt(prompt)
self.logger.log(
f"""Explanation: {response}
"""
)
return response
except Exception as exception:
return (
"Unfortunately, I was not able to explain, "
"because of the following error:\n"
f"\n{exception}\n"
)
def rephrase_query(self, query: str):
try:
prompt = RephraseQueryPrompt(
context=self.context,
query=query,
)
response = self.call_llm_with_prompt(prompt)
self.logger.log(
f"""Rephrased Response: {response}
"""
)
return response
except Exception as exception:
return (
"Unfortunately, I was not able to rephrase query, "
"because of the following error:\n"
f"\n{exception}\n"
)
@property
def logs(self):
return self.logger.logs
@property
def last_error(self):
raise NotImplementedError
@property
def last_query_log_id(self):
raise NotImplementedError