55from langchain_experimental .tools .python .tool import PythonAstREPLTool
66from langchain_core .prompts import ChatPromptTemplate , HumanMessagePromptTemplate , SystemMessagePromptTemplate , MessagesPlaceholder
77
8- import matplotlib .pyplot as plt
9-
10-
11-
128class prompt_repo ():
139
1410 @classmethod
1511 def get_system_prompt (cls , role = None ):
16-
12+
1713 if role == "text2chart" :
18-
14+
1915 system_prompt = dedent (
2016 '''
2117 You are a pandas master bot designed to generate Python code for plotting a chart based on the given dataset and user question.
@@ -25,9 +21,9 @@ def get_system_prompt(cls, role=None):
2521 )
2622 else :
2723 system_prompt = ""
28-
24+
2925 return system_prompt
30-
26+
3127 @classmethod
3228 def get_human_prompt (cls , role = None ):
3329
@@ -56,13 +52,13 @@ def get_human_prompt(cls, role=None):
5652 If you are asked to plot a chart, use "matplotlib" for charts, save as "results.png".
5753 Expaination with Koren.
5854 Do not use legend and title in plot in Korean.
59-
55+
6056 Generate python code and return full updated code within <update_code></update_code>:
61-
57+
6258
6359 '''
6460 )
65-
61+
6662 else :
6763 human_prompt = ""
6864
@@ -78,11 +74,13 @@ def __init__(self, **kwargs):
7874 self .num_rows = kwargs ["num_rows" ]
7975 #self.return_context = kwargs.get("return_context", False)
8076 self .verbose = kwargs .get ("verbose" , False )
81- self .pattern = r'<update_code>(.*?)</update_code>'
77+ self .parsing_pattern = kwargs ["parsing_pattern" ]
78+ self .show_chart = kwargs .get ("verbose" , False )
8279
8380 def query (self , ** kwargs ):
8481
8582 df , query , verbose = kwargs ["df" ], kwargs ["query" ], kwargs .get ("verbose" , self .verbose )
83+ show_chart = kwargs .get ("show_chart" , self .show_chart )
8684
8785 if len (df ) < self .num_rows : dataset = str (df .to_csv ())
8886 else : dataset = str (df .sample (self .num_rows , random_state = 0 ).to_csv ())
@@ -99,28 +97,37 @@ def query(self, **kwargs):
9997 prompt = ChatPromptTemplate .from_messages (
10098 [self .system_message_template , human_message_template ]
10199 )
102-
103- chain = prompt | self .llm_text | StrOutputParser ()
104100
101+ code_generation_chain = prompt | self .llm_text | StrOutputParser ()
102+
105103 self .verbose = verbose
106- response = chain .invoke (
104+ response = code_generation_chain .invoke (
107105 invoke_args ,
108106 config = {'callbacks' : [ConsoleCallbackHandler ()]} if self .verbose else {}
109107 )
110108
111- return response
109+ if show_chart :
110+ results = self .code_execution (
111+ df = df ,
112+ response = response
113+ )
114+ return results
115+ else :
116+ return response
112117
113118 def code_execution (self , ** kwargs ):
114119
115- df , code = kwargs ["df" ], self ._code_paser (response = kwargs ["response" ])
120+ df , code = kwargs ["df" ], self ._code_parser (response = kwargs ["response" ])
116121 tool = PythonAstREPLTool (locals = {"df" : df })
117122
118- tool .invoke (code )
123+ results = tool .invoke (code )
124+
125+ return results
119126
120- def _code_paser (self , ** kwargs ):
127+ def _code_parser (self , ** kwargs ):
121128
122129 parsed_code , response = "" , kwargs ["response" ]
123- match = re .search (self .pattern , response , re .DOTALL )
130+ match = re .search (self .parsing_pattern , response , re .DOTALL )
124131
125132 if match : parsed_code = match .group (1 )
126133 else : print ("No match found." )
0 commit comments