jonathanjordan21 commited on
Commit
e00282b
·
verified ·
1 Parent(s): c27e18c

Update custom_llm.py

Browse files
Files changed (1) hide show
  1. custom_llm.py +39 -46
custom_llm.py CHANGED
@@ -145,7 +145,9 @@ def format_df(df):
145
  return out
146
 
147
 
148
- out_prompt = PromptTemplate.from_template("""<s><INST>Fix the following code:
 
 
149
  {code}
150
 
151
  Error Message : {err}
@@ -158,63 +160,54 @@ out_prompt = PromptTemplate.from_template("""<s><INST>Fix the following code:
158
 
159
  </INST></s>""")
160
 
 
161
 
 
162
 
163
- df_prompt = PromptTemplate.from_template("""<s><INST>You have access to a pandas dataframe variable named "df". Below are the examples of the dataframe:
164
-
165
- {df_example}
166
-
167
- Given the following user input, create relevant python code to get the relevant information in the dataframe and store the response string result in a variable named "response". Do not explain, just create the python code:
168
-
169
- {question}
170
-
171
- Always change the corresponding columns into datetime format with parameter day_first=True, example:
172
- df['column_name'] = pd.to_datetime(df['column_name'], day_first=True)
173
-
174
-
175
- Always use idxmin or idxmax instead of array indicies whenever it is possible
176
 
177
-
178
- Do not import pandas and Do not create or re-assign "df" variable
179
-
180
-
181
- The output must follow the following example format:
182
- ```python
183
- # Generated Code
184
- ```
185
-
186
- </INST></s>""")
 
 
 
 
187
 
188
 
189
 
190
  def custom_dataframe_chain(llm, df):
191
- prompt = df_prompt
192
-
193
- def out_format(text, df):
194
 
195
- prompt = out_prompt
196
 
197
- err_chain = prompt | llm
198
 
199
- e_ = None
200
 
201
- for _ in range(6):
 
202
 
203
- try :
204
- # text = text.split("```python")[-1].split("```")[0]
205
- text = text.split("# Generated Code")[-1].split("`")[0]
206
- print(text)
207
- exec(text)
208
-
209
- return text
210
- except Exception as e:
211
- print(e)
212
- text = err_chain.invoke({"code":text, "err":str(e)})
213
- e_ = e
214
- # exec(text)
215
-
216
- return "Bad Python Code, Error Message : " + str(e_)
217
-
218
 
219
  return prompt | llm | RunnableLambda(lambda x:out_format(x, df))
220
 
 
145
  return out
146
 
147
 
148
+ def out_format(text, df):
149
+
150
+ prompt = PromptTemplate.from_template("""<s><INST>Fix the following code:
151
  {code}
152
 
153
  Error Message : {err}
 
160
 
161
  </INST></s>""")
162
 
163
+ err_chain = prompt | llm
164
 
165
+ e_ = None
166
 
167
+ for _ in range(6):
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
+ try :
170
+ # text = text.split("```python")[-1].split("```")[0]
171
+ text = text.split("# Generated Code")[-1].split("`")[0]
172
+ print(text)
173
+ exec(text)
174
+
175
+ return text
176
+ except Exception as e:
177
+ print(e)
178
+ text = err_chain.invoke({"code":text, "err":str(e)})
179
+ e_ = e
180
+ # exec(text)
181
+
182
+ return "Bad Python Code, Error Message : " + str(e_)
183
 
184
 
185
 
186
  def custom_dataframe_chain(llm, df):
187
+ prompt = PromptTemplate.from_template("""<s><INST>You have access to a pandas dataframe variable named "df". Below are the examples of the dataframe:
 
 
188
 
189
+ {df_example}
190
 
191
+ Given the following user input, create relevant python code to get the relevant information in the dataframe and store the response string result in a variable named "response". Do not explain, just create the python code:
192
 
193
+ {question}
194
 
195
+ Always change the corresponding columns into datetime format with parameter day_first=True, example:
196
+ df['column_name'] = pd.to_datetime(df['column_name'], day_first=True)
197
 
198
+
199
+ Always use idxmin or idxmax instead of array indicies whenever it is possible
200
+
201
+
202
+ Do not import pandas and Do not create or re-assign "df" variable
203
+
204
+
205
+ The output must follow the following example format:
206
+ ```python
207
+ # Generated Code
208
+ ```
209
+
210
+ </INST></s>""")
 
 
211
 
212
  return prompt | llm | RunnableLambda(lambda x:out_format(x, df))
213